Output of Java Programs | Set 42 (Arrays)

29 Sep, 2017
Prerequisite : Java Arrays
Question 1. What is the output of this question?
class Test1 {public    static void main(String[] args)    {        int arr[] = new int[5];        int arr2[] = new int['a'];        byte bt = 10;        int arr3[] = new int[bt];        System.out.println(arr.length);        System.out.println(arr2.length);        System.out.println(arr3.length);    }}
OptionA) ErrorB) Runtime ExceptionC) 59710D) 56510
Output: C

Explanation : To specify array size allowed data type are – byte, short, int, char and all of these are valid data types here.
Question 2. What is the output of this question?
class Test2 {public    static void main(String[] args)    {        int a[] = new int[5]; // line 1        int[] a11 = new int[]; // line 2    }}
OptionA) ErrorB) ExceptionC) Run successfullyD) None
Output: A

Explanation : One Dimension array have size declaration as compulsory feature.
Error : array dimension missing
int []a11 = new int[]; // line 2
Question 3. Which of the following declarations are invalid?
class Test3 {public    static void main(String[] args)    {        int[][] arr1 = new int[2][3]; // Line 1        int[][] arr2 = new int[2][]; // line 2        int[][] arr3 = new int[][]; // line 3        int[][] arr4 = new int[][2]; // line 4    }}
OptionA) AllB) line 1, 3, 4C) line 3, 4D) line 2, 3, 4
Output: C

Explanation : First two declarations are allowed and so no error. line 3 and 4 have zero and last dimension respectively.error: array dimension missing
int [][]arr3=new int[][];//line 3
                          ^
error: ']' expected
  int [][]arr4=new int[][2];//line 4
                         ^
error: ';' expected
  int [][]arr4=new int[][2];//line 4
Question 4. Which of the following lines give error?
class Test4 {public    static void main(String[] args)    {        int[][][] arr1 = new int[1][2][3]; // Line 1        int[][][] arr2 = new int[1][2][]; // Line 2        int[][][] arr3 = new int[2][][]; // Line 3        int[][][] arr4 = new int[][][]; // Line 4        int[][][] arr5 = new int[][2][3]; // Line 5        int[][][] arr6 = new int[][][3]; // Line 6        int[][][] arr7 = new int[][2][]; // Line 7    }}
OptionA) line 4, 5, 6, 7B) AllC) No ErrorD) line 4, 7
Output: A

Explanation : In three dimensional array have first two dimension declaration is compulsory other wise we will get compile time error:illegal startup expression.
Question 5. What is the output of this question?
class Test5 {public    static void main(String[] args)    {        int arr[] = new int[5];        System.out.println(arr);        System.out.println(arr[0]);    }}
OptionA) 00B)[I@6bc7c0540C) 0 0 0 0 00D) none
Output: B

Explanation : arr : It is giving the base address of arrayarr[0] : It is giving value of array element at 0th location.
This article is contributed by Shivakant Jaiswal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Arrays
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of Java Programs | Set 12
Output of C programs | Set 59 (Loops and Control Statements)
unsigned specifier (%u) in C with Examples
Output of C++ Program | Set 2
How to show full column content in a PySpark Dataframe ?
Output of C programs | Set 31 (Pointers)
Output of C programs | Set 35 (Loops)
Output of C programs | Set 64 (Pointers)
Output of Java Programs | Set 14 (Constructors)
Output of C Programs | Set 5