Lists Flashcards

1
Q

A list contains a sequence of elements. Elements in a list can be accessed through an _______.

A

index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an index?

A

An integer value used to specify the position of an element in the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What is list("abcd")?
 A. ['a', 'b', 'c', 'd']
 B. ['ab']
 C. ['cd']
 D. ['abcd']
A

A. [‘a’, ‘b’, ‘c’, ‘d’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when your program attempts to access a list element with an invalid index?

A

Causes a run time error (index out of range)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
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

A. print(list1[0])
B. print(list1[:2])
C. print(list1[:-2])
D. print(list1[4:6])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
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]
A

C. [1, 3, 2, 1, 3, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A

[1, 1, 1, 1, 1, 1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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
A

B. 246

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

B. False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
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
A

B. 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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
A

C. 2 3 4 5 6 6

Hint: The elements in myList are shifted one space left.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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]
A

C. [44, 2, 3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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
A

D. 3 44

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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
A

C. [1, 2, 3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
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
A

C. -(insertion point + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
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
A

A. at index 3

17
Q

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

list1 = [3.1, 3.1, 2.5, 6.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
A

B. list1 is 2.5 3.1, 3.1, 6.4

18
Q

You can use the ________ function in the random module to shuffle the elements in a list.

A

shuffle