Threads is a valuable knowledge for every developer. This free mock exam presents important Thread skills required by the SCJP exam.
Make sure to study the API of Runnable, Thread and Object before moving on.
- Runnable is an interface. (1 correct answer)
- true
- false
- Runnable belongs to the java.lang package. (1 correct answer)
- true
- false
- How many methods does the Runnable interface define? (1 correct answer)
- One: run()
- Two: run() and start()
- Three: run(), start() and join()
- Runnable defines a method run. What’s the signature? (1 correct answer)
- public void run()
- public void run() throws InterruptedException
- public void run(Runnable r)
- public void run(Runnable r) throws InterruptedException
- Class Thread implements the Runnable interface. (1 correct answer)
- true
- false
- Consider these methods.
⇒ run()
⇒ start()
⇒ join()
⇒ sleep()
⇒ yield()
⇒ currentThread()
How many of them are defined in the Thread class? (1 correct answer)- One.
- Two.
- Three.
- All.
- Consider these methods.
⇒ run()
⇒ start()
⇒ join()
⇒ sleep()
⇒ yield()
⇒ currentThread()
How many of them are static? (1 correct answer)- One.
- Two.
- Three.
- All.
- Consider these methods.
⇒ run()
⇒ start()
⇒ join()
⇒ sleep()
⇒ yield()
⇒ currentThread()
How many of them declare a checked exception? (1 correct answer)- One.
- Two.
- Three.
- All.
- What’s the signature of the method start()? (1 correct answer)
- public void start()
- public void start() throws InterruptedException
- public void start() throws IllegalThreadStateException
- What’s the signature of the method yield()? (1 correct answer)
- public void yield()
- public void yield() throws InterruptedException
- public static void yield()
- public static void yield() throws InterruptedException
- There are 2 overloaded methods of Thread with the name sleep. (1 correct answer)
- true
- false
- There are 3 overloaded methods of Thread with the name join. (1 correct answer)
- true
- false
- The Thread class has three public static and final integer fields: MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY. (1 correct answer)
- true
- false
- Consider these methods.
⇒ wait()
⇒ notify()
⇒ notifyAll()
How many of them are defined in the Object class? (1 correct answer)- One.
- All.
- None.
- Consider these methods.
⇒ wait()
⇒ notify()
⇒ notifyAll()
How many of them are static? (1 correct answer)- One.
- All.
- None.
- Consider these methods.
⇒ wait()
⇒ notify()
⇒ notifyAll()
How many of them declare a checked exception? (1 correct answer)- One.
- All.
- None.
- There are 3 overloaded methods of Object with the name wait. (1 correct answer)
- true
- false
- Will this code compile successfully? (1 correct answer)
class Test extends Runnable { }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test implements Runnable { }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test implements Runnable { void run() { } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test implements Runnable { public void run() { } void run(Runnable r) { } void run(String... s) { } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public Test() { super(); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public Test() { super("Nikos"); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public Test(String name) { super(name); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public Test(Runnable job, String name) { super(job); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public Test(Runnable job, String name) { super(job, name); } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
import java.util.NavigableSet; class Test extends Thread { public void run(NavigableSet<Thread> set) { } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public void run() throws InterruptedException { } }- Yes.
- No.
- Will this code compile successfully? (1 correct answer)
class Test extends Thread { public void join() throws InterruptedException { } }- Yes.
- No.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test extends Thread { public void run() { System.out.println("run 1"); } public void run(String s) { System.out.println("run 2"); } public static void main(String[] args) { new Test().start(); } }- It prints “run 1″.
- It prints “run 2″.
- Compilation fails.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test extends Thread { public void run() { System.out.println("run 1"); } public void run(String s) { System.out.println("run 2"); } public static void main(String[] args) { new Test().start("hi"); } }- It prints “run 1″.
- It prints “run 2″.
- Compilation fails.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test { public static void main(String[] args) { String name = Thread.currentThread().getName(); System.out.println(name); } }- It prints nothing.
- It prints “main”.
- It prints “Thread-0″.
- Compilation fails because an InterruptedException is not handled.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test extends Thread { public static void main(String[] args) { String name = Thread.currentThread().getName(); System.out.println(name); } }- It prints nothing.
- It prints “main”.
- It prints “Thread-0″.
- Compilation fails because an InterruptedException is not handled.
- Compilation fails because Test does not implement the run() method.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { new Thread().run(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.run(); thread.run(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { new Thread().start(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); thread.start(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); thread = new Thread(); thread.start(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); Thread.yield(); thread.start(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { Thread thread = new Thread(); thread.run(); thread.start(); } }- Compilation fails.
- It compiles and runs fine.
- An IllegalThreadStateException is thrown at runtime.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { System.out.print("A "); new Thread(new Thread(new Thread())).start(); System.out.println("B"); } }- It prints “A B”.
- Compilation fails.
- It prints “A ” and an exception is thrown.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { System.out.print("A "); new Thread(new Thread(new Thread())).run(); System.out.println("B"); } }- It prints “A B”.
- Compilation fails.
- It prints “A ” and an exception is thrown.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { Thread thread = new Thread() { public void run() { System.out.println("Hello!"); } }; thread.start(); } }- It prints “Hello!”.
- Compilation fails.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { new Thread() { public void run() { System.out.println("Hello!"); } }.start(); } }- It prints “Hello!”.
- Compilation fails.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { new Runnable() { public void run() { System.out.println("Hello!"); } }.start(); } }- It prints “Hello!”.
- Compilation fails.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { new Thread(new Runnable() { public void run() { System.out.println("Hello!"); } }).start(); } }- It prints “Hello!”.
- Compilation fails.
- These classes are defined in the same file. What is the output? (1 correct answer)
class Job implements Runnable { public void run() { System.out.println("Working..."); } } public class My { public static void main(String[] args) { Thread thread1 = new Thread(new Job()); Thread thread2 = new Thread(new Job()); thread1.start(); thread2.start(); } }- Compilation fails.
- It prints “Working…” three times.
- It prints “Working…” one time and then an exception is thrown.
- These classes are defined in the same file. What is the output? (1 correct answer)
class Job implements Runnable { public void run() { System.out.println("Working..."); } } public class My { public static void main(String[] args) { Job job = new Job(); Thread thread1 = new Thread(job); Thread thread2 = new Thread(job); thread1.start(); thread2.start(); } }- Compilation fails.
- It prints “Working…” three times.
- It prints “Working…” one time and then an exception is thrown.
- What is the output? (1 correct answer)
public class Test extends Thread { public void run() { System.out.println(isAlive()); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.isAlive()); test.start(); } }- false false
- false true
- true false
- true true
- What is the output? (1 correct answer)
public class Test extends Thread { public void run() { System.out.println(isAlive()); } public static void main(String[] args) { Test test = new Test(); System.out.println(test.isAlive()); test.run(); } }- false false
- false true
- true false
- true true
- What is the output? (1 correct answer)
public class Test extends Thread { public Test() { System.out.println(isAlive()); } public static void main(String[] args) { new Test(); } }- false
- true
- What is the output? (1 correct answer)
public class Test extends Thread { public Test() { System.out.println(isAlive()); } public static void main(String[] args) { new Test().run(); } }- false
- true
- What is the output? (1 correct answer)
public class Test extends Thread { public Test() { System.out.println(isAlive()); } public static void main(String[] args) { new Test().start(); } }- false
- true
- Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
public class Account { private Integer number = 0; synchronized public void setNumber(Integer number) { this.number = number; } synchronized public Integer getNumber() { return number; } }- Yes.
- No.
- Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
public class Account { private Integer number = 0; public void setNumber(Integer number) { synchronized (this) { this.number = number; } } synchronized public Integer getNumber() { return number; } }- Yes.
- No.
- Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
public class Account { private Integer number = 0; public void setNumber(Integer number) { synchronized (this) { this.number = number; } } public Integer getNumber() { synchronized (this) { return number; } } }- Yes.
- No.
- These classes are defined in a single file. There’s a value object Account that carefully protects it’s state from concurrent access, a Client class of type Thread that puts some money in an account, and a main method that simply starts two clients.
class Account { private Integer number = 0; public synchronized void setNumber(Integer number) { this.number = number; } public synchronized Integer getNumber() { return number; } } class Client extends Thread { Account account; public Client(Account account) { this.account = account; } public void run() { for (int i = 1; i <= 1000; i++) { account.setNumber(account.getNumber() + 1); } } } public class Run { public static void main(String[] args) throws Exception { Account account = new Account(); Client one = new Client(account); Client two = new Client(account); one.start(); two.start(); one.join(); two.join(); // here } }Just before the main method exits, the account’s number field is guaranteed to have value 2000.
- true
- false
- These classes are defined in a single file.
class Account { private Integer number = 0; public synchronized void increase() { number++; } public synchronized Integer getNumber() { return number; } } class Client extends Thread { Account account; public Client(Account account) { this.account = account; } public void run() { for (int i = 1; i <= 1000; i++) { account.increase(); } } } public class Run { public static void main(String[] args) throws Exception { Account account = new Account(); Client one = new Client(account); Client two = new Client(account); one.start(); two.start(); one.join(); two.join(); // here } }Just before the main method exits, the account’s number field is guaranteed to have value 2000.
- true
- false
- These classes are defined in a single file.
class Account { private Integer number = 0; public void increase() { number++; } public synchronized Integer getNumber() { return number; } } class Client extends Thread { Account account; public Client(Account account) { this.account = account; } public void run() { for (int i = 1; i <= 1000; i++) { account.increase(); } } } public class Run { public static void main(String[] args) throws Exception { Account account = new Account(); Client one = new Client(account); Client two = new Client(account); one.start(); two.start(); one.join(); two.join(); // here } }Just before the main method exits, the account’s number field is guaranteed to have value 2000.
- true
- false
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test { public static void main(String[] args) { String message = "Hello!"; message.wait(); // 1 System.out.println(message); } }- Compilation fails.
- It prints “Hello!”.
- An IllegalMonitorStateException is thrown at runtime.
- Nothing gets printed, as the main thread just waits at line 1.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test { public static void main(String[] args) { String message = "Hello!"; try { message.wait(); } catch (InterruptedException e) { } System.out.println(message); } }- Compilation fails.
- It prints “Hello!”.
- An IllegalMonitorStateException is thrown at runtime.
- Nothing gets printed, as the main thread just waits at line 1.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test { public static void main(String[] args) { String message = "Hello!"; synchronized (args) { try { message.wait(); } catch (InterruptedException e) { } } System.out.println(message); } }- Compilation fails.
- It prints “Hello!”.
- An IllegalMonitorStateException is thrown at runtime.
- Nothing gets printed, as the main thread just waits at line 1.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Test { public static void main(String[] args) { String message = "Hello!"; synchronized (message) { try { message.wait(); } catch (InterruptedException e) { } } System.out.println(message); } }- Compilation fails.
- It prints “Hello!”.
- An IllegalMonitorStateException is thrown at runtime.
- Nothing gets printed, as the main thread just waits at line 1.
- These classes are defined in the same file. What is the output? (1 correct answer)
class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) { number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) { Job thread = new Job(); thread.start(); System.out.println(thread.getNumber()); } }- It prints 0.
- It prints 999999.
- The output is not guaranteed to be any of the above.
- These classes are defined in the same file. What is the output? (1 correct answer)
class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) { number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) throws InterruptedException { Job thread = new Job(); thread.start(); synchronized (thread) { thread.wait(); } System.out.println(thread.getNumber()); } }- It prints 0.
- It prints 999999.
- The output is not guaranteed to be any of the above.
- These classes are defined in the same file. What is the output? (1 correct answer)
class Job extends Thread { private Integer number = 0; public void run() { synchronized (this) { for (int i = 1; i < 1000000; i++) { number++; } notify(); } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) throws Exception { Job thread = new Job(); thread.start(); synchronized (thread) { thread.wait(); } System.out.println(thread.getNumber()); } }- It prints 0.
- It prints 999999.
- The output is not guaranteed to be any of the above.
- These classes are defined in the same file. What is the output? (1 correct answer)
class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) { number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) throws Exception { Job thread = new Job(); thread.start(); thread.join(); System.out.println(thread.getNumber()); } }- It prints 0.
- It prints 999999.
- The output is not guaranteed to be any of the above.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { System.out.print("A "); notifyAll(); System.out.println("B"); } }- It prints “A B”.
- Compilation fails.
- It prints “A ” and an exception is thrown.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { System.out.print("A "); new Object().notifyAll(); System.out.println("B"); } }- It prints “A B”.
- Compilation fails.
- It prints “A ” and an exception is thrown.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { System.out.print("A "); synchronized (new Object()) { new Object().notifyAll(); } System.out.println("B"); } }- It prints “A B”.
- Compilation fails.
- It prints “A ” and an exception is thrown.
- What happens when this code gets compiled and executed? (1 correct answer)
public class Run { public static void main(String[] args) { System.out.print("A "); final Object test = new Object(); synchronized (test) { test.notifyAll(); } System.out.println("B"); } }- It prints “A B”.
- Compilation fails.
- It prints “A ” and an exception is thrown.
© 2008 Nikos Pougounias. This is a free contribution to the Java community. Please distribute it for free. http://nikojava.wordpress.com
Answers
- a
- a
- a
- a
- a
- d
- c
- b
- a
- c
- a
- a
- a
- b
- c
- a
- a
- b
- b
- b
- a
- a
- a
- a
- a
- a
- a
- a
- b
- b
- a
- c
- b
- b
- b
- b
- b
- c
- b
- c
- b
- a
- a
- a
- a
- b
- a
- b
- b
- b
- a
- a
- a
- a
- a
- a
- a
- b
- a
- b
- a
- c
- c
- d
- c
- b
- b
- b
- b
- c
- c
- a
References
A friendly community of people preparing for SCJP can be found at the JavaRanch SCJP forum.
Please contact me for any suggestion.
Thanks.
Thanks to Maggie Zhou for her corrections on questions 42 and 43!
For the question no 59, answer should be b i.e. false. I ran and checked.
Hi Garima, now questions 58, 59 and 60 are cut and clear! Just provide the account a print method and check it again! Thanks.
Hello you forum best. And Bye. just test. .
:)
Dear Nikos,
I do not understand why example 66 works the way it does. I have tried and it really prints what you say it does, but why, when nobody calls notify()? Also, if I add a Thread.sleep(1000) before entering the synchronized block for waiting, it hangs. What’s the _important_ difference between the two cases? I kind of think that if something does not work if I sleep in it a little, then it is not certain to work at all.
Similar question for example 67: it hangs if I add a Thread.sleep() before entering the synch block containing the wait call. Which is logical, because the notify() happens before the wait is entered. Then why can we say that without the sleep it certainly works? Does it not depend on the scheduler?
Thanks in advance!
hi, I ran question 48.
“Working…” is only printed twice? not three times?
btw, thanks for your mock exams!
[...] Free SCJP Mock exam for Threads [...]
Very Good Practice Material.Thanks A lot.
Hi can you please explain questions 58,60,66,67 ?
You are right..but why..i couldn’t infer….
The remaining questions are great for self test in Threads.
Thanks
Hi all.
I would like to explain in short some questions:
q58 – you have 2 independent methods “setNumber” and “getNumber”, two threads cannot get into one of them in the same time becouse of synchronization, this is clear, but line “account.setNumber(account.getNumber() + 1);” is unsafe becouse two threads may read the same number (getNumber()) (one thread get into method, read value and release lock, then second thread get into the same method and read the same value) then both tread wants to set their values (setNumber), so You dont have quarantee that the sequence will be: getNumber, setNumber, getNumber, setNumber… it can be: getNumber, getNumber, setNumber,setNumber
q60: – in this example You have quarantee sequence, read and set value (increment)
q66: – when the thread is dead (ended run method) it release lock (if thread end his work before main thread run wait method program will be wait for all)
g67: – similar to above example, when thread run notify method and go out from synchronized block main thread wake up, but You have to know that You imply that the thread will finish his work after main method run wait method on thread object.
I hope this help somone. Best regards
Hi
First of all – good job.
Look again at question 8 – I believe 3 methods (not 2) are static there.
one of the excellent site for scjp aspirants….!!!!!!!!!!!!!!!!!!!!!!!!!!!1
Hello Niko
Can you please explain output of Q 66?
Hey! Thanks for these questions, they are really useful!
But I have a doubt regarding to questions number 24 and 66, though I checked them and the answer for both are correct, I can’t get how they work the way they do ..
If someone is willing to give a hand here please, I’d really appreciate it
Thanks again!
Michał B: q60 is correct because increment/decrement operation is NOT an atomic operation and it takes more than 1 processor cycles.
However questions 66 & 67 ale incorrect because if you have any processes running on your machine which slow down your machine it may cause that scheduler switch threads before main thread lock thread object and than main thread will wait until run method will end. (the effect will be the same if you call Thread.sleep() method between Thread.start() and synchronized block) In that case the program may hang on in Thread.wait() method.
Shouldn’t the answer number 2 in question 48 be “It prints “Working…” two times.” instead of “It prints “Working…” three times.” ?
I hope it will help …
jst wait and watch
Question 48, the correct answer is:
It prints “Working…” TWO times.
Question 49 too.
52 correct answers