Computer Science Practice Questions

6 questions. Work each one out before revealing the answer.

Question 1

Computer Science · Python · Easy

What does print(7 // 2) display in Python?

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.

Question 2

Computer Science · Python · Easy

What does print(7 % 2) display in Python?

Show answer and explanation

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.

Question 3

Computer Science · Data types · Easy

What is the data type of the value "42" in Python (with quotation marks)?

Show answer and explanation

Answer: String

Anything in quotation marks is a string, even if it contains digits.

Common misconception: Treating it as a number.

Question 4

Computer Science · Loops · Medium

How many lines does this print? for i in range(4): print(i)

Show answer and explanation

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.

Question 5

Computer Science · Algorithms · Medium

What must be true of a list before binary search can be used?

Show answer and explanation

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.

Question 6

Computer Science · Loops · Medium

What value does total hold after this code runs? total = 0; for n in range(1, 5): total = total + n

Show answer and explanation

Answer: 10

range(1, 5) gives 1, 2, 3, 4 and their sum is 10.

Common misconception: Including 5 in the range.

Study guides