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.
Questions
Which control structure chooses whether to run code based on a condition?
- Selection
- Sequencing
- Repetition
- Instantiation
Which control structure runs the same code multiple times?
- Repetition (iteration)
- Selection
- Sequencing
- Casting
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
Which are building blocks that can be combined to express any algorithm?
- Sequencing
- Selection
- Repetition
- Compilation
- Commenting
The order in which statements run can change the result of an algorithm.
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
A loop's body might run zero times.
What is printed? int x = 4; System.out.println(x % 2 == 0);
- true
- false
- 0
- 4
What is printed? int a = 5; int b = 7; System.out.println(a != b);
- true
- false
- 2
- -2
What is printed? int a = 5; System.out.println(a >= 5);
- true
- false
- 5
- 1
What is printed? System.out.println(0.1 + 0.2 == 0.3);
- false
- true
- 0.3
- 0.30000000000000004
What is printed? boolean done = 10 > 3; System.out.println(!done);
- false
- true
- !true
- 7
What is printed? int score = 85; boolean passed = score >= 70; System.out.println(passed);
- true
- false
- 85
- 70
Which operator tests whether two int values are equal?
- ==
- =
- equals
- !=
Which expression is true exactly when n is odd?
- n % 2 != 0
- n % 2 == 0
- n / 2 != 0
- n % 1 == 1
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
What is printed? System.out.println(-3 % 2 == 1);
- false
- true
- -1
- 1
The expression x = 5 is a Boolean expression that tests whether x equals 5.
A relational operator such as < produces a boolean value.
int x = 9; What is the value of x % 3 == 0?
Which operator means "not equal to" in Java?
What is printed? int x = 7; if (x > 5) { System.out.print("A"); } System.out.print("B");
- AB
- A
- B
- BA
What is printed? int x = 3; if (x > 5) { System.out.print("A"); } System.out.print("B");
- B
- AB
- A
- Nothing
What is printed? int x = 3; if (x > 5) System.out.print("A"); System.out.print("B");
- B
- AB
- A
- Nothing
What is printed? int x = 3; if (x > 5); { System.out.print("A"); }
- A
- Nothing
- A compile error
- AA
What is printed? int x = 10; if (x % 2 == 0) { System.out.print("even"); } else { System.out.print("odd"); }
- even
- odd
- evenodd
- Nothing
What is printed? int n = 0; if (n > 0) { n = n + 10; } else { n = n - 10; } System.out.println(n);
- -10
- 10
- 0
- -20
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
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
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
Indenting a line under an if statement makes it part of the if, even without braces.
An if statement without an else may run no code at all.
int t = 95; if (t > 90) { t = t - 5; } What is t afterward?
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
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
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
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
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
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
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
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
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
In an else-if chain, more than one branch can run.
int n = 0; if (n > 0) {s = "pos";} else if (n < 0) {s = "neg";} else {s = "zero";} What is s? (no quotes)
What is printed? int x = 7; System.out.println(x > 5 && x < 10);
- true
- false
- 7
- 5
What is printed? int x = 12; System.out.println(x > 5 && x < 10);
- false
- true
- 12
- 1
What is printed? int x = 12; System.out.println(x < 5 || x > 10);
- true
- false
- 12
- 10
What is printed? System.out.println(true || false && false);
- true
- false
- true && false
- A compile error
What is printed? System.out.println(!(5 > 3) || 2 > 1);
- true
- false
- !true
- 2
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