Core Java Practice Test
1. Which of the following statements are true? Select 2 options
1) The idea of threading is the same as multi tasking.
2) No two threads can access the same variables
3) using synchronization within code may cause a performance penalty at
runtime.
4) The use of threads within a program may make the exact path of
execution unpredictable
answer : 1 & 2
2. What will happen when you attempt to compile and run the following code?
public class TadKebab{
public static void main(String argv[]){
new TadKebab();
}
private TadKebab(){
String[] Sa = {"Sam Smith", "John Smith"};
getName(Sa);
System.out.print(Sa[0]);
}
public void getName(String s[]){
s[0]="Coors";
}
}
Select 1 option
1) Compile time error, a constructor cannot be marked as private
2) Compilation and output of "Sam Smith"
3) Compilation and output of "Coors"
4) Compilation, but runtime error.
Answer : 3
3. Given the code. What is the result?
public class TrickyNum<X extends Object> {
private X x;
public TrickyNum(X x) {
this.x = x;
}
private double getDouble() {
return ((Double) x).doubleValue();
}
public static void main(String args[]) {
TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
System.out.print(a.getDouble());
}
}
A) Compilation fails.
B) An exception is thrown at runtime.
C) "1.0" is printed.
D) "1" is printed.
Answer : B
4. Given the code. What is the result?
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class TryMe {
public static void main(String args[]) {
List list = new LinkedList<String>();
list.add("one");
list.add("two");
list.add("three");
Collections.reverse(list);
Iterator iter = list.iterator();
for (Object o : iter) {
System.out.print(o + " ");
}
}
}
A) "three two one " is printed
B) "one two three " is printed
C) Nothing is printed
D) Compilation fails
E) An exception is thrown at runtime
Answer : D
5. What is true? (Choose three)
A) A method with the same signature as a private final method in class Z can be
implemented in a subclass of Z.
B) A final method in class Z can be abstract if and only if Z is abstract.
C) A protected method in class Z can be overriden by any subclass of Z.
D) A private static method can be called only within other static methods in class Z.
E) A non-static public final method in class Z can be overriden in any subclass of Z.
F) A public static method in class Z can be called by a subclass of Z without explicitly
referencing the class Z.
Answer A,C,D
6. Given the code. Which statements are true? (Select two)
public class Hotel {
public static void book() {
//some code goes here
}
public void cancelBooking() {
//some code goes here
}
}
A) Method book() can directly call method cancelBooking()
B) Method cancelBooking() can directly call method book()
C) Hotel.book() is a valid invocation of book()
D) Hotel.cancelBooking() is a valid invocation of cancelBooking()
Answer C.D
7. Given the code. What is the result?
public class SomeClass {
private int value = 1;
public void printVal(int value) {
System.out.print(value);
}
public static void main(String args[]) {
int a = 2;
SomeClass c = new SomeClass();
c.printVal(a);
}
}
A) "1" is printed
B) "2" is printed
C) Compilation fails
D) An exception is thrown at runtime
Answer : 2
8. Given the code. What is the result?
public class Hotel {
private static void book() {
System.out.print("book");
}
public static void main(String[] args) {
Thread.sleep(1);
book();
}
}
A) Compilation fails.
B) An exception is thrown at runtime.
C) "book" is printed.
D) The code executes normally, but nothing is printed
Answer : A
9. Give a piece of code. Which two are possible results? (Select two)
public class Cruiser {
private int a = 0;
public void foo() {
Runnable r = new LittleCruiser();
new Thread(r).start();
new Thread(r).start();
}
public static void main(String arg[]) {
Cruiser c = new Cruiser();
c.foo();
}
public class LittleCruiser implements Runnable {
public void run() {
int current = 0;
for (int i = 0; i < 4; i++) {
current = a;
System.out.print(current + ", ");
a = current + 2;
}
}
}
}
A) 0, 2, 4, 0, 2, 4, 6, 6,
B) 0, 2, 4, 6, 8, 10, 12, 14,
C) 0, 2, 4, 6, 8, 10, 2, 4,
D) 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E) 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
Answer: B
10. Given the exhibit. What is the result?
public class Hotel {
public static void book(short a) {
System.out.print("short ");
}
public static void book(Short a) {
System.out.print("SHORT ");
}
public static void book(Long a) {
System.out.print("LONG ");
}
public static void main(String[] args) {
short shortRoom = 1;
int intRoom = 2;
book(shortRoom);
book(intRoom);
}
}
A) SHORT LONG
B) short LONG
C) Compilation fails
D) An exception is thrown at runtime
Answer C
11. Given the code. What is the result?
import java.io.*;
public class Hotel implements Serializable {
private Room room = new Room();
public static void main(String[] args) {
Hotel h = new Hotel();
try {
FileOutputStream fos = new FileOutputStream("Hotel.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(h);
oos.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
class Room {
}
A) Compilation fails.
B) An instance of Hotel is serialized.
C) An instance of Hotel and an instance of Room are both serialized.
D) An exception is thrown at runtime.
Answer : B
12. Given the code. What is the result?
public static void main(String args[]) {
try {
String arr[] = new String[10];
arr = null;
arr[0] = "one";
System.out.print(arr[0]);
} catch(NullPointerException nex) {
System.out.print("null pointer exception");
} catch(Exception ex) {
System.out.print("exception");
}
}
A) "one" is printed.
B) "exception" is printed.
C) "null pointer exception" is printed.
D) Compilation fails.
Answer : D
13. Given the code. What is the result?
import java.io.*;
public class Hotel implements Serializable {
private Room room = new Room();
public static void main(String[] args) {
Hotel h = new Hotel();
try {
FileOutputStream fos = new FileOutputStream("Hotel.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(h);
oos.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
class Room implements Serializable {
}
A) Compilation fails.
B) An exception is thrown at runtime.
C) An instance of Hotel is serialized.
D) An instance of Hotel and an instance of Room are both serialized.
Answer : C
14. Given the code. What is the output?
1. public static void main(String args[]) {
2. Object myObj = new String[]{"one", "two", "three"};{
3. for (String s : (String[])myObj) System.out.print(s + ".");
4. }
5. }
A) one.two.three.
B) Compilation fails because of an error at line 2
C) Compilation fails because of an error at line 3
D) An exception is thrown at runtime.
Answer : C
15. Given the code. What is the result?
public class Test {
private static void doStuff(String str) {
int var = 4;
if (var == str.length()) {
System.out.print(str.charAt(--var) + " ");
}
else {
System.out.print(str.charAt(0) + " ");
}
}
public static void main(String args[]) {
doStuff("abcd");
doStuff("efg");
doStuff("hi");
}
}
A) Compilation fails.
B) An exception is thrown at runtime.
C) d e h
D) d f i
E) c f i
F) c e h
Answer : C
16. Which code, inserted inserted at line labeled "//some code goes her", allows
the class Test to be compiled?
class Util {
public enum State{ACTIVE, DELETED, INACTIVE}
}
public class Test {
public static void main(String args[]) {
//some code goes here
}
}
A) State state = State.INACTIVE;
B) State state = INACTIVE;
C) Util.State state = Util.State.INACTIVE;
D) State state = Util.INACTIVE;
Answer: B
17. Given the code. What is the result?
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class HashTest {
private String str;
public HashTest(String str) {
this.str = str;
}
@Override
public String toString() {
return this.str;
}
public static void main(String args[]) {
HashTest h1 = new HashTest("2");
String s1 = new String("1");
List<Object> list = new LinkedList<Object>();
list.add(h1);
list.add(s1);
Collections.sort(list);
for (Object o : list) {
System.out.print(o + " ");
}
}
}
A) "2 1" is printed.
B) "1 2" is printed.
C) Compilation fails.
D) An exception is thrown at runtime.
Answer : C
18. Given the code. What is the result?
public class TrickyNum<X extends Object> {
private X x;
public TrickyNum(X x) {
this.x = x;
}
private double getDouble() {
return x.doubleValue();
}
public static void main(String args[]) {
TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
System.out.print(a.getDouble());
}
}
A) Compilation fails.
B) An exception is thrown at runtime.
C) "1.0" is printed.
D) "1" is printed.
Answer : B
19. Given the code. What is the result?
1. public static void main(String args[]) {
2. Object myObj = new String[]{"one", "two", "three"} {
3. for (String s : (String[])myObj) System.out.print(s + ".");
4. }
5. }
A) one.two.three.
B) Compilation fails because of an error at line 2
C) Compilation fails because of an error at line 3
D) An exception is thrown at runtime.
Answer C
20 Given the code. Select correct calls of methods. (Select all that apply)
import java.util.*;
class Empty {
}
class Extended extends Empty {
}
public class TryMe {
public static void doStuff1(List<Empty> list) {
// some code
}
public static void doStuff2(List list) {
// some code
}
public static void doStuff3(List<? extends Empty> list) {
// some code
}
public static void main(String args[]) {
List<Empty> list1 = new LinkedList<Empty>();
List<Extended> list2 = new LinkedList<Extended>();
// more code here
}
}
A) doStuff1(list1);
B) doStuff2(list2);
C) doStuff2(list1);
D) doStuff3(list1);
E) doStuff3(list2);
F) doStuff1(list2);
Answer A,D,B