Sunday, February 5, 2012

SCJP model question answer page2


11)
class C{
            static int f1(int i)
                        {
                        System.out.print(i + ",");
                        return 0;
                        }
public static void main (String[] args) {
int i = 0;
i = i++ + f1(i);
System.out.print(i);
}
}


Prints: 0,0

Prints: 1,0

Prints: 0,1

Compile-time error

None of the above

Ans: 2


12)
class C{
            static String m(int i) {return "int";}
            static String m(float i) {return "float";}
            public static void main (String[] args)
            {
                        long a1 = 1; double b1 = 2;
                        System.out.print(m(a1)+","+ m(b1));
            }
}


Prints: float,double

Prints: float,float

Prints: double,float

Compile-time error

None of the above

Ans: 4


13)
class C
{
            public static void main(String a[])
            {
            C c1=new C();
            C c2=m1(c1);
            C c3=new C();
            c2=c3;              //6
            anothermethod();
            }
            static C m1(C ob1)
            {
            ob1 =new C();
            return ob1;
            }
}
After line 6, how many objects are eligible for garbage collection?


Ans: 1

Ans: 2

Ans: 3

Ans: 4

None of the above

Ans: 2

14)
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");
How many objects are created ?

Ans: 0

Ans: 1

Ans: 2

Ans: 3


Currect Ans: 4

15)
class c2
{
            {
             System.out.println("initializer");
            }
            public static void main(String a[])
            {
            System.out.println("main");
             c2 ob1=new c2();
            }
}


prints main and initializer

prints  initializer and main

compile time error

None of the above

Ans: 1
16)
class c1
{
            public static void main(String a[])
            {
            c1 ob1=new c1();
            Object ob2=ob1;
            System.out.println(ob2 instanceof Object);
            System.out.println(ob2 instanceof c1);
            }
}


Prints true,false

Print false,true

Prints true,true

compile time error

None of the above

Ans: 3

17)
class A extends Thread {
            private int i;
            public void run()
            {
                        i = 1;
            }
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}
}


Prints nothing

Prints: 1

Prints: 01

Compile-time error

None of the above

Explanation:
a.run() method was called instead of a.start(); so the full program runs as a single thread so a.run() is guaranteed to complete
Ans: 2


18)
class bike
{
}
class arr extends bike{
public static void main(String[] args) {
arr[] a1=new arr[2];   
bike[] a2;            
a2=a1;                
arr[] a3;             
a3=a1;               
}}


compile time error at line 3

compile time error at line 5

Runtime exception

The code runs fine

None of the above

Explanation:
bike is the superclass of arr.so they are compatible(superobject=subobject)
but subobject=superobject not allowed
Ans: 4


19)
class C{
            public static void main (String[] args)
            {
            String s1="hjhh";        // 1
            String s2="\u0002";      //2
            String s3="'\\'";        //3
            }
}


compile time error at line 1

compile time error at line 2

compile time error at line 3

Runtime exception

the code runs without any error

Explanation:
A String literal is a sequence of characters enclosed in double quotes
Ans: 5


20)
Which data type is wider for the purpose of casting: float or long?

float

long

Explanation:
float is wider than long, because the entire range of long fits within the range of float.
Ans: 1

No comments:

Post a Comment