Java OCA OCP Practice Question 2905

Question

Given:

3. public class Main {  
4.   static String s;  
5.   static Boolean b;  
6.   static Boolean t1() { return new Boolean("howdy"); }  
7.   static boolean t2() { return new Boolean(s); }  
8.   public static void main(String[] args) {  
9.      if(t1()) System.out.print("t1 ");  
10.     if(!t2()) System.out.print("t2 ");  
11.     if(t1() != t2()) System.out.print("!= ");  
12.   }  //from ww w  .  j av  a  2  s . c o  m
13. } 

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. No output is produced.
  • C. The output will contain "t1 "
  • D. The output will contain "t2 "
  • E. The output will contain "!= "
  • F. The output is "t1 ", followed by an exception.


D is correct.

Note

Unboxing automatically converts the Boolean objects to boolean primitives.

The Boolean(String) constructor views null Strings AND Strings that don't have the value of "true" (case-insensitive) to be false.

It's legal for "if" tests to be based on method calls that return a boolean.

Finally, when "==" or "!=" is used to compare a wrapper object with a primitive, the wrapper is unwrapped before the comparison.




PreviousNext

Related