Which of the following code fragments will successfully initialize a two-dimensional array of chars named v with a size such that v[2][3] refers to a valid element?
1. char [][] v = { { 'a', 'b ', 'c' }, { 'a', 'b ', 'c' } }; 2. char v [][] = new char [3][]; for (int i=0; i<v .length; i++) v [i] = new char [4]; 3. char v [][] = { new char []{ 'a', 'b ', 'c' } , new char []{ 'a', 'b ', 'c' } }; 4. char v [3][2] = new char [][] { { 'a', 'b ', 'c' }, { 'a', 'b ', 'c' } }; 5. char [][] v = { "1234", "1234", "1234" };
Select 1 option
Correct Option is : E
1 and 3 declare a two dimensional array alright but they create the array of size 2, 3.
And v[2][3] means we need an array of size 3, 4 because the numbering starts from 0.
4 : You cannot put array size information on LHS.
5 : This is a one dimensional array and that too of strings.
A java String is not equivalent to 1 dimensional array of chars.
This leaves us with only one choice 2.