Output Prediction Timed Drill Checkpoint
60 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
This drill copies a written round: many short snippets and a clock. Work through the questions in order, trace each one on paper, and write the exact output. Run the code only after you have committed to an answer.
How to take the drill
Exact output means the same characters, spaces, quotes and brackets that the program prints. In a written round a missing bracket or an extra space can cost the whole mark.
Give yourself 20 minutes for the 12 questions and 25 minutes for the eight tasks. Do not run code until time is up. Skip any question after 90 seconds and return to it.
For each snippet, name the trap first, then trace. Write the exact output, including spaces and brackets.
The trap checklist
Before you trace, ask which family the snippet belongs to. Numbers: //, %, round, floats. Truth: falsy values, and / or. Text: slice ends, immutability. Lists: aliasing, copies, defaults. Names: scope, closures, class attributes. Errors: which exception and on which line.
print(-7 // 2, -7 % 3)print(0 or "x", [] and 1)print("abc"[::-1], "abc"[5:])b = a = [1]b.append(2)print(a)Example 1 · A worked trace: two traps in one snippet
def add(x, bag=[]): bag.append(x) return bag
first = add(1)second = add(2)print(first, second, first is second)[1, 2] [1, 2] True
What this code does
- The default list is shared, so both calls return the same list object.
- Both names show
[1, 2], andisconfirms it is one list.
Example 2 · Scope and class attributes together
class Report: total = 0
def add(self, n): self.total += n
r = Report()r.add(5)r.add(7)print(Report.total, r.total)
funcs = [lambda: n for n in range(3)]print(funcs[0]())0 12 2
What this code does
self.total += ncreates an instance attribute, so the class value stays 0.- The lambdas read
nafter the loop, so every one returns 2.
This prints -4 2, then x [], then cba (an empty string at the end) and finally [1, 2]. Four lines, four different traps.
Module quiz
Questions from the whole module. Your first answer is your score.
Problems
Graded easy to hard, using everything from this module.
Try each one yourself first. Use the hint if you get stuck. Mark it as done when it works.
Exercise 1· Easy
Print -17 // 5 and -17 % 5 on one line.
-4 3
Exercise 2· Easy
From [0, "", "a", None, 3, [], [0]], keep only the truthy items and print the list.
['a', 3, [0]]
Exercise 3· Easy
For word = "interview", print word[2:5], word[-4:] and word[::2] on one line.
ter view itriw
Exercise 4· Medium
Write class Team where each team has its own members list. Make two teams, add "Asha" to the first, and print both lists.
['Asha'] []
Exercise 5· Medium
Fix [lambda: n for n in range(3)] so calling each function gives 0, 1, 2. Print the three results as a list.
[0, 1, 2]
Exercise 6· Medium
Print the exception type name for each of these actions, one per line: int("x"), [][1], {}["k"]. Use a loop over small functions.
ValueError IndexError KeyError
Exercise 7· Hard
Write add_tag(tag, tags=None) that returns a list with the tag added, with no sharing between calls. Call it with "a" and then "b" and print the second result.
['b']
Exercise 8· Hard
Write describe(n) that returns "negative", "zero", "even" or "odd". Print the result for -3, 0, 4, 7, one per line.
negative zero even odd
Interview questions
Questions interviewers ask on this topic.
What's next
You finished the preview of Output Prediction & Debugging
Python Programming has 21 sections, 110 modules and 609 lessons. An account opens every lesson and saves your progress.
Finish up
Mark complete & continue
Next: Explaining Your Approach and Its Cost Out Loud