Java Questions & Answers – Java.lang – Boolean Wrapper Advance

«
»

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Java.lang – Boolean Wrapper Advance”.

1. Which of these methods of Boolean wrapper returns boolean equivalent of an object.
a) getBool()
b) booleanValue()
c) getbooleanValue()
d) getboolValue()
View Answer

Answer: b
Explanation: None.
advertisement

2. Which of the following constant are defined in Boolean wrapper?
a) TRUE
b) FALSE
c) TYPE
d) All of the mentioned
View Answer

Answer: d
Explanation: Boolean wrapper defines 3 constants – TRUE, FALSE & TYPE.

3. Which of these methods return string equivalent of Boolean object?
a) getString()
b) toString()
c) converString()
d) getStringObject()
View Answer

Answer: b
Explanation: None.

4. Which of these methods is used to know whether a string contains “true”?
a) valueOf()
b) valueOfString()
c) getString()
d) none of the mentioned
View Answer

Answer: a
Explanation: valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.

5. Which of these class have only one field?
a) Character
b) Boolean
c) Byte
d) void
View Answer

Answer: d
Explanation: Void class has only one field – TYPE, which holds a reference to the Class object for type void. We do not create an instance of this class.

6. What will be the output of the following Java program?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String str = "true";
  6.             boolean x = Boolean.valueOf(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) Compilation Error
d) Runtime Error
View Answer

Answer: a
Explanation: valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
Output:

advertisement
$ javac Output.java
$ java Output
true

7. What will be the output of the following Java program?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String str = "true false true";
  6.             boolean x = Boolean.valueOf(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
Output:

$ javac Output.java
$ java Output
false

8. What will be the output of the following Java program?

advertisement
  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String str = "TRUE";
  6.             boolean x = Boolean.valueOf(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) Compilation Error
d) Runtime Error
View Answer

Answer: a
Explanation: valueOf() returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time.
Output:

$ javac Output.java
$ java Output
true

9. What will be the output of the following Java program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5. 	    String str = "true false";
  6.             boolean x = Boolean.parseBoolean(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) System Dependent
d) Compilation Error
View Answer

Answer: b
Explanation: parseBoolean() Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Example: Boolean.parseBoolean(“True”) returns true.
Example: Boolean.parseBoolean(“yes”) returns false.
Output:

$ javac Output.java
$ java Output
false

10. What will be the output of the following Java program?

advertisement
  1.     class Output
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5. 	   String x = Boolean.toString(false);
  6.         }
  7.     }

a) True
b) False
c) System Dependent
d) Compilation Error
View Answer

Answer: b
Explanation: toString() Returns a String object representing the specified boolean. If the specified boolean is true, then the string “true” will be returned, otherwise the string “false” will be returned.
Output:

$ javac Output.java
$ java Output
false

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn