What will happen if you try to compile and run the following code? public class Q { public static void main(String argv[]){ int anar[]=new int[]{1,2,3}; System.out.println(anar[1]); }}

  1. 1

  2. Error anar is referenced before it is initialized.

  3. 2

  4. Error: size of array must be defined.


Correct Option: C

AI Explanation

To answer this question, let's go through the given code:

public class Q {
    public static void main(String argv[]){
        int anar[]=new int[]{1,2,3};
        System.out.println(anar[1]);
    }
}

This code declares an array anar of type int and initializes it with the values 1, 2, and 3. The System.out.println(anar[1]) statement prints the value at index 1 of the anar array.

When you compile and run this code, it will output:

2

Therefore, the correct answer is C) 2.

Find more quizzes: