Python List Methods Quiz

Quiz 9 11 questions

Prerequisites for the quiz

  1. Python List Methods

Are you ready?

11 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the following code log?
l = [1, 2, 3]
print(l.index(4))
The element 4 doesn't exist in the list l here, likewise the index() method throws an error. This goes with choice (C). For more details, refer to Python List Methods — index().
What does the following code log?
l = [1, 0]
print(l.index(0))
0 exists at index 1 in the list l, therefore l.index(0) returns 1. This goes with choice (B). For more details, refer to Python List Methods — index().
What does the following code log?
l = [1, 2, 3]
l.append()
print(l)
Calling the append() method without an argument is invalid, likewise the code above throws an error. For more details, refer to Python List Methods — append().
What does the following code log?
l = [1, 2, 3]
print(l.append(4))
append() adds a given value to the end of the list it's called on and returns None. Here, we print the expression l.append(4), likewise we merely get None printed. This goes with choice (A). For more details, refer to Python List Methods — append().
Do the following two snippets do the same thing?

Snippet 1:

l = [1, 2, 3]
l.insert(len(l), 4)

Snippet 2:

l = [1, 2, 3]
l.append(4)
l.insert(len(l), 4) adds 4 to the end of l (since len(l) evaluates to 3, which is the next index after the last index of l i.e 2). Similarly, l.append(4) adds 4 to the end of l, as well. This means that yes, both the snippet do the same thing. For more details, refer to Python List Methods — insert().
For a list l, the expression l.extend([10]) mutates l. True or false?
The extend() method modifies the list it's called on, in place. In other words, it mutates the list. For more details, refer to Python List Methods — extend().
What does the following code print?
l = [1, 2, 3]
l.extend(4)
print(l)
Calling extend() with a non-iterable argument (one that is not a sequence) is invalid. Here, we call extend() with an integer which is a non-iterable value, and likewise get an error thrown. This goes with choice (C). For more details, refer to Python List Methods — extend().
For a given list l, what does l.pop(0) do?
The pop(0) call on a list l pops (removes) the element at index 0 (which is the first element) from l and then returns it. Hence, the correct choice is (B). For more details, refer to Python List Methods — pop().
For a given list l, what does l.remove(0) do?
The remove(0) call on a list l looks for the first 0 in l, and if it finds one, removes it and returns None. This goes with choice (C). For more details, refer to Python List Methods — remove().
What does the following code print?
l = [1, 2, 3]
print(l.count(int))
The count() method takes in an argument and returns back the number of its occurences in the calling list. In this case, since there is no int class reference in the list l, l.count(int) returns 0. Hence, the correct choice is (B). For more details, refer to Python List Methods — count().
What does the following code print?
a = [2, 3, 1]
b = [1, 3, 2]

print(a.reverse() == b)
a.reverse() returns None after reverse-ordering the list a. This means that a.reverse() == b converts to None == b which evaluates to False. Hence, False is printed to the shell, and this goes with choice (B). For more details, refer to Python List Methods — reverse().