APCSP Algorithms and Programming 3 ā Questions and Answers
Question 1: A function receives a list of numbers and returns their average. If the list is empty, the function crashes. What is this an example of?
- An infinite loop
- A logic error unrelated to input
- Failing to handle an edge case (Correct answer)
- A syntax error
Correct answer: Failing to handle an edge case
An empty list is an edge case ā a boundary condition the programmer failed to anticipate and handle gracefully.
Question 2: Which of the following correctly describes the difference between a parameter and an argument?
- A parameter is the value passed to a procedure; an argument is the variable that receives it
- A parameter is a variable in the procedure definition; an argument is the value passed when calling it (Correct answer)
- Parameters and arguments are identical terms
- Arguments are only used in recursive procedures
Correct answer: A parameter is a variable in the procedure definition; an argument is the value passed when calling it
Parameters are placeholder variables defined in the procedure header; arguments are the actual values supplied when the procedure is called.
Question 3: What does it mean for an algorithm to run in polynomial time?
- The algorithm uses polynomials in its calculations
- The number of steps grows as a polynomial function of the input size (Correct answer)
- The algorithm can only solve polynomial equations
- The algorithm runs in exactly n^2 steps for input size n
Correct answer: The number of steps grows as a polynomial function of the input size
Polynomial time means the number of operations is bounded by a polynomial expression (like n, n², n³) in terms of the input size n.
Question 4: A robot moves forward, turns right, and repeats this sequence 4 times. What shape does its path form?
- Triangle
- Circle
- Square (Correct answer)
- Hexagon
Correct answer: Square
Repeating 'move forward, turn right 90°' four times traces a square path.
Question 5: Which of the following is the best reason to use a library or API in a program?
- To make the program use more memory
- To reuse existing, tested code and avoid reimplementing common functionality (Correct answer)
- To increase the number of lines of code
- To prevent other programmers from reading your code
Correct answer: To reuse existing, tested code and avoid reimplementing common functionality
Libraries and APIs provide reusable, pre-tested functionality so programmers don't need to rewrite common operations from scratch.
Question 6: Consider this pseudocode: x ā 10 IF x > 5 THEN x ā x + 1 ELSE x ā x - 1 What is the final value of x?
- 9
- 10
- 11 (Correct answer)
- 5
Correct answer: 11
Since 10 > 5 is TRUE, the THEN branch executes: x becomes 10 + 1 = 11.
Question 7: In AP CSP pseudocode, what is the purpose of the APPEND procedure when working with lists?
- It removes the last element from a list
- It adds a new element to the end of a list (Correct answer)
- It inserts an element at the beginning of a list
- It sorts the list in ascending order
Correct answer: It adds a new element to the end of a list
APPEND(list, value) adds the specified value to the end of the list, increasing its length by one.
A function receives a list of numbers and returns their average.
If the list is empty, the function crashes.
What is this an example of?