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?
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)); } }
B.
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.