• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid Mock Exam Array

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
Which code fragments will succeed in initializing a two-dimensional array named tab with a size that will cause the expression tab[3][2] to access a valid element?
CODE FRAGMENT A:
int[][] tab = {
{ 0, 0, 0 },
{ 0, 0, 0 }
};
CODE FRAGMENT B:
int tab[][] = new int[4][];
for (int i=0; i<tab.length; i++) tab[i] = new int[3];>
CODE FRAGMENT C:
int tab[][] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
CODE FRAGMENT D:
int tab[3][2];
CODE FRAGMENT E:
int[] tab[] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
a.Code A
b.Code B
c.Code C
d.Code D
e.Code E
I do not know the correct answers for it
Please help
 
pardha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess answer is "E" ,you require 4 arrays of 3 elements each.
E declares and initialises correctly.
Correct me ,If I am wrong.
 
latha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaa, I agree with Pardha.
The answer is E. To access tab[3][2] elament, tab[][] should have min of [4][3] array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic