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

Home > Java Programming > Threads > Questions and Answers

Q: What will be the output of the program?
class MyThread extends Thread
{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}
A. Prints "Inside Thread Inside Thread" B. Prints "Inside Thread Inside Runnable"
C. Does not compile D. Throws exception at runtime

Answer and Explanation

Answer:Prints "Inside Thread Inside Thread"

Explanation:
If a Runnable object is passed to the Thread constructor, then the run method of the Thread class will invoke the run method of the Runnable object.
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.

Comment on this Question

Name:

Loading...




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