Practice questions
Part of Computer Science Principles in Python
131 exam-style questions across the course, each with an explanation of the right answer and the tempting wrong ones.
Unit 1: Programs, variables and errors
Program Design and Testing
Two students are pair programming on a quiz app. Which description matches how pair programming works?
A team is designing an app that helps students sign up for after-school tutoring. Which two activities are ways to investigate what the app must do? Select two answers.
Select 2 answers.
A student builds a weather app by first getting the part that downloads today's temperature to work, then adding a five-day forecast, then adding storm alerts. Each part is tested before the next one is added. Which approach does this describe?
Mia's game uses a collision-detection procedure that she copied from an open-source project. What is the best way for her to acknowledge it?
The code segment below is meant to display the sum of the values in scores. What happens when it runs?
scores ← [88, 92, 75]total ← 0i ← 0REPEAT 3 TIMES{total ← total + scores[i]i ← i + 1}DISPLAY(total)
The procedure countAtLeast is supposed to return how many values in numList are greater than or equal to target. Which procedure call would reveal that it contains an error?
PROCEDURE countAtLeast(numList, target){count ← 0FOR EACH num IN numList{IF(num > target){count ← count + 1}}RETURN(count)}
A program should accept a quantity from 1 to 10, inclusive, and reject anything else. The tester has already tried 1, 5 and 10. Which two additional inputs best check the behavior one step beyond the extremes of the input? Select two answers.
Select 2 answers.
A video game stores a player's coins in a value that can hold whole numbers from 0 to 65,535. A player with 65,535 coins picks up one more, and the coin counter shows 0. Which kind of error is this?
Unit 3: Loops and algorithms
Developing Algorithms
What is displayed when this code segment runs?
result ← 4 + 18 MOD 5 * 2DISPLAY(result)
What is displayed when this code segment runs?
a ← 3b ← 8a ← bb ← aDISPLAY(a)DISPLAY(b)
Which of the following statements assign the same value to winner as the code segment, for every possible value of points and lives? Select two answers.
Select 2 answers.
IF(points ≥ 50){IF(lives > 0){winner ← true}ELSE{winner ← false}}ELSE{winner ← false}
The two code segments look alike. For which starting value of n do they display different values?
Segment IIF(n MOD 2 = 0){n ← n / 2}IF(n MOD 2 = 1){n ← 3 * n + 1}DISPLAY(n)Segment IIIF(n MOD 2 = 0){n ← n / 2}ELSE{n ← 3 * n + 1}DISPLAY(n)
A student creates a new algorithm by combining two procedures that the class wrote and tested earlier. Which of the following are benefits of this approach? Select two answers.
Select 2 answers.
What is displayed when this code segment runs?
total ← 0n ← 1REPEAT 6 TIMES{IF(n MOD 3 = 0){total ← total + n}n ← n + 1}DISPLAY(total)
Which of the following is NOT an expression?
Unit 4: Lists, search and the robot
Linear Search and Binary Search
A sorted list contains 200 numbers. What is the maximum number of elements binary search examines to find a value or decide it is not in the list?
A program will use binary search on a list of numbers. Which of the following must be true about the list?
Which of the following statements about searching are true? Select two answers.
Select 2 answers.
What value is returned by the call findFirst([4, 9, 2, 9, 5], 9)?
PROCEDURE findFirst(aList, target){index ← 1FOR EACH item IN aList{IF(item = target){RETURN(index)}index ← index + 1}RETURN(-1)}
Binary search is used to look for 26 in the sorted list [3, 8, 12, 17, 21, 26, 30]. Which elements are examined, in order?
Binary search needs at most 10 checks to search a sorted list of 1,000 names. About how many checks would it need at most for a sorted list of 2,000 names?
A coach keeps a list of player names in the order the players signed up and needs to check, once, whether a given name is on it. Which approach is guaranteed to give the correct answer?
Linear search is used on an unsorted list of 500 numbers to look for a value that is not in the list. How many elements are examined?
Grid Robot Algorithms
The robot is facing toward the bottom of the grid. It then runs ROTATE_LEFT() three times. Which way does it face now?
The robot starts in the bottom-left square, facing right. Squares A to D are open. In which square is the robot after the code segment runs?
```
A . . . B
. # # . .
. . C # .
> . . . D
```
REPEAT 4 TIMES{MOVE_FORWARD()}ROTATE_LEFT()REPEAT 3 TIMES{MOVE_FORWARD()}
The robot starts facing toward the bottom of the grid. In which square is the robot when the code segment ends?
```
. . . . .
v # . A .
. . . . .
B . C D #
```
REPEAT UNTIL(NOT CAN_MOVE(forward)){MOVE_FORWARD()}ROTATE_LEFT()REPEAT UNTIL(NOT CAN_MOVE(forward)){MOVE_FORWARD()}
The robot starts in the bottom-left square, facing right. Which of the code segments move the robot to the goal square G? Select two answers.
```
. . . G
. # . .
> . . #
```
Select 2 answers.
Segment IREPEAT 2 TIMES{MOVE_FORWARD()}ROTATE_LEFT()REPEAT 2 TIMES{MOVE_FORWARD()}ROTATE_RIGHT()MOVE_FORWARD()Segment IIROTATE_LEFT()REPEAT 2 TIMES{MOVE_FORWARD()}ROTATE_RIGHT()REPEAT 3 TIMES{MOVE_FORWARD()}Segment IIIMOVE_FORWARD()ROTATE_LEFT()REPEAT 2 TIMES{MOVE_FORWARD()}ROTATE_RIGHT()REPEAT 2 TIMES{MOVE_FORWARD()}Segment IVREPEAT 2 TIMES{MOVE_FORWARD()}ROTATE_LEFT()REPEAT 2 TIMES{MOVE_FORWARD()}ROTATE_LEFT()MOVE_FORWARD()
The robot starts facing right. What happens when the code segment runs?
```
. A # B
> D C .
. # . .
```
MOVE_FORWARD()ROTATE_LEFT()MOVE_FORWARD()ROTATE_RIGHT()MOVE_FORWARD()ROTATE_RIGHT()MOVE_FORWARD()
The robot is in the middle square, facing left. Which of the following expressions evaluate to true? Select two answers.
```
. # .
. < #
. . .
```
Select 2 answers.
The robot starts in the bottom-left corner of an open 3 by 3 grid, facing up. Where is the robot after the code segment runs, and which way does it face?
```
. . .
. . .
^ . .
```
REPEAT 4 TIMES{MOVE_FORWARD()ROTATE_RIGHT()}
Unit 5: Procedures, libraries and simulations
Random Values and Simulations
Which of the following lists every value that RANDOM(2, 5) can return?
Which of the following describes all the values this code segment could display?
x ← RANDOM(1, 4) * 2 + 1DISPLAY(x)
A game should give a player a bonus with a 25% chance. Which of the following conditions are true exactly 25% of the time? Select two answers.
Select 2 answers.
Which of the following lists all the values this code segment could display?
heads ← 0REPEAT 3 TIMES{IF(RANDOM(1, 2) = 1){heads ← heads + 1}}DISPLAY(heads)
For which of the following tasks would a computer simulation be most useful instead of a real-world experiment?
A city builds a simulation of traffic on a planned bridge. To keep it simple, the simulation treats every vehicle as a car and leaves out buses and trucks. Which of the following is the most likely consequence of this choice?
Which of the following statements about simulations are true? Select two answers.
Select 2 answers.
Unit 6: Binary and compression
Binary Numbers and Bits
What is the decimal value of the binary number 11001?
Which binary number is equal to the decimal number 52?
Which list shows the binary numbers 1011, 110, 1101 and 111 in order from least to greatest?
What is displayed when this code segment runs?
PROCEDURE toDecimal(bitList){total ← 0FOR EACH bit IN bitList{total ← total * 2 + bit}RETURN(total)}DISPLAY(toDecimal([1, 0, 1, 1]))
Which of the following statements about representing data with bits are true? Select two answers.
Select 2 answers.
A game adds 0.1 to a player's speed ten times, starting from 0, and then checks whether the speed equals 1.0. The check comes out false. Which of the following best explains why?
A phone app records a singer by sampling the sound. Which change would make the recording a closer approximation of the real sound?
A school gives each of its 1,200 students a unique ID number stored in binary. What is the fewest number of bits that each ID can use?
Data Compression
A student club keeps a spreadsheet of who has paid their dues and how much. The treasurer wants to compress it before emailing it. Which choice is best?
A video chat app needs calls to stay smooth for people on slow home connections. Which compression approach fits best?
Which of the following statements about data compression are true? Select two answers.
Select 2 answers.
Each image below is stored row by row and compressed with run-length encoding. Which image would most likely shrink the most?
The code segment below rebuilds a row of pixels from run-length data, where 0 is a white pixel and 1 is a black one. What does the list pixels contain after it runs?
counts ← [3, 1, 2]colors ← [0, 1, 0]pixels ← []i ← 1REPEAT LENGTH(counts) TIMES{REPEAT counts[i] TIMES{APPEND(pixels, colors[i])}i ← i + 1}
Two files of the same size are compressed with the same lossless algorithm. File A shrinks by 80%, and file B shrinks by only 5%. Which of the following best explains the difference?
A 9,000-bit message is compressed with a lossless algorithm to 3,000 bits. Which statement is true?
Unit 7: Working with data
Extracting Information from Data
A music streaming app stores millions of songs. Which of the following is metadata for a song?
A teacher uploads a video of the science fair. Later, she changes its title and deletes its location tag. Which statement is true?
A study of 200 towns finds that towns with more coffee shops also have more car accidents. Which conclusion is best supported by this finding?
A town wants to know whether residents support building a skate park. It posts an online survey only on a local skate shop's website and gets 3,000 responses, 95% in favor. Which of the following are true? Select two answers.
Select 2 answers.
A form asks students to type the name of their state. Which of the following is an example of cleaning the data?
What is displayed when this code segment runs?
temps ← [72, 85, 90, 64, 88]count ← 0FOR EACH t IN temps{IF(t ≥ 85){count ← count + 1}}DISPLAY(count)
Which of the following best describes what this code segment does?
tempsF ← [50, 68, 86]tempsC ← []FOR EACH f IN tempsF{APPEND(tempsC, (f - 32) * 5 / 9)}
A video service wants to find the most-watched show in each state, using billions of viewing records. The records are too large to process on one computer in a reasonable amount of time. Which approach best addresses this challenge?
Unit 8: The internet
How the Internet Works
A small company invents a new kind of smart speaker. It connects to the Internet without the company asking any other organization for permission. Which of the following best explains why this is possible?
Which of the following are typically included in a packet's metadata? Select two answers.
Select 2 answers.
Which of the following best describes the relationship between the Internet and the World Wide Web?
A network has a bandwidth of 50 megabits per second. What is the least amount of time it can take to send a 200-megabit file over this network?
In a network, these pairs of devices are directly connected: A and B, A and C, B and D, C and D, D and E. Which of the following is a path from A to E?
You load the same website twice, a minute apart, and the packets travel through different routers each time. Which of the following best explains this?
The Internet was designed to be scalable. What does this mean?
Fault Tolerance and Redundancy
Which of the following best describes a fault-tolerant system?
In a network, these pairs of devices are directly connected: A and B, A and C, B and C, C and D, D and E, D and F, E and F. If exactly one connection fails, which failure would stop A from communicating with F?
Use the same network: A and B, A and C, B and C, C and D, D and E, D and F, E and F. Engineers want A to stay able to reach F if any single connection fails. Which of the following changes would do this? Select two answers.
Select 2 answers.
A school pays for a second Internet connection from a different provider, in case the first one goes down. Which of the following best describes this decision?
You are loading a web page when a router on the path between your laptop and the web server fails. Which of the following is most likely to happen?
A city has two cables connecting it to the rest of the Internet. To save money, both cables were laid in the same underground tunnel. Which of the following best describes the vulnerability?
The two lists below describe a network: the connections are A to B, A to C, B to D, C to D and D to E. What is displayed when the code segment runs?
fromList ← ["A", "A", "B", "C", "D"]toList ← ["B", "C", "D", "D", "E"]count ← 0i ← 1REPEAT LENGTH(fromList) TIMES{IF((fromList[i] = "D") OR (toList[i] = "D")){count ← count + 1}i ← i + 1}DISPLAY(count)
Unit 9: Efficiency, limits and parallel computing
Algorithmic Efficiency and Heuristics
Which of the following is an instance of a problem, rather than a general problem?
Which of the following is an optimization problem?
Each option describes how many steps an algorithm takes for an input of n items. Which algorithms run in a reasonable amount of time? Select two answers.
Select 2 answers.
How many times does the statement count ← count + 1 run in this code segment?
count ← 0REPEAT 5 TIMES{REPEAT 4 TIMES{count ← count + 1}count ← count + 1}DISPLAY(count)
The procedure below is called with a list of n numbers. How many times does total ← total + 1 run?
PROCEDURE countPairs(numbers){total ← 0FOR EACH a IN numbers{FOR EACH b IN numbers{total ← total + 1}}RETURN(total)}
An algorithm finds the best team by checking every possible group of members in a club of n members. One more person joins the club. What happens to the number of groups the algorithm checks?
A delivery company plans a route through 60 stops each morning. Checking every possible order of the stops would take far too long. Which approach is most appropriate?
Which of the following best describes a heuristic?
Undecidable Problems
Which statement describes a decidable problem?
A student says: "The halting problem is undecidable, so no program can ever tell whether any particular program stops." What is wrong with this claim?
Which of these is a decidable problem?
Which two statements about undecidable problems are true?
Select 2 answers.
A company advertises a tool that "detects every infinite loop in any program before it runs, with no mistakes." Which response is best?
What does this program display?
n ← 6count ← 0REPEAT UNTIL (n = 1){IF (n MOD 2 = 0){n ← n / 2}ELSE{n ← 3 * n + 1}count ← count + 1}DISPLAY (count)
Parallel and Distributed Computing
A program has four tasks. Tasks P (30 seconds), Q (50 seconds) and R (20 seconds) are independent, and each can run on its own processor at the same time. Task S (10 seconds) can start only after P, Q and R have all finished. What is the least amount of time the program can take?
A computer has two processors that can work in parallel. It must run four independent tasks that take 20, 30, 40 and 50 seconds. Each task runs on one processor. What is the least amount of time needed to finish all four?
A task takes 120 seconds when it is run sequentially and 40 seconds when it is run in parallel. What is the speedup of the parallel solution?
Which of the following statements about parallel computing are true? Select two answers.
Select 2 answers.
Scientists need to analyze 2 petabytes of telescope images, far more data than a single computer can store. Which approach fits this problem best?
The list times holds the times, in seconds, of four independent tasks. What is displayed when the code segment runs, and what does it represent?
times ← [15, 40, 25, 10]total ← 0longest ← 0FOR EACH t IN times{total ← total + tIF(t > longest){longest ← t}}DISPLAY(total / longest)
A program has a sequential portion that takes 30 seconds, and a portion that takes 60 seconds on one processor but can be split evenly among any number of processors. Which of the following statements is true?
Unit 10: The impact of computing
Effects of Computing
A school's grading app sends parents an alert the moment a new grade is posted. Which statement best describes this effect?
What was the World Wide Web originally designed to do?
Which two statements about programmers and the effects of their innovations are accurate? Select two answers.
Select 2 answers.
A program written to animate 3D movie characters is later adapted by surgeons to rehearse operations on 3D models of patients. What does this example show?
Which two situations are examples of the digital divide? Select two answers.
Select 2 answers.
Which statement about the digital divide is accurate?
The code segment below uses invented broadband rates for four towns. What is displayed?
rates ← [72, 95, 64, 88]low ← rates[1]high ← rates[1]FOR EACH r IN rates{IF(r < low){low ← r}IF(r > high){high ← r}}DISPLAY(high - low)
Computing Bias and Crowdsourcing
A company trains a program to rank job applicants using records of the people it hired over the past ten years, most of whom went to the same few colleges. The program now ranks applicants from other colleges lower. What is the most likely cause?
Which two actions would most help programmers reduce bias in a speech-recognition app? Select two answers.
Select 2 answers.
Which statement about bias in computing innovations is accurate?
A hand sensor detects a hand when its reading is at least 45. The list holds the readings from five hands in a group that was under-represented in the training data. What is displayed?
readings ← [52, 38, 61, 44, 35]count ← 0FOR EACH r IN readings{IF(r ≥ 45){count ← count + 1}}DISPLAY(count)
Which of these is an example of citizen science?
Which two are examples of crowdsourcing? Select two answers.
Select 2 answers.
A musician raises the money for her first album by collecting small pledges from 2,000 fans on a website. Which statement best describes this?
Legal and Ethical Concerns
Jayden finds a paragraph on a website that explains recursion clearly. He pastes it into his project write-up with no quotation marks and no source. Which statement is accurate?
A student wants to use a photo licensed CC BY-NC as the title screen of a phone game she plans to sell for $0.99. What does the license allow?
Which two are legal ways to use material created by someone else? Select two answers.
Select 2 answers.
Which description fits open access?
The procedure canShare decides whether a planned use of a licensed work is allowed. Which call returns false?
PROCEDURE canShare(isCommercial, isChanged, allowsCommercial, allowsChanges){IF((isCommercial AND NOT allowsCommercial) OR (isChanged AND NOT allowsChanges)){RETURN(false)}RETURN(true)}
Which two statements about intellectual property are accurate? Select two answers.
Select 2 answers.
Which computing innovation raises legal and ethical concerns mainly because it collects and analyzes data by monitoring activities all the time?
Safe Computing
Which two are examples of personally identifiable information? Select two answers.
Select 2 answers.
Kai never posts his address. His public posts do include a photo of his house with the street sign visible, a check-in at the same gym every weekday at 6 a.m., and his school team's jersey. Which risk does this show best?
Which login process is an example of multifactor authentication?
Ana sends Ben a message using public key encryption. Which statement is accurate?
What is the role of a certificate authority?
A message is stored as a list of numbers from 0 to 25, one per letter. The code segment below encrypts it with a shift of 3. What is displayed?
message ← [7, 24, 2]key ← 3FOR EACH n IN message{DISPLAY((n + key) MOD 26)}
A program secretly installed on a library computer records everything typed on the keyboard, including the passwords people enter. What is this called?
Which two actions best protect a phone from attacks? Select two answers.
Select 2 answers.
Unit 12: Exam review
Exam Pseudocode in Python
What is displayed?
aList ← [1, 2, 3]bList ← aListAPPEND (aList, 4)DISPLAY (LENGTH(bList))
What is displayed?
count ← 7REPEAT UNTIL (count > 5){count ← count - 1}DISPLAY (count)
Which two Python lines do the same thing as REMOVE(aList, 1)?
Select 2 answers.
What is displayed?
PROCEDURE mystery(n){IF (n MOD 2 = 0){RETURN (n / 2)}RETURN (n * 3)}DISPLAY (mystery(4))DISPLAY (mystery(5))
Which Python expression gives every value that RANDOM(1, 6) can?
AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website.