Learn everything about Java Multithreading: Java Multithreading Tutorial
Learn and Master Java Programming: Learn Java Programming with Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
1. What is multithreading in Java?
Answer:
c) A concept of executing multiple threads concurrently within a single program.
Explanation:
2. What is a thread in Java?
Answer:
a) A lightweight process that executes a sequence of instructions.
Explanation:
3. Which class is used to create a thread in Java?
Answer:
a) Thread
Explanation:
4. How can synchronization be achieved in Java threads?
Answer:
a) Using the synchronized keyword.
Explanation:
5. What is the purpose of the wait() method in Java threads?
Answer:
a) To pause the execution of a thread.
Explanation:
6. Which method is used to start the execution of a thread?
Answer:
a) start()
Explanation:
7. What is the maximum number of threads that can be created in a Java program?
Answer:
a) There is no fixed limit.
Explanation:
8. What is the purpose of the join() method in Java threads?
Answer:
b) To wait for a thread to finish its execution.
Explanation:
9. What is the main advantage of multithreading in Java?
Answer:
a) Improved program performance.
Explanation:
10. Consider the following program and predict the output:
class MyThread extends Thread {
public MyThread(String name) {
this.setName(name);
start();
System.out.println("in constructor " + getName());
}
@Override
public void start() {
System.out.println("in start " + getName());
}
public void run() {
System.out.println("in run " + getName());
}
}
public class ThreadTest {
public static void main(String[] args) {
new MyThread("oops");
}
}
in start oops
in constructor oops
in start oops
in run oops
in constructor oops
in start oops
in constructor oops
in run oops
in constructor oops
in start oops
in run oops
Answer:
in start oops
in constructor oops
Explanation:
You have overridden the start() method, so the run() method is never called!11. Consider the following program and predict the output:
class MyThread extends Thread {
@Override
public void run() {
System.out.println("In run method; thread name is: " + Thread.currentThread().getName());
}
}
public class ThreadTest {
public static void main(String args[]) {
Thread myThread = new MyThread();
myThread.run(); // #1
System.out.println("In main method; thread name is: " + Thread.currentThread().getName());
}
}
b) The program results in a runtime exception.
c) The program prints the following:
In run method; thread name is: main
In main method; thread name is: main
d) The program prints:
In the run method; the thread name is: thread-0
In the main method; the thread name is: main
Answer:
In run method; thread name is: main
In main method; thread name is: main
Explanation:
The correct way to invoke a thread is to call the start() method on a Thread object. If you directly call the run() method, the method will run just like any other method (in other words, it will execute sequentially in the same thread without running as a separate thread).12. Consider the following program and predict the output:
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadTest {
public static void main(String arg[]) {
Thread thread = new Thread(new MyThread());
thread.run();
thread.run();
thread.start();
}
}
main
main
Thread-0
Thread-0
main
Thread-1
main
Thread-0
Thread-1
Thread-0
Thread-1
Thread-2
Answer:
main
main
Thread-0
Explanation:
13. Consider the following program and choose the right option:
class MyThread extends Thread {
public void run() {
System.out.println("Running");
}
}
public class ThreadTest {
public static void main(String args[]) throws InterruptedException {
Runnable r = new MyThread(); // #1
Thread myThread = new Thread(r); // #2
myThread.start();
}
}
b) The program will result in a compilation error at statement #2.
c) The program will compile with no errors and will print “Running” in the console.
d) The program will compile with no errors but does not print any output in the console.
Answer:
Explanation:
The class Thread implements the Runnable interface, so the assignment in statement #1 is valid. Also, you can create a new thread object by passing a Runnable reference to a Thread constructor, so statement #2 is also valid. Hence, the program will compile without errors and print “Running” in the console.14. Consider the following program and choose the correct answer:
class MyThread extends Thread {
public MyThread(String name) {
this.setName(name);
}
@Override
public void run() {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
play();
}
private void play() {
System.out.print(getName());
System.out.print(getName());
}
}
public class ThreadTest {
public static void main(String args[]) throws InterruptedException {
Thread tableThread = new MyThread("Table");
Thread tennisThread = new MyThread("Tennis");
tableThread.start();
tennisThread.start();
}
}
b) This program will always print the following: Tennis Tennis Table Table.
c) This program will always print the following: Table Table Tennis Tennis.
d) The output of this program cannot be predicted; it depends on thread scheduling.
Answer:
Explanation:
15. Which of the following state(s) is/are NOT legitimate thread state(s)? (Select all that apply.)
a) NEWAnswer:
Explanation:
16. Consider the following program:
class Worker extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class Master {
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().setName("Master ");
Thread worker = new Worker();
worker.setName("Worker ");
worker.start();
Thread.currentThread().join();
System.out.println(Thread.currentThread().getName());
}
}
a) When executed, the program prints the following: “Worker Master ”.
b) When executed, the program prints “Worker ”, and after that the program hangs (i.e., does not terminate).
c) When executed, the program prints “Worker ” and then terminates.
d) When executed, the program throws an IllegalMonitorStateException.
e) The program does not compile and fails with multiple compiler errors.
Answer:
Explanation:
Conclusion
Learn and Master Java Programming: Learn Java Programming with Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Comments
Post a Comment
Leave Comment