Java OCA OCP Practice Question 639

Question

In the following application, the values of street and city have been omitted.

Which one of the following is a possible output of executing this class?

  • I. 350 5th Ave - New York
  • II. Posted:350 5th Ave - New York
package mypkg; /*from  w w w  . j  a v a2 s . c  om*/
public class Main { 
   public String getMain(String street, String city) { 
      try { 
         return street.toString() + " : " + city.toString(); 
      } finally { 
         System.out.print("Posted:"); 
      } 
   } 
   public static void main(String[] form) { 
      String street = // value omitted 
      String city = // value omitted 
      System.out.print(new Main().getMain(street,city)); 
   } 
} 
  • A. I only
  • B. II only
  • C. I and II
  • D. None of the above


B.

Note

If both values are valid non-null String objects, then no exception will be thrown, with the statement in the finally block being executed first, before returning control to the main() method; therefore, the second statement is a possible output.

If either value is null, then the toString() method will cause a NullPointerException to be thrown.

In both cases, the finally block will execute first, printing Posted:, even if there is an exception.

The first statement is not a possible output, and Option B is correct.




PreviousNext

Related