Witscale Test Center

Answers with explanations


Answers with explanations

1. þ C

The correct answer is C. Note that Runnable is an interface. This option implements this interface with anonymous inner class and then correctly starts the thread.

Options A,B D are incorrect as they try to instantiate an interface Runnable which is invalid. E is incorrect as it creates thread object correctly but does not start it.

 

2. þ A C D E

 The correct answers are A C D and E. All these options invoke the run() method by one way or the other. C and E directly invoke this method. Option A creates and starts a thread that will eventually invoke it. D creates a thread, but does not start it. Instead it invokes the run() method as a normal method.

Options B is incorrect. B will not even compile as class MyRunnable does not have any start() method.

 

3. þ E

The correct answer is E. The Runnable interface has a single method public void run() and option E correctly implements it.

A is incorrect; you can never extend an interface, you always implement it. B and C are incorrect, as the MyRunnable is not implementing the run method correctly. The run method in B has incorrect return type and C is reducing the visibility of run() method by applying a more restrictive modifier (public -->protected) which is not allowed. D is incorrect as it is not implementing the run() method. F is incorrect as it is defining run() without a method body.

 

4. þ B

The correct answer is B. The run() method on thread object t1 will be invoked like any ordinary Java method.

A is incorrect as run() method that takes int argument is never called. C is incorrect; the run(int) method is correctly overloading the run() method and hence there won’t be any compilation problem. D is incorrect as there is nothing wrong in invoking run method like this. E is incorrect for two reasons; there is no question of having “Running 2...” in output as run(int) is never called. The output is very much predictable, as this class is not creating any new thread-of-execution. Hence, everything will be executed in a single thread sequentially. F is also incorrect, you can invoke run() method on a thread object.

 

5. þ E

The correct answer is E. How this code will run depends on the scheduler’s implementation – which algorithms it use, is it priority-based or time-sliced or combination of multiple algorithms etc. 

A could be true in purely priority-based scheduler. B is incorrect, you can set priority before starting a thread. C and D are also possible but you can never be sure. Therefore, the bottom line is you cannot guarantee how this code will work across different implementations of Java.

 

6. þ A

The correct answer is A. The exact output of this code depends on the scheduler’s implementation and may not be same each time you execute it. The call at line 16 could be misleading. But remember that bullockCartThread.yield(); in a jetlinerThread’s run code will cause jetlinerThread to give up its turn of execution. yield() is a static method that causes the current thread to yield. Since the call bullockCartThread.yield(); is in jetlinerThread’s run code, the thread jetlinerThread will be the current thread at that time and it will yield. Since the option A is suggesting a possibility (and not guarantee) it is a true option.

Options B D E are all incorrect as they talk about a guaranteed behavior which is not true. C is incorrect; since yield is a static method, it can be called on any of the thread instances.

 

7. þ D

The correct answer is D. The call sleep(3000) causes the new thread started on g to sleep for 3 seconds. After it wakes up it goes to ready-to-run state. Only after it is again scheduled to run it can print the message “Good morning everyone!”. So all we can say is that this message will be printed sometime after three seconds have elapsed.

A is incorrect; it is a valid way in which the thread is created and started in the same step. B is incorrect, as the new thread (started on g) will definitely sleep for 3 seconds (if it is not interrupted).  The word atmost suggest that the thread can wake up before three seconds are elapsed which is not correct. C is incorrect as there is no guarantee that the thread will start executing immediately after it wakes up. E is incorrect as the thread automatically goes from sleeping to ready-to-run state after the sleep time is elapsed. You need not do anything to wake it up.

 

8. þ F

The correct answer is F. You do not have any control as to which thread gets notified. Therefore, you cannot specifically notify only two of the three waiting threads.

A and B are incorrect for the same reason. Note that they are calling notify() on thread objects which is not correct as the question clearly says that the threads are waiting on someObject, therefore the notify() should be called on someObject to notify the threads waiting on it. C and E are incorrect use of notify methods. D is syntactically correct but it will notify all the three threads t1, t2 and t3.

 

9. þ C

