AP Computer Science A · Topic 3.3

if-else Statements Practice

Part of Boolean Expressions and if Statements.

Practice questions

6

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

  1. Sample 1difficulty 2/5

    if (g >= 90) grade = 'A';
    if (g >= 80) grade = 'B';
    if (g >= 70) grade = 'C';

    Why does a 95 produce grade 'C'?

    • A

      char cannot hold a grade

    • B

      g must be a String

    • C

      All ifs run; later assignments overwrite earlier ones. Use else if.

      check_circle
    • D

      Nothing is wrong

    Why

    Independent ifs all execute; with g=95 every condition is true, so the last assignment ('C') wins. Chain with else if for mutual exclusion.

  2. Sample 2difficulty 2/5

    What does this print when x = 5?

    <code>if (x > 10) System.out.println("big"); else System.out.println("small"); </code></pre>

    • A

      Nothing

    • B

      big small

    • C

      small

      check_circle
    • D

      big

    Why

    5 is not > 10, so else branch runs.

  3. Sample 3difficulty 3/5

    int x = 10;
    String label = "none";
    if (x > 0) {
      label = "positive";
    }
    if (x > 5) {
      label = "big";
    }
    System.out.println(label);

    What is printed?

    • A

      none

    • B

      positive big

    • C

      big

      check_circle
    • D

      positive

    Why

    These are two SEPARATE if statements (no else). With x = 10, both run. label is set to "positive" then overwritten to "big". Final value printed is "big".

  4. Sample 4difficulty 3/5

    int n = 5;
    if (n > 0)
      if (n > 10)
        System.out.println("big");
      else
        System.out.println("small");

    What is printed?

    • A

      big

    • B

      small

      check_circle
    • C

      big small

    • D

      nothing

    Why

    The else binds to the nearest unmatched if (n > 10). Since n = 5, n > 0 is true (enter), n > 10 is false (else), so "small" prints.

  5. Sample 5difficulty 4/5

    For score = 85:

    <code>String grade; if (score >= 90) grade = "A"; else if (score >= 80) grade = "B"; else if (score >= 70) grade = "C"; else grade = "F"; </code></pre> grade is

    • A

      C

    • B

      B

      check_circle
    • C

      A

    • D

      F

    Why

    85 >= 80 (after failing 90 check) → "B".