What will be the output of the following Java program?

class Output 
    {
        public static void main(String args[])
        {
            int a1[] = new int[10];
            int a2[] = {1, 2, 3, 4, 5};
            System.out.println(a1.length + " " + a2.length);
        } 
    }

a) 10 5
b) 5 10
c) 0 10
d) 0 5

1 thought on “What will be the output of the following Java program?”

  1. a
    Explanation: Arrays in java are implemented as objects, they contain an attribute that is length which contains the number of elements that can be stored in the array. Hence a1.length gives 10 and a2.length gives 5.
    output:
    $ javac Output.java
    $ java Output
    10 5

Leave a Comment