Chapter 1: Introduction

Quiz

face Josiah Wang

Let’s get started with the lesson! You are probably expecting this – here’s a quiz to check whether you remember everything that we have covered so far!

1 2 3 4 5 6 7 8 9

Question 1

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

1
2
3
x = [3, 4]
y = x
print(x is y)

True
Explanation:

The variable y refers to the same object as x is pointing to. So x is y is True.

Question 2

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

1
2
3
x = [3, 4]
y = [3, 4]
print(x is y)

False
Explanation:

Remember that just because two objects are equal does not necessarily mean that they are the same object! Note that if you run your code as a script, Python can 'go through' your whole code before compiling and optimise the memory allocation of objects. For example if you run x=9000, y=9000, x is y as a script, it actually produces True since Python already pre-allocates 9000 to the same memory space. Confusingly, this will actually produce False if you run it interactively since Python cannot "look ahead" to optimise the code.

Question 3

What is the value of existing_numbers and new_numbers after executing the following piece of code? Type Error if you are expecting an error.

1
2
3
existing_numbers = [1, 3]
new_numbers = [2, 4]
existing_numbers.extend(new_numbers)

[1, 3, 2, 4]
[2, 4]
Explanation:

Line 3 appends each element in new_numbers to the end of existing_numbers. new_numbers itself is not modified.

Question 4

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

1
2
3
4
5
animals = {"birds": ["swallow", "chicken", "seagull"],
           "mammals": ["cow", "sheep", "whale"],
           "amphibians": ["frog", "salamander"]
}
print(animals["mammals"][2])

whale
Explanation:

The value of animals["mammals"] is the list ["cow", "sheep", "whale"]. So animals["mammals"][2] gives you "whale".

Question 5

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

1
2
3
4
start_point = {"x": 2, "y": 4}
end_point = {"x": 5, "y": 3}
line = (start_point, end_point)
print(line[1]["x"])

5
Explanation:

The value of line[1] is the dict to which end_point refers, i.e. {"x": 5, "y": 3}. So line[1]["x"] gives you the x-coordinate of the line's end point, that is 5.

Question 6

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

1
2
3
4
words = ["singing", "laughing", "soaring", "saw", "sCaLing"]
for word in words:
    if word.startswith("s") and word.endswith("g") and word.islower():
        print(word.upper())

SINGING SOARING
Explanation:

This piece of code checks the list of words, and prints out the uppercase version of the word if it starts with s, and ends with g, and is all lowercase.

Question 7

What is the output after executing the following piece of code? Type Error if you are expecting an error or an infinite recursion.

1
2
3
4
def triangle_num(n):
    return n + triangle_num(n-1)

print(triangle_num(3))

Error
Explanation:

This function results in an infinite recursion. Remember that recursive functions should always define at least one base case, in addition to recursive/inductive case(s).

Question 8

What is the output after executing the following piece of code? Type Error if you are expecting an error or an infinite recursion.

1
2
3
4
5
6
7
8
9
def triangle_num(n):
    if n < 1:
        return None    
    elif n == 1:
        return 1
    else:
        return n + triangle_num(n-1)

print(triangle_num(3))

6
Explanation:

This function computes the triangle number in a triangle number sequence recursively. It adds up the numbers from 1 to n.

Question 9

What is the output after executing the following piece of code?

1
2
3
4
5
6
7
8
try:    
    index = "python".index("s")
except ValueError:
    print("no")
else:
    print("yes")
finally:
    print("done")

no done
Explanation:

Line 2 will cause a ValueError to be raised (because "s" cannot be found in the string "python"). The error will be caught by the except clause in Lines 3-4. Finally, the finally clause in Lines 7-8 will be executed, since this will run whether or not an error occurs.