SCJP Certification : Java Thread with Explanation

 

Java Certifications Preparations

SCJP Certification : Java Thread with Explanation

This SCJP Certification section covers threading.Here you could practice thread SCJP questions with explanation

Q-1 : Given:

1. public class Threads2 implements Runnable {

2.

3. public void run() {

4. System.out.println(“run.”);

5. throw new RuntimeException(“Problem”);

6. }

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

8. Thread t = new Thread(new Threads2());

9. t.start();

10. System.out.println(“End of method.”);

11. }

12. }

Which two can be results? (Choose two.)

A. java.lang.RuntimeException: Problem

B. run.

java.lang.RuntimeException: Problem

C. End of method.

java.lang.RuntimeException: Problem

D. End of method.

run.

java.lang.RuntimeException: Problem

E. run.

java.lang.RuntimeException: Problem

End of method.

Answer: D, E

Explanation :when thread t will start to execute then there is surity that run() method will excute first before println(“End of method.”).

But if run() method is going to execute first then RuntimeException(“Problem”) will be next one and then prinln() method.So both D,E are correct answers.

Q-2 : Which two statements are true? (Choose two.)

A. It is possible for more than two threads to deadlock at once.

B. The JVM implementation guarantees that multiple threads cannot enter into a

deadlocked state.

C. Deadlocked threads release once their sleep() method’s sleep duration has expired.

D. Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are

used incorrectly.

E. It is possible for a single-threaded application to deadlock if synchronized blocks are

used incorrectly.

F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of

deadlocking by inserting

invocations of Thread.yield().

Answer: A, F

Explanation :A option is correct as There is no restriction on number of thread for deadlock.

F option is also correct because yield method has not given any guarrante to change the state of runing thread.

Q-3 : Given :

7. void waitForSignal() {

8. Object obj = new Object();

9. synchronized (Thread.currentThread()) {

10. obj.wait();

11. obj.notify();

12. }

13. }

Which statement is true?

A. This code can throw an InterruptedException.

B. This code can throw an IllegalMonitorStateException.

C. This code can throw a TimeoutException after ten minutes.

D. Reversing the order of obj.wait() and obj.notify() might cause this method to complete

normally.

E. A call to notify() or notifyAll() from another thread might cause this method to

complete normally.

F. This code does NOT compile unless “obj.wait()” is replaced with “((Thread)

obj).wait()”.

Answer: B

Explanation :

If the thread calling wait() does not own the lock on the object, a IllegalMonitorStateException will be thrown.

Here we haven’t take lock for obj we are taking lock for current thread object.

Q-4 : What is the result of compiling and running the following program?

01.public class Tester {

02.

03.public void validate() {

04.int i = 0;

05.while (++i < 3) {

06.try {

07.wait();

08.} catch (InterruptedException e) {

09.e.printStackTrace();

10.}

11.System.out.print(i);

12.}

13.}

14.

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

16.new Tester().validate();

17.}

18.

19.}

A.Compilation error because of calling wait() outside a synchronized block

B.Compilation error because IllegalMonitorStateException is not handled

C.At runtime, it throws an IllegalMonitorStateException when trying to wait

D.12

Answer :A

Explanation:

If the thread calling wait() does not own the lock on the object, a IllegalMonitorStateException will be thrown.

Q-5 : What is the output if the main() method is run?

A. 4

B. 5

C. 8

D. 9

E. Compilation fails.

F. An exception is thrown at runtime.

G. It is impossible to determine for certain.

Answer: D

Explanation :when main() method will run then starter object will create that’s why starter constructor will run that will firstly assign x=5 then thread run method will start that will make this x=10.After that makeItso method will run and make x=9.

Q-6 : Given:

11. class PingPong2 {

12. synchronized void hit(long n) {

13. for(int i = 1; i < 3; i++)

14. System.out.print(n + “-” + i + ” “);

15. }

16. }

17. public class Tester implements Runnable {

18. static PingPong2 pp2 = new PingPong2();

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

20. new Thread(new Tester()).start();

21. new Thread(new Tester()).start();

22. }

