Introduction to Programming Using Python, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.

Chapter 10 Lists


Section 10.2 List Basics
10.1  __________ creates a list.
A. list1 = list()
B. list1 = []
C. list1 = list([12, 4, 4])
D. list1 = [12, 4, 4]
E. list1 = [1, "3", "red"]

10.2  What is list("abcd")?
A. ['a', 'b', 'c', 'd']
B. ['ab']
C. ['cd']
D. ['abcd']

10.3  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is len(list1)?
A. 6
B. 7
C. 8
D. 5
E. 4

10.4  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is max(list1)?
A. 5
B. 4
C. 8
D. 25
E. 1

10.5  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is min(list1)?
A. 5
B. 4
C. 8
D. 25
E. 1

10.6  Suppose list1 is [1, 3, 2], what is sum(list1)?
A. 5
B. 4
C. 6
D. 2
E. 1

10.7  To shuffle list1, use _______.
A. list1.shuffle()
B. shuffle(list1)
C. random.shuffle(list1)
D. random.shuffleList(list1)

10.8  Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], Which of the following is correct?
A. print(list1[0])
B. print(list1[:2])
C. print(list1[:-2])
D. print(list1[4:6])

10.9  Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[-1]?
A. 3
B. 5
C. 1
D. 0

10.10  Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], What is list1[:-1]?
A. 0
B. [1, 3, 2, 4, 5, 2, 1]
C. [1, 3, 2, 4, 5, 2]
D. [1, 3, 2, 4, 5, 2, 1, 0]

10.11  Suppose list1 is [1, 3, 2], What is list1 * 2?
A. [2, 6, 4]
B. [1, 3, 2, 1, 3]
C. [1, 3, 2, 1, 3, 2]
D. [1, 3, 2, 3, 2, 1]

10.12  Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is ________
A. [0, 1, 2, 3]
B. [0, 1, 2, 3, 4]
C. [0.0, 0.5, 1.0, 1.5]
D. [0.0, 0.5, 1.0, 1.5, 2.0]

10.13  list1 = [11, 2, 23] and list2 = [11, 2, 2], list1 < list2 is ________
A. True
B. False

10.14  list1 = [11, 2, 23] and list2 = [2, 11, 23], list1 == list2 is ________
A. True
B. False

10.15  To add 5 to the end of list1, use _______.
A. list1.add(5)
B. list1.append(5)
C. list1.addLast(5)
D. list1.addEnd(5)

10.16  To insert 5 to the third position in list1, use _______.
A. list1.insert(3, 5)
B. list1.insert(2, 5)
C. list1.add(3, 5)
D. list1.append(3, 5)

10.17  To remove string "red" from list1, use _______.
A. list1.remove("red")
B. list1.remove(red)
C. list1.removeAll("red")
D. list1.removeOne("red")

10.18  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.index(5)?
A. 0
B. 4
C. 1
D. 2

10.19  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?
A. 0
B. 4
C. 1
D. 2

10.20  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.sort()?
A. [3, 4, 5, 20, 5, 25, 1, 3]
B. [1, 3, 3, 4, 5, 5, 20, 25]
C. [25, 20, 5, 5, 4, 3, 3, 1]
D. [1, 3, 4, 5, 20, 5, 25, 3]

10.21  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
A. [3, 4, 5, 20, 5, 25, 1, 3]
B. [1, 3, 3, 4, 5, 5, 20, 25]
C. [25, 20, 5, 5, 4, 3, 3, 1]
D. [1, 3, 4, 5, 20, 5, 25, 3]
E. [3, 1, 25, 5, 20, 5, 4, 3]

10.22  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.extend([34, 5])?
A. [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
B. [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
C. [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
D. [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]
E. [3, 1, 25, 5, 20, 5, 4, 3, 34, 5]

10.23  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.pop(1)?
A. [3, 4, 5, 20, 5, 25, 1, 3]
B. [1, 3, 3, 4, 5, 5, 20, 25]
C. [3, 5, 20, 5, 25, 1, 3]
D. [1, 3, 4, 5, 20, 5, 25]
E. [3, 1, 25, 5, 20, 5, 4]

10.24  Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.pop()?
A. [3, 4, 5, 20, 5, 25, 1]
B. [1, 3, 3, 4, 5, 5, 20, 25]
C. [3, 5, 20, 5, 25, 1, 3]
D. [1, 3, 4, 5, 20, 5, 25]
E. [3, 1, 25, 5, 20, 5, 4]

10.25  "Welcome to Python".split() is ________
A. ["Welcome", "to", "Python"]
B. ("Welcome", "to", "Python")
C. {"Welcome", "to", "Python"}
D. "Welcome", "to", "Python"

10.26  What is list("a#b#c#d".split('#')?
A. ['a', 'b', 'c', 'd']
B. ['a b c d']
C. ['a#b#c#d']
D. ['abcd']

10.27  What will be displayed by the following code?

myList = [155551]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
    if myList[i] > max:
        max = myList[i]
        indexOfMax = i

print(indexOfMax)
A. 0
B. 1
C. 2
D. 3
E. 4

10.28  What will be displayed by the following code?

myList = [123456]
for i in range(16):
    myList[i - 1] = myList[i]

for i in range(06): 
    print(myList[i], end = " ")
A. 2 3 4 5 6 1
B. 6 1 2 3 4 5
C. 2 3 4 5 6 6
D. 1 1 2 3 4 5
E. 2 3 4 5 6 1

Section 10.6 Copying Lists
10.29  What will be displayed by the following code?

list1 = [13]
list2 = list1
list1[0] = 4
print(list2)
A. [1, 3]
B. [4, 3]
C. [1, 4]
D. [1, 3, 4]

Sections 10.7-10.8
10.30  What will be displayed by the following code?

def f(values):
    values[0] = 44

v = [123]
f(v)
print(v)
A. [1, 44]
B. [1, 2, 3, 44]
C. [44, 2, 3]
D. [1, 2, 3]

10.31  What will be displayed by the following code?

def f(value, values):
    v = 1
    values[0] = 44

t = 3
v = [123]
f(t, v)
print(t, v[0])
A. 1 1
B. 1 44
C. 3 1
D. 3 44

10.32  What will be displayed by the following code?

def f(i, values = []):
    values.append(i)
    return values

f(1)
f(2)
v = f(3)
print(v)
A. [1] [2] [3]
B. [1] [1, 2] [1, 2, 3]
C. [1, 2, 3]
D. 1 2 3

Section 10.10 Searching Lists
10.33  For the binarySearch function in Section 10.10.2, what is low and high after the first iteration of the while loop when invoking binarySearch([1, 4, 6, 8, 10, 15, 20], 11)?
A. low is 0 and high is 6
B. low is 0 and high is 3
C. low is 3 and high is 6
D. low is 4 and high is 6
E. low is 0 and high is 5

10.34  If a key is not in the list, the binarySearch function returns _________.
A. insertion point
B. insertion point - 1
C. -(insertion point + 1)
D. -insertion point

10.35  If the binary search function returns -4, where should the key be inserted if you wish to insert the key into the list?
A. at index 3
B. at index 4
C. at index 5
D. at index 6

Section 10.11 Sorting Lists
10.36  Use the selectionSort function presented in this section to answer this question. Assume lst is [3.1, 3.1, 2.5, 6.4, 2.1], what is the content of list after the first iteration of the outer loop in the function?
A. 3.1, 3.1, 2.5, 6.4, 2.1
B. 2.5, 3.1, 3.1, 6.4, 2.1
C. 2.1, 2.5, 3.1, 3.1, 6.4
D. 3.1, 3.1, 2.5, 2.1, 6.4
E. 2.1, 3.1, 2.5, 6.4, 3.1

10.37  Use the selectionSort function presented in this section to answer this question. What is list1 after executing the following statements?

list1 = [3.13.12.56.4]
selectionSort(list1)
A. list1 is 3.1, 3.1, 2.5, 6.4
B. list1 is 2.5 3.1, 3.1, 6.4
C. list1 is 6.4, 3.1, 3.1, 2.5
D. list1 is 3.1, 2.5, 3.1, 6.4