Multiple choice questions about Lists in Python

May 13, 2014 • Genevieve Smith

I made use of Christian Jacobs’ concept map.

Question 1. Suppose we create and edit the following three lists (of cities, fruits, and lottery numbers), then we build a list of those lists, using the code below:

cities = [‘London’, ‘Montreal’, ‘Jakarta’]
cities.sort()
fruits = [‘papaya’,’lime’,’banana’,’kiwi’]
fruits.pop()
fruits.append(‘apple’)
fruits[2] = ‘orange’
lottos = [23, 17, 7, 15, 31]
lottos.reverse()
list_of_lists = [cities, lottos, fruits]

Question 1. What will be returned by the following?

list_of_lists

A: [[‘Jakarta’, ‘London’, ‘Montreal’], [31, 15, 7, 17, 23], [‘papaya’, ‘lime’, ‘banana’, ‘apple’]]
B: [[‘Jakarta’, ‘London’, ‘Montreal’], [31, 15, 7, 17, 23], [‘papaya’, ‘lime’, ‘orange’, ‘apple’]]
C: [[‘Jakarta’, ‘London’, ‘Montreal’], [23, 17, 7, 15, 31], [‘papaya’, ‘lime’, ‘orange’, ‘apple’]]
D: [[‘Jakarta’, ‘London’, ‘Montreal’], [31, 15, 7, 17, 23], [‘papaya’, ‘lime’, ‘orange’, ‘kiwi’, ‘apple’]]

Question 2. How would we get python to return just the city ‘Montreal’ from our original cities list and from the list_of_lists we built?

A: »>cities[2]   &  »>list_of_lists[0][2]
B: »>cities[2]   &  »>list_of_lists[1][3]
C: »>cities[1]   &  »>list_of_lists[0][1]
D: »>cities[1]   &  »>list_of_lists[0][2]