Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

What will be output on compiling and running the following code?

  1. public class DemoThread extends Thread{
  2.    
  3.     public void run(String msg){
  4.         System.out.println("Thread Started: " + msg);
  5.     }
  6.    
  7.     public static void main(String args[]){
  8.         DemoThread thread = new DemoThread();
  9.         thread.start();
  10.     }    
  11. }
a. Program will not compile
b. Program will compile, but will not run.
c. Program will run and print "Thread Started"
d. Program will run and print nothing.

Answer:
D is the correct choice. There is no compilation or runtime error.Class extends Thread class. However, when start method of thread is called, it calls run method whose signature is,

public void run()

Here, the class has run method, which is overriden method. Hence, there will call run method defined in the Thread class, which does nothing. So D is the correct choice.
No votes yet
Share this

Post new comment