AP Computer Science Principles · Unit practice
AP Computer Science Principles Big Idea 3: practice questions
5 exam-style questions with answers and explanations. Answer them on FiveStudy to keep a streak and see your AP® score estimate.
Question 1
score ← 4 score ← 9 DISPLAY (score)
What is displayed as a result of executing the code segment shown?
- A. 4
- B. 5
- C. 45
- D. 9
Answer: 9
score holds 4, then is reassigned to 9. The DISPLAY shows the current value, 9.
Question 2
A program must store the value 12 in the variable tickets and then display twice that value. Which of the following code segments displays 24?
- A. ``` tickets ← 12 DISPLAY (tickets * 2) ```
- B. ``` tickets ← 12 DISPLAY (tickets) ```
- C. ``` tickets ← 24 DISPLAY (tickets * 2) ```
- D. ``` DISPLAY (tickets * 2) tickets ← 12 ```
Answer: ``` tickets ← 12 DISPLAY (tickets * 2) ```
The correct segment stores 12 first and then displays tickets * 2, which is 24. Displaying tickets alone shows 12, storing 24 makes the display 48, and displaying before the assignment reads tickets before it has a value.
Question 3
count ← 0 count ← 5 DISPLAY (count)
Which of the following best describes the result of running the code segment shown?
- A. It displays 0, because the first value assigned to a variable is kept
- B. It displays 5, because the second assignment replaces the earlier value
- C. It displays 0 5, because DISPLAY shows each value the variable has held
- D. It displays nothing, because count was assigned more than one value
Answer: It displays 5, because the second assignment replaces the earlier value
A variable holds one value at a time, and a new assignment replaces the old value. Only the current value, 5, is displayed.
Question 4
A weather-station program stores whether the current wind speed is above a safety limit. windSpeed ← 34 limit ← 30 isWindy ← windSpeed > limit DISPLAY (isWindy)
What is displayed as a result of executing the code segment shown?
- A. 34
- B. true false
- C. true
- D. false
Answer: true
The comparison windSpeed > limit is evaluated once: 34 > 30 is true, so isWindy is assigned the Boolean true. DISPLAY then shows that stored value, true. Showing 34 would mean DISPLAY printed windSpeed instead of the Boolean isWindy that the code stores. Showing true false would mean the segment produced two values, but there is only one DISPLAY and isWindy holds a single Boolean. Showing false would come from reading the comparison as 34 > 30 being untrue, but 34 is larger than 30, so the stored result is true.
Question 5
Which of the following variable names best communicates its purpose in a program that tracks a library's books?
- A. b
- B. variable2
- C. number
- D. booksCheckedOut
Answer: booksCheckedOut
A descriptive name tells readers what the value means. Single letters and generic names force readers to hunt through the code to learn what is stored.
More AP Computer Science Principles practice