Welcome Guest | Sign in | Register

Home > Java Programming > Threads > Questions and Answers

01. What will happen when you attempt to compile and run the following code?
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

Answer and Explanation

Answer: run-test run-test

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.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. Which of the following are methods of the Thread class?

1) yield()
2) sleep(long msec)
3) go()
4) stop()
A. 1, 2 and 4 B. 1 and 3
C. 3 only D. None of the above

Answer and Explanation

Answer: 1, 2 and 4

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What notifyAll() method do?
A. Wakes up all threads that are waiting on this object's monitor B. Wakes up one threads that are waiting on this object's monitor
C. Wakes up all threads that are not waiting on this object's monitor D. None of the above

Answer and Explanation

Answer: Wakes up all threads that are waiting on this object's monitor

Explanation:
notifyAll() : Wakes up all threads that are waiting on this object's monitor.A thread waits on an object's monitor by calling one of the wait methods.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Predict the output?

5. try{
6. throw new Exception();
7. }catch(Error e){
8. System.out.print("EXCE");
9. }
A. Compilation error: Error cannot catch B. Run time exception 
C. Output: EXCE D. None of the above

Answer and Explanation

Answer: Run time exception 

Explanation:
No problem for putting Error in catch but it will not catch Exception vice versa,

Following catch block will catch both Exception and Error

5. try{
6. throw new Exception();
7. }catch(Throwable e){
8. System.out.print("EXCE");
9. }

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. 1. class A implements Runnable {
2. int I;
3. public void run () {
4. try {
5. Thread.sleep(5000);
6. i=10;
7. }catch(InterruptedException e) {}
8. }
9. }
10.
11. public class Test {
12. public static void main (String args[]) {
13. try {
14. A a = new A();
15. Thread t = new Thread(a);
16. t.start();
17.
18. int j= a.i;
19.
20. }catch (Exception e) {}
21. }
22. }

Which statement at line 17 will ensure that j=10 at line 19 
A. a.wait() B. t.wait()
C. t.join() D. t.yield()

Answer and Explanation

Answer: t.join()

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Which of the following method can be a override method of
public void run()
A. private void run() B. public void run()
C. public void run() throws Exception D.  public String run()

Answer and Explanation

Answer: public void run()

Explanation:
A - Visilbility cannot be reduced
C- Cannot throw new exceptions
D- cannot change the return type

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. Which two CANNOT directly cause a thread to stop executing? (Choose Two)

A. Calling the yield method.
B. Calling the wait method on an object.
C. Calling the notify method on an object.
D. Calling the notifyAll method on an object.
E. Calling the start method on another Thread object.
A. A,E B. C,D
C. B,E D. A,C

Answer and Explanation

Answer: C,D

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. Which statement is true?

A. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately
resumes execution.

B. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.

C. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.

D. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
A. A B. B
C. C D. D

Answer and Explanation

Answer: B

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Which of the following statements are true? 
A. All the threads created in a class come to an end at the same time.  B. You can stop a thread indefinitely if u wishes to. 
C. You can start a thread only by extending the Thread class  D. Multiple threads accessing a method will lead to producing junk. 

Answer and Explanation

Answer: You can stop a thread indefinitely if u wishes to. 

Explanation:
JVM exits after the main() thread is exited even if there might be some threads running _ This statement is true if the remaining threads are all daemon threads.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. Given the following,
1. class MyThread extends Thread {
2.
3. public static void main(String [] args) {
4. MyThread t = new MyThread();
5. Thread x = new Thread(t);
6. x.start();
7. }
8.
9. public void run() {
10. for(int i=0;i<3;++i) {
11. System.out.print(i + "..");
12. }
13. }
14. }

What is the result of this code?
A. Compilation fails B. 1..2..3..
C. 0..1..2.. D. An exception occurs at runtime.

Answer and Explanation

Answer: 0..1..2..

Explanation:
C. The thread MyThread will start and loop three times (from 0 to 2). A is incorrect because the Thread class implements the Runnable interface; therefore, in line 5, Thread can take an object of type Thread as an argument in the constructor. B is incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2. D is incorrect because of the program logic described above.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
11. Which of the following statements are true. 
 
A. The wait method defined in the Thread class, can be used to convert a
thread from Running state to Waiting state.
B. The wait(), notify(), and notifyAll() methods must be executed in
synchronized code.
c. The notify() and notifyAll() methods can be used to signal and move
waiting threads to ready-to-run state.
d. The Thread class is an abstract class. 
A. A,C B. B,D
C. A,B D. B,C

Answer and Explanation

Answer: B,C

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.