adplus-dvertising
frame-decoration

Question

What will be the output of the program?

public class Test 
{
    public static void main (String [] args) 
    {
        final Foo f = new Foo();
        Thread t = new Thread(new Runnable() 
        {
            public void run() 
            {
                f.doStuff();
            }
        });
        Thread g = new Thread() 
        {
            public void run() 
            {
                f.doStuff();
            }
        };
        t.start();
        g.start();
    }
}
class Foo 
{
    int x = 5;
    public void doStuff() 
    {
        if (x < 10) 
        {
            // nothing to do
            try 
            {
                wait();
                } catch(InterruptedException ex) { }
        } 
        else 
        {
            System.out.println("x is " + x++);
            if (x >= 10) 
            {
                notify();
            }
        }
    }
}

a.

The code will not compile because of an error on notify(); of class Foo.

b.

The code will not compile because of some other error in class Test.

c.

An exception occurs at runtime.

d.

It prints "x is 5 x is 6".

Answer: (c).An exception occurs at runtime.

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What will be the output of the program?

Similar Questions

Discover Related MCQs

Q. Which cannot directly cause a thread to stop executing?

Q. Which three are methods of the Object class?

1. notify();
2. notifyAll();
3. isInterrupted();
4. synchronized();
5. interrupt();
6. wait(long msecs);
7. sleep(long msecs);
8. yield();

Q. Which two are valid constructors for Thread?

1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
5. Thread(Runnable r, int priority)

Q. What is the name of the method used to start a thread execution?

Q. Which class or interface defines the wait(), notify(),and notifyAll() methods?

Q. Which of the following will not directly cause a thread to stop?

Q. Assume the following method is properly synchronized and called from a thread A on an object B:

wait(2000);

After calling this method, when will the thread A become a candidate to get another turn at the CPU?

Q. Which method registers a thread in a thread scheduler?

Q. Which will contain the body of the thread?

Q. Which method must be defined by a class implementing the java.lang.Runnable interface?

Q. Which of the following will directly stop the execution of a Thread?

Q. Which three guarantee that a thread will leave the running state?

1. yield()
2. wait()
3. notify()
4. notifyAll()
5. sleep(1000)
6. aLiveThread.join()
7. Thread.killThread()

Q. Which two of the following methods are defined in class Thread?

1. start()
2. wait()
3. notify()
4. run()
5. terminate()

Q. Which of these method wakes up all the threads?

Q. Which of these method wakes up the first thread that called wait()?

Q. Which of these method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor?

Q. Which of this method is used to avoid polling in Java?

Q. Which of the following will ensure the thread will be in running state?

Q. Which of the following stops execution of a thread?

Q. Which of the following is a correct constructor for thread?