Introduction to Programming (Python)
Level 2 — Recall & Standard Problems
Time: 30 minutes Total Marks: 40
Answer all questions. Assume Python 3. Where output is asked, write exactly what Python prints.
Q1. Define the following in one line each, and give the Python literal for a value of that type: (a) int, (b) str, (c) bool, (d) NoneType. (4 marks)
Q2. Predict the output of each expression: (6 marks)
- (a)
17 // 5 - (b)
17 % 5 - (c)
2 ** 5 - (d)
7 / 2 - (e)
-7 // 2 - (f)
10 % 3
Q3. State whether each is a valid Python variable name. If invalid, give the reason. (4 marks)
- (a)
2nd_value - (b)
_total - (c)
my-name - (d)
class
Q4. Given s = "Programming", write the value produced by: (5 marks)
- (a)
s[0] - (b)
s[-1] - (c)
s[0:4] - (d)
s[::2] - (e)
len(s)
Q5. Evaluate these logical/comparison expressions to True or False: (4 marks)
- (a)
5 > 3 and 2 == 2 - (b)
not (4 < 2) - (c)
3 != 3 or 1 < 0 - (d)
True and not False
Q6. Write the output: (4 marks)
nums = [4, 1, 3, 1, 2]
nums.append(5)
nums.sort()
print(nums)
print(nums.count(1))Q7. Write a for loop using range() that prints the sum of all integers from 1 to 100 (inclusive). Then state the numeric value of that sum. (4 marks)
Q8. Write the output of this f-string program: (3 marks)
name = "Ada"
age = 36
print(f"{name} is {age} years old, next year {age + 1}")Q9. Write a recursive Python function fact(n) that returns n!. Clearly mark the base case and recursive case. State the value of fact(5). (4 marks)
Q10. Given d = {"a": 1, "b": 2}, write what each returns: (2 marks)
- (a)
d.get("c", 0) - (b)
list(d.keys())
Answer keyMark scheme & solutions
Q1. (4 marks) — 1 mark each (definition + valid literal)
- (a)
int: whole number without decimal — e.g.7. - (b)
str: ordered sequence of characters (text) — e.g."hi". - (c)
bool: truth value,True/False— e.g.True. - (d)
NoneType: the type of the absence-of-value object — literalNone. Why: recall of core built-in types (1.2.4).
Q2. (6 marks) — 1 each
- (a)
3(floor division) - (b)
2(remainder) - (c)
32() - (d)
3.5(true division always float) - (e)
-4(floor division rounds toward −∞) - (f)
1Why://floors toward negative infinity, so-7//2 = floor(-3.5) = -4(1.2.6).
Q3. (4 marks) — 1 each
- (a) Invalid — cannot start with a digit.
- (b) Valid — leading underscore allowed.
- (c) Invalid — hyphen not allowed (parsed as subtraction).
- (d) Invalid —
classis a reserved keyword. Why: naming rules — letters/digits/underscore, not starting with digit, no keywords (1.2.3).
Q4. (5 marks) — 1 each. s = "Programming" (indices 0–10)
- (a)
'P' - (b)
'g' - (c)
'Prog'(indices 0,1,2,3) - (d)
'Pormig'(every 2nd char: P,o,r,m,i,g) - (e)
11Why: slicing stop is exclusive; step 2 takes indices 0,2,4,6,8,10 (1.2.11).
Q5. (4 marks) — 1 each
- (a)
True - (b)
True(4<2 is False, not False = True) - (c)
False(both operands False) - (d)
TrueWhy: precedence not > and > or; short-circuit rules (1.2.7, 1.2.8).
Q6. (4 marks)
- After append:
[4,1,3,1,2,5]; after sort:[1,1,2,3,4,5]. - Output line 1:
[1, 1, 2, 3, 4, 5](2 marks) - Output line 2:
2(count of 1) (2 marks) Why:sort()mutates in place ascending;counttallies occurrences (1.2.22).
Q7. (4 marks)
total = 0
for i in range(1, 101): # 1 to 100 inclusive
total += i
print(total)- Correct loop with
range(1,101): 2 marks - Correct accumulation/print: 1 mark
- Value
5050: 1 mark Why: (1.2.19).
Q8. (3 marks)
Output: Ada is 36 years old, next year 37
- Correct substitution of name/age: 2 marks; correct
age+1=37: 1 mark. Why: f-strings evaluate embedded expressions (1.2.13).
Q9. (4 marks)
def fact(n):
if n == 0 or n == 1: # base case
return 1
return n * fact(n - 1) # recursive case- Base case marked: 1 mark
- Recursive case correct: 2 marks
fact(5) = 120: 1 mark Why: ; base case stops recursion (1.2.38).
Q10. (2 marks) — 1 each
- (a)
0(key absent → default returned) - (b)
['a', 'b']Why:getreturns default when key missing;keys()preserves insertion order (1.2.24).
[
{"claim":"17//5=3 and 17%5=2 and 2**5=32", "code":"result = (17//5==3) and (17%5==2) and (2**5==32)"},
{"claim":"-7//2 == -4", "code":"result = (-7//2 == -4)"},
{"claim":"sum 1..100 = 5050", "code":"result = (sum(range(1,101))==5050)"},
{"claim":"fact(5)=120", "code":"from sympy import factorial\nresult = (factorial(5)==120)"},
{"claim":"slice 'Programming'[::2]=='Pormig'", "code":"result = ('Programming'[::2]=='Pormig')"}
]