Sei sulla pagina 1di 23

Java Placement Questions

PART – A( 1 mark)

1. What will be the result of compiling the following code:


public class Test {
public static void main (String args []) {
int age;
age = age + 1;
System.out.println(“The age is “ + age);
}
}
A. Compiles and runs with no output
B. Compiles and runs printing out The age is 1
C. Does not compile
D. Compiles but generates a compile time error
Ans: D (means that age is not initialized)

2. What is the result of compiling and running the following code:


public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}
}
A. The class will not compile
B. The compiler reports and error at line 2
C. The compiler reports an error at line 9
D. The value 10 is one of the elements printed to the standard output
Ans: D
3. Which of the following is correct:
A. String temp [] = new String {"j" "a" "z"};
B. String temp [] = {"a", "b", "c"};
C. String temp [] = { "j " " b" "c"};
D. String temp = {"a", "b", "c"};
Select the most appropriate answer.
Ans: B

4. What is the correct declaration of an abstract method that is intended to be public:


A. public abstract void add();
B. public abstract void add() {}
C. public abstract add();
D. public virtual add();
Select the most appropriate answer.
Ans: A

5. Given the following code what is the effect of a being 5:


public class Test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
System.out.println(i * j);
}}}}
A. Generate a runtime error
B. Throw an ArrayIndexOutOfBoundsException
C. Produces no output
D. Print the values: 1, 2, 2, 4
Select the most appropriate answer.
Ans: C

6. What is the result of executing the following code when the value of x is 2:
switch (x) {
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
A. Nothing is printed out
B. The value 3 is printed out
C. The values 3 and 4 are printed out
D. The values 1, 3 and 4 are printed out
Select the most appropriate answer.
Ans: C

7. Consider the following classes:


public class Test {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String args []) {
test();
}
}
What is the result of compiling and running this class?

A. The string Test is printed to the standard out.


B. The class fails to compile stating that the variable this is undefined.
C. Nothing is printed to the standard output.
D. An exception is raised stating that the method test cannot be found.

Select all correct answers.


Ans: B

8. What is the result of compiling and executing the following Java class:
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
suspend();
resume();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. Compilation will fail in the method main.
B. Compilation will fail in the method run.
C. A warning will be generated for method run.
D. The string “In run” will be printed to standard out.
Select the most appropriate answer.
Ans: D

9. Examine the following code, it includes an inner class:


public final class Test4 {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = true;
public void sample() {
System.out.println("Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}
What is the result?
A. Prints out “Sample”
B. Program produces no output but terminates correctly.
C. Program does not terminate.
D. The program will not compile
Select the most appropriate answer.
Ans: A

10. Examine the following code:


public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}
}
Does this code:
A. Produce a runtime error
B. Produce a compile time error
C. Print out “Total 0”
D. Generate the following as output:
i = 0 : j = 10
i=1:j=9
i=2:j=8
Total 30
Please select the most appropriate answer.
Ans: C

11. Given:
interface Foo {}
class Alpha implements Foo { }
class Beta extends Alpha {}
class Delta extends Beta {
public static void main( String[] args) {
Beta x = new Beta();
// insert code here
}
}
Which code, inserted at line 16, will cause a
java.lang.ClassCastException?
A. Alpha a = x;
B. Foo f= (Delta)x;
C. Foo f= (Alpha)x;
D. Beta b = (Beta)(Alpha)x;
Ans: B

12. Given:
public class Pass {
public static void main(String [1 args) {
int x 5;
Pass p = new Pass();
p.doStuff(x);
System.out.print(” main x = “+ x);
}
void doStuff(int x) {
System.out.print(” doStuff x = “+ x++);
}
}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 5 main x = 5
Ans: D

13. Given:
class Nav{
public enum Direction { NORTH, SOUTH, EAST, WEST }
}
public class Sprite{
// insert code here
}
Which code, inserted at line 14, allows the Sprite class to compile?
A. Nav.Direction d = Nav.Direction.NORTH;
B. Nav.Direction d = NORTH;
C. Direction d = Direction.NORTH;
B. Direction d = NORTH;
Answer: A

14. Given:
class TestA {
public void start() { System.out.println(”TestA”); }
}
public class TestB extends TestA {
public void start() { System.out.println(”TestB”); }
public static void main(String[] args) {
((TestA)new TestB()).start();
}
}
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B

15. public static void main (String args[]) {


int[]a1[]=new int[3][3]; //3
int a2[4]={3,4,5,6}; //4
int a2[5]; //5
}}

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


A. Compile time error at lines 3,4,5
B. Compile time error at line 4,5
C. Compile time error at line 3
D. Run time Exception

Ans: B

16. Given:
int x=12;
while (x < 10) {
x--;
}
System.out.print(x);
What is the result?
A. 0
B. 10
C. 12
D. Line 29 will never be reached.
Answer: C

17. Given:
int x= 10;
do {
x--;
} while(x< 10);
How many times will line 37 be executed?
A. ten times
B. zero times
C. one to me times
D. more than ten times
Answer: D
18. Which of the following Thread class methods are static ?
a) Sleep
b) Join
c) wait
d) notify
Ans: a
Part – B( 2 marks)
1. Given:
public static void main(String[] args) {
for (int i=0;i<= 10;i++){
if( i>6) break;
} System.out.println(i); }
What is the result?
A. 6
B. 7
C. Compilation fails.
D. 11
Answer: C

