AP CSP Big Idea 3 Q&A Notes

AP CSP Big Idea 3 - Questions and Answers

Question 1

  • Question: A student writes the following code to find the average of three numbers: average = num1 + num2 + num3 / 3. Which error does this code contain?
    • A. The code uses incorrect variable names.
    • B. The division should be done after summing all three numbers.
    • C. There are parentheses missing around the sum.
    • D. It's correct as it is.
  • Answer: C
  • Explanation: The code lacks parentheses to ensure the sum of num1, num2, and num3 is calculated before division. The correct code should be average = (num1 + num2 + num3) / 3.

Question 2

  • Question: What is the result of the following code snippet? x = 10 if x > 5: if x < 15: print("A") else: print("B")
    • A. A
    • B. B
    • C. Nothing
    • D. Error
  • Answer: A
  • Explanation: Since x is 10, the condition x > 5 is true. The nested if statement checks if x < 15, which is also true, so it prints "A".

Question 3

  • Question: Which of the following is the best explanation for why a programmer would use a procedure?
    • A. To slow down program execution.
    • B. To avoid writing any conditions.
    • C. To break code into reusable, manageable parts.
    • D. To decrease memory usage.
  • Answer: C
  • Explanation: Procedures (or functions) help in modularizing code, making it easier to understand, maintain, and reuse.

Question 4

  • Question: Which of the following Boolean expressions is equivalent to not (A or B)?
    • A. not A and B
    • B. not A and not B
    • C. not A or not B
    • D. A and B
  • Answer: B
  • Explanation: According to DeMorgan's Law, not (A or B) is equivalent to not A and not B.

Question 5

  • Question: What will this code output? x = 3 y = 4 x, y = y, x print(x, y)
    • A. 3 4
    • B. 4 3
    • C. Error
    • D. None
  • Answer: B
  • Explanation: The line x, y = y, x swaps the values of x and y. Therefore, x becomes 4 and y becomes 3.

Question 6

  • Question: A list L contains 5 elements. Which of the following will cause an error?
    • A. L[4]
    • B. L[-1]
    • C. L[5]
    • D. L[0]
  • Answer: C
  • Explanation: In a list with 5 elements, the valid indices are 0 to 4. L[5] attempts to access an element beyond the list's bounds, causing an error. L[-1] is valid and returns the last element.

Question 7

  • Question: What is the best reason to use a loop?
    • A. To write complex Boolean expressions.
    • B. To repeat code efficiently.
    • C. To create functions.
    • D. To declare variables.
  • Answer: B
  • Explanation: Loops are used to repeat a block of code multiple times, making code more efficient and reducing redundancy.

Question 8

  • Question: Which of the following describes a binary search?
    • A. Scans the list linearly from start to end.
    • B. Repeatedly divides the search interval in half.
    • C. Checks every third element.
    • D. Uses recursion only.
  • Answer: B
  • Explanation: Binary search is an efficient algorithm for finding an item in a sorted list by repeatedly dividing the search interval in half.

Question 9

  • Question: Which of these control structures is not typically used to implement an algorithm?
    • A. Sequence
    • B. Selection
    • C. Iteration
    • D. Declaration
  • Answer: D
  • Explanation: Sequence, selection (conditionals), and iteration (loops) are fundamental control structures used in algorithms. Declaration is about defining variables, not controlling the flow of the algorithm.

Question 10

  • Question: What does the following code print? for i in range(3): for j in range(2): print(i, j)
    • A. (0,0) to (3,2)
    • B. 6 times same output
    • C. 0 0 0 1 1 0 1 1 2 0 2 1
    • D. Error
  • Answer: C
  • Explanation: The outer loop iterates from i = 0 to 2, and the inner loop iterates from j = 0 to 1. The code prints all combinations of (i, j).

Question 11

  • Question: Which of the following data types is most appropriate to store a list of temperatures recorded hourly?
    • A. Integer
    • B. Float
    • C. List
    • D. Boolean
  • Answer: C
  • Explanation: A list is the most appropriate data type to store a collection of temperatures, as it can hold multiple values of different (or same) types. The individual temperatures themselves would likely be floats.

Question 12

  • Question: What will the following function output? def mystery(x): return x * x print(mystery(5))
    • A. 10
    • B. 25
    • C. 5
    • D. Error
  • Answer: B
  • Explanation: The function mystery(x) returns the square of x. When called with mystery(5), it returns 5 * 5 = 25.

Question 13

  • Question: In the context of programming, abstraction is:
    • A. Removing unnecessary details.
    • B. Making code slower.
    • C. Debugging code.
    • D. Using a loop.
  • Answer: A
  • Explanation: Abstraction involves simplifying complex systems by hiding unnecessary details and focusing on essential features.

Question 14

  • Question: Which of the following will reverse a list myList?
    • A. myList.sort()
    • B. myList = myList[::-1]
    • C. myList.reverse()
    • D. myList = sorted(myList)
  • Answer: C
  • Explanation: myList.reverse() reverses the list in place. myList = myList[::-1] also reverses the list, but creates a new reversed list and assigns it back to myList. myList.sort() sorts the list, and sorted(myList) makes a sorted copy.

Question 15

  • Question: Which of the following is an example of an event-driven programming construct?
    • A. for loop
    • B. function call
    • C. onClick handler
    • D. variable assignment
  • Answer: C
  • Explanation: An onClick handler is triggered by a user event (a click), making it an example of event-driven programming. Event-driven programming relies on events to trigger specific functions/methods.

Question 16

  • Question: Which of these best describes how an algorithm functions?
    • A. Randomly selects outcomes.
    • B. Step-by-step procedure.
    • C. A static picture.
    • D. A syntax rule.
  • Answer: B
  • Explanation: An algorithm is a well-defined, step-by-step procedure to solve a problem.

Question 17

  • Question: Which of the following is not a benefit of using procedures?
    • A. Reusability
    • B. Easier debugging
    • C. Slower execution
    • D. Readability
  • Answer: C
  • Explanation: Procedures generally do not cause slower execution. In fact, they can improve performance due to better organization and reusability. Reusability, easier debugging, and readability are all benefits of using procedures.

Question 18

  • Question: What does len([1, 2, 3, 4]) return?
    • A. 3
    • B. 4
    • C. 5
    • D. 0
  • Answer: B
  • Explanation: len() function returns the number of elements in a list. Here, the list has 4 elements.

Question 19

  • Question: What is the result of True and False or True?
    • A. True
    • B. False
    • C. None
    • D. Error
  • Answer: A
  • Explanation: True and False evaluates to False. Then, False or True evaluates to True.

Question 20

  • Question: Which of these algorithms has the best average-case time complexity for searching in a sorted list?
    • A. Linear search
    • B. Binary search
    • C. Brute force
    • D. Tree traversal
  • Answer: B
  • Explanation: Binary search has a time complexity of O(log n), which is more efficient than linear search's O(n) for sorted lists.

Question 21

  • Question: What will this output? x = [1, 2, 3] y = x y.append(4) print(x)
    • A. [1, 2, 3]
    • B. [1, 2, 3, 4]
    • C. [4]
    • D. Error
  • Answer: B
  • Explanation: Because y = x, y is a reference to the same list as x. Modifying y therefore also modifies x. y.append(4) adds 4 to the end of the list, so both x and y become [1, 2, 3, 4].

Question 22

  • Question: What will this code output? print("3" + "4")
    • A. 7
    • B. 34
    • C. 3 4
    • D. Error
  • Answer: B
  • Explanation: String concatenation is performed. `