Java OCA OCP Practice Question 181

Question

Given:

class Main { 
  public static void main(String[] args) { 
    if(args.length == 1 | args[1].equals("test")) { 
      System.out.println("test case"); 
    } else { 
      System.out.println("production " + args[0]); 
    } 
  } 
} 

And the command-line invocation:

java Main live2 

What is the result?

  • A. test case
  • B. production live2
  • C. test case live2
  • D. Compilation fails
  • E. An exception is thrown at runtime


P:E is correct.

Note

Because the short circuit ( ||) is not used, both operands are evaluated.

Since args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.




PreviousNext

Related