Wednesday, January 17, 2007

SCJP 5.0 Mock Questions - Boxing Conversion

SCJP 5.0 Mock Questions - Boxing Conversion

SCJP 5.0 Mock Questions - Boxing Conversion



Question 1

public class Boxing1 {
public static void main(String[] args) {
Integer i = null;
method(i);
}
static void method(int k){
System.out.println(k);
}
}

What is the output of the above program?
1)Null Pointer Exception
2)Number Format Exception
3)null
4)0


Question 2

public class Boxing2 {
public static void main(String[] args) {
byte b = 10;
method(b);
}
static void method(int i){
System.out.println("Primitivae Type call");
}
static void method(Integer i){
System.out.println("Wrapper Type Call");
}
}

What will be output for the above program?
1)Wrapper Type Call
2)Primitive Type Call
3)Compiler Error
4)Compiles fine, throws runtime exception


Question 3

public class Boxing3 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Long l){
System.out.println("Widening conversion");
}
}


What will be output for the above program?
1)Widening conversion
2)Compiler Error
3)Runtime Exception
4)0

Question 4

public class Boxing4 {
public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}

What will be output for the above program?
1)falsefalse
2)truetrue
3)truefalse
4)falsetrue

Question 5

public class Boxing5 {
public static void main(String[] args) {
Integer i = 200;
Integer j = 200;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}

What will be output for the above program?
1)falsefalse
2)truetrue
3)truefalse
4)falsetrue

Question 6

public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3 == b4);
System.out.println(b1 == b4);
}
}

What is the output for the above program?
1)false true true false
2)false false false false
3)true true true true
4)false false true true


Question 7

public class Boxing7 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(long l){
System.out.println("long called");
}
static void method(Integer i){
System.out.println("Integer called");
}
}


What will be the output for the above program?
1)long called
2)Integer called
3)Compiler error
4)Runtime exception

Question 8

public class Boxing8 {
public static void main(String[] args) {
Integer i = 10;
int k = 10;
method(i,k);

}
static void method(int i,Integer k){
System.out.println("int,Integer called");
}
static void method(int i,int k){
System.out.println("int,int called");;
}
static void method(Integer i,Integer k){
System.out.println("Integer,Integer called");;
}
static void method(Integer i,int k){
System.out.println("Integer,int called");;
}
}


What will be the output for the above program?
1)int,Integer called
2)Integer,int called
3)int,int called
4)Integer,Integer called

Question 9

public class Boxing9 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Object o){
System.out.println("Object called");
}
static void method(Number n){
System.out.println("Number called");
}
}


What will be the output for the above program?
1)Object called
2)Number called
3)Compiler Error
4)Runtim Exception

Question 10

public class Boxing10 {
public static void main(String[] args) {
int i = 10;
int k = 20;
method(i,k);
}
static void method(Integer... i){
System.out.println("Integer varargs is called");
}
static void method(Integer i,Integer j){
System.out.println("Integer,Integer is called");
}
static void method(int... i){
System.out.println("int varargs is called");
}
}


What will be the output for the above program?

1)Integer varargs is called
2)Integer,Integer is called
3)int varargs is called
4)Compiler Error

Answers


1)
1)Null Pointer Exception

Explanation :
When wrapper type is null, we cannot do the boxing conversion. It will throw the NullpointerException when its is trying the convert to primitive type.
2)
2)Primitive Type Call

Explanation :
When comes to method overloading in Java 5.0, it works like the previous versions. First JVM will check for the matching primitive types, then it will search for the Wrapper types.

3)
2)Compiler Error

Explanation :
Primitive tpes cannot be widened to the Wrapper classes. This behaviour is restricted in Java 5.0.
4)
2)truetrue

Explanation :

Consider the following code fragment:
   Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 1000;
Integer i4 = 1000;
System.out.println(i1==i2);
System.out.println(i3==i4);

Can you guess what will be printed on the screen? If your answer is false--well, you're wrong.

In this case, J2SE 5.0 works differently. Certain ranges of values are stored as immutable objects by the Java Virtual Machine. So, in this case, the output is:

true
false

Normally, when the primitive types are boxed into the wrapper types, the JVM allocates memory and creates a new object. But for some special cases, the JVM reuses the same object.

The following is the list of primitives stored as immutable objects:

  • boolean values true and false
  • All byte values
  • short values between -128 and 127
  • int values between -128 and 127
  • char in the range u0000 to u007F

5)
4)falsetrue

Explanation :
When you assign a primitive to wrapper, compiler still creates a new object and assign into the same.

6)
1)false true true false

7)
1)long called
Explanation:
Method overloading gives preference to the primitive types.

8)

2)Integer,int called

9)

2)Number Called
Explanation: The int i was boxed to a Integer.The Integer reference was widened to an Object (since Integer extends Object).The method() method got an Number reference that actually refers to a Integer object.

10)

2)Integer,Integer is called
Explanation : Compiler will check for the matching method signature then it look for the
varargs method. This intend to maintain the behaviour of the previous versions.

Source :
www.javabeat.net
Author : Krishna Srinivasan(krishnas@javabeat.net)



No comments: