Java OCA OCP Practice Question 1122

Question

What is the result of the following when run with java mypkg.Main September 3 2020?

package mypkg; 
public class Main { 
       public static void main(String[] args) { 
         for (int i = args.length; i>=0; i--) 
            System.out.println(args[i]); 
       } 
} 
  • A. September
  • B. 2020
  • C. The code does not compile.
  • D. None of the above


D.

Note

There are three arguments passed to the program.

This means that i is 3 on the first iteration of the loop.

The program attempts to print args[3].

Since indexes are zero based in Java, it throws an ArrayIndexOutOfBoundsException.




PreviousNext

Related