AP Computer Science A Unit 2: Selection and Iteration Practice Questions

Practice questions for AP Computer Science A Unit 2 on selection (if statements) and iteration (loops) in Java, with explanations for every answer.

171 questions · AP Computer Science A · by Study Foxy

Study these questions free

Questions

  1. Which control structure chooses whether to run code based on a condition?

    • Selection
    • Sequencing
    • Repetition
    • Instantiation
  2. Which control structure runs the same code multiple times?

    • Repetition (iteration)
    • Selection
    • Sequencing
    • Casting
  3. An algorithm must keep asking for a password until the user types the correct one. Which structure is essential?

    • A loop
    • A single if statement checked once
    • A cast from String to int
    • A constructor that runs once when the program starts
  4. Which are building blocks that can be combined to express any algorithm?

    • Sequencing
    • Selection
    • Repetition
    • Compilation
    • Commenting
  5. The order in which statements run can change the result of an algorithm.

    True or false

  6. A program should print "Freezing" only when temp is below 32. What should it use?

    • An if statement
    • A for loop that checks temp
    • A while loop around temp
    • A static variable
  7. A loop's body might run zero times.

    True or false

  8. What is printed? int x = 4; System.out.println(x % 2 == 0);

    • true
    • false
    • 0
    • 4
  9. What is printed? int a = 5; int b = 7; System.out.println(a != b);

    • true
    • false
    • 2
    • -2
  10. What is printed? int a = 5; System.out.println(a >= 5);

    • true
    • false
    • 5
    • 1
  11. What is printed? System.out.println(0.1 + 0.2 == 0.3);

    • false
    • true
    • 0.3
    • 0.30000000000000004
  12. What is printed? boolean done = 10 > 3; System.out.println(!done);

    • false
    • true
    • !true
    • 7
  13. What is printed? int score = 85; boolean passed = score >= 70; System.out.println(passed);

    • true
    • false
    • 85
    • 70
  14. Which operator tests whether two int values are equal?

    • ==
    • =
    • equals
    • !=
  15. Which expression is true exactly when n is odd?

    • n % 2 != 0
    • n % 2 == 0
    • n / 2 != 0
    • n % 1 == 1
  16. Why is n % 2 == 1 not a reliable test for odd numbers in Java?

    • Negative odd numbers give -1
    • The % operator only works with double values in Java
    • Odd numbers always produce a remainder of 2
    • The expression causes a compile error
  17. What is printed? System.out.println(-3 % 2 == 1);

    • false
    • true
    • -1
    • 1
  18. The expression x = 5 is a Boolean expression that tests whether x equals 5.

    True or false

  19. A relational operator such as < produces a boolean value.

    True or false

  20. int x = 9; What is the value of x % 3 == 0?

  21. Which operator means "not equal to" in Java?

  22. What is printed? int x = 7; if (x > 5) { System.out.print("A"); } System.out.print("B");

    • AB
    • A
    • B
    • BA
  23. What is printed? int x = 3; if (x > 5) { System.out.print("A"); } System.out.print("B");

    • B
    • AB
    • A
    • Nothing
  24. What is printed? int x = 3; if (x > 5) System.out.print("A"); System.out.print("B");

    • B
    • AB
    • A
    • Nothing
  25. What is printed? int x = 3; if (x > 5); { System.out.print("A"); }

    • A
    • Nothing
    • A compile error
    • AA
  26. What is printed? int x = 10; if (x % 2 == 0) { System.out.print("even"); } else { System.out.print("odd"); }

    • even
    • odd
    • evenodd
    • Nothing
  27. What is printed? int n = 0; if (n > 0) { n = n + 10; } else { n = n - 10; } System.out.println(n);

    • -10
    • 10
    • 0
    • -20
  28. What is printed? int a = 4; int b = 9; int max = a; if (b > max) { max = b; } System.out.println(max);

    • 9
    • 4
    • 13
    • 5
  29. What is printed? int x = 5; if (x > 2) { x = x * 2; } if (x > 8) { x = x + 1; } System.out.println(x);

    • 11
    • 10
    • 6
    • 5
  30. How many branches of an if-else statement run?

    • Exactly one
    • Both, one after the other
    • Zero or one, depending on the condition
    • It depends on the number of braces
  31. Indenting a line under an if statement makes it part of the if, even without braces.

    True or false

  32. An if statement without an else may run no code at all.

    True or false

  33. int t = 95; if (t > 90) { t = t - 5; } What is t afterward?

  34. What is printed? int score = 85; String g; if (score >= 90) { g = "A"; } else if (score >= 80) { g = "B"; } else if (score >= 70) { g = "C"; } else { g = "F"; } System.out.println(g);

    • B
    • A
    • C
    • BC
  35. What is printed? int score = 70; String g; if (score >= 90) { g = "A"; } else if (score >= 80) { g = "B"; } else if (score >= 70) { g = "C"; } else { g = "F"; } System.out.println(g);

    • C
    • F
    • B
    • CF
  36. What is printed? int score = 95; String g = ""; if (score >= 70) { g = "C"; } else if (score >= 80) { g = "B"; } else if (score >= 90) { g = "A"; } System.out.println(g);

    • C
    • A
    • B
    • ABC
  37. What is printed? int score = 95; String g = ""; if (score >= 90) { g = "A"; } if (score >= 80) { g = "B"; } if (score >= 70) { g = "C"; } System.out.println(g);

    • C
    • A
    • ABC
    • B
  38. What is printed? int x = 5; int y = 12; if (x > 3) { if (y > 15) { System.out.print("P"); } else { System.out.print("Q"); } } else { System.out.print("R"); }

    • Q
    • P
    • R
    • QR
  39. What is printed? int x = 1; int y = 20; if (x > 3) { if (y > 15) { System.out.print("P"); } } else { System.out.print("R"); }

    • R
    • P
    • Nothing
    • PR
  40. What is printed? int temp = 50; if (temp > 80) { System.out.print("hot"); } else if (temp > 60) { System.out.print("warm"); } else { System.out.print("cool"); }

    • cool
    • warm
    • hot
    • warmcool
  41. In an if / else if / else chain, which branch runs?

    • The first one whose condition is true, or else the final else
    • Every branch whose condition is true, in order from top to bottom
    • The last one whose condition is true
    • Only the else branch
  42. A program checks score >= 70 before score >= 90 in an else-if chain. What problem results?

    • A score of 95 is treated as the 70+ case instead of the 90+ case
    • The program fails to compile
    • Scores below 70 throw an exception
    • Nothing; the order of else-if conditions never matters
  43. In an else-if chain, more than one branch can run.

    True or false

  44. int n = 0; if (n > 0) {s = "pos";} else if (n < 0) {s = "neg";} else {s = "zero";} What is s? (no quotes)

  45. What is printed? int x = 7; System.out.println(x > 5 && x < 10);

    • true
    • false
    • 7
    • 5
  46. What is printed? int x = 12; System.out.println(x > 5 && x < 10);

    • false
    • true
    • 12
    • 1
  47. What is printed? int x = 12; System.out.println(x < 5 || x > 10);

    • true
    • false
    • 12
    • 10
  48. What is printed? System.out.println(true || false && false);

    • true
    • false
    • true && false
    • A compile error
  49. What is printed? System.out.println(!(5 > 3) || 2 > 1);

    • true
    • false
    • !true
    • 2
  50. What is printed? int x = 0; if (x != 0 && 10 / x > 1) { System.out.print("yes"); } else { System.out.print("no"); }

    • no
    • yes
    • An ArithmeticException is thrown
    • Nothing

Showing 50 of 171. Open the quiz to study them all.