Sail E0 Webinar

MCQs

Total Questions : 86 | Page 3 of 9 pages
Question 21. What will be output of the following program code?
public class Test implements Runnable{
public void run(){
System.out.print("go");
}
public static void main(String arg[]) {
Thread t = new Thread(new Test());
t.run();
t.run();
t.start();
}
}
  1.    Compilation fails.
  2.    An exception is thrown at runtime.
  3.    "go" is printed
  4.    "gogogo" is printed
  5.    "gogo" is printed
 Discuss Question
Answer: Option D. -> "gogogo" is printed
Question 22. Given the code. What will be the result?
public class Test implements Runnable{
public static void main(String[] args) throws InterruptedException{
Thread a = new Thread(new Test());
a.start();
System.out.print("Begin");
a.join();
System.out.print("End");
}
public void run(){
System.out.print("Run");
}
}
  1.    Compilation fails.
  2.    An exception is thrown at runtime.
  3.    "BeginRunEnd" is printed.
  4.    "BeginEndRun" is printed.
  5.    "BeginEnd" is printed.
 Discuss Question
Answer: Option C. -> "BeginRunEnd" is printed.
Question 23.

Which statement is true?


  1.    The notifyAll() method must be called from a synchronized context.
  2.    To call wait(), an object must own the lock on the thread.
  3.    The notify() method is defined in class java.lang.Thread.
  4.    The notify() method causes a thread to immediately release its locks.
 Discuss Question
Answer: Option A. -> The notifyAll() method must be called from a synchronized context.

Option A is correct because the notifyAll() method (along with wait() and notify()) must always 

be called from within a synchronized context.

Option B is incorrect because to call wait(), the thread must own the lock on the object that wait() 

is being invoked on, not the other way around.

Option C is wrong because notify() is defined in java.lang.Object.

Option D is wrong because notify() will not cause a thread to release its locks. The thread can only 

release its locks by exiting the synchronized code.


Question 24.


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?


  1.    public class MyRunnable extends Runnable{public void run(){}}
  2.    public class MyRunnable extends Object{public void run(){}}
  3.    public class MyRunnable implements Runnable{public void run(){}}
  4.    public class MyRunnable implements Runnable{void run(){}}
 Discuss Question
Answer: Option C. -> public class MyRunnable implements Runnable{public void run(){}}

The class correctly implements the Runnable interface with a legal public void run() method.

Option A is incorrect because interfaces are not extended; they are implemented.

Option B is incorrect because even though the class would compile and it has a valid public 

void run() method, it does not implement the Runnable interface, so the compiler would complain 

when creating a Thread with an instance of it.

Option D is incorrect because the run() method must be public.


Question 25.

Which two statements are true?

     1. Deadlock will not occur if wait()/notify() is used

     2. A thread will resume execution as soon as its sleep duration expires.

     3. Synchronization can prevent two objects from being accessed by the same thread.

     4. The wait() method is overloaded to accept a duration.

     5. The notify() method is overloaded to accept a duration.

     6. Both wait() and notify() must be called from a synchronized context.


  1.    1 and 2
  2.    3 and 5
  3.    4 and 6
  4.    1 and 3
 Discuss Question
Answer: Option C. -> 4 and 6

Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to 

accept a wait duration in milliseconds. If the thread has not been notified by the time the wait 

duration has elapsed, then the thread will move back to runnable even without having been 

notified.

