Unit 2 Quiz: Selection and Iteration

  • 16 multiple choice questions in the style of the AP exam. Each one gives instant feedback.
  • Use the tabs to move between questions, and check an answer as many times as you need. Your last saved answers are sent when you click Submit Code at the bottom of this page.
  • Two short coding challenges follow the quiz. Both have tests built in.
  • No notes needed. If you miss a question, its feedback names the lesson 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

16 questions. Use the tabs to move between them, and check an answer as many times as you need. Each check saves that answer, and your last saved answers are sent when you click Submit Code at the bottom of this page.

Unit 2 Quiz

Check each answer as many times as you need. Your last saved answers are the ones submitted.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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.

Submit Assignment

Choose what to hand in: click Add to submission on each runner you want graded (hacks, popcorn hacks), and save it. Leaving out practice runners does not lower your grade, but a required hack you leave out is graded as missing. Your selection is sent as a Gist for automatic review.

    Need to update a submission later? Open the submissions dashboard.