Hello Friends Pls Explain.... 1. Which declaration for 2D array are true ? a> int a[][] = new int[][] b> int []a[] = new int[5][] c> int []a[] = new int[][5] d> int [][]a = new int[5][]
ans given are : b, d. If I am not wrong according to me the ans is : a,d. Pls let me clarify .....
2. What is the result of the following program if it is compiled. class Continue { public static void main(String args[]) { outer: for(int i=0;i<2;i++) {
for: for (int k=0;k<2;k++) { if (i==j) break for; System.out.println(i+ " " +k); } } } } Ans given is rints 10 11 According to me the code will not compile for two reasons. 1. variable 'j' is not defined .(i.e. int, byte, short..etc) 2. 'for' is the java keyword and can not be used as label. Pls Let me sure. Tejas Nakawala.
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
For the first one, I think the answer is A, B and D. A will work because you don't have to initialze size. B will work because you can initialize size for first array and not second array, and the int []a[] is awkward, but allowed C will not work because you can't initialze the size for the inner array if you don't do the outer array. D will work for the same reason B will work, So I like A B and D As for the second one, I think you are correct. j is not declared anywhere, so you are going to run into problems and for is a keyword so you can't use it. Bill
madhuri vl
Ranch Hand
Joined: Jan 17, 2001
Posts: 86
posted
0
Hi , For the first question my answers are a,b,d and i have a doubt on array dec. Is this correct int a[][]=new int[5][5]; please reply
madhuri.
Tejas Nakawala
Ranch Hand
Joined: Jan 04, 2001
Posts: 80
posted
0
hi Madhavi and Bill, Thankx for reply, But i am still not convince for the first one. Is for 2D array declaration like... int []a[] = new int [][] is valid or not ? I have never seen such declaration in any book. If u have seen pls let me know the book name. Can any one thorw more light on this ? Tejas Nakawala
Lori Battey
Ranch Hand
Joined: May 17, 2000
Posts: 37
posted
0
According to all I've seen, A) is incorrect. When you initialize an array with the keyword "new" you must either state the size or input the data so the space can be allocated on the heap. At declaration time you don't need the size but when instantiating, you do...
Lori Battey<br />SCJP2
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
Lori you are correct, my mistake. A is not correct as you didn't initialize the size of the array. Try this code:
Bill
Tejas Nakawala
Ranch Hand
Joined: Jan 04, 2001
Posts: 80
posted
0
Thankx, Lori , Bill and Madhavi.. I got the point. Tejas Nakawala
Vijayalakshmi Chipada
Greenhorn
Joined: Dec 04, 2000
Posts: 17
posted
0
Hi Tejas, Can you let me know the website for Javeed Mock exam Thank you