Java OCA OCP Practice Question 411

Question

Consider the following class :

public class Test{ 
   public static void main (String [] args){ 
      if  (args [0].equals ("open")) 
         if  (args [1].equals ("someone")) 
            System.out.println ("Hello!"); 
      else System.out.println ("Go away "+ args [1]); 
     } 
 } 

Which of the following statements are true if the above program is run with the command line:

java Test closed 

Select 1 option

  • A. It will throw ArrayIndexOutOfBoundsException at runtime.
  • B. It will end without exceptions and will print nothing.
  • C. It will print Go away
  • D. It will print Go away and then will throw ArrayIndexOutOfBoundsException.
  • E. None of the above.


Correct Option is  : B

Note

Both the outer if statement and the inner if statement might conceivably own the else clause.

In this example, one might be tempted to assume that the programmer intended the else clause to belong to the outer if statement.




PreviousNext

Related