CommerceThreads MCQs
Practice Threads MCQs for competitive exams.
Threads MCQs
Practice questions from this topic.
What will happen after compiling and running following code? class A implements Runnable{ public void run(){ System.out.println("run-a"); } } public class Test{ public static void main(String... args){ A a = new A(); Thread t = new Thread(a); t.start(); t.start(); } }
- A. run-a
- B. run-a run-a
- C. Compilation fails with an error at line 6
- D. Compilation succeed but Runtime Exception E. None of these
Correct Answer: D
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"); } }
- 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 E. None of these
Correct Answer: A
What will be the output? class One extends Thread{ public void run(){ for(int i=0; i<2; i++){ System.out.print(i); } } } public class Test{ public static void main(String args[]){ Test t = new Test(); t.call(new One()); } public void call(One o){ o.start(); } }
- A. 0 0
- B. Compilation Error
- C. 0 1
- D. None of these
Correct Answer: C
Analyze the following code: public class Test implements Runnable{ public static void main(String[] args){ Test t = new Test(); } public Test(){ Thread t = new Thread(this); t.start(); } public void run(){ System.out.println("test"); } }
- A. The program has a compilation error because t is defined in both the main() method and the constructor Test().
- B. The program compiles fine, but it does not run because you cannot use the keyword this in the constructor.
- C. The program compiles and runs and displays nothing.
- D. The program compiles and runs and displays test.
Correct Answer: D
Analyze the following code: public class Test implements Runnable{ public static void main(String[] args){ Test t = new Test(); t.start(); } public void run() { } }
- A. The program does not compile because the start() method is not defined in the Test class.
- B. The program compiles, but it does not run because the start() method is not defined.
- C. The program compiles, but it does not run because the run() method is not implemented.
- D. The program compiles and runs fine.
Correct Answer: A
Analyze the following code: public abstract class Test implements Runnable{ public void doSomething() { }; }
- A. The program will not compile because it does not implement the run() method.
- B. The program will not compile because it does not contain abstract methods.
- C. The program compiles fine.
- D. None of the above
Correct Answer: C
Which of the following constructor of class Thread is valid one?
- A. Thread(Runnable threadOb, int priority)
- B. Thread(int priority)
- C. Thread(Runnable threadOb, String threadName)
- D. Thread(String threadName, int priority) E. None of these
Correct Answer: C
What will be the output of the following program code? public class Test implements Runnable{ public static void main(String[] args){ Thread t = new Thread(this); t.start(); } public void run(){ System.out.println("test"); } }
- A. The program does not compile because this cannot be referenced in a static method.
- B. The program compiles fine, but it does not print anything because t does not invoke the run() method
- C. The program compiles and runs fine and displays test on the console.
- D. None of the above
Correct Answer: A
When a class extends the Thread class ,it should override ............ method of Thread class to start that thread.
- A. start()
- B. run()
- C. init()
- D. go()
Correct Answer: B
In java a thread can be created by ..........
- A. Extending the thread class.
- B. Implementing Runnable interface.
- C. Both of the above
- D. None of these
Correct Answer: C
In Java, what is the maximum thread priority value?
- A. 100
- B. 255
- C. 1000
- D. 10
Correct Answer: D
What is the purpose of the getState() method in Java threads?
- A. It suspends a thread
- B. It starts a new thread
- C. It retrieves the current state of a thread
- D. It sets the state of a thread
Correct Answer: C