File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B , Chap1 Q11

 
Ram Han
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below question is from K&B book Chap 1, Q11
Given the following,
1. public class Test {
2. public static void main(String [] args) {
3. int [] [] [] x = new int [3] [] [];
4. int i,j;
5. x[0] = new int[4][];
6. x[1] = new int[2][];
7. x[2] = new int[5][];
8. for (i=0; i<x.length; i++)
9. for (j=0; j<x[i].length; j++) {
10. x[i][j] = new int [i + j + 1];
11. System.out.println("size = " + x[i][j].length);
12. }
13.

how many lines of output will be produced? (Choose one.)
A. 7
B. 9
C. 11
D. 13
E. Compilation fails
F. An exception is thrown at runtime


Answer C.


Author nicely escapes without giving a explanation on this question

Can someone explain , how it works
 
Saif Ullah
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't run this example, but the loops sound to work like that. The loop with variable i, (outer loop) runs for three iterations as the first dimension of x array is 3, for each iteration, the inner loop iterates for 4, 2 and 5 times so total iterations are 11. I hope you can understand how inner loop is iterated for each individual size mentioned for 2nd dimension of x-array.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awww,

Author nicely escapes without giving a explanation on this question


I thought our "explanation" was pretty useful



Bert
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. public static void main(String [] args) {
3. int [] [] [] x = new int [3] [] [];
// Here an array of size 3 is created. Each element in the array is an
array which contains an array of ints.

4. int i,j;
5. x[0] = new int[4][];
// The first entry in the array referred to by x is set to an array containing 4 int arrays.

6. x[1] = new int[2][];
// The second entry in the array referred to by x is set to an array containing 2 int arrays.

7. x[2] = new int[5][];
// The third entry in the array referred to by x is set to an array containing 5 int arrays.

8. for (i=0; i<x.length; i++)
9. for (j=0; j<x[i].length; j++) {
10. x[i][j] = new int [i + j + 1];
11. System.out.println("size = " + x[i][j].length);
12. }
13.

// The outer loop here executes 3 times. On the first pass, a loop of length 4 is run. On the second pass, a loop of length 2 is run. On the third pass, a loop of length 5 is run.

So you have 11 runs. Each time you print a line.
 
sumitk das
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

MuraliRam Narasimhan has mentioned the name of the book as k&b , is it
khalid mughal's book for scjp. I am a new one in this forum, so pls let
me know.

Thanks,
Sumit
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its certification book by Kathy Sierra, Bert Bates
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...the only way to solve questions "involving dynamic initialization of arrays using fo loops" is to dray a picture of the array. its a piece of cake then

bye
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith, your explanation is good, but for a minor (major?) detail.
Each element in the x int array refers to an array of ints and not an array of int arrays, which would make x a 3D array.
Sashi
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But isn't it the case that

int[] x -> x refers to an array of ints

int[][] x -> x refers to an array of arrays of ints

int[][][] x -> x refers to an array of arrays of arrays of ints
 
Junilu Lacar
Bartender
Pie
Posts: 6548
22
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sashi, I concur with Keith:

The original declaration int[][][] x means that each element of the array x is an int[][].
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I came, I saw, I concurred.
Sashi
 
Gyan Shankar
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Number of lines of o/p = 4+2+5 = 11
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Sashi]: Well, I came, I saw, I concurred

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic