AP Computer Science A · Topic 3.6

Equivalent Boolean Expressions (De Morgan's Laws) Practice

Part of Boolean Expressions and if Statements.

Practice questions

10

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 10 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 3/5

    if (!(a > 0 && b > 0)) {
        System.out.println("no");
    }

    Which condition is equivalent to !(a > 0 && b > 0)?

    • A

      !(a > 0) && !(b > 0)

    • B

      a < 0 && b < 0

    • C

      a <= 0 || b <= 0

      check_circle
    • D

      a <= 0 && b <= 0

    Why

    By De Morgan's law, !(P && Q) becomes !P || !Q, and !(a > 0) is a <= 0.

  2. Sample 2difficulty 3/5

    int a = 5;
    int b = 10;
    boolean p = !(a > 3 && b < 20);
    boolean q = (a <= 3 || b >= 20);
    System.out.println(p == q);

    What is printed?

    • A

      true

      check_circle
    • B

      false

    • C

      p

    • D

      q

    Why

    By De Morgan's law, !(A && B) is equivalent to !A || !B. So p and q are logically equivalent and both evaluate to false here, making p == q true.

  3. Sample 3difficulty 4/5

    "If a then b" can be written as

    • A

      a || b

    • B

      a && b

    • C

      !(a || b)

    • D

      !a || b

      check_circle

    Why

    <code>a → b</code> is logically equivalent to <code>(NOT a) OR b</code>.

  4. Sample 4difficulty 4/5

    <code>(a || !a)</code> evaluates to

    • A

      Compile error

    • B

      false (always)

    • C

      depends on a

    • D

      true (always; tautology)

      check_circle

    Why

    Either a or its negation is true; always true.

  5. Sample 5difficulty 4/5

    int a = 5;
    int b = 3;
    boolean c1 = !(a > 0 || b > 0);
    boolean c2 = (a <= 0 && b <= 0);
    System.out.println(c1 == c2);

    What is printed?

    • A

      false

    • B

      true

      check_circle
    • C

      c1

    • D

      c2

    Why

    By De Morgan's: !(A || B) is equivalent to !A && !B. So c1 and c2 are logically equivalent (both false here), making c1 == c2 true.