This section of our 1000+ Java MCQs focuses on creating threads in Java Programming Language.
1. Which of these keywords are used to implement synchronization?
a) synchronize
b) syn
c) synch
d) synchronized
View Answer
Explanation: None.
2. Which of this method is used to avoid polling in Java?
a) wait()
b) notify()
c) notifyAll()
d) all of the mentioned
View Answer
Explanation: 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().
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?
a) wait()
b) notify()
c) notifyAll()
d) sleep()
View Answer
Explanation: 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.
4. Which of these method wakes up the first thread that called wait()?
a) wake()
b) notify()
c) start()
d) notifyAll()
View Answer
Explanation: None.
5. Which of these method wakes up all the threads?
a) wakeAll()
b) notify()
c) start()
d) notifyAll()
View Answer
Explanation: notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.
6. What is synchronization in reference to a thread?
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
View Answer
Explanation: 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
7. What will be the output of the following Java 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");
}
}
}
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
View Answer
Explanation: 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
8. What will be the output of the following Java 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
{
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
}
catch(InterruptedException e)
{
System.out.print("Main thread interrupted");
}
}
}
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
View Answer
Explanation: 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
9. What will be the output of the following Java 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
{
System.out.print(obj1.t.equals(obj2.t));
}
catch(Exception e)
{
System.out.print("Main thread interrupted");
}
}
}
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
View Answer
Explanation: 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
10. What will be the output of the following Java program?
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();
}
}
a) true
b) false
c) truetrue
d) falsefalse
View Answer
Explanation: 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
Sanfoundry Global Education & Learning Series – Java Programming Language.
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Check Java Books
- Practice Programming MCQs