QA 76.73 · The Examination Desk — tests, typeset properly
Examination — Programming I: First Programs in Python
SERIAL FG-QA76.73-—
Answers are marked only when you deliver the paper — no nudges mid-exam. Declare your confidence on each answer; a sure miss earns an errata slip worth reading twice. Pass mark: 80%. Nothing here punishes a retake.
Part the First — Values, Names, and the Machine
1.A Python file holds three statements on lines 1, 2, and 3 — no loops, no functions, no input. In what order does the machine carry them out?
2.Match each value to the type Python gives it.
3.In Python 3, what type is the value produced by 7 / 2?
4.After these three lines run, what is the value of y? y gets the value x holds at that moment. x = 5 y = x x = 10
5.This line runs and the user types 20 then presses Enter: age = input("Age? ") What does age hold?
Part the Second — Expressions and Decisions
6.Evaluate 2 + 3 * 4.
7.What is 17 // 5?
8.With x = 5, what is the value of x > 0 and x < 3?
9.What is the value of not (3 > 4)?
10.What does this print when score = 75? if score >= 90: print("A") elif score >= 70: print("B") else: print("C")
Part the Third — Repetition and Tracing
11.What does this print? n = 1 total = 0 while n <= 4: total = total + n n = n + 1 print(total)
12.This loop never stops. Which change makes it certain to end? n = 10 while n > 0: print(n)
13.How many times does the body run? for i in range(2, 8): print(i)
14.What is the last number printed? for i in range(0, 10, 2): print(i)
15.You want to print the whole numbers 1 through 5, including 5. Which loop header is right?
16.What does this print? total = 0 for i in range(1, 6): if i % 2 == 0: total = total + i print(total)
Part the Fourth — Functions, Collections, and Everyday Algorithms
17.Given this definition, what does the bare line greet (with no parentheses) do? def greet(): print("hi") greet
18.This function should return the area of a rectangle. Fill in the missing expression in terms of w and h. def area(w, h): return ____
19.What happens when this runs? def f(): n = 3 f() print(n)
20.Given nums = [10, 20, 30, 40, 50], what does nums[1:3] give?
21.In one sentence, say what "cat"[-1] evaluates to and why.
22.What does this print? nums = [4, 9, 2, 7] best = nums[0] for x in nums: if x > best: best = x print(best)
23.Arrange these lines into a working program that counts how many numbers in nums are negative, then prints the count.
- for x in nums:
- count = count + 1
- count = 0
- print(count)
- if x < 0:
24.Running print(10 / 0) stops the program. What does Python report?