AP Computer Science A · Topic 3.4

else if Statements Practice

Part of Boolean Expressions and if Statements.

Practice questions

4

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

Sample questions

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

  1. Sample 1difficulty 2/5

    int score = 75;
    if (score >= 90) {
      System.out.println("A");
    } else if (score >= 80) {
      System.out.println("B");
    } else if (score >= 70) {
      System.out.println("C");
    } else {
      System.out.println("F");
    }

    What is printed?

    • A

      C

      check_circle
    • B

      B

    • C

      F

    • D

      A

    Why

    score is 75. The first two conditions (>=90, >=80) fail. The third (>=70) is true, so "C" is printed and the rest of the chain is skipped.

  2. Sample 2difficulty 3/5

    int score = 95;
    if (score >= 70) {
      System.out.println("C");
    }
    if (score >= 80) {
      System.out.println("B");
    }
    if (score >= 90) {
      System.out.println("A");
    }

    What is printed (each on its own line)?

    • A

      C, B, A on three lines

      check_circle
    • B

      A only

    • C

      C only

    • D

      A, B, C on three lines

    Why

    These are three separate if statements (not else if). All three conditions are true for score 95, so "C", "B", and "A" all print in that order.

  3. Sample 3difficulty 3/5

    For grade computation, after <code>if (g >= 90)</code> returns A, the next <code>else if (g >= 80)</code> should have what condition?

    • A

      g == 80

    • B

      g >= 80 && g < 90 (redundant)

    • C

      g >= 80 (suffices because earlier branch caught >= 90)

      check_circle
    • D

      g >= 80 || g < 90

    Why

    Cascading else-if eliminates the need for explicit upper bound.

  4. Sample 4difficulty 3/5

    For <code>int x = 5;</code>:

    <code>if (x > 10) System.out.println("A"); else if (x > 0) System.out.println("B"); else System.out.println("C"); </code></pre> What is printed?

    • A

      B

      check_circle
    • B

      A B

    • C

      A

    • D

      C

    Why

    5 > 10 false; 5 > 0 true → "B" prints; else doesn't run.