The correct answer is C. The call to printIt() at line 7 and 8 are invoked before the threads are started. Therefore, there is no uncertainty yet. Since both threads are different Java objects, each has its own instance variable i which will be 0 initially. Thus 0 0 will be printed. Now t1.i will be 1 and t2.i will also be 1. Remember that they are two different objects. Now when the threads are started at line 9 and 10, we do not know which one will get its turn first. But since they are different objects, they are operating on two separate copies of i. If t1 gets to execute first, it will print t1.i and increment it (t1.i ). Therefore, it will print 1 and increment i so that t1.i will be 2. Similarly when t2 is executed it will print t2.i which is 1 and increment t2.i . Interesting the order in which t1 or t2 will be executed (t1 first or t2 ?) will not change the output in this case.

 

10. þ A D E G

The correct answers are A D E and G. A calls the notify() method of Object class. G calls the notifyAll() method of Object class. D and E are calls to the overloaded wait() methods of Object class that allows the threads to wait for some predefined time. The first argument in both wait calls is the timeout in milliseconds. The second argument in option E is the additional timeout in nanoseconds.

B C and F are all incorrect options. B is incorrect, as there is no method called synchronized in any of the threading related classes let alone Object class. C and F are calls to the Thread class’s method.

 

 

11. þ B C E F

The correct answers are B C E and F. B represents the start() method invokes when you wish to start the thread. F represents the run() method usually invoked internally (by JVM) sometime after start() is called. C and E are the methods of Object class. Since Thread class (like all other Java classes) inherits from the Object class, you can call these inherited methods on an object of type Thread.

A D and G are all incorrect options. There are no such methods in Thread class.

 

12. þ B

The correct answer is B. A thread immediately releases an object’s lock after it invokes the wait() method on it to give the other threads a chance to get that object’s lock.

A C D and E are all incorrect statement. A is incorrect; A Java object has a single lock which can be awarded to only one thread at a time. C is incorrect, as threads do not need lock for accessing the non-synchronized methods of Java object. D is incorrect as you can synchronize only methods or code blocks. E is incorrect as a call to yield() takes the thread back to ready-to-run state (and not to the sleeping state).

 

13. þ B

The correct answer is B. The thread that has acquired an object’s lock keeps it even while sleeping.

A C D E F and G are all incorrect statements. A is incorrect; thread immediately release the lock after a call to a wait(). It does not try to acquire lock while it is waiting for notification. C is incorrect, as the thread calls an object’s wait() method and hence it is the one that needs to acquire the lock (not the other way). D is incorrect; notifyAll() is an instance method of Object class. E is incorrect; a call to notify() do not immediately release the lock. The thread calling it will keep the lock as long as it is in the synchronized block in which notify() is called. G is incorrect as notifyAll() (on an object) notifies only to the threads waiting on that object.

 

14. þ C

The correct answer is C. The call wait(10000) sets a timeout for waiting. The thread t1 will wait for maximum 10 seconds for notification. If it receives it before 10 seconds, fine. Otherwise, it anyway stops waiting.

A B D and E are all incorrect. A is incorrect, as t1 do not start executing right away after the wait is over. It has to contend again with the other threads to get a turn-to execute. B is incorrect as t1 is waiting on notification and not on lock. D is incorrect as wait(10000) causes a timed waiting. Therefore, it does not keep waiting. E is incorrect as t1 is the one waiting, so it does not notify.

 

15. þ B E

The correct answer is D. Thread.MIN_PRIORITY is the lowest priority (1) that a thread can have. Since paintThread is slow, it should have a low priority so that it cannot stop other threads from executing.

A C D and F are all incorrect. Please note that 1 is considered as lowest priority and 10 is considered as highest priority. C is incorrect as with priority 10, the painterThread will grab the JVM and will never give it to any other threads with lesser priority. Moreover, it’s a slow thread so it will take lot of time before other thread processInputsThread will get a chance to execute, making the GUI application poorly responsive. A and F will keep a normal priority to thread which is not good to give a higher preference to processInputsThread over paintThread. D is incorrect, as thread priority must be between 1 to 10.