Java Placement Questions
Java Placement Questions
PART – A( 1 mark)
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
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
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
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
{}
}
5. class test
{
int x;
public static void main ( String [] args )
{
final int i;
i = 127;
byte b = i;
System.out.println(b);
}
}
6. class test
methodOne(20);
System.out.println("long");
System.out.println("float");
Prints long
Prints float
Compile error: Too ambiguous
Runtime error
Ans: a
import java.util.*;
7.
class test
List < String > least = new ArrayList < String > ();
meth(list);
seth(least);
System.out.println("List");
System.out.println("Mist");
Which function call(s) is/are allowed? Here allowed refers to legality, and that compilations succeeds.
import java.util.*;
8. class test
int Identify;
test(int ID)
Identify = ID;
9. import java.io.*;
try
t.start();
catch ( IOException e)
System.out.println(e);
System.out.println("int[]");
System.out.println("int ...");
a) Prints int[]
b) Prints int…
c) Compile error
d) Runtime error
Ans:C
System.out.println("hello");
}
class test
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
System.out.println( tester.x );
a) Prints 10
b) Prints 0
c) Compile error
d) Runtime error
Ans: C
13. class test
System.out.println("" + b + "20" );
a) Compile error
b) Runtime Exceptions are thrown.
c) Prints 120
d) Prints 12
Ans: A
System.out.println(m(2));
int ret;
if( i == 2 )
ret = i;
return ret;
a) Prints 2
b) Prints 0
c) Compile error
d) Runtime error
Ans: c
15.
for ( Object o : q )
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
System.out.printf("%1$b", "123");
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 void methodA(int i)
{
assert i >= 0 : methodB();
System.out.println(i);
}
2. What will happen when you attempt to compile and run the following code?
static
int x = 5;
}
x--; myMethod();
System.out.println(x + y + ++x);
y = x++ + ++x;
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;
else
else
B) Runtime error
Ans: A
4. enum cafe {
BIG ( 10 ) ,
SMALL ( 1 ),
MED ( 5 )
int mySize = 0;
mySize = size;
a. Compiles fine
b. Compiler error
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;
}
Answer: B