# Peer Instruction: Lists Multiple Choice QuestionsΒΆ

- 3
- Correct! lst[1] = 'wxyz' replaces the second element with 'wxyz'. But the total numebr of elements still remains 3.
- 9
- Try again. lst[1] = 'wxyz' replaces the second element with 'wxyz'. But the total numebr of elements still remains 3.
- 10
- Try again. lst[1] = 'wxyz' replaces the second element with 'wxyz'. But the total numebr of elements still remains 3.
- 4
- Try again. lst[1] = 'wxyz' replaces the second element with 'wxyz'. But the total numebr of elements still remains 3.
- No output; there is an error in the second line
- Try again. There is no bug with the second line.

Q-1: What does the following code print?

```
lst = ['abc', 'def', 'ghi']
lst[1] = 'wxyz'
print(len(lst))
```

- [2, 4]
- Try again. a.remove(4) removes 4 from the list.
- [6, 8]
- Try again. a.pop(2) pops out the value at Index 2 and removes it from the list. Therefore, 8 is removed.
- [2, 6]
- Correct! a.remove(4) removes 4 and a.pop(2) pops out 8.
- [2, 8]
- Try again. a.pop(2) pops out the value at Index 2 and removes it from the list. Therefore, 8 is removed.
- Nothing. The code produces an error.
- Try again. The code will not produce an error.

Q-2: What is the value of `a`

after this code runs?

```
a = [2, 4, 6, 8]
a.remove(4)
a.pop(2)
```

- [2, 4]
- Try again. a.remove(4) removes 4 from the list.
- [6, 8]
- Try again. a.pop(2) pops out the value at Index 2 and removes it from the list. Therefore, 6 is removed.
- [2, 6]
- Try again. a.pop(2) pops out the value at Index 2 and removes it from the list. Therefore, 6 is removed.
- [2, 8]
- Correct! a.pop(2) pops out 6, and a.remove(4) removes 4 from the list.
- Nothing. The code produces an error.
- Try again. The code will not produce an error.

Q-3: What is the value of `a`

after this code runs?

```
a = [2, 4, 6, 8]
a.pop(2)
a.remove(4)
```

- [[1, 2, 3], [4, 5]] (unchanged)
- Correct! [:] makes a shallow copy of the array. b.append(8) allows to modify the copy without damaging the original.
- [[1, 2, 3], [4, 5], 8]
- Try again. [:] makes a shallow copy of the array a. Therefore, b.append(8) allows to modify the copy without damaging the original array a.
- [[1, 2, 3], [4, 5, 8]]
- Try again. [:] makes a shallow copy of the array a. Therefore, b.append(8) allows to modify the copy without damaging the original array a.
- [[1, 2, 3], [4, 5], [8]]
- Try again. [:] makes a shallow copy of the array a. Therefore, b.append(8) allows to modify the copy without damaging the original array a.

Q-4: What is the value of the list `a`

after the code below runs?

```
a = [[1, 2, 3], [4, 5]]
b = a[:]
b.append(8)
```

- [2, 5, 8]
- Try again. range(2, 7, 3) creates a sequence of numbers from 2 to 7, but increment by 3. Therefore, 8 is not included.
- [2, 5]
- Correct. range(2, 7, 3) creates a sequence of numbers from 2 to 7, but increment by 3.
- [2, 5, 7]
- Try again. range(2, 7, 3) creates a sequence of numbers from 2 to 7, but increment by 3. Therefore, 7 is not included.
- [2, 3, 4, 5, 6, 7]
- Try again. range(2, 7, 3) creates a sequence of numbers from 2 to 7, but increment by 3. Therefore, 3, 4, 6 and 7 are not included.

Q-5: Which list is produced by this code?

```
list(range(2, 7, 3))
```

- [4, 8]
- Correct! range(4, 9, 4) creates a sequence of numbers from 4 to 9, but increment by 4.
- [4, 8, 12]
- Try again. range(4, 9, 4) creates a sequence of numbers from 4 to 9, but increment by 4. Therefore, 12 is not included.
- [4, 8, 9]
- Try again. range(4, 9, 4) creates a sequence of numbers from 4 to 9, but increment by 4. Therefore, 9 is not included.
- [4, 5, 6, 7, 8, 9]
- Try again. range(4, 9, 4) creates a sequence of numbers from 4 to 9, but increment by 4. Therefore, 5, 6, 7 and 9 are not included.

