Threads

Q1: What will be the output of the program?
class MyThread extends Thread 
{
    MyThread() 
    {
        System.out.print(' MyThread');
    }
    public void run() 
    {
        System.out.print(' bar');
    }
    public void run(String s) 
    {
        System.out.println(' baz');
    }
}
public class TestThreads 
{
    public static void main (String [] args) 
    {
        Thread t = new MyThread() 
        {
            public void run() 
            {
                System.out.println(' foo');
            }
        };
        t.start();
    }
}

A foo

B MyThread foo

C MyThread bar

D foo bar

ANS:A - foo

Option B is correct because in the first line of main we're constructing an instance of an anonymous inner class extending from MyThread. So the MyThread constructor runs and prints 'MyThread'. The next statement in main invokes start() on the new thread instance, which causes the overridden run() method (the run() method defined in the anonymous inner class) to be invoked, which prints 'foo'



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.