Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt About the output of this Question

 
Akhila Rayaprolu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
7.
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option C and D that is
will not compile as the assigned array dimensions are not compatible .
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic