CommerceThreads MCQs
Practice Threads MCQs for competitive exams.
Threads MCQs
Practice questions from this topic.
Given the code. What will be the result? public class Test implements Runnable{ public static void main(String[] args) throws InterruptedException{ Thread a = new Thread(new Test()); a.start(); System.out.print("Begin"); a.join(); System.out.print("End"); } public void run(){ System.out.print("Run"); } }
- A. Compilation fails.
- B. An exception is thrown at runtime.
- C. "BeginRunEnd" is printed.
- D. "BeginEndRun" is printed. E. "BeginEnd" is printed.
Correct Answer: C
What will be output of the following program code? public class Test implements Runnable{ public void run(){ System.out.print("go"); } public static void main(String arg[]) { Thread t = new Thread(new Test()); t.run(); t.run(); t.start(); } }
- A. Compilation fails.
- B. An exception is thrown at runtime.
- C. "go" is printed
- D. "gogogo" is printed E. "gogo" is printed
Correct Answer: D
What will be the output after compiling and executing the following code? public class Test implements Runnable{ public static void main(String[] args) throws InterruptedException{ Thread a = new Thread(new Test()); a.start(); System.out.print("Begin"); a.join(); System.out.print("End"); } public void run(){ System.out.print("Run"); } }
- A. Compilation fails.
- B. An exception is thrown at runtime.
- C. "BeginRunEnd" is printed.
- D. "BeginEndRun" is printed. E. "BeginEnd" is printed.
Correct Answer: C
Predict the output: public class Test extends Thread{ private int i; public void run(){ i++; } public static void main(String[] args){ Test a = new Test(); a.run(); System.out.print(a.i); a.start(); System.out.print(a.i); } }
- A. Run time error
- B. Compiler error
- C. Prints
- D. IllegalThreadStateException is thrown
Correct Answer: C
What is the output for the below code? class A implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName()); } } public class Test{ public static void main(String... args){ A a = new A(); Thread t = new Thread(a); t.setName("good"); t.start(); } }
- A. good
- B. null
- C. Compilation fails with an error at line 5
- D. Compilation succeed but Runtime Exception E. None of these
Correct Answer: A
Which keyword when applied on a method indicates that only one thread should execute the method at a time.
- A. volatile
- B. synchronized
- C. native
- D. static E. final
Correct Answer: B
What is the output for the below code? public class Test extends Thread{ public static void main(String argv[]){ Test t = new Test(); t.run(); } public void start(){ for(int i = 0; i < 10; i++){ System.out.println("Value of i = " + i); } } }
- A. A compile time error indicating that no run method is defined for the Thread class
- B. A run time error indicating that no run method is defined for the Thread class
- C. Clean compile and at run time the values 0 to 9 are printed out
- D. Clean compile but no output at runtime E. None of these
Correct Answer: D
What notifyAll() method do?
- A. Wakes up one threads that are waiting on this object's monitor
- B. Wakes up all threads that are not waiting on this object's monitor
- C. Wakes up all threads that are waiting on this object's monitor
- D. None of the above
Correct Answer: C
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
Correct Answer: A
What will happen when you attempt to compile and run the following code? class A implements Runnable{ public void run(){ System.out.println("run-A"); } } public class Test{ public static void main(String argv[]){ A a = new A(); Thread t = new Thread(a); System.out.println(t.isAlive()); t.start(); System.out.println(t.isAlive()); } }
- A. false run-A true
- B. false run-A false
- C. true run-A true
- D. Compilation fails due to an error on line 7 E. None of these
Correct Answer: A
What will be the output? class A extends Thread{ public void run(){ for(int i=0; i<2; i++){ System.out.println(i); } } } public class Test{ public static void main(String argv[]){ Test t = new Test(); t.check(new A(){}); } public void check(A a){ a.start(); } }
- A. 0 0
- B. Compilation error, class A has no start method
- C. 0 1
- D. Compilation succeed but runtime exception E. None of these
Correct Answer: C
Predict the output: 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(); } }
- A. A A A A B B B B
- B. A B A B A B A B
- C. Output order is not guaranteed
- D. Compilation succeed but Runtime Exception E. None of these
Correct Answer: A