Lists Flashcards
A list contains a sequence of elements. Elements in a list can be accessed through an _______.
index.
What is an index?
An integer value used to specify the position of an element in the list.
What is list("abcd")? A. ['a', 'b', 'c', 'd'] B. ['ab'] C. ['cd'] D. ['abcd']
A. [‘a’, ‘b’, ‘c’, ‘d’]
What happens when your program attempts to access a list element with an invalid index?
Causes a run time error (index out of range)
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])
A. print(list1[0])
B. print(list1[:2])
C. print(list1[:-2])
D. print(list1[4:6])
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]
C. [1, 3, 2, 1, 3, 2]
What is the output of the following code?
lst = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
lst[i] = lst[i - 1]
print(lst)
[1, 1, 1, 1, 1, 1]
Suppose list1 = [1, 2, 3], what is the output of the following code? for i in list1: print(i * 2, end = '') A. 2 4 6 B. 246 C. [2, 4, 6] D. 2, 4, 6
B. 246
list1 = [11, 2, 23] and list2 = [2, 11, 23], list1 == list2 is ________
A. True
B. False
B. False
myList = [1, 5, 5, 5, 5, 1] 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
B. 1
What will be displayed by the following code?
myList = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
myList[i - 1] = myList[i]
for i in range(0, 6): 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
C. 2 3 4 5 6 6
Hint: The elements in myList are shifted one space left.
What will be displayed by the following code?
def f(values): values[0] = 44
v = [1, 2, 3] f(v) print(v) A. [1, 44] B. [1, 2, 3, 44] C. [44, 2, 3] D. [1, 2, 3]
C. [44, 2, 3]
What will be displayed by the following code?
def f(value, values): v = 1 values[0] = 44
t = 3 v = [1, 2, 3] f(t, v) print(t, v[0]) A. 1 1 B. 1 44 C. 3 1 D. 3 44
D. 3 44
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
C. [1, 2, 3]
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
C. -(insertion point + 1)