MCQs
isAlive() method is used to check whether the thread being called is running or not, here
thread is the main() method which is running till the program is terminated hence it returns
true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true
Option B is correct because in an interface all methods are abstract by default therefore
they must be overridden by the implementing class. The Runnable interface only contains
1 method, the void run() method therefore it must be implemented.
Option A and D are incorrect because they are narrowing the access privileges i.e. package
(default) access is narrower than public access.
Option C is not method in the Runnable interface therefore it is incorrect.
What is the output of this program?
class newthread implements Runnable {
Thread t;
newthread() {
t = new Thread(this,"New Thread");
t.start();
}
public void run() {
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t);
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
Thread t has been made with default priority value 5 but in run method the priority has
been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code 't.setPriority(Thread.MAX_PRIORITY);' using the setPriority function of thread t.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,10,main]
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 {
System.out.print(obj1.t.equals(obj2.t));
}
catch(Exception e) {
System.out.print("Main thread interrupted");
}
}
}
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
Option A is correct. wait() causes the current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object.
Option B is wrong. notify() - wakes up a single thread that is waiting on this object's monitor.
Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor.
Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words,
the thread currently in the synchronized method) exits the synchronized method. At that point,
the lock is free until some other thread enters a synchronized method on that object. Does
entering/exiting synchronized code mean that the thread execution stops? Not necessarily
because the thread can still run code that is not synchronized. I think the word directly in the
question gives us a clue. Exiting synchronized code does not directly stop the execution of
a thread.
What is the output of this program?
class newthread extends Thread {
Thread t;
newthread() {
t = new Thread(this,"My Thread");
t.start();
}
public void run() {
try {
t.join()
System.out.println(t.getName());
}
catch(Exception e) {
System.out.print("Exception");
}
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
join() method of Thread class waits for thread being called to finish or terminate, but here
we have no condition which can terminate the thread, hence code 't.join()' leads to runtime
error and nothing will be printed on the screen.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread t has been made by using Runnable interface, hence it is necessary to use
inherited abstract method run() method to specify instructions to be implemented on
the thread, since no run() method is used it gives a compilation error.
Output:
$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method Runnable.run()
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 {
Thread.sleep(1000);
System.out.print(obj1.t.isAlive());
}
catch(InterruptedException e) {
System.out.print("Main thread interrupted");
}
}
}
Thread.sleep(1000) has caused all the threads to be suspended for some time,
None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]
Although we have not created any object of thread class still we can make a thread
pointing to main method, we can refer it by using this.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]