Submitted by c-admin on Wed, 05/29/2019 - 05:11
Question:
Which of these array declarations and initializations are NOT legal?
Select 2 options
A. int[ ] i[ ] = { { 1, 2 }, { 1 }, { }, { 1, 2, 3 } } ;
B. int i[ ] = new int[2] {1, 2} ;
C. int i[ ][ ] = new int[ ][ ] { {1, 2, 3}, {4, 5, 6} } ;
D. int i[ ][ ] = { { 1, 2 }, new int[ 2 ] } ;
E. int i[4] = { 1, 2, 3, 4 } ;
Answer and Explanation (Click to expand) Answer:
B - If you give the elements explicitly you can't give the size. So it should be just int[] { 1, 2 } or just { 1, 2 }
E - You cannot specify the size on left hand side.
Explanation:
If you explicitly specify the members then you can't give the size. So option 2 is wrong. The size of the array is never given during the declaration of an array reference. So option 5 is wrong. The size of an array is always associated with the array instance, not the array reference.