50 real SCJP Mock Exam Questions. Write your answers down
SCJP Threads
QUESTION : 1
True or false?
Any class that implements the Runnable interface
has to provide the implementation for the following methods:
public void start();
public void run();
1. True.
2. False.
QUESTION : 2
True or false?
A thread that has called the wait() method of an object
still owns the lock of the object.
1. True
2. False
QUESTION : 3
A number of threads of the same priority have relinquished the lock
on a monitor and are in a waiting state after having called the wait()
method of the object. A new thread enters the monitor and calls the
notifyAll() method of the meonitor. Which of these threads will be the
first one to resume?
1. The thread that has been waiting the longest.
2. The thread that was the last one to to exit the monitor.
3. You can never be sure which thread will get to run first.
4. The the first thread that called the wait() method
QUESTION : 4
Which of these are valid contructors of a Thread object.
1. public Thread(Object obj)
2. public Thread(String name)
2. public Thread(Runnable trgt)
4. public Thread(ThreadGroup grp, Runnable trgt, String name)
5. public Thread(ThreadGroup grp, Object ob)
QUESTION : 5
True or false?
If you call the interrupted() method of a thread object twice
the second call will always return false.
1. True
2. False
QUESTION : 6
True or false?
If you call the isInterrupted() method of a thread object twice
the second call will always return false.
1. True
2. False
QUESTION : 7
Which of the following are methods of the Thread class.
1. public void run()
2. public void start()
3. public void exit()
4. public final void setAccess()
5. public final void setPriority(int priNbr)
6. public final int getPriority()
QUESTION : 8
Consider the following class
True or False?public class Test implements Runnable{ public void run(){} }
Creating an instance of this class and calling its run() method
will spawn a new thread.
1. True
2. False
QUESTION : 9
True or false?
A Thread object has a method called notify().
1. False
2. True
QUESTION : 10
Calling the destroy() method of a thread object relases all the locks held by
the thread ?
1. True
2. False
SCJP Collections
QUESTION : 1
What is the result of attempting to compile and run the following code?
1. The code will fail to compile.public class Test1{ public static void main(String[] args) { Integer int1 = new Integer(10); Vector vec1 = new Vector(); LinkedList list = new LinkedList(); vec1.add(int1); list.add(int1); if(vec1.equals(list)) System.out.println("equal"); else System.out.println("not equal"); } }
2. Runtime error due to incompatible object comparison
3. Will run and print "equal".
4. Will run and print "not equal".
QUESTION : 2
What is the result of attempting to compile and run the following code?
1. Will print [8, 4]
2. Will print [4, 8, 4]
3. Will print [8, 4, 4]
QUESTION : 3
What is the result of attempting to compile and run the following code?
1. Will print [8, 4]
2. Will print [4, 8, 4]
3. Will print [8, 4, 4]
4. Will print [4, 8]
5. Will print [4, 4, 8]
QUESTION : 4
What will this print out ?
1. 8 , 4 and 4public class Test { public static void main(String[] args){ Integer a = new Integer(8); Integer b = new Integer(4); Integer c = new Integer(4); Vector vec = new Vector(); Iterator itr; vec.add(a); vec.add(b); vec.add(c); itr = vec.iterator(); while (itr.hasNext()) { System.out.println("" + itr.next()); } } }
2. 4 , 4 and 8
3. 8 and 4
4. 4 and 8
QUESTION : 5
Which of these statements are true?
1. HashTable is a sub class of Dictionary
2. ArrayList is a sub class of Vector
3. LinkedList is a subclass of ArrayList
4. Stack is a subclass of Vector
QUESTION : 6
Which of these statements are true?
1. LinkedList extends List
2. AbstractSet extends Set
3. HashSet extends AbstractSet
4. WeakHashMap extends HashMap
5. TreeSet extends AbstractSet
QUESTION : 7
Which of these statements are true?
1. A HashSet does not permit duplicates
2. A Vector permits duplicates
3. A TreeSet is an ordered Set
4. A LinkedList is sorted in descending order
5. A LinkedList is sorted in ascending order
QUESTION : 8
True or False?
A WeakHashMap is synchronized.
1. True
2. False
QUESTION : 9
True or False?
A Set rejects duplicates and is ordered
1. True
2. False
QUESTION : 10
Select the true statements
1. AbstractSet extends AbstractCollection
2. AbstractList extends AbstractCollection
3. HashSet extends AbstractSet
4. Vector extends AbstractList
5. AbstrctSequentialList extends AbstractList
6. LinkedList extends AbstrctSequentialList
SCJP Object Oriented Programming
QUESTION : 1
What is the result of compiling and running this program?
1. prints "Mammal eats food"class Mammal{ void eat(Mammal m){ System.out.println("Mammal eats food"); } } class Cattle extends Mammal{ void eat(Cattle c){ System.out.println("Cattle eats hay"); } } class Horse extends Cattle{ void eat(Horse h){ System.out.println("Horse eats hay"); } } public class Test{ public static void main(String[] args){ Mammal h = new Horse(); Cattle c = new Horse(); c.eat(h); } }
2. prints "Cattle eats hay"
3. prints "Horse eats hay"
4. Class cast Exception at runtime.
QUESTION : 2
Consider the following class hierarchy.
What will be the outcome on attempting to compile and run this ?1. interface A{ 2. public void method1(); 3. } 4. class One implements A{ 5. public void method1(){ 6. System.out.println("hello"); 7. } 8. } 9. class Two extends One{} 10. public class Test extends Two{ 11. public static void main(String[] args) 12. { 13. A a; 14. Two t = new Two(); 15. a = t; 16. a.method1(); 17. } 18. }
1. Compiles and runs printing out "hello".
2. Compilation error at line 16.
3. The compiler raises an objection to the assignment at line 15.
4. Throws a NoSuchMethodException at runtime.
QUESTION : 3
What will happen if you try to compile and run this?
1. Compiler error.
2. NoSuchMethodException at runtime.
3. Compiles and runs printing 1
4. Throws a NullPointerException at runtime.
QUESTION : 4
What will happen if you try to compile and run this code?
1. Will not compile.class Rectangle{ public int area(int length , int width) { return length * width; } } class Square extends Rectangle{ public int area(long length , long width) { return (int) Math.pow(length ,2); } } class Test{ public static void main(String args[]) { Square r = new Square(); System.out.println(r.area(5 , 4)); } }
2. Will compile and run printing out 20
3. Runtime error
4. Will compile and run printing out 25
QUESTION : 5
What will be the result of attempting to compile and run this.
1. Will not compileclass Base{} class Derived extends Base{} public class Test { public static void main(String[] args){ Derived d = (Derived) new Base(); } }
2. Compiles and runs without error.
3. Runtime error
QUESTION : 6
What will this program print out?
1. 10class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; } } public class Test { public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }
2. 20
3. 30
4. 40
QUESTION : 7
Almost the same code as in the previous question.
The only difference is the methods are static now.
What will it print now?
1. 10class Base{ static int value = 0; Base(){ addValue(); } static void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } static void addValue(){ value += 20; } } public class Test { public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }
2. 20
3. 30
4. 40
QUESTION : 8
What is the result of attempting to compile and run this?
1. Code will not compileinterface ITest{ public void setVal(); } public class Test { private String a; void aMethod(){ final String b; ITest it = new ITest() { public void setVal(){ a = "Hello"; b = " World"; }}; it.setVal(); System.out.println(a + b); } public static void main(String[] args) { Test t = new Test(); t.aMethod(); } }
2. Run time error
3. Will compile and run printing "Hello"
4. Will compile and run without any output
QUESTION : 9
What is the result of attempting to compile and run this?
1. Code will not compileclass Base{ String s = "Base"; String show(){ return s; } } class Derived extends Base{ String s = "Derived"; } public class Test { void print(Base b){ System.out.println(b.show()); } void print(Derived d){ System.out.println(d.show()); } public static void main(String[] args){ Test t = new Test(); Base b = new Derived(); t.print(b); } }
2. Run time error
3. Will compile and run printing "Derived"
4. Will compile and run printing "Base"
QUESTION : 10
What is the result of attempting to compile and run this ?
1. Code will not compileinterface ITest{ public void setVal(); } public class Test { private String a; void aMethod(){ final String b = " World"; ITest it = new ITest() { public void setVal(){ a = "Hello" + b; }}; it.setVal(); System.out.println(a); } public static void main(String[] args) { Test t = new Test(); t.aMethod(); } }
2. Run time error
3. Will compile and run printing "Hello World"
4. Will compile and run printing "Hello"
SCJP Language Fundamentals
QUESTION : 1
You have the following code in a file called Test.java
What will happen if you try to compile and run this?
1. It will fail to compile.
2. Runtime error
3. Compiles and runs with no output.
4. Compiles and runs printing "Hello"
QUESTION : 2
What is the result of trying to compile and run the following code?
1. output Positive infinity
2. output Negative infinity
3. Will fail to compile
4. Runtime exception
QUESTION : 3
What is the result that will be printed out?
1. 2
2. 0
3. 3
4. 2.5
5. 25
QUESTION : 4
Which of the following are valid declarations?
Note : None of the literals used here
contain the character O they are all zeroes.
1. int i = 0XCAFE;
2. boolean b = 0;
3. char c = 'A';
4. byte b = 128;
5. char c = "A";
QUESTION : 5
What is the result of trying to compile and run this program?
1. Compiler error.
2. Compiles and runs printing out 2
3. Compiles and runs printing out 1
4. An ArrayIndexOutOfBounds Exception at runtime
QUESTION : 6
What will happen if you try to compile and run this?
1. Compiler error.
2. Will throw a NoSuchMethod error at runtime.
3. It will compile and run printing out "10"
4. It will run with no output.
5. It will run and print "10" and then crash with an error.
QUESTION : 7
Is this legal?
1. Yeslong longArr[]; int intArr[] = { 7 ,8 , 9}; longArr = intArr;
2. No
QUESTION : 8
True or False?
The range of a byte is from -127 to 128
1. True
2. False
QUESTION : 9
Identify the valid assignments.
1. float f = \u0038;
2. long L2 = 2L;
3. float f = 1.2;
4. char c = '/u004E';
5. byte b = 100;
QUESTION : 10
What is the result of trying to compile and run the following code?
1. output Positive infinity
2. output Negative infinity
3. Will fail to compile
4. Runtime exception
SCJP java.lang
QUESTION : 1
What is the result of attempting to compile and run this?
1. The code will fail to compile.
2. Compile and run printing out "orld".
3. Compile and run printing out "oworl"
4. Compile and run printing out "World"
5. Run time exception
QUESTION : 2
Select one right answer.
-----------------------
What is the result of attempting to compile and run this ?
1. Compiler error caused by line 4.
2. Compiler error caused by line 7.
3. Compiler error caused by line 9.
4. Compiles and throws a NumberFormatException at runtime.
5. Compiles and runs printing out a number.
6. Compiles and runs printing out an alphabet.
QUESTION : 3
What is the value of d that will be printed out.
1. 22
2. 22.0
3. -22
4. -23
5. -22.0
6. 23.0
QUESTION : 4
What is the result of attempting to compile and run this ?
1. It will print "World"public class Test { public static void main(String[] args) { StringBuffer a = "Hello"; StringBuffer b = a.append("World"); System.out.println(a); } }
2. It will print "HelloWorld"
3. It will print "Hello World"
4. The code will not compile.
5. It will print "Hello"
6. It will throw a runtime exception
QUESTION : 5
Which of the follwing are valid methods of the String class.
1. String append(String s);
2. int length();
3. String toString(String str);
4. String trim();
5. int indexOf(int ch);
6. String append(char c);
QUESTION : 6
What is the result of attempting to compile and run this?
1. Does not compile
2. Compiles and runs printing out "32"
3. Compiles and runs printing out "32.0"
4. Compiles but throws an error at runtime
QUESTION : 7
What is the result of attempting to compile and run this?
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "0"
5. Runs and prints "1"
6. Runs adn prints "-1"
QUESTION : 8
What is the result of attempting to compile and run this?
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "true"
QUESTION : 9
What is the result of attempting to compile and run this ?
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "true"
QUESTION : 10
What is the result of attempting to compile and run this?
1. Compiler error
2. Runtime error
3. Runs and prints "256"
4. Runs and prints "0"
5. Runs and prints "127"
-------------------
Good luck!!