Given:
1. class Alligator {
2. public static void main(String[] args) {
3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }
A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.
Answer: E
1. class Alligator {
2. public static void main(String[] args) {
3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
4. int [][]y = x;
5. System.out.println(y[2][1]);
6. }
7. }
A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.
Answer: E
Explanation:
In General we can declare int array as int[] x or int []x or int x[] all of them are equivalent and same for one dimensional array.For two dimensional array int [][] x or int []x[] or int x [][] all of them equivalent and same .
Here in the Line 3 we declaring an integer multi dimensional array named as x and initializes integer values 1,2,0,0 for 0th row , 3,4,5,0 for 1st row , 6,7,8,9 for 2nd row .
Next we are storing the reference variable x in the y , therefore integer reference variable x and y both point to same array with elements 1,2,0,0 for 0th row , 3,4,5,0 for 1st row , 6,7,8,9 for 2nd row .
Then we are accessing 2nd row first element - y[2][1] .Remember array index starts with 0 and not from 1.Output is :7 ,Option E.
0 comments:
Post a Comment