Open In App

Output of Java Programs | Set 38 (Arrays)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Arrays in Java

Question 1. What is the output of this question




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


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?




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();
        }
    }
}


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?




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();
        }
    }
}


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 ?




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();
        }
    }
}


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?




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());
    }
}


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().



Last Updated : 25 Sep, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads