AP Computer Science A • Unit 2.1

Algorithms with Selection and Repetition

Detailed answer explanations for the Unit 2.1 multiple-choice practice questions.

Q1B
Q2C
Q3C
Q4D
Q5B
Q6B
Q7C
Q8A
Q9B
Q10A
Q11C
Q12A
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following pseudocode. What does total represent after the algorithm finishes?

total = 0
FOR each package in shipment
    IF package weight is greater than 10 THEN
        total = total + package weight
    END IF
END FOR
Athe number of packages in the shipment
Bthe sum of the weights of packages heavier than 10
Cthe weight of the heaviest package
Dthe sum of the weights of all packages
Correct answer: B. The condition inside the loop admits only packages heavier than 10, so total accumulates just those weights.
Explanation

A loop may run once for each item in a group, and a selection inside the loop may decide what to do with that item. Here the selection decides whether a package weight joins the running sum, so lighter packages leave total unchanged.

  • A is not correct: the algorithm adds package weights rather than counting packages, so total never records how many packages the shipment holds.
  • C is not correct: the running sum keeps every qualifying weight, so several heavy packages can contribute together; nothing in the algorithm compares one weight against another.
  • D is not correct: packages whose weight is not greater than 10 fail the condition, and a branch whose condition is not satisfied does not run.
Q2

Consider the following pseudocode. How many times does the loop body execute?

count = 0
WHILE count is less than 3
    count = count + 1
END WHILE
A0
B2
C3
D4
Correct answer: C. count begins at 0 and rises by one each repetition, so the body runs while count is 0, 1, and 2.
Explanation

Repetition repeats a block while a condition stays true. A loop should eventually stop, and this usually happens because something in the loop changes the condition. Here each repetition raises count, and the loop ends once count reaches 3.

  • A is not correct: the condition is tested before any repetition, and 0 is already less than 3, so the body runs at least once.
  • B is not correct: stopping after two repetitions would leave count at 2, which is still less than 3, so the condition allows another repetition.
  • D is not correct: once count reaches 3 the condition is no longer satisfied, so the body cannot run a fourth time.
Q3

Consider the following pseudocode. What is the value of count after the algorithm finishes?

count = 0
value = 1

WHILE value is at most 4
    IF value is greater than 2 THEN
        count = count + 1
    END IF
    value = value + 1
END WHILE
A0
B1
C2
D4
Correct answer: C. value takes 1 through 4, and the inner condition holds only for 3 and 4, so count ends at 2.
Explanation

This algorithm combines repetition and selection. The loop controls how many times the block runs, while the inner condition decides whether the counter changes on that repetition. Tracking both value and count in a small table keeps the two roles separate.

  • A is not correct: the loop body does run, and the inner condition is satisfied on the later repetitions, so the counter does not stay at 0.
  • B is not correct: two separate repetitions satisfy the inner condition, not one, because both 3 and 4 are greater than 2.
  • D is not correct: 4 counts the repetitions of the loop, not the repetitions in which the inner condition was satisfied.
Q4

Consider the following pseudocode. What is the value of score after the algorithm finishes?

score = 0
value = 1

WHILE value is at most 3
    IF value is less than 3 THEN
        score = score + 2
    ELSE
        score = score + 5
    END IF
    value = value + 1
END WHILE
A6
B7
C8
D9
Correct answer: D. value is 1, 2, then 3, so the algorithm adds 2 twice and 5 once.
Explanation

The algorithm never runs both branches. On each repetition the condition selects one path, so the amount added depends on the current value. The loop runs three times, and the final repetition is the one that takes the ELSE path.

  • A is not correct: adding 2 on all three repetitions ignores the ELSE branch, which runs when value is no longer less than 3.
  • B is not correct: no combination of the two amounts produces 7, because the three repetitions add 2, 2, and 5.
  • C is not correct: this falls one short, because the last repetition takes the ELSE branch and adds 5 rather than a smaller amount.
Q5

An algorithm checks whether a badge is active and then chooses either the entry path or the denial path. Which structure performs this choice?

Asequencing
Bselection
Cdeclaration
Dcompilation
Correct answer: B. Selection is how an algorithm chooses between paths after checking a Boolean condition, which is exactly what the badge check does.
Explanation

Selection checks a Boolean condition, one that is either true or false, and then follows the path that matches the answer. The algorithm never runs both branches, so the badge leads either to entry or to denial.

  • A is not correct: sequencing puts steps in order; it carries out every step in turn rather than choosing between an entry path and a denial path.
  • C is not correct: declaration is not one of the three control-flow ideas in this topic, which are sequencing, selection, and repetition.
  • D is not correct: compilation is not one of the control-flow ideas named here, and it does not choose which branch of an algorithm runs.
Q6

A registrar processes every student record in a list, one record at a time. Which structure is needed to perform the same steps for all records?

Aselection
Brepetition
Cassignment
Dcasting
Correct answer: B. Repetition repeats a block of steps, so identical work on every student record is handled by a loop.
Explanation

Repetition, also called iteration, repeats a block of steps while a condition stays true or until a goal is reached. A loop may run once for each item in a group, which is what processing one record at a time requires.

  • A is not correct: selection chooses a path based on a Boolean condition; it decides what happens once, not how often a block of steps runs.
  • C is not correct: assignment is not one of the control-flow ideas described in this topic, and by itself it does not repeat a block of steps.
  • D is not correct: casting is not one of the control-flow ideas described here, so it cannot make the same steps run for every record.
Q7

An algorithm must open a file, read its value, and then close the file. Which control-flow idea requires these steps to occur in that specified order?

AChoosing one path based on a condition
BRepeating a block while a condition remains true
CExecuting steps in a specified order
DChanging Java source code into bytecode
Correct answer: C. Sequencing means that steps are performed in order, which is what forces the file to be opened, read, and closed in turn.
Explanation

Sequencing is the first control-flow idea, met in Unit 1 as the normal top-to-bottom flow of execution. Selection and repetition are added in Unit 2, but neither is needed when every step happens once in a fixed order.

  • A is not correct: choosing one path based on a condition describes selection, and none of these steps is skipped depending on a Boolean condition.
  • B is not correct: repeating a block while a condition remains true describes repetition, but each of these steps happens once rather than again and again.
  • D is not correct: changing Java source code into bytecode is not a control-flow idea, and it does not decide the order of the steps.
Q8

A programmer wants to plan two possible paths before deciding which Java statements to use. Why is pseudocode useful at this stage?

AIt lets the programmer describe the algorithm without exact Java syntax.
BIt is executed directly by the Java compiler.
CIt prevents all logic errors automatically.
DIt replaces the need for testing.
Correct answer: A. Pseudocode helps describe an algorithm before writing exact Java syntax, which suits planning two paths that are not yet written.
Explanation

An algorithm is the plan, not the code. Java is one way to write that plan down, and pseudocode is another. The daily admissions algorithm is written in pseudocode so that its selection and repetition can be read without exact Java syntax.

  • B is not correct: pseudocode describes an algorithm without exact Java syntax, so it is not the Java source code that a compiler processes.
  • C is not correct: planning in pseudocode helps a programmer see the logic, but nothing in this topic claims that it removes logic errors on its own.
  • D is not correct: pseudocode is a way of describing an algorithm before writing Java, and this topic does not present it as a substitute for testing.
Q9

An algorithm processes one item per repetition. A trace-table row shows itemsLeft changing from 2 before a repetition to 1 afterward. Which conclusion is best supported?

AThe loop has finished processing every item.
BOne item was processed during that repetition.
CThe selection condition was necessarily false.
DThe algorithm added two new items.
Correct answer: B. The loop processes one item per repetition, so a drop from 2 to 1 records one item handled.
Explanation

A trace records how an algorithm's state changes, and each repetition is given its own row. Reading a single row shows what changed during that one repetition, so a decrease of one matches processing one item.

  • A is not correct: one item still remains after that repetition, so the row does not show a loop that has reached its exit condition.
  • C is not correct: the row shows only how many items remain; it does not report the result of any condition tested inside the loop.
  • D is not correct: the count of remaining items fell rather than rose, so nothing was added to the group during that repetition.
Q10

A scanner should continue only while documents remain and storage space is available. Which condition best matches this rule?

Adocuments remain AND storage is available
Bdocuments remain OR storage is available
Cno documents remain AND storage is available
Dstorage is full
Correct answer: A. AND requires both conditions to be true, matching a rule that needs documents remaining and storage available together.
Explanation

AND requires both conditions to be true, OR requires at least one, and NOT reverses a condition. A loop continues while its condition stays true, so the operator decides which combination keeps the scanner running.

  • B is not correct: OR requires only one condition to be true, so the scanner would keep running with no documents left or with no storage.
  • C is not correct: this condition reverses the document test, so it would allow scanning only when no documents remain.
  • D is not correct: a full-storage check names the wrong state and leaves out documents entirely, so it cannot express a rule with two parts.
Q11

A loop begins with itemsLeft = 4 and repeats while itemsLeft is greater than 0. Which statement inside the loop most directly ensures that it eventually stops?

AitemsLeft = 4
BitemsLeft = itemsLeft + 1
CitemsLeft = itemsLeft - 1
DitemsLeft = itemsLeft
Correct answer: C. Subtracting 1 each repetition moves itemsLeft toward 0, which is what ends the loop.
Explanation

A loop should eventually stop, and this usually happens because something in the loop changes the condition. Here the condition tests itemsLeft, so the body must move that value toward the point where the test fails.

  • A is not correct: resetting the value to 4 restores the starting state each repetition, so the condition stays satisfied and the loop never ends.
  • B is not correct: raising itemsLeft moves the value away from 0, so the condition keeps allowing another repetition.
  • D is not correct: leaving the value as it is changes nothing in the condition, so the test gives the same answer on every repetition.
Q12

A flowchart currently follows a decision once and then ends. Which change would turn part of that flow into repetition?

AAdd an arrow from a later action back to the decision.
BEnd both paths immediately after the decision.
CPlace all actions in one straight path.
DReplace the start symbol with a process rectangle.
Correct answer: A. In a flowchart, an arrow from a later step back to an earlier decision represents repetition.
Explanation

A flowchart uses standard shapes, and arrows show the direction of control flow. To represent repetition, an arrow from a later step returns to an earlier decision. The decision is tested again, and the cycle continues until the exit condition is reached.

  • B is not correct: ending both paths right after the decision leaves a flow that runs once, so no step is ever reached a second time.
  • C is not correct: one straight path is sequencing, and it removes the decision instead of sending the flow back to it.
  • D is not correct: a rounded terminator marks the start or end and a rectangle represents a process, so swapping them does not send the flow backward.