Q-6: Which list is produced by this code?

```
list(range(4, 9, 4))
```

- 18
- Try again. Since len(lst) = 3, when counter = 4, the loop stops. Because sum += counter comes before counter += 2, sum = 2.
- 6
- Try again. Since len(lst) = 3, when counter = 4, the loop stops. Because sum += counter comes before counter += 2, sum = 2.
- 2
- Correct! Since len(lst) = 3, when counter = 4, the loop stops. Because sum += counter comes before counter += 2, sum = 2.
- 9
- Try again. Since len(lst) = 3, when counter = 4, the loop stops. Because sum += counter comes before counter += 2, sum = 2.
- None of the above
- Try again. Try to think about what is the value of counter when the loop stops.

Q-7: What does the following code print?

```
lst = [3, 6, 9]
sum = 0
counter = 0
while counter < len(lst):
sum += counter
counter += 2
print(sum)
```

- 8
- Try again. The only contiguous portion of the list that has the greatest sum is [8, -6, 10], summing up tp 12.
- 9
- Try again. The only contiguous portion of the list that has the greatest sum is [8, -6, 10], summing up tp 12.
- 10
- Try again. The only contiguous portion of the list that has the greatest sum is [8, -6, 10], summing up tp 12.
- 12
- Correct! The only contiguous portion of the list that has the greatest sum is [8, -6, 10], summing up tp 12.
- 20

Q-8: What is the maximum segment sum in this list?

```
[2, -5, 8, -6, 10]
```

- 3
- Try again. The only contiguous portion of the list that has the greatest sum is [10], summing up tp 10.
- 8
- Try again. The only contiguous portion of the list that has the greatest sum is [10], summing up tp 10.
- 10
- Correct! The only contiguous portion of the list that has the greatest sum is [10], summing up tp 10.
- 12
- Try again. The only contiguous portion of the list that has the greatest sum is [10], summing up tp 10.
- 15

Q-9: What is the maximum segment sum in this list?

```
[2, -5, 8, -6, 10]
```

- 1
- Try again. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2,3] and [0,1,2,3,4] were computed. During the second pass, Approach A would start from lower = 1, moving upper from 1 to 4. In this pass, the sum of [1,2,3] and [1,2,3,4] were computed. There would be no more computations of 1+2+3 later since lower would move pass 1. Therefore there are 4 computations of 1+2+3 in total.
- 2
- Try again. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2,3] and [0,1,2,3,4] were computed. During the second pass, Approach A would start from lower = 1, moving upper from 1 to 4. In this pass, the sum of [1,2,3] and [1,2,3,4] were computed. There would be no more computations of 1+2+3 later since lower would move pass 1. Therefore there are 4 computations of 1+2+3 in total.
- 3
- Try again. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2,3] and [0,1,2,3,4] were computed. During the second pass, Approach A would start from lower = 1, moving upper from 1 to 4. In this pass, the sum of [1,2,3] and [1,2,3,4] were computed. There would be no more computations of 1+2+3 later since lower would move pass 1. Therefore there are 4 computations of 1+2+3 in total.
- 4
- Correct. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2,3] and [0,1,2,3,4] were computed. During the second pass, Approach A would start from lower = 1, moving upper from 1 to 4. In this pass, the sum of [1,2,3] and [1,2,3,4] were computed. There would be no more computations of 1+2+3 later since lower would move pass 1. Therefore there are 4 computations of 1+2+3 in total.
- 5

Q-10: [0, 1, 2, 3, 4] How many times does Approach A compute the sum 1 + 2 + 3 in the above list?

```
Approach A:
def max_segment_sum(L):
'''(list of int) -> int
Return maximum segment sum of L.
'''
max_so_far = 0
for lower in range(len(L)):
for upper in range(lower, len(L)):
sum = 0
for i in range(lower, upper+1):
sum = sum + L[i]
max_so_far = max(max_so_far, sum)
return max_so_far
```

