aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B , Chap1 Q11 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B , Chap1 Q11" Watch "K&B , Chap1 Q11" New topic
Author

K&B , Chap1 Q11

Ram Han
Ranch Hand

Joined: Feb 26, 2002
Posts: 49
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


SCJP,SCWCD,IBM 285,MCTS
Saif Ullah
Greenhorn

Joined: Mar 21, 2002
Posts: 29
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

Joined: Oct 14, 2002
Posts: 8829
    
    5
Awww,

Author nicely escapes without giving a explanation on this question


I thought our "explanation" was pretty useful



Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Dec 01, 2005
Posts: 4
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

Joined: Sep 13, 2004
Posts: 105
Its certification book by Kathy Sierra, Bert Bates


SCJP 1.4
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 26, 2001
Posts: 4757
    
    7

Sashi, I concur with Keith:

The original declaration int[][][] x means that each element of the array x is an int[][].


Junilu - [How to Ask Questions] [How to Answer Questions]
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Well, I came, I saw, I concurred.
Sashi
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
Number of lines of o/p = 4+2+5 = 11


SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Sashi]: Well, I came, I saw, I concurred



"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B , Chap1 Q11