Java OCA OCP Practice Question 2668

Question

Consider the following program and choose the correct answer:

class MyThread extends Thread {
        public MyThread(String name) {
                this.setName(name);
        }/*from  w  ww.  j  a  va  2 s  .com*/
        public void run(){
                try {
                        sleep(100);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                play();
        }
        private void play() {
                System.out.print(getName());
                System.out.print(getName());
        }
        public static void main(String args[]) throws InterruptedException  {
                Thread tableThread = new MyThread("Table");
                Thread tennisThread = new MyThread("Tennis");
                tableThread.start();
                tennisThread.start();
        }
}
  • a) This program will throw an IllegalMonitorStateException.
  • b) This program will always print the following: Tennis Tennis Table Table.
  • c) This program will always print the following: Table Table Tennis Tennis.
  • d) The output of this program cannot be predicted; it depends on thread scheduling.


d)

Note

Since the threads are not synchronized in this program, the output of this program cannot be determined.

Depending on how the threads are scheduled, it may even generate output such as Table Tennis Tennis Table.




PreviousNext

Related