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

Unit 2 Multiple Choice
Choose the best answer. Wrong answers explain the trap; you can try again.
Question 1

Which of the following is a Boolean expression that is true when x is even?

Question 2

What is the value of 10 / 4 == 2.5?

Question 3

What does this code print?

int x = 4;
if (x > 5)
System.out.println("big");
System.out.println("done");
Question 4

What does this code print when score is 92?

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

What is the value of !true || false && true?

Question 6

count is 0. What happens?

if (count > 0 && total / count > 10) {
System.out.println("High");
}
Question 7

Which expression is equivalent to !(a && b)?

Question 8

Which expression is equivalent to !(x >= 10)?

Question 9

What does this print?

String a = new String("cat");
String b = new String("cat");
System.out.println(a == b);
System.out.println(a.equals(b));
Question 10

How many times does this loop print?

int k = 12;
while (k > 0) {
System.out.println(k);
k = k - 4;
}
Question 11

What is wrong with this loop?

int i = 0;
while (i < 5) {
System.out.println(i);
}
Question 12

How many times does the body execute?

for (int i = 2; i <= 20; i += 6) {
System.out.println(i);
}
Question 13

What does this print?

int n = 305;
int sum = 0;
while (n > 0) {
sum += n % 10;
n = n / 10;
}
System.out.println(sum);
Question 14

word is "banana". What does this print?

int count = 0;
for (int i = 0; i <= word.length() - 2; i++) {
if (word.substring(i, i + 2).equals("an")) {
count++;
}
}
System.out.println(count);
Question 15

How many times does System.out.print("*") execute?

for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
Question 16

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);
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.