SCJP : API Contents autoboxing & unboxing Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
Boolean expr = true;
if (expr) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
options
A)true
B)Compile Error - can't use Boolean object in if().
C)false
D)Compile Properly but Runtime Exception.
Correct answer is : A
Explanations : In the if statement, condition can be Boolean object in jdk1.5 and jdk1.6.
In the previous version only boolean is allowed.
Questions no -2
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
List list = new ArrayList();
list.add(0, 59);
int total = list.get(0);
System.out.println(total);
}
}
options
A)59
B)Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();
C)Compile time error, because can't add primitive type in List.
D)Compile Properly but Runtime Exception.
Correct answer is : A
Explanations :Manual conversion between primitive types (such as an int) and wrapper classes (such as Integer) is necessary when adding a primitive data type to a collection in jdk1.4 but The new autoboxing/unboxing feature eliminates this manual conversion in jdk 1.5 and jdk 1.6.
Questions no -3
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
Integer i = null;
int j = i;
System.out.println(j);
}
}
options
A)0
B)Compile with error
C)null
D)NullPointerException
Correct answer is : D
Explanations :An Integer expression can have a null value. If your program tries to autounbox null, it will throw a NullPointerException.
|