Unit 2 Quiz
Sixteen multiple choice questions covering Boolean expressions, selection, iteration, String algorithms, nested loops, and run-time analysis, plus two coding challenges.
- Unit 2 Quiz: Selection and Iteration
- Multiple Choice
- Coding Challenge 1: Collatz Steps
- Code Runner Challenge
- Coding Challenge 2: Count Words That Start With a Letter
- Code Runner Challenge
- After the Quiz
Unit 2 Quiz: Selection and Iteration
- 16 multiple choice questions in the style of the AP exam. Each one gives instant feedback.
- Your score updates at the bottom of the quiz. Aim for a first try score of 13 or better.
- Two short coding challenges follow the quiz. Both have tests built in.
- No notes needed. If a question stumps you, the lesson number next to it tells you where to review.
| Questions | Topic |
|---|---|
| 1 to 2 | 2.2 Boolean Expressions |
| 3 to 4 | 2.3 and 2.4 if Statements and Nested if |
| 5 to 6 | 2.5 Compound Boolean Expressions |
| 7 to 9 | 2.6 Comparing Boolean Expressions |
| 10 to 12 | 2.7 and 2.8 while and for Loops |
| 13 to 14 | 2.9 and 2.10 Selection, Iteration, and String Algorithms |
| 15 to 16 | 2.11 and 2.12 Nested Iteration and Run-Time Analysis |
Multiple Choice
Which of the following is a Boolean expression that is true when x is even?
What is the value of 10 / 4 == 2.5?
What does this code print?
What does this code print when score is 92?
What is the value of !true || false && true?
count is 0. What happens?
Which expression is equivalent to !(a && b)?
Which expression is equivalent to !(x >= 10)?
What does this print?
How many times does this loop print?
What is wrong with this loop?
How many times does the body execute?
What does this print?
word is "banana". What does this print?
How many times does System.out.print("*") execute?
Which loop does the least work while printing the same numbers as the others (the multiples of 4 from 0 to 96)?
Coding Challenge 1: Collatz Steps
Starting from n, repeat: if n is even divide it by 2, otherwise multiply by 3 and add 1. Stop when n reaches 1. Return how many steps it took. Use a while loop and an if-else.
Code Runner Challenge
Challenge 1: Collatz steps with a while loop and an if-else.
View IPYNB Source
// CODE_RUNNER: Challenge 1: Collatz steps with a while loop and an if-else.
public class Collatz {
public static int steps(int n) {
int count = 0;
// TODO: while n is not 1:
// if n is even, n = n / 2, otherwise n = 3 * n + 1
// add 1 to count
return count;
}
public static void main(String[] args) {
System.out.println("steps(1) = " + steps(1) + " expected 0");
System.out.println("steps(6) = " + steps(6) + " expected 8");
System.out.println("steps(27) = " + steps(27) + " expected 111");
}
}
Collatz.main(null);
Coding Challenge 2: Count Words That Start With a Letter
Count how many times letter appears as the first character of a word in sentence. A word starts at index 0 or right after a space. Use a for loop, substring, and equals.
Code Runner Challenge
Challenge 2: count words that start with a letter using substring and equals.
View IPYNB Source
// CODE_RUNNER: Challenge 2: count words that start with a letter using substring and equals.
public class FirstLetters {
public static int countStarts(String sentence, String letter) {
int count = 0;
// TODO: for each index i:
// if i == 0 or the character before i is a space,
// and the character at i equals letter, count it
return count;
}
public static void main(String[] args) {
System.out.println(countStarts("sally sells sea shells by the shore", "s") + " expected 5");
System.out.println(countStarts("the cat sat on the mat", "t") + " expected 2");
System.out.println(countStarts("java is fun", "z") + " expected 0");
}
}
FirstLetters.main(null);
After the Quiz
- Missed questions 1 to 9? Review the Boolean lessons: 2.2, 2.5, and 2.6.
- Missed questions 10 to 16? Review the loop lessons: 2.7, 2.8, 2.11, and 2.12.
- Both coding challenges are the shape of AP FRQ question 1 (Methods and Control Structures). Practice more in the Unit 2 FRQ page.