Java OCA OCP Practice Question 512

Question

Which of the following is not a possible result of executing the following application?

? 
public class MyClass { 
        public static void main(String... argv) { 
           System.out.print(argv[0]!=null 
                 && argv[0].equals("sunny") 
                 && !false 
              ? "Go Outside" : "Stay Inside"); 
        } 
} 
  • A. Nothing is printed.
  • B. The application throws an exception at runtime.
  • C. Go Outside is printed.
  • D. Stay Inside is printed.


A.

Note

The application uses the conditional conjunction && operator to test if argv[0] is null, but unfortunately this test does not work on zero-length arrays.

Therefore, it is possible this code will throw an ArrayIndexOutOfBoundsException at runtime.

The second part of the expression evaluates to true if the first input of argv matches sunny.

The final part of the expression, && !false, is a tautology in that it is always true and has no impact on the expression.

Either an exception will be thrown or text will be output, based on the value of argv, therefore Option A is the correct answer.




PreviousNext

Related