AP Computer Science Principles Session1 MCQ
AP Computer Science Principles Session1 MCQ
A. Change Line 3 to b ← 10
B. Change Line 3 to a ← b+10
C. Change Line 3 to b ← 20
D. Change Line 3 to a ← b+20
A. y+3
B. 3y
C. y3
D. 3y
3. Programs I and II below are each intended to calculate the sum of the integers from 1 to n. Assume
that n is a positive integer (e.g., 1, 2, 3, …).
4. The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is
initially in the bottom-left square of the grid and facing toward the top of the grid.
Code for the procedure Mystery is shown below. Assume that the parameter p has been assigned a
positive integer value (e.g., 1, 2, 3, …).
B.
C.
D.
Which of the following initial values for x, y, and z can be used to show that the code segment does not
work as intended?
A. x = 1, y = 2, z = 3
B. x = 1, y = 3, z = 2
C. x = 2, y = 3, z = 1
D. x = 3, y = 2, z = 1
A. The procedure returns true when the initial value of number is 2, and it otherwise returns false.
B. The procedure returns true when the initial value of number is greater than 2, and it otherwise
returns false.
C. The procedure returns true when the initial value of number is even, and it otherwise returns false.
D. The procedure returns true when the initial value of number is odd, and it otherwise returns false.
7. An algorithm will be used to identify the maximum value in a list of one or more integers. Consider the
two versions of the algorithm below.
Algorithm I : Set the value of a variable max to − 1. Iterate through the list of integer values. If a data
value is greater than the value of the variable max, set max to the data value.
Algorithm II : Set the value of a variable max to the first data value. Iterate through the remaining values
in the list of integers. If a data value is greater than the value of the variable max, set max to the data value.
Which of the following statements best describes the behavior of the two algorithms?
8. A programmer is deciding between using a linear or binary search to find a target value in a sorted list.
Which of the following is true?
A. In all cases, a binary search of a sorted list requires fewer comparisons than a linear search.
B. Generally, the advantage of using a binary search over a linear search increases as the size of the
list increases.
C. A linear search will generally run faster than a binary search because a linear search requires fewer
lines of code to implement.
D. Using a linear search is preferable to using a binary search if there is a chance that the target may
not be found in the list.