Unit 2: Selection and Iteration
CS A · Unit 2 · Paper 1

Selection and Iteration unit test

A test on this unit alone, marked as a percentage and a letter grade — for the test your class is actually sitting, rather than for May. Answer everything, then submit once: seeing the answer to question 3 before attempting question 4 makes the final percentage meaningless.

Each paper is built from this unit’s 53 terms and is the same for everyone, so a teacher can assign “Unit 2, Paper 1” and every student sits the identical test. Multiple choice is marked objectively; the written sections you mark yourself against the model answer and rubric.
Suggested time 36 min 33 points0/17 attempted
1

if statement

2

Relational operators

3

Recursive method with an accumulating return

4

De Morgan's laws

5

Loop invariant thinking

6

else if chain

7

Accumulator pattern

8

Standard algorithm: sum and average

9

StackOverflowError

10

Conditional (ternary) operator

11

Standard algorithm: determine if all elements meet a condition

12

Compound boolean in a loop condition

Short answer 1. Define or explain: Empty loop body danger

3 pts

Short answer 2. Define or explain: for loop

3 pts

Short answer 3. Define or explain: Tracing a loop

3 pts

Short answer 4. Define or explain: Binary search

3 pts

Free response

9 pts

METHODS AND CONTROL STRUCTURES (Question 1, 9 points). The AccountTools class contains static methods for working with user account information. You will write two of them. public static String initials(String name) — returns a string of the uppercase first letter of each word in name, where words are separated by single spaces and name contains at least one word with no leading or trailing spaces. (For example, initials("ada byron lovelace") returns "ABL"; initials("Grace Hopper") returns "GH"; initials("plato") returns "P".) public static boolean isStrong(String password) — returns true if password satisfies all three of the following, and false otherwise: it is at least 8 characters long; it contains at least one digit ('0' through '9'); and it contains at least one uppercase letter ('A' through 'Z'). (For example, isStrong("Passw0rdX") returns true; isStrong("Password") returns false because it has no digit; isStrong("pass123") returns false because it is too short and has no uppercase letter.) You may use the String methods length(), substring(int), substring(int, int), indexOf(String), equals(String), and toUpperCase().

A. Write method initials. Assume name contains at least one word and that words are separated by single spaces.

B. Write method isStrong.