It's not a secret anymore!
The moose likes Beginning Java and the fly likes Doubt About the output of this Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt About the output of this Question" Watch "Doubt About the output of this Question" New topic

Doubt About the output of this Question

Akhila Rayaprolu

Joined: Jan 30, 2006
Posts: 1
Given the following,
1. public class Test {
2. public static void main(String [] args) {
3. byte [][] big = new byte [7][7];
4. byte [][] b = new byte [2][1];
5. byte b3 = 5;
6. byte b2 [][][][] = new byte [2][3][1][2];
8. }
9. }
which of the following lines of code could be inserted at line 7, and still allow the code to
compile? (Choose four that would work.)
A. b2[0][1] = b;
B. b[0][0] = b3;
C. b2[1][1][0] = b[0][0];
D. b2[1][2][0] = b;
E. b2[0][1][0][0] = b[0][0];
F. b2[0][1] = big;

I would like to know the set of answers for the above question along with an explanation as to why.
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
Option C and D that is
will not compile as the assigned array dimensions are not compatible .

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15037

If you have a computer with the JDK installed, you can find out for yourself which of the answers is correct.

A hint: Multi-dimensional arrays in Java are just arrays of arrays. So byte[][] b is an array of arrays: the entries b[0], b[1], ... are each an array of bytes (byte[]).

byte[][][][] b2 is an array of arrays of arrays of arrays.
b2[0] is a byte[][][].
b2[0][0] is a byte[][].
b2[0][0][0] is a byte[].
b2[0][0][0][0] is a byte.

Do you see it now?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Doubt About the output of this Question
It's not a secret anymore!