Welcome Guest | Sign in | Register
Threads - Discussion Page For Q.2966 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Threads > Questions and Answers

Q: 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.

Comment on this Question

Name:

Loading...




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