Java OCA OCP Practice Question 2657

Question

Given:

class Printable {
   void print() {
      assert false;
   }/*from   w  w w.  j a v  a2 s  . c  o  m*/
}

public class Main extends Printable {
   public static void main(String[] args) {
      new Main().go(args);
   }

   void go(String[] args) {
      if (args.length > 0)
         print();
      if (!args[0].equals("x"))
         System.out.println("!x");
   }
}

And, if the code compiles, the invocation:

java -ea Main 

What is the result?

  • A. !x
  • B. Compilation fails.
  • C. An AssertionError is thrown.
  • D. A NullPointerException is thrown.
  • E. An IllegalArgumentException is thrown.
  • F. An ArrayIndexOutOfBoundsException is thrown.


F   is correct.

Note

The java invocation passes no arguments to main(), so the args array has a length of 0, print() is not called, and line 10 throws an exception.




PreviousNext

Related