JAVA Programming :: Threads

Home > JAVA Programming > Threads > General Questions

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

1 / 6

 What will happen when you try to compile and run this code?

public class TGo implements Runnable{
public static void main(String argv[]){
TGo tg = new TGo();
Thread t = new Thread(tg);
t.start();
}
public void run() {
while(true) {
Thread.currentThread().sleep(1000);
System.out.println("looping while");
}
}
}

ACompilation and no output

BCompilation and repeated output of "looping while"

CCompilation and single output of "looping while"

DCompile time error

Answer: Option B

Explanation:

Thread TGo is created by implementing the Runnable interface.

When the run() method executes,

Our thread first waits for a time unit of 1000 ms after which it prints looping while, and this goes on repeatedly.

Hence, the code compiles fine and we get repeated output of "looping while". 

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

2 / 6

 What is the output for the below code ?

class A implements Runnable{
public void run(){
System.out.println("run-a");
}
}
1. public class Test {
2. public static void main(String... args) {
3. A a = new A();
4. Thread t = new Thread(;
5. t.start();
6. t.start();
7. }
8. }

Arun-a

Brun-a run-a

CCompilation fails with an error at line 6

DCompilation succeed but Runtime Exception

Answer: Option D

Explanation:

Once a thread has been started, it can never be started again. 2nd time t.start() throws javlang.IllegalThreadStateException.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

3 / 6

 What is the output for the bellow code?

import javutil.Iterator;
import javutil.Set;
import javutil.TreeSet;
public class Test {
public static void main(String... args) {
Set s = new TreeSet();
s.add("7");
s.add(9);
Iterator itr = s.iterator();
while (itr.hasNext())
System.out.print(itr.next() + " ");
}
}

ACompile error

BRuntime Exception

C7 9

DNone of the above

Answer: Option B

Explanation:

Without generics, the compiler does not know what type is appropriate for this TreeSet, so it allows everything to compile. But at runtime he TreeSet will try to sort the elements as they are added, and when it tries to compare an Integer with a String it will throw a ClassCastException. Exception in thread "main" javlang.ClassCastException: javlang.String cannot be cast to javlang.Integer.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

4 / 6

 What will happen when you attempt to compile and run the following code ?

public class Test extends Thread{
public static void main(String argv[]){
Test t = new Test(); t.run();
t.start();
}
public void run(){
System.out.println("run-test");
}
}

Arun-test run-test

Brun-test

CCompilation fails due to an error on line 4

DCompilation fails due to an error on line 7

Answer: Option A

Explanation:

t.run() Legal, but does not start a new thread , it is like a method call of class Test BUT t.start() create a thread and call run() method.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

5 / 6

 What is the output for the below code?

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();
}
}

AA A A A B B B B

BA B A B A B A B

COutput order is not guaranteed

DCompilation succeed but Runtime Exception

Answer: Option A

Explanation:

t.join(); means Threat t must finish before Thread t1 start.

Workspace

NA
SHSTTON
0
Solv. Corr.
0
Solv. In. Corr.
0
Attempted
0 M:0 S
Avg. Time

6 / 6

 Which of the following statements are true.

AThe wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.

BThe wait(), notify(), and notifyAll() methods must be executed in synchronized code.

CThe notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.

DThe Thread class is an abstract class.

EBoth B & C

Answer: Option E

Explanation:

Here is no explanation for this answer

Workspace