Java OCA OCP Practice Question 2510

Question

What is the output of the following code?

class Main extends Thread {
    public void run() {
        this.yield();                      //line1
        for (int i = 0; i < 1000; i++)
            System.out.print(i);//  w  w w  .  ja v  a  2s.c o m
    }
    public static void main(String... args) {
        Thread myThread = new Main();
        myThread.run();                    //line2
    }
}
  • a On execution, code at line 1 might make the thread main give up its execution slot.
  • b On execution, code at line 1 makes the thread myThread give up its execution slot.
  • c Compilation error at line 1
  • d Runtime exception at line 2


a

Note

The code at line 2 doesn't start a new thread of execution.

So myThread.run() executes in the main thread and not a separate thread.

When main executes this.yield() (yield() is a static method), it might make main give up its execution slot and wait until the scheduler allows it to run again.




PreviousNext

Related