2. Given:
try {
// some code here
} catch (NullPointerException e1) {
System.out.print(”a”);
} catch (RuntimeException e2) {
System.out.print(”b”);
} finally {
System.out.print(”c”);
}
What is the result if a NullPointerException occurs on line 34?
A. c
B. a
C. ab
D. ac

Answer: D

3. Given:
Date date = new Date();
df.setLocale(Locale.ITALY);
String s = df.format(date);
The variable df is an object of type DateFormat that has been
initialized in line 11. What is the result if this code is run on December
14, 2000?
A. The value of s is 14-dic-2004.
B. The value of s is Dec 14, 2000.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D

4.

class Animal

void method throws IOException


{}

class dog extends Animal

void method throws FileNotFoundException

{}
}

a) Compile error : Incompatible types.


b) Runtime error
c) Compiles fine
d) Compile error : incorrect syntax
Ans: d

5. class test
{
int x;
public static void main ( String [] args )
{
final int i;
i = 127;
byte b = i;
System.out.println(b);

}
}

a. Compile error: loss of precision


b. No error. Compiles fine. Prints 0
c. Runtime error
d. Compiles with a warning
Ans : a

6. class test

public static void main ( String [] args )

methodOne(20);

static void methodOne( long l )


{

System.out.println("long");

static void methodOne( float f )

System.out.println("float");

Prints long
Prints float
Compile error: Too ambiguous
Runtime error
Ans: a

import java.util.*;

7.

class test

public static void main ( String [] args )

List < String > least = new ArrayList < String > ();

List list = new ArrayList();

meth(list);

seth(least);

public static void meth(List < String > list)

System.out.println("List");

public static void seth(List list)


{

System.out.println("Mist");

Which function call(s) is/are allowed? Here allowed refers to legality, and that compilations succeeds.

a) Both are allowed and there are no warnings


b) Both are not allowed - they don’t compile
c) Both are allowed but the code compiles with warnings
d) Meth is allowed but seth is not
Ans: C

import java.util.*;

8. class test

int Identify;

public static void main( String args[] )

Set set = new HashSet ();

System.out.println( set.add( "new" ) );

System.out.println( set.add( "new" ) );

System.out.println( set.add( new test(127) ) );

System.out.println( set.add( new test(127) ) );

test(int ID)

Identify = ID;

a) Prints true false true false


b) Prints true true true true
c) Prints true false true true
d) Prints false true false true
Ans: C

9. import java.io.*;

class test implements Runnable

public static void main( String args[] )

Thread t = new Thread(this);

try

t.start();

catch ( IOException e)

System.out.println(e);

public void run() throws IOException

File f = new File("f");

FileWriter fw = new FileWriter(f);

One Compile error


Runtime error
Compiles with warnings.
Two Compiler errors
Ans: D

10. class test

public static void main( String args[] )

test( new int[] { 1 , 2 } );

public static void test (int[] input)

System.out.println("int[]");

public static void test (int... input)

System.out.println("int ...");

a) Prints int[]
b) Prints int…
c) Compile error
d) Runtime error
Ans:C

11. class face

public void meth()

System.out.println("hello");
}

class test

public static void main( String args[] )

seth( new face(){} ); // 1

public static void seth( face f )

f.meth(); // 2

a) Compilations fails at 2
b) Compilations fails at 1
c) Runtime error at 2
d) Prints hello after successfully compiling.
Ans: D

12. class test{ int x = 10; static test tester = this;

public static void main( String args[] ){

System.out.println( tester.x );

a) Prints 10
b) Prints 0
c) Compile error
d) Runtime error
Ans: C
13. class test

public static void main( String... args )

Byte b = new Byte(1);

System.out.println("" + b + "20" );

a) Compile error
b) Runtime Exceptions are thrown.
c) Prints 120
d) Prints 12
Ans: A

14. public static void main(String [] args)

System.out.println(m(2));

static int m(int i)

int ret;

if( i == 2 )

ret = i;

return ret;

a) Prints 2
b) Prints 0
c) Compile error
d) Runtime error
Ans: c

15.

Queue q = new PriorityQueue();

q.offer(new String("hello "));

q.offer(new String("hi ") );

q.offer(new String("bye "));

