Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

JAVA Programming :: Threads

Home > JAVA Programming > Threads > General Questions

1. 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");
}
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

Tags:

No Tags on this question yet!

2. 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. }

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option D

Explanation:

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

Workspace

Tags:

No Tags on this question yet!

3. 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() + " ");
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

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

Tags:

No Tags on this question yet!

4. 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");
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

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

Tags:

No Tags on this question yet!

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

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option A

Explanation:

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

Workspace

Tags:

No Tags on this question yet!

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option E

Explanation:

Here is no explanation for this answer

Workspace

Tags:

No Tags on this question yet!