(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, 

context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.

(1) is incorrect because wait()/notify() will not prevent deadlock.

(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might 

not necessarily resume execution right away. To resume executing, the newly awakened thread 

must still be moved from runnable to running by the scheduler.

(3) is incorrect because synchronization prevents two or more threads from accessing the same

 object.

(5) is incorrect because notify() is not overloaded to accept a duration.


Question 26.

Which statement is true?


  1.    If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
  2.    If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
  3.    If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
  4.    If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the not
 Discuss Question
Answer: Option B. -> If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.

Option B is correct - The notify method only wakes the thread. It does not guarantee that the 

thread will run.

Option A is incorrect - just because another thread activates the modify method in A this does 

not mean that the thread will automatically resume execution

Option C is incorrect - This is incorrect because as said in Answer B notify only wakes the thread 

but further to this once it is awake it goes back into the stack and awaits execution therefore it is 

not a "direct and sole consequence of the notify call"

Option D is incorrect - The notify method wakes one waiting thread up. If there are more than one 

sleeping threads then the choice as to which thread to wake is made by the machine rather than 

you therefore you cannot guarantee that the notify'ed thread will be the first waiting thread.


Question 27.

Which two can be used to create a new Thread?

     1. Extend java.lang.Thread and override the run() method.

     2. Extend java.lang.Runnable and override the start() method.

     3. Implement java.lang.Thread and implement the run() method.

     4. Implement java.lang.Runnable and implement the run() method.

     5. Implement java.lang.Thread and implement the start() method.


  1.    1 and 2
  2.    2 and 3
  3.    1 and 4
  4.    3 and 4
 Discuss Question
Answer: Option C. -> 1 and 4

There are two ways of creating a thread; extend (sub-class) the Thread class and implement the Runnable interface. For both of these ways you must implement (override and not overload) 

the public void run() method.

(1) is correct - Extending the Thread class and overriding its run method is a valid procedure.

(4) is correct - You must implement interfaces, and runnable is an interface and you must also

 include the run method.

(2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No 

interface expected here)

(3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, 

gives the error: Interface expected). Implements expects an interface.

(5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, 

and Implement interfaces. (Implements Thread, gives the error: Interface expected)


Question 28.

Which statement is true?


  1.    A static method cannot be synchronized.
  2.    If a class has synchronized code, multiple threads can still access the nonsynchronized code.
  3.    Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
  4.    When a thread sleeps, it releases its locks.
 Discuss Question
Answer: Option B. -> If a class has synchronized code, multiple threads can still access the nonsynchronized code.

B is correct because multiple threads are allowed to enter nonsynchronized code, even within 

a class that has some synchronized methods.

A is incorrect because static methods can be synchronized; they synchronize on the lock on 

the instance of class java.lang.Class that represents the class type.

C is incorrect because only methods—not variables—can be marked synchronized.

D is incorrect because a sleeping thread still maintains its locks.


Question 29.


What will be the output of the program?


class Test116
{
static final StringBuffer sb1 = new StringBuffer();
static final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("A");
sb2.append("B");
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(sb1)
{
sb1.append("C");
sb2.append("D");
}
}
}.start(); /* Line 28 */
System.out.println (sb1 + " " + sb2);
}
}
  1.    main() will finish before starting threads.
  2.    main() will finish in the middle of one thread.
  3.    main() will finish after one thread.
  4.    Cannot be determined.
 Discuss Question
Answer: Option D. -> Cannot be determined.

Can you guarantee the order in which threads are going to run? No you can't. So how do 

you know what the output will be? The output cannot be determined.

add this code after line 28:

try { Thread.sleep(5000); } catch(InterruptedException e) { }

and you have some chance of predicting the outcome.


Question 30.


What will be the output of the program?


class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread(); /* Line 5 */
t.run(); /* Line 6 */
}
public void run()
{
for(int i=1; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}
  1.    This code will not compile due to line 5.
  2.    This code will not compile due to line 6.
  3.    1..2..
  4.    1..2..3..
 Discuss Question
Answer: Option C. -> 1..2..

Line 6 calls the run() method, so the run() method executes as a normal method should 

and it prints "1..2.."

A is incorrect because line 5 is the proper way to create an object.

B is incorrect because it is legal to call the run() method, even though this will not start a true 

thread of execution. The code after line 6 will not execute until the run() method is complete.

D is incorrect because the for loop only does two iterations.


Latest Videos

Latest Test Papers