
Multithreading

Quiz
•
Computers
•
University
•
Hard
SUBHASH AGRAWAL
Used 4+ times
FREE Resource
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
8.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
class A implements Runnable{
public void run(){
try{
for(int i=0;i<4;i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName());
}
}catch(InterruptedException e){
}
}
}
public class Test{
public static void main(String argv[]) throws Exception{
A a = new A();
Thread t = new Thread(a, "A");
Thread t1 = new Thread(a, "B");
t.start();
t.join();
t1.start();
}
}
Compilation succeed but Runtime Exception
A B A B A B A B
Output order is not guaranteed
A A A A B B B B
Similar Resources on Wayground
10 questions
Week 12 Quiz

Quiz
•
University
7 questions
Session 10

Quiz
•
University
13 questions
Cuestionario de Programación Avanzada

Quiz
•
University
11 questions
C++ Funzioni

Quiz
•
University
12 questions
PBO TRKJ

Quiz
•
University
10 questions
DEBUG THE CODE

Quiz
•
University
10 questions
C Programming - Basics 001

Quiz
•
University
10 questions
Java Quiz

Quiz
•
University
Popular Resources on Wayground
10 questions
Video Games

Quiz
•
6th - 12th Grade
20 questions
Brand Labels

Quiz
•
5th - 12th Grade
15 questions
Core 4 of Customer Service - Student Edition

Quiz
•
6th - 8th Grade
15 questions
What is Bullying?- Bullying Lesson Series 6-12

Lesson
•
11th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
22 questions
Adding Integers

Quiz
•
6th Grade
10 questions
Exploring Digital Citizenship Essentials

Interactive video
•
6th - 10th Grade
Discover more resources for Computers
20 questions
Definite and Indefinite Articles in Spanish (Avancemos)

Quiz
•
8th Grade - University
7 questions
Force and Motion

Interactive video
•
4th Grade - University
36 questions
Unit 5 Key Terms

Quiz
•
11th Grade - University
7 questions
Figurative Language: Idioms, Similes, and Metaphors

Interactive video
•
4th Grade - University
15 questions
Properties of Equality

Quiz
•
8th Grade - University
38 questions
WH - Unit 3 Exam Review*

Quiz
•
10th Grade - University
21 questions
Advise vs. Advice

Quiz
•
6th Grade - University
12 questions
Reading a ruler!

Quiz
•
9th Grade - University