Java OCA OCP Practice Question 1754

Question

Which statements are true about the lines of output printed by the following program?.

public class Main {
  static void op(boolean a, boolean b) {
    boolean c = a != b;
    boolean d = a ^ b;
    boolean e = c == d;
    System.out.println(e);/*w  w  w. ja v a 2  s. c  o m*/
  }

  public static void main(String[] args) {
    op(false, false);
    op(true, false);
    op(false, true);
    op(true, true);
  }
}

Select the three correct answers.

  • (a) All lines printed are the same.
  • (b) At least one line contains false.
  • (c) At least one line contains true.
  • (d) The first line contains false.
  • (e) The last line contains true.


(a), (c), and (e)

Note

The != and ^ operators, when used on boolean operands, will return true if and only if one operand is true, and false otherwise.

This means that d and e in the program will always be assigned the same value, given any combination of truth values in a and b.

The program will, therefore, print true four times.




PreviousNext

Related