Sail E0 Webinar

MCQs

Total Questions : 86 | Page 7 of 9 pages
Question 61.

Which three guarantee that a thread will leave the running state?

     1. yield()

     2. wait()

     3. notify()

     4. notifyAll()

     5. sleep(1000)

     6. aLiveThread.join()

     7. Thread.killThread()


  1.    1, 2 and 4
  2.    2, 5 and 6
  3.    3, 4 and 7
  4.    4, 5 and 7
 Discuss Question
Answer: Option B. -> 2, 5 and 6

(2) is correct because wait() always causes the current thread to go into the object's wait pool.

(5) is correct because sleep() will always pause the currently running thread for at least the 

duration specified in the sleep argument (unless an interrupted exception is thrown).

(6) is correct because, assuming that the thread you're calling join() on is alive, the thread calling

 join() will immediately block until the thread you're calling join() on is no longer alive.

(1) is wrong, but tempting. The yield() method is not guaranteed to cause a thread to leave the 

running state, although if there are runnable threads of the same priority as the currently running

 thread, then the current thread will probably leave the running state.

(3) and (4) are incorrect because they don't cause the thread invoking them to leave the running state.

(7) is wrong because there's no such method.


Question 62.


What is the output of this program?


class newthread extends Thread {
Thread t;
String name;
newthread(String threadname) {
name = threadname;
t = new Thread(this,name);
t.start();
}
public void run() {
}
}
class multithreaded_programing {
public static void main(String args[]) {
newthread obj1 = new newthread("one");
newthread obj2 = new newthread("two");
try {
obj1.t.wait();
System.out.print(obj1.t.isAlive());
}
catch(Exception e) {
System.out.print("Main thread interrupted");
}
}
}
  1.    true
  2.    false
  3.    Main thread interrupted
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> Main thread interrupted

obj1.t.wait() causes main thread to go out of processing in sleep state hence causes 

exception and "Main thread interrupted" is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Main thread interrupted


Question 63.

Which two of the following methods are defined in class Thread?

    1. start()

    2. wait()

    3. notify()

    4. run()

    5. terminate()


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

(1) and (4). Only start() and run() are defined by the Thread class.

(2) and (3) are incorrect because they are methods of the Object class. (5) is incorrect 

because there's no such method in any thread-related class.


Question 64.

What is synchronization in reference to a thread?


  1.    Its a process of handling situations when two or more threads need access to a shared resource.
  2.    Its a process by which many thread are able to access same shared resource simultaneously.
  3.    Its a process by which a method is able to access many different threads simultaneously.
  4.    Its a method that allow to many threads to access any information the require.
 Discuss Question
Answer: Option A. -> Its a process of handling situations when two or more threads need access to a shared resource.

When two or more threads need to access the same shared  resource,  they need 

some way to ensure that the resource will be used by only one thread at a time, the 

process by which this is achieved is called synchronization


Question 65.

Which cannot directly cause a thread to stop executing?


  1.    Calling the SetPriority() method on a Thread object.
  2.    Calling the wait() method on an object.
  3.    Calling notify() method on an object.
  4.    Calling read() method on an InputStream object.
 Discuss Question
Answer: Option C. -> Calling notify() method on an object.

Option C is correct. notify() - wakes up a single thread that is waiting on this object's monitor.

Question 66.


What is the output of this program?


class newthread implements Runnable {
Thread t;
newthread() {
t = new Thread(this,"My Thread");
t.start();
}
public void run() {
System.out.println(t.getName());
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
  1.    My Thread
  2.    Thread[My Thread,5,main]
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option A. -> My Thread

None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
My Thread


Question 67.

What is synchronization in reference to a thread?


  1.    It's a process of handling situations when two or more threads need access to a shared resource.
  2.    Its a process by which many thread are able to access same shared resource simultaneously.
  3.    Its a process by which a method is able to access many different threads simultaneously.
  4.    Its a method that allow to many threads to access any information require.
 Discuss Question
Answer: Option A. -> It's a process of handling situations when two or more threads need access to a shared resource.

When two or more threads need to access the same shared resource, they  need some 

way to ensure that the resource will be used by only one thread at a time, the process by 

which this is achieved is called synchronization


Question 68.

Which of these method is used to explicitly set the priority of a thread?


  1.    set()
  2.    make()
  3.    setPriority()
  4.    makePriority()
 Discuss Question
Answer: Option C. -> setPriority()

The default value of priority given to a thread is 5 but we can explicitly change that value 

between the permitted values 1 & 10, this is done by using the method setPriority().


Question 69.

Which of these method wakes up all the threads?


  1.    wakeAll()
  2.    notify()
  3.    start()
  4.    notifyAll()
 Discuss Question
Answer: Option D. -> notifyAll()

notifyAll() wakes up all the threads that called wait() on the same object. The highest 

Question 70.
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}


Which of the following line of code is suitable to start a thread ?


  1.    Thread t = new Thread(X);
  2.    Thread t = new Thread(X); t.start();
  3.    X run = new X(); Thread t = new Thread(run); t.start();
  4.    Thread t = new Thread(); x.run();
 Discuss Question
Answer: Option C. -> X run = new X(); Thread t = new Thread(run); t.start();

Option C is suitable to start a thread.


Latest Videos

Latest Test Papers