Intro to Computer Science Practice Questions
Intro to computer science practice questions with answers: 12 Python questions on output, scope, recursion and Big-O, each with the reason behind the answer.
These are 12 practice questions from Encodr's free Intro to Computer Science deck, taken from 10 of the course's 16 units. They are the kind an intro Python course tests: read a short program, predict what it prints or which exception it raises, and know the one rule that explains why. Each question comes with the answer and the reason.
How to use this set
Cover the answer, write down your prediction, and only then read on. Do this on paper, with the value of each variable after each line, before you look at the options. Trying to answer before you see the solution is the testing effect at work, and it tends to work better than rereading the same material. If you get one wrong, find the exact line where your trace and Python disagreed. Then run the code and check.
A note on the first question: the answer comes from the fact that Python stores floats in binary. If you want to see base 2 for yourself, the number base converter shows the place-value working for any whole number.
The questions
1. Expressions and operators
What does this program print?
``python print(0.1 + 0.2) ``
- A. 0.3
- B. 0.30
- C. 0.30000000000000004
- D. 0.29999999999999993
Answer: C. 0.30000000000000004
Why: The binary approximations of 0.1 and 0.2 sum to a value just above 0.3, and print shows all the digits needed to tell it apart.
2. Objects, variables and types
What does this print?
``python grid = [[0] * 2] * 2 grid[0][0] = 9 print(grid) ``
- A. [[9, 0], [0, 0]]
- B. [[9, 0], [9, 0]]
- C. [[9, 9], [0, 0]]
- D. [[0, 0], [0, 0]]
Answer: B. [[9, 0], [9, 0]]
Why: [[0] 2] 2 makes two references to the same row list, so setting grid[0][0] also shows up in grid[1][0].
3. Decisions
True or false? The condition if x == 3 or 4: is true only when x is 3 or 4.
``python x = 7 if x == 3 or 4: print("yes") ``
Answer: False
Why: It parses as (x == 3) or 4, and 4 is truthy, so the condition is always true.
4. Loops
A loop else clause runs only if the loop ends without hitting _____.
Answer: break
Why: A loop else clause runs when the loop finishes without hitting break, including when the loop body never ran. Only break (or return or an exception) skips it.
5. Functions and scope
What does this program print?
``python def add(x, lst=[]): lst.append(x) return lst a = add(1) b = add(2) print(a is b, a) ``
- A. False [1]
- B. False [1, 2]
- C. True [1, 2]
- D. True [2]
Answer: C. True [1, 2]
Why: Both calls return the same default list, so a is b is True, and by the time it prints it holds both items.
6. Functions and scope
Which exact exception class does f() raise?
``python x = 5 def f(): print(x) x = 10 f() ``
- A. NameError
- B. TypeError
- C. ValueError
- D. UnboundLocalError
Answer: D. UnboundLocalError
Why: The assignment x = 10 makes x local for the whole function, so print(x) reads a local that has no value yet.
7. Strings
Why is "10" < "9" True in Python?
``python print("10" < "9") ``
- A. The first differing character decides, and "1" < "9"
- B. Python compares the numbers 10 and 9, and 10 is smaller
- C. Python converts both strings to integers before comparing
- D. Longer strings are always considered smaller than shorter
Answer: A. The first differing character decides, and "1" < "9"
Why: String comparison is by characters, not numeric value. The first characters "1" and "9" differ and decide it.
8. Dictionaries, sets and tuples
What does this print?
``python d = {"a": 1, "a": 2} print(d, len(d)) ``
- A. {'a': 2} 1
- B. {'a': 1} 1
- C. {'a': 1, 'a': 2} 2
- D. SyntaxError
Answer: A. {'a': 2} 1
Why: The later "a": 2 overwrites the earlier pair, leaving one key.
9. Recursion
What happens when this program runs?
``python def total_rec(n): return 0 if n == 0 else n + total_rec(n - 1) print(total_rec(10000)) ``
- A. It prints 50005000
- B. It prints 0
- C. A RecursionError is raised
- D. An OverflowError is raised
Answer: C. A RecursionError is raised
Why: Each call adds a stack frame, and about 1000 nested calls is the default limit, so 10000 raises RecursionError.
10. Recursion
True or false? Python removes tail calls, so a function whose last action is the recursive call never hits the recursion limit.
Answer: False
Why: Python has no tail-call elimination; count(5000) style recursion still raises RecursionError.
11. Searching, sorting and Big-O
A binary search for 3 is run on the unsorted list [5, 1, 9, 3, 7]. The middle item is 9, which is larger than 3, so it keeps only the left half and continues. What is the outcome?
- A. It finds 3 at index 3
- B. It finds 3 but takes more comparisons than usual
- C. It raises an IndexError
- D. It reports not found, even though 3 is in the list
Answer: D. It reports not found, even though 3 is in the list
Why: Discarding the right half on a hunch about order threw away the target, so the result is "not found". Unsorted data breaks the precondition.
12. Files and exceptions
Which exception does this raise?
``python int("3.5") ``
- A. TypeError
- B. ValueError
- C. IndexError
- D. NameError
Answer: B. ValueError
Why: The string "3.5" is the right type but is not a valid integer literal, so int() raises ValueError.
Where to go from here
Count how many you got right, then look at which units the misses came from. A cluster of misses in one area, such as aliasing or scope, means that chapter needs another pass, not the whole course.
- The full set is in the free Intro to Computer Science deck, which spaces your reviews so the traps you miss come back sooner.
- What's in Intro to Computer Science lists all 16 units in order.
- How to study for Intro to Computer Science covers tracing, the common traps and how to practice for the written exam.
- The binary search question above sits in the Big-O unit; Big-O notation explained for beginners walks through the idea from scratch.
- Build a plan around the exam date with how to build a study schedule before an exam.
Encodr turns this into a habit: study anything in a feed, and it schedules the rest.
Get started free