AP Computer Science Unit 2 Review (Edhesive) Exam Questions and Answers, Exams of Computer Science

AP Computer Science Unit 2 Review (Edhesive) Exam Questions and Answers, Exams of Computer Science

AP Computer Science Unit 2 Review (Edhesive) Exam Questions and Answers, Exams of Computer Science

Category:

/ 7
AP Computer Science Principles
Unit 2 Review — Edhesive | Exam Questions & Answers (Graded A+)
Complete exam-style Q&A for AP CSP Unit 2 (Edhesive). Covers all major topics: algorithms, conditionals,
iteration, REPEAT UNTIL loops, random numbers, heuristics, algorithm efficiency, Big-O, searching, abstraction,
and undecidable problems. Verified answers — graded A+.
SectionTopic SectionTopic
1 Key Vocabulary & Definitions 2 Conditionals & IF/ELSE
3 Iteration & Loops (REPEAT UNTIL) 4 Random Numbers & Simulation
5 Algorithm Design & Tracing 6 Searching Algorithms
7 Algorithm Efficiency & Big-O 8 Heuristics & Problem Types
9 Abstraction & Procedures 10 Test Cases & Debugging
Section 1: Key Vocabulary & Definitions
Algorithm
A process or set of rules to be followed in calculations or other problem-solving operations. Algorithms are
built from three building blocks: sequencing, selection, and iteration.
Sequencing
Logic structure where instructions are executed in order, one after another — linear execution.
Selection (Conditional)
Algorithmic structure that uses IF…THEN to tell the computer which step or sequence to execute based on a
Boolean condition.
Iteration (Loop)
Repetition of instructions. Types: FOR loops (fixed count), WHILE loops (condition checked before each run),
REPEAT UNTIL loops (stops when condition becomes TRUE).
Pseudocode
An informal method of writing algorithmic instructions that does not follow the grammar/syntax of any specific
programming language. Also called 'false code.'
Flowchart
A diagram with symbols showing the 'flow' of a process or program.
Control Flow
The direction a program moves from instruction to instruction over time. Can be controlled by IF statements
and Boolean conditions.
Boolean
Binary values — TRUE or FALSE — representing the truth values of logic and Boolean algebra.
Nesting
Where different logic structures (sequence, selection, loops) are combined or placed inside one another.
Heuristic
A method for deriving an approximate solution — 'rules of thumb' — not guaranteed to be perfectly correct,
but produces a reasonable answer quickly.
Undecidable Problem
A problem where no algorithm can always lead to a correct yes-or-no answer. Example: The Halting Problem
— you cannot always determine if a program will halt or run forever.
Halting Problem
There cannot be a program that will always determine which programs will halt (exit) and which will run forever
(infinite loop). This is a classic undecidable problem.
Scalability
How well an algorithm performs at increasingly larger input sizes.
Big-O Notation
A mathematical concept used to determine how well algorithms scale. Classifies performance: O(1) constant,
O(log n) logarithmic, O(n) linear, O(n²) quadratic, O(2) exponential.
Sequential Search (Linear Search)
Looking through a list one item at a time until a match is found. O(n) — less efficient.
Binary Search
Divides the search interval in half each time (requires sorted data). Logarithmic behavior — doubling the input
only requires one extra step. O(log n).
Abstraction
Hiding complex details and providing a simpler interface. In programming: procedures/functions that can be
reused without restating detailed steps each time.
Procedure (Function)
A named collection of steps in an algorithm that can be reused anytime without restating the detailed
procedures. A key form of abstraction.
Brute Force
Trial-and-error method used to decode data (e.g., passwords). O(2) — exponential — not practical for large
inputs.
Section 2: Conditionals & IF/ELSE — Exam Questions
Q: What will be the output of the following program if the user input number is -5? number INPUT()
DISPLAY(number * 3)
–15 (The program multiplies the input –5 by 3.)
Q: Consider the following code. What will be output if n = 0? IF (n > 0) { DISPLAY('The number is
positive.') } ELSE { DISPLAY('The number is not positive.') }
'The number is not positive.' — 0 is not greater than 0, so the ELSE branch executes.
Q: A travel organisation's program displays the price of a trip to London based on applicants. IF
(Applicants < 50) { DISPLAY('The price is $300!') } ELSE IF (Applicants < 100) { DISPLAY('The price is
$250!') } ELSE { DISPLAY('The price is $200!') } If the number of applicants is 60, what is displayed?
'The price of the trip is $250!' — 60 50 and 60 < 100, so the second branch runs.
Q: An IF/ELSE IF/ELSE chain vs. nested IF statements — what is the key difference?
IF/ELSE IF/ELSE checks conditions in order and executes only the FIRST matching branch. Nested IFs
check every condition independently and may execute multiple branches.
Q: What is a one-way selection vs. a two-way selection?
One-way selection: IF (condition) { … } — only executes if TRUE, does nothing otherwise. Two-way
selection: IF/ELSE — executes one block if TRUE, another block if FALSE.
Section 3: Iteration & Loops (REPEAT UNTIL)
Q: What are the three types of loops in AP CSP pseudocode?
1) REPEAT n TIMES { … } — runs exactly n times.
2) REPEAT UNTIL (condition) { … } — repeats UNTIL condition is TRUE (stops when TRUE).
3) FOR EACH item IN list { … } — iterates over each element of a list.
Q: Which of the following best describes the result of running this program? number RANDOM(1, 6)
REPEAT UNTIL (number > 100) { number number * 2 } IF (number = 128) { DISPLAY(true) } ELSE {
DISPLAY(false) }
If number starts as 1, 2, or 4, then the program will display TRUE.
• Starting at 1: 1248163264128
• Starting at 2: 248163264128
• Starting at 4: 48163264128
• Starting at 3: 3612244896192 (192 128)
• Starting at 5 or 6: also skip 128.
Q: A program has been developed to compute the sum of all elements with a value > 10 in a list. Which
statement best describes how iteration is used?
The program will iterate (loop) through each element of the list, check if the element is greater than 10, and
add it to a running total if so. Iteration allows this check to be performed on every element without writing
separate code for each one.
Q: Consider two equivalent code segments that display the first 12 positive even integers. Segment A
uses 12 separate DISPLAY statements. Segment B uses a loop. Which of the following is NOT an
advantage of Segment B over Segment A?
Segment B will execute in less time than Segment A.
(Both segments produce the same output in roughly the same execution time — the advantage of Segment B
is readability and maintainability, not speed.)
Q: When is a REPEAT UNTIL loop preferred over a REPEAT n TIMES loop?
When it is unknown how many times the loop will need to iterate — usually when we are waiting for an
event to occur (e.g., 'repeat until the user enters a valid password').
Q: A study of beech trees shows 40% may be susceptible to a disease. A program counts susceptible
trees in a forest of k trees. Which condition replaces <MISSING CONDITION>? total 1 ; REPEAT k
TIMES { IF () { total total + 1 } } DISPLAY(total) — Select TWO answers.
RANDOM(1, 10) 4 (40% probability — 4 out of 10)
RANDOM(1, 100) 40 (40% probability — 40 out of 100)
Both correctly simulate a 40% chance for each tree.
Section 4: Random Numbers & Simulation
Q: What does RANDOM(a, b) return in AP CSP pseudocode?
A random integer from a to b, inclusive. Each integer in the range has an equal probability of being selected.
Q: How do you simulate rolling a 6-sided die in pseudocode?
result RANDOM(1, 6) — returns 1, 2, 3, 4, 5, or 6 with equal probability.
Q: A string 'Value' has 5 characters (indices 1–5). The code below generates a random number: String
word = new String('Value') int random = (int)(Math.random() * word.length() + 1) What is the range of
numbers that could be printed?
1 to 5. (Math.random() * 5 gives [0.0, 5.0), casting to int gives 0–4, +1 gives 1–5.)
Q: Why are random number generators used in simulations?
To model real-world uncertainty and probability. Random values allow programs to simulate unpredictable
events (weather, disease spread, dice rolls) without requiring actual randomness.
Section 5: Algorithm Design & Tracing
/ 7
Related