Java Autoboxing and Unboxing MCQ Questions and Answers

Study and learn Interview MCQ Questions and Answers on Java Autoboxing and Unboxing. 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 Wrapper and Autoboxing before reading these objective questions.



1) Choose a correct statement about Autoboxing in Java?
A) Boxing is the process of converting a primitive data to its equivalent Wrapper class type
B) Unboxing is the process of converting a Wrapper class object to its equivalent primitive data type
C) Autoboxing is the process of converting Primitive data type to Wrapper Object type automatically. Auto-Unboxing is the process of converting Wrapper type object to Primitive type automatically.
D) All the above
Answer [=]
D
2) Which does autoboxing or unboxing in Java?
A) Compiler
B) Runtime
C) Third party tools
D) None
Answer [=]
A
Explanation:

Yes. The compiler does all the Autoboxing and unboxing for you.

3) Autoboxing or unboxing involves replacement of Wrapper objects with Primitives and vice versa. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
4) Which Java version introduced the feature Autoboxing and Unboxing?
A) Java 4
B) Java 5
C) Java 6
D) Java 7
Answer [=]
B
Explanation:

Yes. Java 5 introduced many new features like AUTOBOXING, UNBOXING, advanced FOR loop, VARARGS, ENUM, GENERICS etc.

5) What is the need for Autoboxing and Unboxing in Java?
A) Reduces code to be written by developers to convert from Wrapper to Primitive vice versa
B) Compile time is reduced due to fixed methods followed to automatically convert Objects and Primitives
C) Auto-unboxing improves memory efficiency by converting Wrapper to Primitive and process when object version is not needed
D) All the above
Answer [=]
D
6) Choose the right statement which does autoboxing in Java?
A)
Integer temperature1 = 100;
B)
int temperature2 = 101;
C)
Integer temperature1 = 100;
int temperature2 = 101;
D) All the above
Answer [=]
A
Explanation:

As part of auto-boxing, an INT number is converted to INTEGER object automatically.

7) Choose the correct way of autoboxing Primitives to Wrapper objects below?
A)
Float f1 = 1.0f;
B)
Boolean bull = false;
C)
Character ch = 'a';
D) All the above
Answer [=]
D


8) Does the below Java code undergoes autoboxing correctly?
long weight = 10;
Long wei = weight;
System.out.println(ch.SIZE);
A) Yes
B) No
C) -
D) -
Answer [=]
A
Explanation:

Yes. You can assign a primitive LONG value to a Wrapper LONG object. The assignment undergoes auto-boxing by the compiler.

9) Auto-boxing or Auto-unboxing involves assignment (not always) in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

TRUE

10) Auto-boxing or Auto-unboxing does not work inside static methods in Java. State TRUE or FALSE
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
A
Explanation:

False. The method type can be anything.

11) What is the output of the below java code with autoboxing?
public class AutoBoxingTest2
{
  static void show(int reading)
  {
    System.out.println("Reading: " + reading);
  }
  public static void main(String[] args)
  {
    Integer a = Integer.valueOf(10);
    show(a);
  }
}
A) Reading: 0
B) Reading: 10
C) Compiler error
D) None
Answer [=]
B
Explanation:

Autoboxing or unboxing works with method arguments or parameters.

12) Which is faster Auto-boxing or Auto-unboxing?
A) Auto-boxing
B) Auto-unboxing
C) -
D) -
Answer [=]
B
Explanation:

Auto-unboxing is faster as it does not need to allocate memory for creation of Object. An already existing object will be used to extract value instead. Auto-boxing involves creation of Object and assigning value.

13) Does autoboxing work with creation of an Array in Java?
A) Yes
B) No
C) -
D) -
Answer [=]
A
Explanation:

Yes. You can create an Array of Wrapper objects just like creating a Primitive array. Example is below.

Integer k = 20;
Integer ary[] = {1, 4, 8, k};
System.out.println(ary[3]);

//OUTPUT
20
14) Autoboxing or Auto-unboxing works with ___ in Java.
A) Loops, Switch statements
B) Ternary Operator
C) IF ELSE statements
D) All the above
Answer [=]
D


15) Autoboxing or Auto-unboxing is ___ in Java.
A) Implicit
B) Explicit
C) -
D) -
Answer [=]
A
Explanation:

Implicit.

16) Choose the correct code that does auto-unboxing?
A)
Character ch = 'S';
switch(ch)
{
  case 'S': System.out.println("OK"); break;
  case 'p': break;
}

//OUTPUT
OK
B)
Integer t = 90;
int abc = ++t;
System.out.println(abc);

//OUTPUT
91
C)
int b =  10;
Integer c = 20;
if(b < c)
{
  System.out.println("LESS");
}

//OUTPUT
LESS
D) All the above
Answer [=]
D
17) What is the output of the below Java code snippet that use Autoboxing and Unboxing?
public class AutoUnboxingTest1
{
  public static void main(String[] args)
  {
    int apple1 = 10;
    Integer apple2 = 10;
    if(apple1 == apple2)
      System.out.println("apple1=apple2");
    else
      System.out.println("apple1!=apple2");
  }
}
A)
apple1=apple2
B)
apple1!=apple2
C) Compiler error
D) Non
Answer [=]
A
Explanation:

Both primitive and wrapper versions hold the same number. So, the comparison results in equality.

18) Autoboxing and unboxing in Java language saves the programmer from errors and extra lines of code. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

True. You can use either Primitive version or Wrapper object version interchangeably. The compiler or Runtime does not throw errors.

19) What is the output of the below Java code with Autoboxing and unboxing?
float f1 = 10.0f;
Float f2 = Float.valueOf(10);
if(f1 == f2)
  System.out.println("FLOATs are equal.");
else
  System.out.println("FLOATs are not equal.");
A) FLOATs are equal.
B) FLOATs are not equal.
C) Compiler error
D) None
Answer [=]
A
Explanation:

You can compare float and Float variables as these are automatically boxed and unboxed.

20) Choose the correct statement with auto-unboxing in Java?
A)
Short s1 = (short)30;
short s2 = s1;
B)
Boolean b1 = Boolean.valueOf(true);
boolean b2 = b1;
C)
Character ch1 = Character.valueOf('a');
char ch2 = ch1;
D) All the above
Answer [=]
D