This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 1: Introduction

Quiz

face Josiah Wang Joe Stacey

Let us have a quick quiz to check whether you actually remembered and understood what you have learnt!

This quiz is ungraded, so feel free to make as many mistakes as you like - that is how you learn!

Type “error” if you are expecting an invalid output

Print out the type and value of the variable a. Write “error” if you are expecting an error.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Question 1

What is the type and value of a?

a = 10 / 5

float
2.0
Explanation:

Remember that the division operator results in a float.

Question 2

What is the type and value of a?

a = 13 // 5

int
2
Explanation:

Remember that a floor division operation results in an int.

Question 3

What is the type and value of a?

a = 13 % 5

int
3
Explanation:

Remember that a modulo operation results in an int.

Question 4

What is the type and value of a?

a = 4 ** 2.5

float
32.0
Explanation:

The resulting type of an exponentiation depends on the operand. If either of the operand is a float, then the resulting type is also a float.

Question 5

What is the type and value of a?

a = 3 + 4 * 2

int
11
Explanation:

Remember operator precedence! Multiplication goes first!

Question 6

What is the type and value of a? If it is a string, remember to quote your strings in your answer.

a = "python" + '''fun'''

str
'pythonfun'
Explanation:

Both operands are strings, whatever the type of quotes used.

Question 7

What is the type and value of a? If it is a string, remember to quote your strings in your answer.

a = "Welcome to London " * 4

str
'Welcome to London Welcome to London Welcome to London Welcome to London '
Explanation:

String multiplication. Note that there should be a space at the end of the string to be exactly correct, i.e. 'Welcome to London Welcome to London Welcome to London Welcome to London '.

Question 8

What is the type and value of a? If it is a string, remember to quote your strings in your answer.

a = "My age is " + 20

Error
Error
Explanation:

You cannot concatenate a str to an int. The correct way to do this is either to explicitly convert the int to a str (a = "My age is " + str(20)), or even better use Python f-strings (a = f"My age is {20}").

Question 9

What is the type and value of a? If it is a string, remember to quote your strings in your answer.

a = true

Error
Error
Explanation:

The literal True is case sensitive. Otherwise, true is just a variable. Python returns a NameError: name 'true' is not defined because you have not defined the variable true!

Question 10

What is the type and value of a? If it is a string, remember to quote your strings in your answer.

a = False

bool
False

Question 11

What is the output of the following piece of Python code? Write Error if it results in an error.

a = 3
b = a
a = 4
print(f"The value of b is {b}")

The value of b is 3
Explanation:

The variable b points to the int object with value 3, no matter what a points to after that.

Question 12

What is the output of the following piece of Python code? Write Error if it results in an error.

x = x + 8
print(x)

Error
Explanation:

How can you add x + 8 when you do not even know what x is? What do you think Python's error message will look like? (Test it out yourself!)

Question 13

What is the output of the following piece of Python code? Write Error if it results in an error.

if = 2
print(if)

Error
Explanation:

The identifier if is a Python reserved word, so you cannot use it as a variable name. It does not make semantic sense anyway - would you typically name someone as if?

Question 14

What is the output of the following piece of Python code? Write Error if it results in an error.

float = 4
print(type(float))

<class 'int'>
Explanation:

It is fine if you simply entered int, I am providing you the exact answer returned by Python.

The data type float is not a reserved word, so you can technically use it as a variable name. Please do not do this though -- it is confusing and hard to read!