Hi,
can anybody please tell me what will be answer to the question given below,
[select 2 correct answers]
Question is:
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?
1]
int [][]tab{
{0,0,0},
{0,0,0}
};
2]
int tab[][]=new int[4][];
3]
int tab[3][2];
4]
int [] tab[]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
what i feel is,
option 4 is the correct ,since on accessing tab[3][2] it will return 0 which is valid.
option 1 is incorrect ,since size of the array tab is of 2*3 ,so on accessing tab[3][2] it will throw "array index out of bounds exception".
option 2 creates & initialises the int array tab with default value(is it correct?) which is 0.
but here on accessing tab[3][2] it throws null pointer exception.
option 3 is the only array tab declarations.
so i think option 4 is the correct ,i am confused which is the second correct answer?
Thank u,
Ekta