23. public void run() { pp2.hit(Thread.currentThread().getId()); }

24. }

Which statement is true?

A. The output could be 5-1 6-1 6-2 5-2

B. The output could be 6-1 6-2 5-1 5-2

C. The output could be 6-1 5-2 6-2 5-1

D. The output could be 6-1 6-2 5-1 7-1

Answer: B

Explanation :

public long getId()
Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

Here we have to check answer options for correct answer.Here B option is correct because sequence is correct as per loop.

Q-7 : Given:

1. public class Threads4 {

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

3. new Threads4().go();

4. }

5. public void go() {

6. Runnable r = new Runnable() {

7. public void run() {

8. System.out.print(“foo”);

9. }

10. };

11. Thread t = new Thread(r);

12. t.start();

13. t.start();

14. }

15. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes normally and prints “foo”.

D. The code executes normally, but nothing is printed.

Answer: B

Explanation : This will throw exception as you can’t start one thread object more than one.

Q-8 : Given :

1. The following block of code creates a Thread using a Runnable target:

Runnable target = new MyRunnable();

Thread myThread = new Thread(target);

Which of the following classes can be used to create the target, so that the preceding code

compiles correctly?

A. public class MyRunnable extends Runnable{public void run(){}}

B. public class MyRunnable extends Object{public void run(){}}

C. public class MyRunnable implements Runnable{public void run(){}}

D. public class MyRunnable implements Runnable{void run(){}}

E. public class MyRunnable implements Runnable{public void start(){}}

Answer:C

Explanation :C is correct. The class implements the Runnable interface with a legal run() method.

A is incorrect because interfaces are implemented, not extended. B is incorrect because even though the class has a valid public void run() method, it does not implement the Runnable interface. D is incorrect because the run() method must be public. E is incorrect because the method to implement is run(), not start().

Q-9 : Given:

3. class MyThread extends Thread {

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

5. MyThread t = new MyThread();

6. Thread x = new Thread(t);

7. x.start();

8. }

9. public void run() {

10. for(int i=0;i<3;++i) {

11. System.out.print(i + “..”);

12. } } }

What is the result of this code?

A. Compilation fails

B. 1..2..3..

C. 0..1..2..3..

D. 0..1..2..

E. An exception occurs at runtime

Answer:D

Explanation : D is correct. The thread MyThread will start and loop three times (from 0 to 2).

A is incorrect because the Thread class implements the Runnable interface; therefore,in line 5, Thread can take an object of type Thread as an argument in the constructor (this is NOT recommended). B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2. E is incorrect based on the above.

Q-10 : Given:

1. public class WaitTest {

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

3. System.out.print(“1 “);

4. synchronized(args){

5. System.out.print(“2 “);

6. try {

7. args.wait();

8. }

9. catch(InterruptedException e){}

10. }

11. System.out.print(“3 “);

12. } }

What is the result of trying to compile and run this program?

A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt

with in line 7

B. 1 2 3

C. 1 3

D. 1 2

E. At runtime, it throws an IllegalMonitorStateException when trying to wait

F. It will fail to compile because it has to be synchronized on the this object

Answer:D

Explanation :D is correct. 1 and 2 will be printed, but there will be no return from the wait call because no other thread will notify the main thread, so 3 will never be printed. It’s frozen at line 7.

A is incorrect; IllegalMonitorStateException is an unchecked exception. B and C

are incorrect; 3 will never be printed, since this program will wait forever. E is incorrect because IllegalMonitorStateException will never be thrown because the wait() is done on args within a block of code synchronized on args. F is incorrect because any object can be used to synchronize on and this and static don’t mix.

This entry was posted in Java Certifications, Java Programing, Programing and tagged . Bookmark the permalink.

One Response to SCJP Certification : Java Thread with Explanation

  1. Jagdeep Gupta says:

    As per your explanation , the END Of Method should never print before run, so how come the D option could be correct

    Explanation :when thread t will start to execute then there is surity that run() method will excute first before println(“End of method.”).

    But if run() method is going to execute first then RuntimeException(“Problem”) will be next one and then prinln() method.So both D,E are correct answers.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>