-
1. What does this Python 3 code print?
print(len([1, [2, 3], 4]))
- A. 4
- B. 3
- C. 2
- D. An error
Answer: B. 3
The list has three elements: 1, the inner list [2, 3], and 4. len counts only top-level elements, so the inner list counts as one.
-
2. What does this Python 3 code print?
s = 'interview'
print(s[1:4])
- A. nte
- B. int
- C. nter
- D. ter
Answer: A. nte
Slicing s[1:4] starts at index 1 and stops before index 4. The characters at indexes 1, 2 and 3 of "interview" are n, t and e.
-
3. What does this Python 3 code print?
word = 'python'
print(word[::-1])
- A. python
- B. pytho
- C. nohtyp
- D. n
Answer: C. nohtyp
A slice with step -1 walks the string from the end to the start, so [::-1] returns the string reversed: "nohtyp".
-
4. What does this Python 3 code print?
print(7 // 2, 7 % 2, 7 / 2)
- A. 3.5 1 3.5
- B. 3 1 3
- C. 4 1 3.5
- D. 3 1 3.5
Answer: D. 3 1 3.5
// is floor division (3), % is the remainder (1), and / is true division, which always returns a float in Python 3 (3.5).
-
5. What does this Python 3 code print?
print(2 ** 3 ** 2)
- A. 64
- B. 36
- C. 512
- D. 12
Answer: C. 512
The power operator is right-associative, so 2 ** 3 ** 2 means 2 ** (3 ** 2) = 2 ** 9 = 512, not (2 ** 3) ** 2 = 64.
-
6. What does this Python 3 code print?
a = [1, 2, 3]
b = a
b.append(4)
print(a)
- A. [1, 2, 3, 4]
- B. [1, 2, 3]
- C. [4]
- D. [1, 2, 3, [4]]
Answer: A. [1, 2, 3, 4]
b = a does not copy the list; both names refer to the same list object. Appending through b changes the one list, so printing a shows [1, 2, 3, 4].
-
7. What does this Python 3 code print?
a = [1, 2, 3]
b = a[:]
b.append(4)
print(len(a))
- A. 4
- B. 3
- C. 1
- D. An error
Answer: B. 3
a[:] creates a new (shallow) copy of the list. Appending to the copy b does not affect a, which still has 3 elements.
-
8. What does this Python 3 code print?
def add(x, items=[]):
items.append(x)
return items
add(1)
print(add(2))
- A. [2]
- B. [1]
- C. [1, 2]
- D. [2, 1]
Answer: C. [1, 2]
A default argument is created once, when the function is defined, not on each call. The same list is reused, so the first call leaves [1] in it and the second call returns [1, 2]. Use items=None and create the list inside instead.
-
9. What does this Python 3 code print?
print([x * x for x in range(5) if x % 2 == 0])
- A. [0, 1, 4, 9, 16]
- B. [1, 9]
- C. [0, 4, 16]
- D. [4, 16]
Answer: C. [0, 4, 16]
range(5) gives 0 to 4. The condition keeps even numbers 0, 2 and 4, and each is squared: [0, 4, 16].
-
10. What does this Python 3 code print?
d = {'a': 1, 'b': 2}
d['c'] = 3
print(list(d.keys()))
- A. ['a', 'b', 'c']
- B. ['c', 'a', 'b']
- C. ['a', 'b']
- D. [1, 2, 3]
Answer: A. ['a', 'b', 'c']
Assigning to a new key adds it to the dictionary. Since Python 3.7, dictionaries keep insertion order, so the keys come out as a, b, c.
-
11. What does this Python 3 code print?
d = {'a': 1, 'b': 2}
print(d.get('z', 0))
- A. None
- B. KeyError
- C. z
- D. 0
Answer: D. 0
get returns the value for the key if it exists, otherwise the default given as the second argument. "z" is missing, so it returns 0 instead of raising KeyError.
-
12. What happens when this Python 3 code runs?
t = (1, 2, 3)
t[0] = 9
print(t)
- A. It prints (9, 2, 3)
- B. It prints (1, 2, 3)
- C. It raises IndexError
- D. It raises TypeError
Answer: D. It raises TypeError
Tuples are immutable, so assigning to t[0] raises TypeError ("tuple object does not support item assignment") before print runs.
-
13. What does this Python 3 code print?
print(bool([]), bool('0'))
- A. False False
- B. True True
- C. True False
- D. False True
Answer: D. False True
An empty list is falsy, so bool([]) is False. The string "0" is not empty, and any non-empty string is truthy, so bool("0") is True.
-
14. What does this Python 3 code print (each value on its own line)?
try:
x = int('5')
except ValueError:
print('A')
else:
print('B')
finally:
print('C')
- A. A, then C
- B. B, then C
- C. C only
- D. A, then B, then C
Answer: B. B, then C
int("5") succeeds, so the except block is skipped. The else block runs only when no exception occurred, printing B. The finally block always runs, printing C.
-
15. Which exception does this Python 3 code raise?
print(int('abc'))
- A. TypeError
- B. ValueError
- C. 0
- D. NameError
Answer: B. ValueError
int() receives a string, which is the right type, but its content is not a valid number. A value of the right type with a bad value raises ValueError.
-
16. What does this Python 3 code print?
print(len({1, 2, 2, 3, 3, 3}))
- A. 6
- B. 4
- C. 3
- D. 1
Answer: C. 3
A set stores each value only once. The duplicates collapse, leaving {1, 2, 3}, so the length is 3.
-
17. What does this Python 3 code print?
print(sorted(['banana', 'Apple', 'cherry']))
- A. ['Apple', 'banana', 'cherry']
- B. ['banana', 'cherry', 'Apple']
- C. ['cherry', 'banana', 'Apple']
- D. ['Apple', 'cherry', 'banana']
Answer: A. ['Apple', 'banana', 'cherry']
Strings sort by character code. Uppercase letters come before lowercase ones, so "Apple" is first, then "banana" and "cherry". Here that also matches alphabetical order.
-
18. What does this Python 3 code print?
x = [1, 2, 3, 4, 5]
print(x[-2:])
- A. [5]
- B. [1, 2]
- C. [4]
- D. [4, 5]
Answer: D. [4, 5]
Negative indexes count from the end: -2 is the second-last element. x[-2:] takes from there to the end, giving [4, 5].
-
19. What does this Python 3 code print?
print({k: v for k, v in zip('abc', range(3))})
- A. {'a': 0, 'b': 1, 'c': 2}
- B. {'a': 1, 'b': 2, 'c': 3}
- C. {0: 'a', 1: 'b', 2: 'c'}
- D. [('a', 0), ('b', 1), ('c', 2)]
Answer: A. {'a': 0, 'b': 1, 'c': 2}
zip pairs the characters of "abc" with 0, 1 and 2. The dictionary comprehension turns each pair into key: value, giving {"a": 0, "b": 1, "c": 2}.
-
20. What does this Python 3 code print?
print(0.1 + 0.2 == 0.3)
- A. True
- B. None
- C. 0.3
- D. False
Answer: D. False
Floats are stored in binary, and 0.1 and 0.2 cannot be represented exactly. Their sum is 0.30000000000000004, which is not equal to 0.3. Use math.isclose for such comparisons.
-
21. What does this Python 3 code print?
a = [1]
b = [1]
print(a == b, a is b)
- A. True True
- B. True False
- C. False False
- D. False True
Answer: B. True False
== compares values, and both lists contain [1], so it is True. "is" checks whether both names point to the same object; a and b are two separate lists, so it is False.
-
22. What does this Python 3 code print?
fs = [lambda: i for i in range(3)]
print([f() for f in fs])
- A. [0, 1, 2]
- B. [0, 0, 0]
- C. [2, 2, 2]
- D. [1, 2, 3]
Answer: C. [2, 2, 2]
Each lambda looks up i when it is called, not when it is created. By the time the functions are called, the loop has finished and i is 2, so all three return 2. Writing lambda i=i: i would capture each value.
-
23. What does this Python 3 code print?
print(list(map(lambda n: n * 2, filter(lambda n: n > 1, [1, 2, 3]))))
- A. [2, 4, 6]
- B. [4, 6]
- C. [2, 3]
- D. [4]
Answer: B. [4, 6]
filter keeps the numbers greater than 1, which are 2 and 3. map then doubles each of them, giving [4, 6].
-
24. What does this Python 3 code print?
grid = [[0] * 2] * 2
grid[0][0] = 1
print(grid)
- A. [[1, 0], [0, 0]]
- B. [[1, 0], [1, 0]]
- C. [[1, 1], [0, 0]]
- D. [[1, 1], [1, 1]]
Answer: B. [[1, 0], [1, 0]]
Multiplying the outer list copies the reference, not the inner list, so both rows are the same list object. Changing grid[0][0] shows up in both rows. Use [[0] * 2 for _ in range(2)] to get independent rows.
-
25. What does this Python 3 code print?
g = (x for x in range(3))
print(sum(g), sum(g))
- A. 3 0
- B. 3 3
- C. 0 3
- D. 3 None
Answer: A. 3 0
A generator can be consumed only once. The first sum reads 0, 1 and 2 and returns 3. The generator is then exhausted, so the second sum gets no values and returns 0.