Stage 5 of 6 · 100 points · suggested 75 minutes, closed book
Practice exam
A full paper in the style the hubs have taught all term, with worked answers and marking notes behind every question. It is a training instrument in the midterm's spirit, not a copy of the real paper, whose exact format the teacher announces in class.
How to use this paper
- Once, properly. Late in the runway, 75 minutes on a timer, paper and pen, every hub closed, answers hidden. A practice exam taken open-book measures your searching, not your knowledge.
- Mark honestly. Reveal each answer only after writing yours. The marking notes say what earns full and partial credit; score yourself the way a stranger would.
- Mine the misses. Every lost point names a review page; the table at the bottom routes each section back to its material. A 70% with a study list beats an unexamined feeling of readiness.
Section A · Tools, short answers (16 points, 2 each)
A1 · Write the command: stage every change in the current repository.
git add . (naming files individually also accepted). Marking: 2 for a correct command; commands that commit or push instead score 0, they are different moves.A2 · Your two commits from last night are not on GitHub. One command fixes it; name it and the concept behind the situation.
git push; commits are local until pushed, because the local repository and the remote are separate areas. Marking: 1 command, 1 concept.A3 · Give the commands, in order, that refresh the package catalogue and then install the newer versions, confirming automatically.
sudo apt update then sudo apt full-upgrade -y. Marking: 1 per command in the right order; reversed order scores 1 total with the note that upgrade would act on stale information.A4 · In SSH key authentication, which key is installed on the Pi, and which never leaves your laptop?
ssh-copy-id); the private key never leaves the laptop. Marking: 1 each; answers sending the private key anywhere score 0 for that half.A5 · A teammate runs sudo ufw enable on a headless Pi before any allow rule. State the consequence and the correct order.
sudo ufw allow 22, then enable. Marking: 1 consequence, 1 order.A6 · Write the command that opens a terminal on the Pi named lastname-pi as user username, from your laptop.
ssh username@lastname-pi. Marking: 2; user and host reversed scores 0, the form is user@host.A7 · What is the difference between rm file on the Pi and deleting a file on your desktop OS at home?
rm removes immediately and permanently; the safety net, if any, is your Git history. Marking: 2 for permanence; mentioning Git as the real undo earns the second point if permanence is vague.A8 · You see ======= in the middle of main.py after a pull. Name the situation and the three-step exit.
git add; git commit. Marking: 1 name, 1 the steps.Section B · Electricity and GPIO (12 points)
B1 (6) · A red LED (forward voltage 2.0 V) is wired from a 3.3 V GPIO pin through a 390 Ω resistor to ground. Compute the loop current; show formula, substitution and units.
B2 (3) · The same LED must now draw at most 2 mA. Minimum resistance, and which way do you round to a standard value?
B3 (3) · A described hookup: an LED wired pin-to-ground with no resistor, its short leg toward the pin, rewired while the script runs. Name the three violations, by rule.
Section C · Reading code (12 points)
C1 (6) · A script ends with this traceback. Name the error family, the faulty line's job, and the fix.
Traceback (most recent call last):
File "blink.py", line 7, in <module>
led.blink(0.2 0.2)
SyntaxError: invalid syntax
Traceback (most recent call last):
File "blink.py", line 7, in <module>
led.blink(0.2 0.2)
SyntaxError: invalid syntaxSyntaxError, so Python could not parse line 7, whose job is to start the LED blinking; the two arguments lack their separating comma: led.blink(0.2, 0.2). Marking: 2 family plus the bottom-up habit, 2 locating the fault, 2 fix.C2 (6) · Predict the complete output, then justify the final line.
def double(n):
print(n * 2)
result = double(4)
print(result)
def double(n):
print(n * 2)
result = double(4)
print(result)8 then None. The function prints 8 but has no return, so the call evaluates to None, which the second print shows. Printing and returning are different channels. Marking: 2 the 8, 2 the None, 2 the reason.Section D · Trace (14 points)
D1 · Trace this pseudocode to completion: columns count, total, condition, OUTPUT; one row per loop pass plus the final line. 10 points for the table, 4 for the final output being right for the right reason.
SET count TO 0
SET total TO 0
WHILE count < 4
SET count TO count + 1
IF count = 3 THEN
SET total TO total + 10
ELSE
SET total TO total + count
END IF
END WHILE
OUTPUT total
SET count TO 0
SET total TO 0
WHILE count < 4
SET count TO count + 1
IF count = 3 THEN
SET total TO total + 10
ELSE
SET total TO total + count
END IF
END WHILE
OUTPUT total| Pass | count < 4? | count | branch | total |
|---|---|---|---|---|
| start | – | 0 | – | 0 |
| 1 | 0 < 4 yes | 1 | ELSE: +1 | 1 |
| 2 | 1 < 4 yes | 2 | ELSE: +2 | 3 |
| 3 | 2 < 4 yes | 3 | IF: +10 | 13 |
| 4 | 3 < 4 yes | 4 | ELSE: +4 | 17 |
| exit | 4 < 4 no | 4 | – | 17 |
OUTPUT: 17. Marking: 2 per correct pass row, 4 for 17 with the exit condition shown. The planted traps: the increment happens before the test of count = 3, and the loop runs while count is less than 4, entering at 3 one last time.
Section E · Conversions (16 points)
E1 (8) · Convert to Python, course conventions in, correct syntax out.
FUNCTION classify(ms)
IF ms < 250 THEN
RETURN "fast"
ELSE IF ms < 450 THEN
RETURN "ok"
ELSE
RETURN "slow"
END IF
END FUNCTION
FUNCTION classify(ms)
IF ms < 250 THEN
RETURN "fast"
ELSE IF ms < 450 THEN
RETURN "ok"
ELSE
RETURN "slow"
END IF
END FUNCTIONdef classify(ms):
if ms < 250:
return "fast"
elif ms < 450:
return "ok"
else:
return "slow"
Marking: 2 def line, 2 if/elif/else with colons, 2 indentation as the block structure, 2 returns. ELSE IF → elif is the conversion the question watches.
E2 (8) · A night lamp spec, to a state diagram in words: OFF and ON states; a short press in either state toggles to the other; in ON, 30 minutes with no press transitions to OFF automatically. Name states, and each transition as event [guard] / action.
Section F · Spec repair (12 points, 4 each)
F1 · Repair: "The LED should blink quickly when something goes wrong."
F2 · Repair: "The system shall be easy to use and respond fast, storing results."
F3 · Repair: "The program shall use monotonic() in a while loop to measure the time."
Section G · Classes (18 points)
G1 (8) · From spec to class, on paper. D1: A new Door shall start closed. D2: open() and close() shall set the state accordingly. D3: describe() shall return "open" or "closed" to match the state.
class Door:
def __init__(self):
self.is_open = False # D1
def open(self):
self.is_open = True # D2
def close(self):
self.is_open = False
def describe(self): # D3
if self.is_open:
return "open"
return "closed"
Marking: 2 class and __init__ shape, 2 state created through self in __init__, 2 methods with self, 2 describe returning (not printing) per spec.
G2 (4) · In the call d.open() on the class above, what is self, and rewrite the call in the form that shows it.
self is the instance d; the call is equivalent to Door.open(d). Marking: 2 each. This rewrite is the whole theory of self in one line.G3 (6) · Predict the output and explain the fault:
class Log:
items = []
def add(self, x):
self.items.append(x)
a = Log()
b = Log()
a.add(1)
print(len(b.items))
class Log:
items = []
def add(self, x):
self.items.append(x)
a = Log()
b = Log()
a.add(1)
print(len(b.items))items is assigned in the class body, so it is a class attribute, one list on the blueprint shared by every instance; a's append is visible through b. Fix: create it per instance, def __init__(self): self.items = []. Marking: 2 the output, 2 the shared-attribute diagnosis, 2 the fix.Marking and what your score means
| Section | Points | Weak score sends you to |
|---|---|---|
| A · Tools | 16 | Toolkit review, then the Week 1 to 3 hub of the missed question |
| B · Electricity | 12 | The LED mathematics and Week 4's GPIO page |
| C · Reading code | 12 | Tracebacks and error families |
| D · Trace | 14 | Trace tables, then one trace daily until exam day |
| E · Conversions | 16 | The conventions table and Week 5's notations |
| F · Spec repair | 12 | The five qualities and Week 5's repair shop |
| G · Classes | 18 | The compressed core, then Week 6 in full |
Rough reading: 85+ means polish and rest; 70 to 85 means two targeted sessions on the weakest sections; below 70 means rerun the review pages in order before any second attempt at timed questions. Whatever the number, the section profile is the real result.