AP Computer Science Principles Big Idea 3: Algorithms and Programming Practice Questions
Practice questions for AP Computer Science Principles Big Idea 3 (Algorithms and Programming), covering variables, algorithms, and program logic, with explanations for every answer.
Questions
What is a variable?
- A named place to store a value
- A value that stays exactly the same for as long as the program runs
- A comment that describes a procedure
- A type of error that occurs at runtime
Consider: a ← 5; b ← a; a ← 8. What is the value of b?
- 5
- 8
- 13
- It changes to 8 when a changes
Consider: x ← 3; y ← x * 2; x ← y + 1. What are the final values of x and y?
- x = 7, y = 6
- x = 3, y = 6
- x = 7, y = 14
- x = 6, y = 7
Consider: a ← 4; b ← 9; temp ← a; a ← b; b ← temp. What are the final values?
- a = 9, b = 4
- a = 4, b = 9
- a = 9, b = 9
- a = 4, b = 4
A student tries to swap two values: a ← 4; b ← 9; a ← b; b ← a. What are the final values?
- a = 9, b = 9
- a = 9, b = 4
- a = 4, b = 9
- a = 4, b = 4
Consider: x ← 10; x ← x + 5; x ← x * 2. What is the final value of x?
- 30
- 25
- 20
- 15
Consider: p ← 2; q ← p; q ← q + p; p ← q * q. What are the final values?
- p = 16, q = 4
- p = 4, q = 4
- p = 16, q = 2
- p = 8, q = 4
When a new value is assigned to a variable, the previous value is replaced.
The statement x ← x + 1 is an error because x cannot equal x + 1.
Why is it good practice to give variables meaningful names, such as totalScore instead of t?
- It makes the code easier to read and maintain
- Meaningful names make the program run noticeably faster on most computers
- Short variable names cause syntax errors in most languages
- Meaningful names take up less of the computer's memory
In the statement score ← score + 10, what happens first?
- score + 10 is calculated using the current value
- score is set to 0 before anything else in the statement happens
- The value 10 is stored in score, replacing the old value
- score is compared with 10 to see whether they are equal
Consider: n ← 7; n ← n * 2; n ← n - 4. What is the final value of n?
Which statements correctly describe variables?
- A variable can store a number, string, Boolean, or list
- A variable's value can be changed by assignment
- A variable keeps every value ever assigned to it
- A variable can hold only whole numbers
In this course's pseudocode, lists start at index 1. If list ← [10, 20, 30, 40], what is list[2]?
- 20
- 30
- 10
- 2
list ← [10, 20, 30, 40]. After APPEND(list, 50), what is LENGTH(list)?
- 5
- 4
- 50
- 6
list ← [10, 20, 30, 40]. What is the list after INSERT(list, 2, 15)?
- [10, 15, 20, 30, 40]
- [10, 20, 15, 30, 40]
- [15, 10, 20, 30, 40]
- [10, 15, 30, 40]
list ← [10, 20, 30, 40]. What is the list after REMOVE(list, 1)?
- [20, 30, 40]
- [10, 20, 30]
- [10, 30, 40]
- [20, 30, 40, 10]
list ← [5, 8, 2]. After list[1] ← list[3], what is the list?
- [2, 8, 2]
- [5, 8, 5]
- [2, 8, 5]
- [8, 5, 2]
letters ← ["a", "b", "c"]. After REMOVE(letters, 2) and then APPEND(letters, "d"), what is the list?
- ["a", "c", "d"]
- ["a", "b", "d"]
- ["a", "d", "c"]
- ["d", "a", "c"]
nums ← [4, 7, 2, 9]. What does nums[LENGTH(nums)] evaluate to?
- 9
- 4
- 2
- An error, since that index is past the end of the list
In this course's pseudocode, where lists start at index 1, what happens when a program evaluates list[0]?
- An error occurs
- It returns the first element of the list
- It returns the last element of the list
- It returns 0
A program stores 30 test scores. Why is a list better than 30 separate variables?
- One list can be processed with a loop
- Lists can store only numbers, which keeps the data safely consistent
- Separate variables cannot hold test scores
- Lists never cause any errors when a program runs
Using a list to store related data is an example of data abstraction.
A list is allowed to contain just a single element.
A list can hold only values of the same type.
What does it mean to traverse a list?
- To access its elements one at a time
- To sort the list into alphabetical order and remove any repeated values
- To delete the list and all of its elements from memory
- To compress the list so it takes up less storage
vals ← [1, 2, 3]. What is vals after this code? i ← 1; REPEAT LENGTH(vals) TIMES { vals[i] ← vals[i] * vals[i]; i ← i + 1 }
- [1, 4, 9]
- [1, 2, 3]
- [2, 4, 6]
- [1, 4, 9, 16]
nums ← [8, 3, 9, 1]. What does this code display? minIndex ← 1; i ← 2; REPEAT 3 TIMES { IF (nums[i] < nums[minIndex]) { minIndex ← i }; i ← i + 1 }; DISPLAY(minIndex)
- 4
- 1
- 2
- 3
Which operations change the length of a list?
- APPEND
- INSERT
- REMOVE
- Assigning a new value to list[2]
- Reading list[1]
list ← [3, 6, 9, 12]. What is list[3]?
A list contains [2, 7, 7, 3, 7]. A loop counts how many elements equal 7. What is the count?
- 3
- 2
- 7
- 5
What is 17 MOD 5?
- 2
- 3
- 3.4
- 85
What is 20 MOD 4?
- 0
- 5
- 4
- 1
What is 3 MOD 7?
- 3
- 0
- 4
- 7
What is the value of 7 + 3 * 2?
- 13
- 20
- 12
- 17
What is the value of (7 + 3) * 2?
- 20
- 13
- 17
- 10
Which expression is true exactly when the integer n is even?
- n MOD 2 = 0
- n MOD 2 = 1
- n / 2 = 0
- n MOD 0 = 2
Which expression gives the last digit of a positive integer n?
- n MOD 10
- n / 10
- n MOD 2
- 10 MOD n
Which expression is true when n is a multiple of 3?
- n MOD 3 = 0
- n / 3 = 0
- 3 MOD n = 0
- n MOD 3 = 3
A procedure substring(str, start, length) returns length characters beginning at position start (positions start at 1). What does substring("COMPUTER", 4, 3) return?
- "PUT"
- "MPU"
- "PUTE"
- "UTE"
What does concatenating the strings "tic" and "tac" produce?
- "tictac"
- "tic tac"
- "tactic"
- 6
Consider: word ← ""; FOR EACH ch IN ["c", "a", "t"] { word ← concat(ch, word) }. What is word?
- "tac"
- "cat"
- "act"
- "ttt"
In this course's pseudocode, 10 / 4 evaluates to 2.5.
The expression 9 MOD 3 evaluates to 3.
What is 25 MOD 10?
What is 14 MOD 4?
What is a string?
- An ordered sequence of characters
- A number that is stored in binary form using only the digits 0 and 1
- A list of Boolean values that are each true or false
- A type of loop that repeats a fixed number of times
A program computes the average of a, b, and c. Which expression is correct?
- (a + b + c) / 3
- a + b + c / 3
- a + b + (c / 3)
- (a + b) + c / 3
What is the value of NOT (true AND false)?
- true
- false
- true AND false
- undefined
If x is 10, what is the value of (x > 5) AND (x < 10)?
- false
- true
- 10
- 5