Java Type Wrapper Classes MCQ Questions and Answers

Study and learn Interview MCQ Questions and Answers on Java Type Wrapper Classes. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Type Wrappers before reading these objective questions.



1) What is a Type Wrapper or simply a Wrapper in Java?
A) A Wrapper class is an object version of Primitive Data Type
B) A Wrapper provides additional methods for ease of use
C) Wrapper types are useful for using with generic collections
D) All the above
Answer [=]
D
2) Which primitive data types have wrapper classes available?
A) byte, short, int, long
B) float, double
C) boolean, char
D) All the above
Answer [=]
D
3) Choose the correct mapping of Primitive Data Types and Wrapper Classes below?
A) boolean = Boolean, char = Character
B) byte = Byte, short = Short, int = Integer, long = Long
C) float = Float, double = Double
D) All the above
Answer [=]
D
4) State TRUE or FALSE. Java primitive data types work faster compared to their Wrapper counter parts.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes. Developers are encouraged to use primitive types like int, float, long etc for better memory management and performance.

5) Which is the correct way of converting an int value to Integer in below Java code?
A)
Integer in = new Integer(9);
B)
Integer in = Integer.valueOf(9);
C)

 
Integer in = new Integer("9"); 
D) None of the above
Answer [=]
B
Explanation:

The method valueOf() is used to convert a primitive data type like "int" to "Integer". All constructors of Wrapper classes are deprecated as of Java 9.

6) Which is the Superclass of Wrapper types like Byte, Short, Integer, Long, Float and Double?
A) Math
B) System
C) Number
D) Enum
Answer [=]
C
Explanation:

Number class is the super class of almost all of Wrapper classes.

7) Which are the methods a Wrapper class object can use in Java?
A) byteValue(), shortValue, intValue(), longValue()
B) floatValue(), doubleValue()
C) booleanValue(), charValue()
D) All the above
Answer [=]
D
Explanation:

Except booleanValue() and charValue(), all valueOf() methods are from Number class.



8) Which is the superclass of wrapper classes like Boolean and Character?
A) Number
B) Wrapper
C) Object
D) Math
Answer [=]
C
9) Are the sizes of float and Float same or different in Java?
A) Same
B) Different
C) -
D) -
Answer [=]
A
10) Which are the constant fields available a Wrapper Class object in Java?
A) BYTES, SIZE
B) MAX_VALUE, MAX_EXPONENT
C) MIN_VALUE, MIN_EXPONENT
D) All the above
Answer [=]
D
Explanation:

The above constants are available only to non Boolean wrapper classes. MIN_EXPONENT and MAX_EXPONENT are available only to Float and Double.

11) What is the output of the below Java code snippet on wrapper classes?
System.out.println(Byte.BYTES);
System.out.println(Character.BYTES);
System.out.println(Short.BYTES);
System.out.println(Integer.BYTES);
System.out.println(Long.BYTES);
System.out.println(Float.BYTES);
System.out.println(Double.BYTES);
A)
1
2
2
4
8
4
8
B)
1
4
2
4
8
4
8
C)
1
2
4
8
16
8
16
D) None of the above
Answer [=]
A
Explanation:

The sizes of Byte, Character, Short, Integer, Long, Float and Double are 1, 2, 2, 4, 8, 4 and 8 bytes respectively.

12) Choose the correct way of creating a Float wrapper object in Java?
A)
Float pi = Float.valueOf(2.54f);
B)
Float pi = Float.valueOf(2.54);
C)
Float pi = new Float(2.54f);
D)
Float pi = new Float("2.54f");
Answer [=]
A
Explanation:

If you are using a non string argument, suffix the float number with a "f". Otherwise, the compiler throws error. Passing a string to Float.valueOf("2.54") works fine.

Error: The method valueOf(String) in the type Float is not applicable for the arguments (double)
13) Choose the correct way of comparing Wrapper objects in Java?
A)
float p1 = 10.45f;
float p2 = 5.2f;
if(Float.compare(p1, p2) == 1)
{
  System.out.println("10.45 > 5.2");
}
else
  System.out.println("No Biscuits");

