Purpose: Test your understanding of variables, types, I/O, selection, iteration, and program tracing. Instant feedback on every question. All code samples are Python 3.
Score: 0 / 12
Question 1 — Type conversion
What is the output of:
x = "5"
y = 2
print(int(x) + y)
Solution:
int("5") converts the string "5" to the integer 5. Then 5 + 2 = 7. Output: 7. Without int(), you would get a TypeError because "5" + 2 mixes incompatible types.
Question 2 — Operator precedence
What does this expression evaluate to: 2 + 3 * 4 ** 2 - 1?
x = 75
if x >= 90:
print("A")
elif x >= 80:
print("B")
elif x >= 70:
print("C")
else:
print("F")
Solution:
75 fails >=90 and >=80 but passes >=70. So we print "C" and exit the chain (elif/else branches are mutually exclusive).
Question 4 — Loop trace
What is the value of total after this loop?
total = 0
for i in range(1, 6):
total += i * 2
print(total)
Solution:
range(1,6) → 1,2,3,4,5. Each iteration adds i*2: 2+4+6+8+10 = 30.
Question 5 — Boolean logic
What is the value of: (5 > 3) and not (10 < 5)?
Solution:
5>3 → True. 10<5 → False. not False → True. True and True → True.
Question 6 — Identify the bug
This code is supposed to print numbers 1 through 10. What's wrong?
i = 1
while i < 10:
print(i)
i += 1
Solution:
The condition i < 10 stops at i=9. The fix: change to i <= 10. This is a classic off-by-one error.
Question 7 — Write a small function
Write a Python function is_even(n) that returns True if n is even, otherwise False. Type your one-line function body (everything after the colon).
Solution:
return n % 2 == 0. The modulo operator % gives the remainder; an even number divided by 2 has remainder 0.
Question 8 — Predict output
What does this print?
a, b = 3, 7
a, b = b, a
print(a, b)
Solution:
Tuple unpacking. The right side (b, a) evaluates to (7, 3), then is unpacked to a and b. Output: 7 3. This is a swap without a temporary variable.
Question 9 — Predict nested-loop output
How many "*" characters are printed?
for i in range(3):
for j in range(4):
print("*", end="")
Solution:
Outer runs 3 times; inner runs 4 times each iteration: 3 × 4 = 12 stars.
Question 10 — Modulo and integer division
What does the following print?
x = 17
print(x // 5, x % 5)
Solution:
// is integer division: 17 // 5 = 3. % is remainder: 17 % 5 = 2. Output: 3 2.
Question 11 — Complete the pseudocode
Convert this pseudocode to Python (fill in the missing line). The function returns the maximum of two numbers.
def maximum(a, b):
if a >= b:
return a
____________________
Solution:
return b (the else is implicit because the first return exits the function). Some teachers prefer the explicit else: return b.
Question 12 — Identify a runtime error
Which of the following lines will raise a runtime error in Python 3?
Solution:
int("hello") raises ValueError because "hello" is not a parseable integer. "3" + "4" concatenates to "34" (no error). 10/2 = 5.0 (no error). bool(0) = False (no error).