Java MCQs on creating threads in Java Programming Language.
1. Which of these keywords are used to implement synchronization? Answer: d 2. Which of this method is used to avoid polling in Java? Answer: d 3. Which of these method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor? Answer: a 4. Which of these method wakes up the first thread that called wait()? Answer: b 5. Which of these method wakes up all the threads? Answer: d 6. What is synchronization in reference to a thread? Answer: a 7. What will be the output of the following Java program? a) true 8. What will be the output of the following Java program? a) true 9. What will be the output of the following Java program? a) true 10. What will be the output of the following Java program? a) true
a) synchronize
b) syn
c) synch
d) synchronized
Clarification: None.
a) wait()
b) notify()
c) notifyAll()
d) all of the mentioned
Clarification: Polling is a usually implemented by looping in CPU is wastes CPU time, one thread being executed depends on other thread output and the other thread depends on the response on the data given to the first thread. In such situation CPU time is wasted, in Java this is avoided by using methods wait(), notify() and notifyAll().
a) wait()
b) notify()
c) notifyAll()
d) sleep()
Clarification: wait() method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor. This helps in avoiding polling and minimizes CPU idle time.
a) wake()
b) notify()
c) start()
d) notifyAll()
Clarification: None.
a) wakeAll()
b) notify()
c) start()
d) notifyAll()
Clarification: notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.
a) It’s a process of handling situations when two or more threads need access to a shared resource
b) It’s a process by which many thread are able to access same shared resource simultaneously
c) It’s a process by which a method is able to access many different threads simultaneously
d) It’s a method that allow too many threads to access any information the require
Clarification: 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
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");
}
}
}
b) false
c) Main thread interrupted
d) None of the mentioned
Clarification: 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
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
{
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
}
catch(InterruptedException e)
{
System.out.print("Main thread interrupted");
}
}
}
b) false
c) Main thread interrupted
d) None of the mentioned
Clarification: Thread.sleep(1000) has caused all the threads to be suspended for some time, hence onj1.t.isAlive() returns false.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
false
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
{
System.out.print(obj1.t.equals(obj2.t));
}
catch(Exception e)
{
System.out.print("Main thread interrupted");
}
}
}
b) false
c) Main thread interrupted
d) None of the mentioned
Clarification: Both obj1 and obj2 have threads with different name that is “one” and “two” hence obj1.t.equals(obj2.t) returns false.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
false
class newthread extends Thread
{
Thread t;
newthread()
{
t1 = new Thread(this,"Thread_1");
t2 = new Thread(this,"Thread_2");
t1.start();
t2.start();
}
public void run()
{
t2.setPriority(Thread.MAX_PRIORITY);
System.out.print(t1.equals(t2));
}
}
class multithreaded_programing
{
public static void main(String args[])
{
new newthread();
}
}
b) false
c) truetrue
d) falsefalse
Clarification: This program was previously done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse