Mock Test 3

Home

1.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);

}}



What is the result of attempting to compile and run the program?

1.Prints: 0,0

2.Prints: 1,0

3.Prints: 0,1

4.Compile-time error

5.None of the above

Answer: 2

2)

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));

}}

What is the result of attempting to compile and run the program?
 

1.Prints: float,double

2.Prints: float,float

3.Prints: double,float

4.Compile-time error

5.None of the above

Answer: 4

3)

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?

1.     1

2.     2

3.     3

4.     4

5.      None of the above

Answer: 2

4)

1. StringBuffer s1 = new StringBuffer("abc");

2. StringBuffer s2 = s1;

3. StringBuffer s3 = new StringBuffer("abc");

How many objects are created ?

1.  0

2.  1

3.   2

4.  3

Answer : 4

5)

class c2

{

{

System.out.println("initializer");

}

public static void main(String a[])

{

System.out.println("main");

c2 ob1=new c2();

}

}

What is the result of attempting to compile and run the program?

1.prints main and initializer

2.prints initializer and main

3.compile time error

4.None of the above

Answer: 1

6)

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);

}

}

What is the result of attempting to compile and run the program?

1.Prints true,false

2.Print false,true

3.Prints true,true

4.compile time error

5.None of the above

Answer: 3

7)

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);

}}

What is the result of attempting to compile and run the program?
 

1.Prints nothing

2.Prints: 1

3.Prints: 01

4.compile-time error

5.None of the above

Answer: 2

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

8)

class bike

{

}

class arr extends bike{

public static void main(String[] args) {

arr[] a1=new arr[2];

bike[] a2;

a2=a1; //3

arr[] a3;

a3=a1; //5

}}

What is the result of attempting to compile and run the program?

1.compile time error at line 3

2.compile time error at line 5

3.Runtime exception

4.The code runs fine

5.None of the above

Answer: 4

Explanation:

bike is the superclass of arr.so they are compatible(superobject=subobject)

but subobject=superobject not allowed

9)

class C{

public static void main (String[] args) {

String s1="hjhh"; // 1

String s2="\u0002"; //2

String s3="'\\'"; //3

}}

What is the result of attempting to compile and run the program?

1.compile time error at line 1

2.compile time error at line 2

3.compile time error at line 3

4.Runtime exception

5.the code runs without any error

Answer: 5

Explanation:

A String literal is a sequence of characters enclosed in double quotes

10)

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

1.float

2.long

Answer: 1

Explanation:

float is wider than long, because the entire range of long fits within the range of float.