What is legal declaration of array in the following and explain why?and also explain why not others? | SoloLearn: Learn to code for FREE!

0

What is legal declaration of array in the following and explain why?and also explain why not others?

int a[][]=new int[4][4]; int[] a[]=new int[4][4]; int a[][]=new int[][4]; int[] a[]=new int[4][];

12/25/2017 7:30:52 AM

Saikrishna Reddy

1 Answer

New Answer

+7

An OCA question. All of them are legal except for the third one. int a[][] = new int[][4]; The dimensions must be created from left to right. The compiler cannot possibly allocate 4 slots of int to an arbitrary number of int[]. int[] a[] is simply equivalent to int[][] a and int a[][].