Output of Java Programs | Set 38 (Arrays)

Prerequisite : Arrays in Java

Question 1. What is the output of this question

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test1 {
public
    static void main(String[] args)
    {
        int arr[] = { 11, 22, 33 };
        System.out.print(arr[-2]);
    }
}

chevron_right


Option
A) 11 33
B) Error
C) exception
D) 11 -33



Output: C

Explanation : We will get java.lang.ArrayIndexOutOfBoundsException because [-2] index is out of range.

Question 2. What is the output of this question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test2 {
public
    static void main(String[] args)
    {
        int arr[][] = { { 11, 22 }, { 33, 44, 55 } };
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < arr.length; j++)
                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
    }
}

chevron_right


Option
A) 11 22
33 44 55
B) 11 22
33 44
C) Error
D) Exception

Output: B

Explanation : Here arr.length returns 2 of the array size, because first dimension size if 2.

Question 3. What is the output of this question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test2 {
public
    static void main(String[] args)
    {
        int arr[][] = { { 11, 22 }, { 33, 44, 55 } };
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < arr[i].length; j++)
                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
    }
}

chevron_right


Option
A) 11 22
33 44 55
B) 11 22
33 44
C) Exception
D) Error

Output: A

Explanation : Here arr[i].length returns first time 2 because first dimension size is 2 and second time 3 because 3 second dimension array size is 3.

Question 4. What is the output of this question ?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test2 {
public
    static void main(String[] args)
    {
        int arr[][] = { { 11, 22 }, { 33, 44, 55 } };
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++)
                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
    }
}

chevron_right


Option
A) 11 22
33 44 55
B) 11 22
33 44
C) Error
D) Exception

Output: D

Explanation : This program will give exception :java.lang.ArrayIndexOutOfBoundsException
because we want to print the value out of range of array.


Question 5. What is the output of this question?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test5 {
public
    static void main(String[] args)
    {
        int arr[] = new int[5];
        int arr2[] = new int[5];
        System.out.print(arr.length + " ");
        System.out.print(arr2.length());
    }
}

chevron_right


Option
A) 5 5
B) Error
C) Exception
D) None

Output: B

Explanation : It will give error because length() method is not in java. error : Can not find symbol length().

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


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.