What will be the result of compilation and execution of the following code?

public class Test {
    public static void main(String [] a) {
        int [] b = { 1,2,3,4,5 };
        System.out.println("a[2]=" + a[2]);
    }
}
Explanation
Got you! Array named 'b' is declared and initialized in this code but array named 'a' is used. Therefore, ArrayIndexOutOfBoundsException will be thrown while trying to access a[2], because array 'a' is empty.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes