ExamVeda
Login
Home
This question belongs to Java Program Array
Array
?

What is the result of compiling and running the following code?
public class Test{
        public static void main(String[] args){
                int[] a = new int[0];
                System.out.print(a.length);
        }
}

Answer & Solution
Correct Answer: Option A
Step 1:
Declare a public class named Test.

Step 2:
Define the main method with the public and static modifiers and a parameter of type String array named args.

Step 3:
Declare an array variable a of type int[].

Step 4:
Initialize the array a with a size of 0 using the new keyword.

Step 5:
Print the length of the array a using the length property.

Therefore, when you compile and run the code, the output will be:
0

This output corresponds to the length of the array a, which is 0.
Examveda
Question posted by Examveda
Community

Join the Discussion

2 Comments
Joseph Guerra
Joseph Guerra 3 years ago
This should be 1 since it is printing length, not index code.
Limali Sahoo
Limali Sahoo 7 years ago
As we know counting is done from 1 when length is calculated. then why answer is 0.