Skip to content
Related Articles

Related Articles

Output of Java Programs | Set 42 (Arrays)
  • Difficulty Level : Medium
  • Last Updated : 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);
    }
}

Option
A) Error
B) Runtime Exception
C) 5
97
10
D) 5
65
10

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
    }
}

Option
A) Error
B) Exception
C) Run successfully
D) 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
    }
}

Option
A) All
B) line 1, 3, 4
C) line 3, 4
D) 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
    }
}

Option
A) line 4, 5, 6, 7
B) All
C) No Error
D) 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]);
    }
}

Option
A) 0
0
B)[I@6bc7c054
0
C) 0 0 0 0 0
0
D) none

Output: B

Explanation : arr : It is giving the base address of array
arr[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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :