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

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 2” 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

break

2

Order of evaluation in a condition

3

Standard algorithm: determine if any element meets a condition

4

Converting for to while

5

Binary search

6

De Morgan's laws

7

Counting nested-loop iterations

8

Nested if

9

Compound boolean in a loop condition

10

Recursive method with an accumulating return

11

Logical OR

12

Infinite loop

Short answer 1. Define or explain: do-while loop

3 pts

Short answer 2. Define or explain: if / else

3 pts

Short answer 3. Define or explain: Binary search step count

3 pts

Short answer 4. Define or explain: Relational operators

3 pts

Free response

9 pts

METHODS AND CONTROL STRUCTURES (Question 1, 9 points). The Account class represents a website user account with a private String username. A provided static helper, boolean isAvailable(String str), returns whether a username is available. You will write: public Account(String requestedName) — if requestedName is available, assign it to username; otherwise try requestedName followed by successive integers starting at 1 ("Luis-Cruz1", "Luis-Cruz2", …) until an available variation is found, and assign the first available one. public String getShortenedName() — returns a shortened copy of username with each hyphen AND the character immediately before it removed (username never starts or ends with a hyphen, has no consecutive hyphens, and has length ≥ 2; username itself is unchanged). Examples: "Amy-Marie-Lin" → "AmMariLin"; "SammyB3" → "SammyB3".

A. Complete the Account constructor. You must use isAvailable appropriately to receive full credit.

B. Complete method getShortenedName.