Java OCA OCP Practice Question 2189

Question

Given the following program, which statements are guaranteed to be true?

public class Main {
  static Thread makeThread(final String id, boolean daemon) {
    Thread t = new Thread(id) {
      public void run() {
        System.out.println(id);/* w w w  . j  a  va2  s.c o  m*/
      }
    };
    t.setDaemon(daemon);
    t.start();
    return t;
  }

  public static void main(String[] args) {
    Thread a = makeThread("A", false);
    Thread b = makeThread("B", true);
    System.out.print("End\n");
  }
}

    

Select the two correct answers.

  • (a) The letter A is always printed.
  • (b) The letter B is always printed.
  • (c) The letter A is never printed after End.
  • (d) The letter B is never printed after End.
  • (e) The program might print B, End, and A, in that order.


(a) and (e)

Note

Because the exact behavior of the scheduler is undefined, the text A, B, and End can be printed in any order.

The thread printing B is a daemon thread, which means that the program may terminate before the thread manages to print the letter.

Thread A is not a daemon thread, so the letter A will always be printed.




PreviousNext

Related