Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Review Questions

13.9

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

public class ThreadedPrint {
  static Thread makeThread(final String id, boolean daemon) {
    Thread t = new Thread(id) {
      public void run() {
        System.out.println(id);
      }
    };
    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.

  1. The letter A is always printed.
  2. The letter B is always printed.
  3. The letter A is never printed after End.
  4. The letter B is never printed after End.
  5. The program might print B, End, and A, in that order.

13.10

Given the following program, which alternatives would make good choices to synchronize on at (1)?

public class Preference {
  private int account1;
  private Integer account2;

  public void doIt() {
    final Double account3 = new Double(10e10);
    synchronized(/* ___(1)___ */) {
      System.out.print("doIt");
    }
  }
}

Select the two correct answers.

  1. Synchronize on account1.
  2. Synchronize on account2.
  3. Synchronize on account3.
  4. Synchronize on this.

13.11

Which statements are not true about the synchronized block?

Select the three correct answers.

  1. If the expression in a synchronized block evaluates to null, a NullPointerException will be thrown.
  2. The lock is only released if the execution of the block terminates normally.
  3. A thread cannot hold more than one lock at a time.
  4. Synchronized statements cannot be nested.
  5. The braces cannot be omitted even if there is only a single statement to execute in the block.

13.12

Which statement is true?

Select the one correct answer.

  1. No two threads can concurrently execute synchronized methods on the same object.
  2. Methods declared synchronized should not be recursive, since the object lock will not allow new invocations of the method.
  3. Synchronized methods can only call other synchronized methods directly.
  4. Inside a synchronized method, one can assume that no other threads are currently executing any other methods in the same class.

13.13

Given the following program, which statement is true?

public class MyClass extends Thread {
  static Object lock1 = new Object();
  static Object lock2 = new Object();
  static volatile int i1, i2, j1, j2, k1, k2;
  public void run() { while (true) { doIt(); check(); } }
  void doIt() {
    synchronized(lock1) { i1++; }
    j1++;
    synchronized(lock2) { k1++; k2++; }
    j2++;
    synchronized(lock1) { i2++; }
  }
  void check() {
    if (i1 != i2) System.out.println("i");
    if (j1 != j2) System.out.println("j");
    if (k1 != k2) System.out.println("k");
  }
  public static void main(String[] args) {
    new MyClass().start();
    new MyClass().start();
  }
}

Select the one correct answer.

  1. The program will fail to compile.
  2. One cannot be certain whether any of the letters i, j, and k will be printed during execution.
  3. One can be certain that none of the letters i, j, and k will ever be printed during execution.
  4. One can be certain that the letters i and k will never be printed during execution.
  5. One can be certain that the letter k will never be printed during execution.

13.14

Given the following program, which code modifications will result in both threads being able to participate in printing one smiley (:-)) per line continuously?

public class Smiley extends Thread {

  public void run() {                                              // (1)
    while(true) {                                                  // (2)
      try {                                                        // (3)
          System.out.print(":");                                   // (4)
          sleep(100);                                              // (5)
          System.out.print("-");                                   // (6)
          sleep(100);                                              // (7)
          System.out.println(")");                                 // (8)
          sleep(100);                                              // (9)
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
    new Smiley().start();
    new Smiley().start();
  }
}

Select the two correct answers.

  1. Synchronize the run() method with the keyword synchronized, (1).
  2. Synchronize the while loop with a synchronized(Smiley.class) block, (2).
  3. Synchronize the try-catch construct with a synchronized(Smiley.class) block, (3).
  4. Synchronize the statements (4) to (9) with one synchronized(Smiley.class) block.
  5. Synchronize each statement (4), (6), and (8) individually with a synchronized(Smiley.class) block.
  6. None of the above will give the desired result.
  • + Share This
  • 🔖 Save To Your Account