MCQs
What will be the output of the program?
class s1 extends Thread
{
public void run()
{
for(int i = 0; i < 3; i++)
{
System.out.println("A");
System.out.println("B");
}
}
}
class Test120 extends Thread
{
public void run()
{
for(int i = 0; i < 3; i++)
{
System.out.println("C");
System.out.println("D");
}
}
public static void main(String args[])
{
s1 t1 = new s1();
Test120 t2 = new Test120();
t1.start();
t2.start();
}
}
We cannot predict the order in which threads are going to run.
What will be the output of the program?
class MyThread extends Thread
{
MyThread()
{
System.out.print(" MyThread");
}
public void run()
{
System.out.print(" bar");
}
public void run(String s)
{
System.out.println(" baz");
}
}
public class TestThreads
{
public static void main (String [] args)
{
Thread t = new MyThread()
{
public void run()
{
System.out.println(" foo");
}
};
t.start();
}
}
Option B is correct because in the first line of main we're constructing an instance of an
anonymous inner class extending from MyThread. So the MyThread constructor runs
and prints "MyThread". The next statement in main invokes start() on the new thread
instance, which causes the overridden run() method (the run() method defined in the
anonymous inner class) to be invoked, which prints "foo"
The Object class defines these thread-specific methods.
Option B, C, and D are incorrect because they do not define these methods. And yes, the Java
API does define a class called Class, though you do not need to know it for the exam.
Option A is correct. notify() - wakes up a single thread that is waiting on this object's monitor.
Option B is wrong. wait() causes the current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object.
Option C is wrong. Methods of the InputStream class block until input data is available, the end
of the stream is detected, or an exception is thrown. Blocking means that a thread may stop until
certain conditions are met.
Option D is wrong. sleep() - Causes the currently executing thread to sleep (temporarily cease
execution) for a specified number of milliseconds. The thread does not lose ownership of any
monitors.
Option A. Either of the two events (notification or wait time expiration) will make the thread
become a candidate for running again.
Option B is incorrect because a waiting thread will not return to runnable when the lock is
released, unless a notification occurs.
Option C is incorrect because the thread will become a candidate immediately after notification,
not two seconds afterwards.
Option D is also incorrect because a thread will not come out of a waiting pool just because a l
ock has been released.
Option A is Correct. The run() method to a thread is like the main() method to an application.
Starting the thread causes the object's run method to be called in that separately executing
thread.
Option B is wrong. The start() method causes this thread to begin execution; the Java Virtual
Machine calls the run method of this thread.
Option C is wrong. The stop() method is deprecated. It forces the thread to stop executing.
Option D is wrong. Is the main entry point for an application.
Option C is correct. The start() method causes this thread to begin execution; the Java Virtual
Machine calls the run method of this thread.
Option A is wrong. The run() method of a thread is like the main() method to an application.
Starting the thread causes the object's run method to be called in that separately executing
thread.
Option B is wrong. There is no construct() method in the Thread class.
Option D is wrong. There is no register() method in the Thread class.
What is the output of this program?
class newthread extends Thread {
Thread t1,t2;
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();
}
}
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
What is the output of this 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();
}
}
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
What is the output of this program?
class newthread implements Runnable {
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();
}
}
Threads t1 & t2 are created by class newthread that is implementing runnable interface,
hence both the threads are provided their own run() method specifying the actions to be
taken. When constructor of newthread class is called first the run() method of t1 executes
than the run method of t2 printing 2 times “false†as both the threads are not equal one is
having different priority than other, hence falsefalse is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse