




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























Course: CS 106A — Programming Methodology Format: Closed-book, multiple-choice Language: Python and Karel/Bit-style robot programming Total Questions: 25 Total Points: 100 Suggested Time: 90 minutes
This exam follows the standard CS 106A midterm scope: robot programming, Python fundamentals, control flow, functions, strings, lists, nested loops, images, and 2-D lists. Stanford’s current midterm guidance identifies these areas—through 2-dimensional lists—as core midterm material.
Choose the single best answer for each question.
Each question is worth 4 points.
Assume standard Python 3 syntax unless otherwise stated.
For Karel questions, assume the standard Karel commands: move(), turn_left(), put_beeper(), pick_beeper(), front_is_clear(), beepers_present(), and related condition checks.
Code fragments are intended to be read independently unless a complete program is shown.
What is the primary purpose of decomposition in programming?
A. To make a program run faster by using fewer lines of code
B. To divide a complex problem into smaller, manageable subproblems
C. To prevent all runtime errors from occurring
D. To convert Python code into machine code
Correct Answer: B
Rationale: Decomposition is a central software-engineering principle in CS 106A: a large task is broken into smaller functions or steps that are easier to design, test, and debug. Stanford describes the course as emphasizing decomposition, abstraction, and other modern software-engineering principles.
A is wrong: Fewer lines do not necessarily mean faster execution; decomposition improves clarity, not line count.
C is wrong: Decomposition reduces complexity but cannot prevent every runtime error.
D is wrong: Converting source code to machine code is compilation/interpretation, not decomposition.
Which statement best describes an algorithm?
A. A specific Python library used for graphics
B. A finite, ordered set of steps that solves a problem
C. A variable that stores multiple values
A. turn_left()
B. turn_left() twice
C. turn_left() three times
D. turn_left() four times
Correct Answer: C
Rationale: Karel turns 90 degrees counterclockwise with each turn_left(). From East, one left turn gives North, two give West, and three give South. Karel’s directional movement and conditions are foundational Karel topics in CS 106A.
A is wrong: One turn faces North.
B is wrong: Two turns face West.
D is wrong: Four turns return Karel to East.
Which Karel condition should be used to determine whether there is a wall directly in front of Karel?
A. beepers_present()
B. front_is_clear()
C. not front_is_clear()
D. left_is_clear()
Correct Answer: C
Rationale: front_is_clear() returns True when Karel can move forward. Therefore, not front_is_clear() is True when a wall blocks the front. Karel conditionals are explicitly part of the introductory Karel curriculum.
A is wrong: This checks for a beeper on Karel’s current square.
B is wrong: This checks whether the front is open, not blocked.
D is wrong: This checks the left side, not the front.
Karel must place one beeper on every square in a row of unknown length until reaching a wall. Which loop is most appropriate?
A. A for loop using range(10)
B. A while loop that continues while front_is_clear()
C. An if statement without a loop
D. A recursive function call
Correct Answer: B
Rationale: A while loop is appropriate when the number of repetitions is unknown in advance but depends on a condition—in this case, whether another square is available. Karel instruction includes both for and while loops and conditions.
A is wrong: range(10) works only for exactly 10 squares, not an unknown row length.
C is wrong: An if statement executes once; it cannot repeat the placement.
D is wrong: Recursion is not the standard Karel approach and is unnecessary here.
In Karel, why is defining a new function such as turn_right() useful?
A. It makes Karel move twice as fast
B. It replaces the need for all conditional statements
C. It improves readability by giving a meaningful name to a repeated command sequence
D. It allows Karel to ignore walls
Correct Answer: C
Rationale: turn_right() can be defined as three turn_left() commands. Naming that sequence makes the program more readable and reusable, illustrating decomposition and abstraction.
A is wrong: Karel’s speed is not changed by defining a function.
B is wrong: Functions do not eliminate conditionals.
D is wrong: Karel still cannot move through walls.
Correct Answer: A
Rationale: range(1, 5) produces 1, 2, 3, 4 ; their sum is 10. Python’s range(start, stop) excludes the stop value, a common midterm concept.
B is wrong: 15 would result from range(1, 6), which includes 5.
C is wrong: 6 is the sum of 1 + 2 + 3, omitting 4.
D is wrong: 4 is only the final loop value, not the accumulated total.
What is the output? python count = 0 n = 24 while n > 1: n = n // 2 count += 1 print (count)
A. 3
B. 4
C. 5
D. Infinite loop
Correct Answer: B
Rationale: The values of n are 24 → 12 → 6 → 3 → 1. The loop runs four times before n > 1 becomes false. Integer division with // repeatedly halves the value.
A is wrong: Three iterations would stop at 3 , but 3 > 1 is still true.
C is wrong: Once n becomes 1 , the condition fails.
D is wrong: n strictly decreases toward 1, so the loop terminates.
Which Boolean expression correctly tests whether score is between 70 and 89, inclusive?
A. 70 <= score <= 89
B. 70 < score < 89
C. score >= 70 or score <= 89
D. score == 70 and score == 89
Correct Answer: A
Rationale: Python supports chained comparisons, so 70 <= score <= 89 is true exactly when score is at least 70 and at most 89.
B is wrong: It excludes both 70 and 89.
C is wrong: or makes the condition true for almost every number, including values below 70 or above 89.
D is wrong: A single number cannot equal both 70 and 89 simultaneously.
What is the output? python def double(n): return n * 2
x = double(4) print (x + 1)
A. 8
B. 9
C. 10
D. None
Correct Answer: B
Rationale: double(4) returns 8. Then x + 1 evaluates to 9. Functions with parameters and return values are a central CS 106A midterm topic.
A is wrong: 8 is the return value before adding 1.
C is wrong: 10 would require double(4) + 2.
A is wrong: count() requires an argument and counts occurrences of a substring.
C is wrong: size() is not a built-in Python string-length function.
D is wrong: length() is not a Python string method.
What is the output? python def mystery(s): result = "" for ch in s: if ch != "a": result += ch return result
print (mystery("banana"))
A. bnn
B. banana
C. aaa
D. bnna
Correct Answer: A
Rationale: The function builds a new string containing every character except "a". From "banana", removing all a characters leaves "bnn". This mirrors common CS 106A string-traversal problems.
B is wrong: The function filters characters rather than returning the original.
C is wrong: It removes "a", rather than collecting only "a".
D is wrong: It would retain an extra a.
Which function definition header correctly accepts a string and returns an integer?
A. def count_vowels(word):
B. def count_vowels(int word):
C. function count_vowels(word):
D. def count_vowels(): return word
Correct Answer: A
Rationale: Python function definitions use def name(parameters):; type declarations are not required in the parameter list. The function can return an integer computed from the string parameter.
B is wrong: Python does not use Java-style type declarations such as int word.
C is wrong: function is not a Python keyword for defining functions.
D is wrong: It accepts no parameter, so word would be undefined.
What is the output? python nums = [4, 8, 15, 16, 23, 42] print (nums[1] + nums[3])
A. 20
B. 24
C. 31
D. 19
Correct Answer: B
Rationale: Python list indices begin at 0. nums[1] is 8 , and nums[3] is 16 ; their sum is
A is wrong: 20 would be nums[0] + nums[3].
C is wrong: 31 would be nums[2] + nums[3].
D is wrong: 19 would be nums[0] + nums[2].
What is the output? python grid = [ [1, 2, 3], [4, 5, 6] ]
print (grid[1][2])
A. 2
B. 3
C. 5
D. 6
Correct Answer: D
Rationale: grid[1] selects the second row, [4, 5, 6]. Then [2] selects the third value in that row, which is 6. This tests standard row-then-column indexing in 2-D lists.
A is wrong: 2 is grid[0][1].
B is wrong: 3 is grid[0][2].
C is wrong: 5 is grid[1][1].
Which loop correctly visits every pixel in an image represented as a 2-D list with height rows and width columns?
A.
python for y in range(height): for x in range(width): # process image[y][x]
B.
python for x in range(width): for y in range(height): # process image[x][y]
python for y in range(width): for x in range(height): # process image[y][x]
D.
python for pixel in image: # process pixel
Correct Answer: A
Rationale: In a row-major 2-D list, the outer loop should iterate over rows (height) and the inner loop over columns (width). The pixel is then accessed as image[y][x]. Image processing with nested loops is a recurring CS 106A midterm topic.
B is wrong: It swaps the loop bounds and indexes the grid incorrectly.
C is wrong: It swaps height and width.
D is wrong: Iterating directly over image visits rows, not individual pixels.
What is the output? python def update(lst): lst[0] = 99
nums = [1, 2, 3] update(nums) print (nums[0])
A. 1
B. 2
C. 99
D. Error
Correct Answer: C
Rationale: Lists are mutable, and the function receives a reference to the same list object. Changing lst[0] therefore changes nums[0] to 99. List mutation and references are important Python fundamentals covered in CS 106A.
A is wrong: The original value is overwritten.
python print (word)
D.
python for ch in len(word): print (ch)
Correct Answer: A
Rationale: A for loop over a string visits each character directly. This is the simplest and most idiomatic Python string-traversal pattern.
B is wrong: range() requires an integer, not a string.
C is wrong: It prints the entire string once.
D is wrong: len(word) is an integer, not an iterable sequence of characters.
Which practice best helps ensure that a function works correctly?
A. Testing it with normal cases, boundary cases, and unusual inputs
B. Running it only once with one input
C. Avoiding return values so errors cannot occur
D. Giving variables short, unclear names to save time
Correct Answer: A
Rationale: Effective testing examines typical inputs, edge cases such as empty strings or zero-length lists, and unusual cases. CS 106A emphasizes testing and software- engineering discipline alongside programming fundamentals.
B is wrong: One test provides very limited evidence of correctness.
C is wrong: Avoiding return values does not prevent errors and often makes functions less useful.
D is wrong: Unclear names reduce readability and increase the likelihood of mistakes.
Question Correct Answer Topic
1 B Decomposition
2 B Algorithms
3 A Abstraction
4 C Karel directions
5 C Karel conditions
6 B Karel loops
7 C Karel functions
8 B Conditionals
9 A for loops and range()
10 B while loops
11 A Boolean logic
12 B Functions and returns
13 A String slicing
14 B String length
15 A String traversal
16 A Function syntax
17 B List indexing
18 B List iteration
19 A 2 - D list creation
20 D 2 - D list indexing
Choose the single best answer for each question.
Each question is worth 4 points.
Assume standard Python 3 syntax unless otherwise stated.
For Karel questions, assume the standard Karel commands: move(), turn_left(), put_beeper(), pick_beeper(), front_is_clear(), beepers_present(), and related condition checks.
Code fragments are intended to be read independently unless a complete program is shown.
What is the primary purpose of decomposition in programming?
A. To make a program run faster by using fewer lines of code
B. To divide a complex problem into smaller, manageable subproblems
C. To prevent all runtime errors from occurring
D. To convert Python code into machine code
Correct Answer: B
Rationale: Decomposition is a central software-engineering principle in CS 106A: a large task is broken into smaller functions or steps that are easier to design, test, and debug. Stanford describes the course as emphasizing decomposition, abstraction, and other modern software-engineering principles.
A is wrong: Fewer lines do not necessarily mean faster execution; decomposition improves clarity, not line count.
C is wrong: Decomposition reduces complexity but cannot prevent every runtime error.
D is wrong: Converting source code to machine code is compilation/interpretation, not decomposition.
Which statement best describes an algorithm?
A. A specific Python library used for graphics
B. A finite, ordered set of steps that solves a problem
C. A variable that stores multiple values
D. A syntax error detected before a program runs
Correct Answer: B
Rationale: An algorithm is a step-by-step procedure for accomplishing a task. CS 106A introduces algorithms early through Karel, where students translate a goal into ordered commands and control structures.
A is wrong: A library is a collection of reusable code, not an algorithm itself.
C is wrong: A variable stores data; it does not describe a solution procedure.
D is wrong: A syntax error is a language-rule violation, not an algorithm.
Which of the following is an example of abstraction?
A. Calling draw_circle(x, y, radius) without knowing its internal implementation
B. Writing every pixel operation manually inside main()
C. Using a variable named x1 instead of x
D. Printing a debugging message after every statement
Correct Answer: A
Rationale: Abstraction lets a programmer use a well-defined operation without needing to understand every internal detail. Function calls such as draw_circle() hide implementation details behind a simple interface, which is a core CS 106A design principle.
B is wrong: Manual pixel-level work is the opposite of abstraction because it exposes low-level detail.
C is wrong: Variable naming may affect readability but is not abstraction by itself.
D is wrong: Debug printing is a testing technique, not abstraction.