Which two are true?

Given:

public class DoubleThread {

public static void main(String[] args) {

Thread t1 = new Thread() {

public void run() {

System.out.print(“Greeting”);

}

};

Thread t2 = new Thread(t1); // Line 9

t2.run();

}

}

Which two are true?

Given:

public class DoubleThread {

public static void main(String[] args) {

Thread t1 = new Thread() {

public void run() {

System.out.print(“Greeting”);

}

};

Thread t2 = new Thread(t1); // Line 9

t2.run();

}

}

Which two are true?

A.
A runtime exception is thrown on line 9.

B.
No output is produced.

C.
Greeting is printed once.

D.
Greeting is printed twice.

E.
No new threads of execution are started within the main method.

F.
One new thread of execution is started within the main method.

G.
Two new threads of execution are started within the main method.

Explanation:
Thread t2 is executed. Execution of T2 starts executionen of t1. Greeting is printed during the execution of t1.



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Elaine

Elaine

A F
public class Q2DoubleThread {

public static void main(String[] args) {

Thread t1 = new Thread() {

public void run() {

System.out.print(“Greeting”);

}

};

Thread t2 = new Thread(t1); // Line 9

t2.run();

System.out.println(“\n” + Thread.currentThread().getName());

}

}

output:
Greeting
main

Jav

Jav

C and E, compiled and runed

Shamitha Silva

Shamitha Silva

Thread itself implements Runnable so can pass a Thread object to the Thread Constructor.

gelete

gelete

CE

Eclipse output:
Greeting