AP Computer Science A · Topic 3.1

Boolean Expressions Practice

Part of Boolean Expressions and if Statements.

Practice questions

24

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 24 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 1/5

    Which evaluates to <code>true</code>?

    • A

      5 != 5

    • B

      5 <= 5

      check_circle
    • C

      5 > 7

    • D

      5 == 6

    Why

    <code><=</code> is "less than or equal"; 5 ≤ 5 is true.

  2. Sample 2difficulty 1/5

    int a = 5;
    int b = 5;
    if (a <= b) {
      System.out.println("yes");
    } else {
      System.out.println("no");
    }

    What is printed?

    • A

      true

    • B

      false

    • C

      no

    • D

      yes

      check_circle

    Why

    a <= b means "a less than or equal to b". 5 <= 5 is true, so "yes" prints.

  3. Sample 3difficulty 2/5

    Which assigns <code>true</code> to <code>b</code>?

    • A

      boolean b = 5 > 3;

      check_circle
    • B

      boolean b = 5 = 3;

    • C

      boolean b = 5;

    • D

      boolean b = (5 = 3);

    Why

    <code>></code> is comparison; 5 > 3 evaluates to <code>true</code>. <code>=</code> is assignment, not comparison.

  4. Sample 4difficulty 2/5

    int x = 3;
    int y = 3;
    if (x == y) {
      System.out.println("equal");
    } else {
      System.out.println("not equal");
    }

    What is printed?

    • A

      not equal

    • B

      equal

      check_circle
    • C

      compile error

    • D

      true

    Why

    == is the equality comparison operator. x and y both equal 3, so the condition is true and "equal" is printed.

  5. Sample 5difficulty 2/5

    int a = 5;
    int b = 5;
    if (a != b) {
      System.out.println("X");
    } else {
      System.out.println("Y");
    }

    What is printed?

    • A

      Y

      check_circle
    • B

      X

    • C

      true

    • D

      false

    Why

    a and b are equal, so a != b is false, and the else branch prints "Y".