Question 1
What does print(7 // 2) display in Python?
- A. 3
- B. 3.5
- C. 4
- D. 1
Show answer and explanation
Answer: 3
The // operator gives the whole-number part of the division.
Common misconception: Expecting 3.5, which is the result of 7 / 2.
6 questions. Work each one out before revealing the answer.
What does print(7 // 2) display in Python?
Answer: 3
The // operator gives the whole-number part of the division.
Common misconception: Expecting 3.5, which is the result of 7 / 2.
What does print(7 % 2) display in Python?
Answer: 1
The % operator gives the remainder, and 7 divided by 2 leaves a remainder of 1.
Common misconception: Confusing the remainder operator with percentages.
What is the data type of the value "42" in Python (with quotation marks)?
Answer: String
Anything in quotation marks is a string, even if it contains digits.
Common misconception: Treating it as a number.
How many lines does this print? for i in range(4): print(i)
Answer: 4
range(4) produces 0, 1, 2 and 3, so there are four lines.
Common misconception: Counting from 1 or including the stop value.
What must be true of a list before binary search can be used?
Answer: It must be sorted.
Binary search discards half the list at each step by comparing with the middle item, which only works if the list is in order.
Common misconception: Using binary search on unsorted data.
What value does total hold after this code runs? total = 0; for n in range(1, 5): total = total + n
Answer: 10
range(1, 5) gives 1, 2, 3, 4 and their sum is 10.
Common misconception: Including 5 in the range.