- 1
- Try again. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2], [0,1,2,3] and [0,1,2,3,4] were computed. There would be no more computations of 0+1+2 since lower would move pass 0. Therefore there would be 3 times.
- 2
- Try again. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2], [0,1,2,3] and [0,1,2,3,4] were computed. There would be no more computations of 0+1+2 since lower would move pass 0. Therefore there would be 3 times.
- 3
- Correct. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2], [0,1,2,3] and [0,1,2,3,4] were computed. There would be no more computations of 0+1+2 since lower would move pass 0. Therefore there would be 3 times.
- 4
- Try again. In the first pass of the outer loop, Approach A would start from lower = 0, moving upper from 0 to 4. In this pass, the sum of [0,1,2], [0,1,2,3] and [0,1,2,3,4] were computed. There would be no more computations of 0+1+2 since lower would move pass 0. Therefore there would be 3 times.
- 5

Q-11: [0, 1, 2, 3, 4] How many times does Approach A compute the sum 0 + 1 + 2 in the above list?

```
Approach A:
def max_segment_sum(L):
'''(list of int) -> int
Return maximum segment sum of L.
'''
max_so_far = 0
for lower in range(len(L)):
for upper in range(lower, len(L)):
sum = 0
for i in range(lower, upper+1):
sum = sum + L[i]
max_so_far = max(max_so_far, sum)
return max_so_far
```

- Displaying the top fiction sales on Amazon
- Try Again. In this senerio sorting is useful because ranking the sales needs sorting the numbers. Is there other case you find useful?
- Putting a list of words in alphabetical order
- Try Again. In this senerio sorting is useful because ranking the words needs sorting the strings. Is there other case you find useful?
- Printing the average GPA of 100 students
- Try Again. Avergaing a set is permutation invariant, so there is no need to sort.
- Two of the above
- Correct. Both A and B needs sorting.
- All of the above
- Try Again. In case C, avergaing a set is permutation invariant, so there is no need to sort.

Q-12: For which of the following is a sort useful?

- Once a value is placed in the sorted part, it will never move again
- Try Again. This is false because the sorted part may expect another value that is smaller than the leftmost value of the sorted part. So the elements in the sorted part may still need to swap.
- All values in the sorted part are always less than or equal to all values in the unsorted part
- Try Again. This is false because the sorted part may expect another value that is smaller than the leftmost value of the sorted part. So the elements in the sorted part may still need to swap.
- Both of the above are true
- Try Again. None of A and B are correct.
- None of the above is true
- Correct. None of A and B are correct.

Q-13: Which of the following is true of insertion sort?

- [8, 20, 30, 40, 16, 94, 10, 22]
- Try Again. After the third pass the sorted part is [10, 20, 30, 40], and the unsorted part is [16, 94, 8, 22]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [10, 16, 20, 30, 40] and the rest is [94, 8, 22]. So the whole list is [10, 16, 20, 30, 40, 94, 8, 22].
- [10, 16, 20, 30, 40, 94, 8, 22]
- Correct. After the third pass the sorted part is [10, 20, 30, 40], and the unsorted part is [16, 94, 8, 22]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [10, 16, 20, 30, 40] and the rest is [94, 8, 22]. So the whole list is [10, 16, 20, 30, 40, 94, 8, 22].
- [10, 16, 30, 40, 20, 94, 8, 22]
- Try Again. After the third pass the sorted part is [10, 20, 30, 40], and the unsorted part is [16, 94, 8, 22]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [10, 16, 20, 30, 40] and the rest is [94, 8, 22]. So the whole list is [10, 16, 20, 30, 40, 94, 8, 22].
- [8, 10, 20, 30, 40, 16, 94, 22]
- Try Again. After the third pass the sorted part is [10, 20, 30, 40], and the unsorted part is [16, 94, 8, 22]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [10, 16, 20, 30, 40] and the rest is [94, 8, 22]. So the whole list is [10, 16, 20, 30, 40, 94, 8, 22].
- [10, 20, 30, 40, 8, 94, 16, 22]

Q-14: The list below reflects the state of the list after 3 passes of insertion sort. What will be in the list after the next (fourth) pass?

```
[10, 20, 30, 40, 16, 94, 8, 22]
```

- [5, 7, 14, 16, 19, 2, 32, 9]
- Correct. After the third pass the sorted part is [5, 7, 14, 19], and the unsorted part is [16, 2, 32, 9]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [5, 7, 14, 16, 19] and the rest is [2, 32, 9]. So the whole list is [5, 7, 14, 16, 19, 2, 32, 9].
- [5, 7, 14, 19, 2, 16, 32, 9]
- Try Again. After the third pass the sorted part is [5, 7, 14, 19], and the unsorted part is [16, 2, 32, 9]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [5, 7, 14, 16, 19] and the rest is [2, 32, 9]. So the whole list is [5, 7, 14, 16, 19, 2, 32, 9].
- [5, 7, 16, 19, 14, 2, 32, 9]
- Try Again. After the third pass the sorted part is [5, 7, 14, 19], and the unsorted part is [16, 2, 32, 9]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [5, 7, 14, 16, 19] and the rest is [2, 32, 9]. So the whole list is [5, 7, 14, 16, 19, 2, 32, 9].
- [2, 5, 7, 14, 19, 16, 32, 9]
- Try Again. After the third pass the sorted part is [5, 7, 14, 19], and the unsorted part is [16, 2, 32, 9]. The next value in the unsorted part is 16 and the algorithm will place 16 in the correct position in the sorted part. Thus, the sorted part becomes [5, 7, 14, 16, 19] and the rest is [2, 32, 9]. So the whole list is [5, 7, 14, 16, 19, 2, 32, 9].
- [2, 7, 14, 19, 16, 5, 32, 9]

Q-15: The list below reflects the state of the list after 3 passes of insertion sort. What will be in the list after the next (fourth) pass?

```
[5, 7, 14, 19, 16, 2, 32, 9]
```

- Once a value is placed in the sorted part, it will never move again
- Try Again. This is correct because in every pass, the greatest value of the unsorted pass will be moved to the sorted part. Therefore, the sorted part in the right of the array contains sorted elements that are greater than every elements in the unsorted part. Therefore, the sorted part will not expect any elements to affect it.
- There is never a value in the sorted part that is smaller than some value in the unsorted part
- Try Again. This is correct because in every pass, the greatest value of the unsorted pass will be moved to the sorted part. Therefore, the sorted part in the right of the array contains sorted elements that are greater than every elements in the unsorted part. Therefore, the sorted part will not expect any elements to affect it.
- Both of the above are true
- Correct. All of A and B are correct.
- None of the above is true
- Try Again. None of A and B are correct.

Q-16: Which of the following is true of bubble sort?

- [5, 0, 9, 6, 4, 2, 8]
- Try Again. During the first pass, the list becomes [-, 5, 9, -, 0, 4, 6, 8, 2], and then [5, -, 9, 0, -, 4, 6, 8, 2], and then [5, 0, -, 9, 4, -, 6, 8, 2], and then [5, 0, 4, -, 9, 6, -, 8, 2], and then [5, 0, 4, 6, -, 9, 8, -, 2], and then [5, 0, 4, 6, 8, -, 9, 2, -], and then [5, 0, 4, 6, 8, 2, 9]
- [5, 9, 0, 4, 6, 2, 8]
- Try Again. During the first pass, the list becomes [- , 5, 9, -, 0, 4, 6, 8, 2], and then [5, -, 9, 0, -, 4, 6, 8, 2], and then [5, 0, -, 9, 4, -, 6, 8, 2], and then [5, 0, 4, -, 9, 6, -, 8, 2], and then [5, 0, 4, 6, -, 9, 8, -, 2], and then [5, 0, 4, 6, 8, -, 9, 2, -], and then [5, 0, 4, 6, 8, 2, 9]
- [5, 0, 9, 4, 6, 8, 2]
- Try Again. During the first pass, the list becomes [-, 5, 9, -, 0, 4, 6, 8, 2], and then [5, -, 9, 0, -, 4, 6, 8, 2], and then [5, 0, -, 9, 4, -, 6, 8, 2], and then [5, 0, 4, -, 9, 6, -, 8, 2], and then [5, 0, 4, 6, -, 9, 8, -, 2], and then [5, 0, 4, 6, 8, -, 9, 2, -], and then [5, 0, 4, 6, 8, 2, 9]
- [5, 0, 4, 6, 8, 2, 9]
- Correct. During the first pass, the list becomes [-, 5, 9, -, 0, 4, 6, 8, 2], and then [5, -, 9, 0, -, 4, 6, 8, 2], and then [5, 0, -, 9, 4, -, 6, 8, 2], and then [5, 0, 4, -, 9, 6, -, 8, 2], and then [5, 0, 4, 6, -, 9, 8, -, 2], and then [5, 0, 4, 6, 8, -, 9, 2, -], and then [5, 0, 4, 6, 8, 2, 9]

Q-17: Which of the following matches the contents of the list after one pass of bubble sort?

```
[5, 9, 0, 4, 6, 8, 2]
```

- [2, 1, 7, 15, 9, 1, 10]
- Try Again. During the first pass, the list becomes [-,2,10,-,1,7,15,9,1], and then [2,-,10,1,-,7,15,9,1], and then [2,1,-,10,7,-,15,9,1], and then [2,1,7,-,10,15,-,9,1], and then [2,1,7,10,-,15,9,-,1], and then [2,1,7,10,9,-,15,1,-], and then [2,1,7,10,9,1,15]
- [2, 1, 7, 10, 9, 1, 15]
- Try Again. During the first pass, the list becomes [-,2,10,-,1,7,15,9,1], and then [2,-,10,1,-,7,15,9,1], and then [2,1,-,10,7,-,15,9,1], and then [2,1,7,-,10,15,-,9,1], and then [2,1,7,10,-,15,9,-,1], and then [2,1,7,10,9,-,15,1,-], and then [2,1,7,10,9,1,15]
- [2, 1, 10, 7, 15, 9, 1]
- Try Again. During the first pass, the list becomes [-,2,10,-,1,7,15,9,1], and then [2,-,10,1,-,7,15,9,1], and then [2,1,-,10,7,-,15,9,1], and then [2,1,7,-,10,15,-,9,1], and then [2,1,7,10,-,15,9,-,1], and then [2,1,7,10,9,-,15,1,-], and then [2,1,7,10,9,1,15]
- [2, 1, 10, 7, 15, 1, 9]
- Correct. During the first pass, the list becomes [-,2,10,-,1,7,15,9,1], and then [2,-,10,1,-,7,15,9,1], and then [2,1,-,10,7,-,15,9,1], and then [2,1,7,-,10,15,-,9,1], and then [2,1,7,10,-,15,9,-,1], and then [2,1,7,10,9,-,15,1,-], and then [2,1,7,10,9,1,15]

Q-18: Which of the following matches the contents of the list after one pass of bubble sort?

```
[2, 10, 1, 7, 15, 9, 1]
```

- Selection
- Try Again. During the first pass, selection sort would find the min value of the entire list, which is 2 in this case, and then swap with the first index, so after the first round it would be [2, 9, 0, 4, 6, 8, 5]
- Insertion
- Correct. During each pass i, the ith value of the list is inserted into the left sorted part of the list.
- Bubble
- Try Again. Bubble sort places the sorted part on the right part after each round, but the lists in the question has left part sorted.

Q-19: Which sort produces the following values on each pass?

```
[5, 9, 0, 4, 6, 8, 2]
[5, 9, 0, 4, 6, 8, 2]
[5, 9, 0, 4, 6, 8, 2]
[0, 5, 9, 4, 6, 8, 2]
[0, 4, 5, 9, 6, 8, 2]
[0, 4, 5, 6, 9, 8, 2]
[0, 4, 5, 6, 8, 9, 2]
[0, 2, 4, 5, 6, 8, 9]
```