aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Expalin the answer -- Arrays 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 "Expalin the answer -- Arrays" Watch "Expalin the answer -- Arrays" New topic
Author

Expalin the answer -- Arrays

Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
19. If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I think the right answer is e...
Try and run it but at a first sight e seems to be the right answer...
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
When you run it
it prints
1
2
3
4
5
6
7
8
9
10
So is both a and e correct?
I am confused
Can someone explain how the code is working?
Thankx
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
it's not a matter of printing it is a matter of HOW the arrays look like...
the first array has one element: 1
the second array has two elements: 2 3
the third array has three elements: 4 5 6
the fourth array has four elements: 7 8 9 10
so answer e is correct !!
Val
Amit Agrawal
Ranch Hand

Joined: Aug 23, 2001
Posts: 282


Originally posted by Roopa Bagur:
19. If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good job Amit !!
Val
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Excellent everybody thankyou
Originally posted by Roopa Bagur:
19. If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}

a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Expalin the answer -- Arrays