-
Which symbol starts a comment in Python?
- //
- #
- /*
- --
Show answer
Answer: B · #
Python ignores everything from # to the end of the line; // and /* start comments in languages such as C and Java.
-
Which data type does Python give to the value 3.0?
- float
- int
- str
- double
Show answer
Answer: A · float
Any number written with a decimal point is a float, even if the decimal part is zero; Python has no separate double type.
-
Which keyword defines a function in Python?
- function
- define
- fun
- def
Show answer
Answer: D · def
A function definition starts with def, followed by the name, the parameters in brackets and a colon.
-
What does print(f"{7 / 2:.2f}") display?
- 3
- 3.5
- 3.50
- 3.500
Show answer
Answer: C · 3.50
7 / 2 is 3.5, and the format .2f always shows exactly two digits after the decimal point.
-
After total = 0 and then for n in range(1, 5): total += n, what is the value of total?
- 15
- 10
- 4
- 5
Show answer
Answer: B · 10
range(1, 5) gives 1, 2, 3 and 4, and 1 + 2 + 3 + 4 = 10; the loop stops before 5.
-
Which statement about tuples is true?
- Tuples can grow with append()
- Tuples cannot contain repeated values
- Tuples are written in square brackets
- The items of a tuple cannot be changed after it is created
Show answer
Answer: D · The items of a tuple cannot be changed after it is created
Tuples are immutable and written in round brackets, and they may contain duplicates; lists are the changeable version.
-
What does len(set([3, 1, 3, 2, 1])) return?
- 5
- 2
- 3
- 1
Show answer
Answer: C · 3
A set keeps only unique values, so the set is {1, 2, 3}, which has 3 items.
-
In a method of a class, what does self refer to?
- The particular object on which the method was called
- The class itself, shared by all objects
- The file in which the class is saved
- The value that the method returns
Show answer
Answer: A · The particular object on which the method was called
When you call s1.introduce(), Python passes s1 as self, so self gives the method access to that object's own attributes.
-
count = 0, then while count < 5: print(count), with nothing else inside the loop. What happens when this runs?
- It prints 0 to 4 and stops
- It prints nothing
- It prints 0 again and again without stopping
- It raises an error at once
Show answer
Answer: C · It prints 0 again and again without stopping
count never changes inside the loop, so count < 5 stays True forever; adding count += 1 inside the loop fixes it.
-
If d = {'a': 1}, which expression gives 0 instead of raising an error for the missing key 'b'?
- d['b']
- d.get('b', 0)
- d.find('b')
- d['b'] or 0
Show answer
Answer: B · d.get('b', 0)
d.get('b', 0) returns the default 0 when the key is missing, while d['b'] raises a KeyError and dictionaries have no find() method.
Want to test yourself? Take the same questions as a timed practice test and see your score. Start the practice test