for ( Object o : q )

System.out.println(q.poll() + " " + q.size() );

a) Prints hello 3 hi 2 bye 1


b) Prints hello 2 hi 1 bye 0
c) Prints bye 3 hello 2 hi 1
d) Prints bye 2 followed by an Exception.
Ans: d

16. List l = Arrays.asList( new int[] {1,2,3,4} );

for ( int i : l )

System.out.println(i);

a) Prints 1 2 3 4
b) Prints 4 3 2 1
c) Prints nothing
d) Compile error
Ans: d

17. class test

public static void main ( String [] blah )


{

System.out.printf("%s", new test());

public String toString()

return "testing something";

Prints testing something


Gives a runtime exception
Prints nothing
Prints test@13234 or something like that.
Ans: a

18. class test

public static void main ( String [] blah )

System.out.printf("%1$b", "123");

public String toString()

return "testing something";

Prints false
Prints true
Runtime Exception
Compile error
Ans: b

Part – C ( 3 marks)

1. What will happen when you attempt to compile and run the following code? (Assume that the
code is compiled and run with assertions enabled.)

public class AssertTest

{
public void methodA(int i)
{
assert i >= 0 : methodB();
System.out.println(i);
}

public void methodB()


{
System.out.println("The value must not be negative");
}

public static void main(String args[])


{
AssertTest test = new AssertTest();
test.methodA(-10);
}
}

A) It will print -10


B) It will result in Assertion Error showing the message -"The value must not be negative".
C) The code will not compile.
D) None of these
Ans: C

2. What will happen when you attempt to compile and run the following code?

public class Static

static

int x = 5;
}

static int x,y;

public static void main(String args[])

x--; myMethod();

System.out.println(x + y + ++x);

public static void myMethod()

y = x++ + ++x;

A) Compile time error

B) prints : 1

C) prints : 2

D) prints : 3

Ans: D

3. What will happen when you attempt to compile and run the following code?

interface MyInterface

}
public class MyInstanceTest implements MyInterface

static String s;

public static void main(String args[])

MyInstanceTest t = new MyInstanceTest();

if(t instanceof MyInterface)

System.out.println("I am true interface");

else

System.out.println("I am false interface");


}

if(s instanceof String)

System.out.println("I am true String");

else

System.out.println("I am false String");

A) Prints : "I am true interface" followed by " I am false String"

B) Runtime error

C) Prints : "I am true interface" followed by " I am true String"

D) Prints : "I am false interface" followed by " I am false String"

Ans: A

4. enum cafe {
BIG ( 10 ) ,

SMALL ( 1 ),

MED ( 5 )

int mySize = 0;

cafe ( int size )

mySize = size;

What happens when this enum is in the code outside a class ?

a. Compiles fine

b. Compiler error

c. Runtime Exception occurs if mySize is accessed.

d. Runtime Error

Ans: A
Part – D( 4 marks)
1. class Polish {
public static void main(String[] args) {
int x = 4;
StringBuffer sb = new StringBuffer("..fedcba");
sb.delete(3,6);
sb.insert(3, "az");
if(sb.length() > 6) x = sb.indexOf("b");
sb.delete((x-3), (x-2));
System.out.println(sb);
}
}
What is the result?
A. .faza
B. .fzba
C. ..azba
D. .fazba

Ans: C

2. Given:
String test= “a1b2c3”;
String[] tokens = test.split(”\\d”);
for(String s: tokens) System.out.print(s +“ “);
What is the result?
A. a b c
B. 1 2 3
C. a1b2c3
D. a1 b2 c3

Answer: A

3. Given:
public class Starter extends Thread {
private int x= 2;
public static void main(String[] args) throws Exception {
new Starter().makeItSo();
}
public Starter() {
x=5;
start();
}
public void makeItSo() throws Exception {
join();
x=x- 1;
System.out.println(x);
}
public void run() { x *= 2; }
}
What is the output if the main() method is rum?
A. 4
B. 5
C. 8
D. 9

Answer: D

4. Given
class Computation extends Thread {
private int num;
private boolean isComplete;
private int result;
public Computation(int num) { this.num = num; }
public synchronized void run() {
result = num * 2;
isComplete = true;
notify();
}
public synchronized int getResult() {
while (!isComplete) {
try {
wait();
} catch (InterruptedException e) { }
}
return result;
}

public static void main(String[] args) {


Computation[] computations = new Computation [4];
for (int i = 0; i < computations.length; i++) {
computations[i] = new Computation(i);
computations[i] .start();
}
for (Computation c : computations)
System.out.print(c.getResult() +“ “);
}
}
What is the result?
A. The code will deadlock.
B. The code may ruin with output “0 2 4 6”.
C. An exception is thrown at runtime.
D. The code may run with output “0 6”.

Answer: B

Potrebbero piacerti anche