Multithreading

Multithreading

University

8 Qs

quiz-placeholder

Similar activities

totally not some random shark-related quiz

totally not some random shark-related quiz

University

9 Qs

Multiahtreading

Multiahtreading

University

10 Qs

Java Static

Java Static

University

10 Qs

Inheritance

Inheritance

University

10 Qs

Java Quiz 2

Java Quiz 2

University

8 Qs

java operators

java operators

University

10 Qs

Java Basic Day-2

Java Basic Day-2

University

10 Qs

Multithreading + java

Multithreading + java

University

10 Qs

Multithreading

Multithreading

Assessment

Quiz

Computers

University

Hard

Created by

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