Java OCA OCP Practice Question 2666

Question

Consider the following program and choose the best option:

class MyThread extends Thread {
        public void run() {
                System.out.print("Burp! ");
        }//from  w ww  .  j a  va2  s . c o m
        public static void main(String args[]) throws InterruptedException  {
                Thread myThread = new MyThread();
                myThread.start();
                System.out.print ("Eat! ");
                myThread.join();
                System.out.print ("Run! ");
        }
}
  • a) When executed, it prints always the following: Eat! Burp! Run!
  • b) When executed, it prints one of the following: Eat! Burp! Run! or Burp! Eat! Run!
  • c) When executed, it prints one of the following: Eat! Burp! Run!; Burp! Eat! Run!; or Run! Eat! Burp!
  • d) When executed, it prints one of the following: Burp! Eat! Run! or Burp! Run! Eat!


b)

Note

If the thread myThread is scheduled to run first immediately after start() is called, it will print "Burp! Eat! Run!"; otherwise it will print "Eat! Burp! Run!" The output "Run!" will always be executed last because of the join() method call in the main() method.




PreviousNext

Related