Java Multiple Choice Questions & Answers on isAlive(), Join() & Thread Synchronization for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_30.html
1. Which of these method can be used to make the main thread to be executed last among all the threads?
a) stop()
b) sleep()
c) join()
d) call()
Answer: b
Explanation: By calling sleep() within main(), with long enough delay to ensure that all child threads terminate prior to the main thread.
2. Which of these method is used to find out that a thread is still running or not?
a) run()
b) Alive()
c) isAlive()
d) checkRun()
Answer: c
Explanation: The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise.
3. What is the default value of priority variable MIN_PRIORITY AND MAX_PRIORITY?
a) 0 & 256
b) 0 & 1
c) 1 & 10
d) 1 & 256
Answer: c
4. Which of these method waits for the thread to treminate?
a) sleep()
b) isAlive()
c) join()
d) stop()
Answer: c
5. Which of these method is used to explicitly set the priority of a thread?
a) set()
b) make()
c) setPriority()
d) makePriority()
Answer: c
Explanation: 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().
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) Its a process by which many thread are able to access same shared resource simultaneously.
c) Its a process by which a method is able to access many different threads simultaneously.
d) Its a method that allow to many threads to access any information require.
Answer: a
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 is the output of this program?
class newthread extends Thread {
newthread() {
super("My Thread");
start();
}
public void run() {
System.out.println(this);
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
a) My Thread
b) Thread[My Thread,5,main] c) Compilation Error
d) Runtime Error
b) Thread[My Thread,5,main] c) Compilation Error
d) Runtime Error
Answer: b
Explanation: 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.
8. 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();
}
}
a) My Thread
b) Thread[My Thread,5,main] c) Exception
d) Runtime Error
b) Thread[My Thread,5,main] c) Exception
d) Runtime Error
Answer: d
Explanation: 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.
9. What is the output of this program?
class newthread extends Thread {
Thread t;
newthread() {
t = new Thread(this,"New Thread");
t.start();
}
public void run() {
System.out.println(t.isAlive());
}
}
class multithreaded_programing {
public static void main(String args[]) {
new newthread();
}
}
a) 0
b) 1
c) true
d) false
b) 1
c) true
d) false
Answer: c
Explanation: 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.
10. 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();
}
}
a) true
b) false
c) truetrue
d) falsefalse
b) false
c) truetrue
d) falsefalse
Answer: d
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.