
Multithreading
Quiz
•
Computers
•
University
•
Practice Problem
•
Hard
SUBHASH AGRAWAL
Used 4+ times
FREE Resource
Enhance your content in a minute
8 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
class MyThread implements Runnable {
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
2.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
class MyThread extends Thread {
public MyThread(String name) {
this.setName(name);
}
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();
}
}
a) This program will throw an IllegalStateException.
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.
3.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
class Worker extends Thread {
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 IllegalMonitorStateException.
e) The program does not compile and fails with multiple compiler errors.
4.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What code is missing?
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
new Thread(MyRunnable).run();
new Runnable(MyRunnable).start();
new Thread(new MyRunnable()).start();
new MyRunnable().start();
5.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Which of the following line of code is suitable to start a thread?
class Demo implements Runnable
{
public void run() {
System.out.println(“Thread is in Running state”);
}
public static void main(String args[])
{
/* Missing code? */
}
}
Demo obj = new Demo();
Thread tobj = new Thread(obj);
tobj.start();
Thread t = new Thread(X);
t.start();
Thread t = new Thread(Demo);
Thread t = new Thread();
Demo.run();
6.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
class ThreadDemo extends Thread
{
public static void main(String [] args)
{
ThreadDemo t = new ThreadDemo();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run()
{
System.out.print("Thread ");
}
}
An exception occurs at runtime.
Compilation fails.
. It prints "Thread one. Thread two.".
The output cannot be determined.
7.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
1. public class Test extends Thread{
2. public static void main(String argv[]){
3. Test t = new Test();
4. t.run();
5. t.start();
6. }
7. public void run(){
8. System.out.println("run-test");
9. }
10. }
A. run-test run-test
B. run-test
C. Compilation fails due to an error on line 4
D. Compilation fails due to an error on line 7
Access all questions and much more by creating a free account
Create resources
Host any resource
Get auto-graded reports

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?
Similar Resources on Wayground
13 questions
Internet Basics - Northstar Module 2
Quiz
•
University
9 questions
Python
Quiz
•
University
10 questions
Chapter 5 Test Management Part 1
Quiz
•
University
10 questions
GODOT ENGINE
Quiz
•
University
12 questions
Intro to Scratch 2
Quiz
•
KG - University
12 questions
Lesson 1 - Introduction to Operating Systems
Quiz
•
University
10 questions
CSNP-04103 Chapter 6 - Functions
Quiz
•
University
10 questions
Cross-Site Scripting
Quiz
•
University
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
22 questions
fractions
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
15 questions
Equivalent Fractions
Quiz
•
4th Grade
20 questions
Figurative Language Review
Quiz
•
6th Grade
