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.
Yes. The compiler does all the Autoboxing and unboxing for you.
Yes. Java 5 introduced many new features like AUTOBOXING, UNBOXING, advanced FOR loop, VARARGS, ENUM, GENERICS etc.
Integer temperature1 = 100;
int temperature2 = 101;
Integer temperature1 = 100; int temperature2 = 101;
As part of auto-boxing, an INT number is converted to INTEGER object automatically.
Float f1 = 1.0f;
Boolean bull = false;
Character ch = 'a';
long weight = 10; Long wei = weight; System.out.println(ch.SIZE);
Yes. You can assign a primitive LONG value to a Wrapper LONG object. The assignment undergoes auto-boxing by the compiler.
TRUE
False. The method type can be anything.
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); } }
Autoboxing or unboxing works with method arguments or parameters.
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.
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
Implicit.
Character ch = 'S'; switch(ch) { case 'S': System.out.println("OK"); break; case 'p': break; } //OUTPUT OK
Integer t = 90; int abc = ++t; System.out.println(abc); //OUTPUT 91
int b = 10; Integer c = 20; if(b < c) { System.out.println("LESS"); } //OUTPUT LESS
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"); } }
apple1=apple2
apple1!=apple2
Both primitive and wrapper versions hold the same number. So, the comparison results in equality.
True. You can use either Primitive version or Wrapper object version interchangeably. The compiler or Runtime does not throw errors.
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.");
You can compare float and Float variables as these are automatically boxed and unboxed.
Short s1 = (short)30; short s2 = s1;
Boolean b1 = Boolean.valueOf(true); boolean b2 = b1;
Character ch1 = Character.valueOf('a'); char ch2 = ch1;