//OUTPUT
10.45 > 5.2
B)
Float f1 = Float.valueOf("2.55");
Float f2 = Float.valueOf("4.5");

if(f1.compareTo(f2) == -1)
{
  System.out.println("2.55 < 4.5");
}
else
  System.out.println("No Tea");

//OUTPUT
2.55 < 4.5
C)
float t1 = 2.0f;
float t2 = 2.0f;
if(Float.compare(t1, t2) == 0)
{
  System.out.println("Float numbers are equal");
}

//OUTPUT
Float numbers are equal
D) All the above
Answer [=]
D
Explanation:

The methods Float.compare(f1,f2) and Float1.compareTo(Float2) works in the same way for Integer numbers also.

14) Choose the correct statement about Character wrapper class in Java?
A) Character class has static methods toUpperCase(char), toLowerCase(char)
B) Character class has static methods isDigit(char), isLetter(char)
C) Character class has static method valueOf(char) to convert char to Character.
D) All the above
Answer [=]
D
Explanation:

Example:

public class WrapperTest3
{
  public static void main(String[] args)
  {
    Character ct = Character.valueOf('a');
    char ch = ct.charValue();
    System.out.println(ch);
    System.out.println("Uppercase = " + Character.toUpperCase(ch));
    System.out.println("Is digit = " + Character.isDigit(ch));
    System.out.println("is Letter = " + Character.isLetter(ch));
  }
}

//OUTPUT
a
Uppercase = A
Is digit = false
is Letter = true



15) Choose the correct statement about Boolean wrapper class in Java?
A) Boolean class has static methods valueOf(boolean), valueOf("boolean") to convert boolean to Boolean object
B) Boolean class has static methods logicalAnd(boolean, boolean), logicalOr(boolean, boolean), logicalXor(boolean,boolean)
C) Boolean class has instance method equals() to check equality. compare() and compareTo() methods are also available
D) All the above
Answer [=]
D
Explanation:

Example:

public class WrapperTest4
{
  public static void main(String[] args)
  {
    Boolean b1 = Boolean.valueOf(false);
    Boolean b2 = Boolean.FALSE;
    System.out.println(b1 == b2);
    System.out.println(b1.equals(b2));
    System.out.println(Boolean.logicalAnd(true, false));
    System.out.println(Boolean.logicalOr(true, false));
    System.out.println(Boolean.logicalXor(true, false));
  }
}

//OUTPUT
true
true
false
true
true

16) Which exception is thrown for trying to create Wrapper class objects with wrong number type?
A) ArithmeticException
B) NumberFormatException
C) IllegarArgumentException
D) TypeNotPresentException
Answer [=]
B
Explanation:

Example with NumberFormatException:

public class WrapperTest5
{
  public static void main(String[] args)
  {
    Integer in = Integer.valueOf("0.0");
    System.out.println(in.intValue());
  }
}

OUTPUT:
java.lang.NumberFormatException: For input string: "0.0"
17) What is the output of the below code snipper with Short class object?
Short sh = Short.valueOf(10);
System.out.println(sh);
A) 10
B) 0
C) Compiler error
D) None
Answer [=]
C
Explanation:

The static method valueOf(short) accepts a short number but not integer. So, use casting to convert to short.

Error:
java.lang.Error: Unresolved compilation problem: 
	The method valueOf(String) in the type Short is not applicable for the arguments (int)

//So, use casting.
Short sh = Short.valueOf((short)10);
18) Choose a correct statement about Type Casting using Wrapper class objects in Java?
A) You can not convert from Byte to Integer
B) You can not convert from Integer to Float
C) You can not convert from Integer to Long
D) All the above
Answer [=]
D
Explanation:

Yes. You can not convert one type of Wrapper object to another type Wrapper.

Error:

java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from Byte to Integer