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

Now, let’s start with a quiz as usual! Let’s see you much your brain has really absorbed over the past five lessons!

Making a mistake in this quiz is good - you will know if you have misunderstood or forgotten anything. Learn from it!

1 2 3 4 5 6 7 8 9

Question 1

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Remember to include the quotes to a string if the output is a string.

x = 2
y = 5
y //= x

2
Explanation:

Just a refresher for operators! y //= x would be y = y // x, so the floor (integer part of the division) for 5 // 2 would be 2.

Question 2

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Remember to include the quotes to a string if the output is a string.

x = 2
y += x

Error
Explanation:

Remember that y has not yet been defined, so you cannot add x to something that is not defined!

Question 3

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Remember to include the quotes to a string if the output is a string.

y = int("11", base=2)

3
Explanation:

You can create an integer (base 10) from a non-decimal string representation by providing an optional argument to int() to specify the base of a string representation. In this case, "11" in base 2 is equal to 3 in base 10.

Question 4

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Remember to include the quotes to a string if the output is a string.

import math
y = math.isinf(-float("inf"))

True
Explanation:

The function isinf() from the math module checks whether a number is infinity. This function apparently also returns True for negative infinity! Also note that you can represent infinity with math.inf, which is equivalent to float("inf").

Question 5

What is the output of the following code snippet? Type Error if you are expecting an error. Type Infinite if you are expecting an infinite loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
counter = 1
number = 0
while counter < 5:
    if counter == 1:
        pass
    elif counter % 4 == 0:
        break
    else:  
        number += counter
    counter += 1
print(number)
5
Explanation:

The program adds 2, and 3, resulting in 5. It skips counter == 1 (Lines 4-5), and breaks out of the loop when counter is divisible by 4 (Lines 6-7) - this first occurs when counter == 4.

Question 6

What is the output of the following code snippet? Type Error if you are expecting an error. Type Infinite if you are expecting an infinite loop.

1
2
3
4
5
6
7
8
9
def is_between(x, a, b):
    return a < x < b

lower_bound = 2
upper_bound = 5
x = 1
while x < 7:
    print(is_between(x, lower_bound, upper_bound))
    x += 1
False False True True False False
Explanation:

For each integer between 1 to 6 (inclusive), check whether it is between 2 and 5 (exclusive). Only the numbers 3 and 4 fulfil this criteria.

Question 7

What is the output of the following code snippet? Type Error if you are expecting an error. Type Infinite if you are expecting an infinite loop.

1
2
3
4
5
6
7
def is_sum_even(x, y):
    (x+y) % 2 == 0

number = 0
while number < 4:
    print(is_sum_even(number, number + 1))
    number += 1
None None None None
Explanation:

The function is_sum_even() does not return anything, so it returns None by default, whatever the input!

Question 8

What is the output of the following code snippet? Type Error if you are expecting an error.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def increment(x, step=1):
    return x + step

def decrement(x, step=1):
    return x - step

def delta(x, y, z=1, margin=0):
    return (x-y-margin) / z

a = 3
b = 5
result = delta(decrement(increment(b), 3), increment(a, 2), margin=1)
print(result)
-3.0
Explanation:

If you entered -3, remember that division results in a float in Python!

Otherwise, you can observe a nested set of compositions in this piece of code in Line 12. You might find it quite hard to read. If you cannot work it out, try a higher level of abstraction! Store the return values of the function calls as variables in separate lines, and use them as input arguments to the enclosing function.

Also remember how default arguments and keyword arguments work!

Question 9

What is the output of the following code snippet? Type Error if you are expecting an error.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def do_first(x):
    print(x)
    return do_second(x+3) + 1

def do_second(x):
    print(x)
    x -= 2
    return do_third(x)

def do_third(x):
    print(x)
    x *= 2
    return x

x = 5
x = do_first(x - 1)
print(x)
4 7 5 11
Explanation:

This question tests your understanding about function scopes in Python, and also makes sure that you understand how to call functions from inside another function definition. All the x in this code are local to each function, so Lines 2, 6, 11 will print whatever value is passed as an argument to their respective parameter x. If you have trouble understanding how the value is returned in Lines 3 and 8, try to break the lines down into multiple lines, e.g.

y = do_second(x+3)
z = y + 1
return z