Java OCA OCP Practice Question 3245

Question

Which statements are true about the following code?

public class Main {
  private int alt;
  public synchronized void up() {
    ++alt;/* w w  w.  j av  a  2 s.c  o  m*/
  }
  public void down() {
    --alt;
  }
  public synchronized void jump() {
    int a = alt;
    up();
    down();
    assert(a == alt);
  }
}

Select the two correct answers.

  • (a) The code will fail to compile.
  • (b) Separate threads can execute the up() method concurrently.
  • (c) Separate threads can execute the down() method concurrently.
  • (d) Separate threads can execute both the up() and the down() methods concurrently.
  • (e) The assertion in the jump() method will not fail under any circumstances.


(c) and (d)

Note

Executing synchronized code does not guard against executing non-synchronized code concurrently.




PreviousNext

Related