COMP560 W3 ZyCh6_lists

1) What is a list in Python?
A) a container that stores a collection of elements that are arranged in a linear or sequential order

2) Given the following list, what value is at index 5?

values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

A) 6

3) Consider the following line of code:

somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",  "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Which one of the following options is a valid line of code for displaying the element at the twenty-second position in the list?
A) print(somelist[21])

4) Given the list below, what are the upper and lower bounds?

somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",  "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

A) 0, 25

5) What is the valid range of index values for a list?
A) at least zero and less than the number of elements in the list

6) What is printed after executing the following code snippet?

somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",  "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
for i in range(len(somelist)) :
   print(somelist[i], end = "")

A) abcdefghijklmnopqrstuvwxyz

7) What is printed after executing the following code snippet?

somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",  "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
for i in range(0, len(somelist), 2) :
   print(somelist[i], end = "")

A) acegikmoqsuwy

8) Which statement correctly creates a list that contains four elements?
A) values = [1, 2, 3, 4]

9) What is the difference between a list and a string?
A) lists can hold values of any type, whereas strings are only sequences of characters

10) Which statement gets the length of the list values?
A) numValues = len(values)

11) Given a list values that contains the first ten prime numbers, which statement prints the index and value of each element in the list?
A) for i in range(10) :
print(i, values[i])

12) Given a list values that contains the first ten prime numbers, which statement does NOT print each element in the list?
A) for i in range(1, len(values)) :
print(values[i])

13) For the list: prices = [10.00, 15.50, 13.25, 20.15], what value is contained in prices?
A) the location of the list
14) Given the following code snippet, which statement correctly creates an alias for the list prices?

prices = [10.00, 15.50, 13.25, 20.15]

 A) values = prices

15) Which of the following statements stores the numbers 1 through 5 into a list named values?
A) values = [1, 2, 3, 4, 5]

16) Consider the following code segment:

values = [4, 12, 0, 1, 5]
print(values[1])

What is displayed when it runs?

A) 12

17) Consider the following list:

values = [12, 4, 8, 16, 24]

Which statement displays the last element in the list?
A) print(values[4])

18) Consider the following code segment:

values = [1, 2, 3, 4, 5]
moreValues = values
moreValues[3] = 6


print(values)

What is displayed when it runs?
A) [1, 2, 3, 6, 5]

19) What will happen when the following code segment is executed?

values = [1.618, 2.71, 3.14]
print(values[3])

A) Python will display an out-of-range error
20) Consider the following code snippet. What will be stored in the list prices after this code executes?

prices = [10.00, 15.50, 13.50, 20.15]
for i in range(len(prices)) :
   prices[i] = prices[i] * 1.06

A) [10.60, 16.43, 14.31, 21.36]

21) Given the list values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], which statement fills the list with these numbers:

1 2 3 4 5 6 7 8 9 10

A) for i in range(10) :
values[i] = i + 1

22) Which statement creates a new, empty list?
A) data = []

23) Which statement correctly identifies the error in this code snippet?

scores = [80, 85, 60.5, 95, 70]
print(scores[5])

A) IndexError: list index out of range

24) Given the following code snippet, which statement correctly copies the contents of the list prices?

prices = [10.00, 15.50, 13.25, 20.15]

A) values = list(prices)

25) Given the definition of a list: names = [] which statement correctly adds "Harry" to the list?
A) names.append(“Harry”)

26) Which of the following statements looks for the name 'Bob' in the list names?

A) if “Bob” in names :
print(“found Bob”)

27) Given the following code snippet, what is the value of the variable indexValue?

states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
indexValue = states.index("Hawaii")

A) 1

28) Given the following code snippet, what is the value of the variable removedValue?

states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
removedValue = states.pop(3)

A) Maine

29) Given the following code snippet, what is the content of the list states?

states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
states.pop()

A) [“Alaska”, “Hawaii”, “Florida”, “Maine”, “Ohio”]

30) Given the following code snippet, what are the contents of the list states?

states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
states.remove("Florida")

A) [“Alaska”, “Hawaii”, “Maine”, “Ohio”, “Florida”]

31) Given the following code snippet, what are the contents of the list fullNames?

firstNames = ["Joe", "Jim", "Betsy", "Shelly"]
lastNames = states = ["Jones", "Patel", "Hicks", "Fisher"]
fullNames = firstNames + lastNames

A) [“Joe”, “Jim”, “Betsy”, “Shelly”, “Jones”, “Patel”, “Hicks”, “Fisher”]

32) Given the following code snippet, what are the contents of the list names?

firstNames = ["Joe", "Jim", "Betsy", "Shelly"]
names = firstNames * 2

A) [“Joe”, “Jim”, “Betsy”, “Shelly”, “Joe”, “Jim”, “Betsy”, “Shelly”]

33) Which statement(s) allows us to initialize the list numbers with 10 elements all set to zero?
A) numbers = [0] * 10

34) Given the following code snippet, what is printed?

nums = [1, 2, 3, 4, 5]
nums2 = [5, 4, 3, 2, 1]
if nums == nums2 :
   print("lists are equal")
else :
   print("lists are not equal")

A) lists are not equal

35) What is the resulting value of this code snippet?

sum([1, 2, 3, 4, 5])

A) 15

36) What is the resulting value of this code snippet?

max(["Roland", "Kent", "Ravi", "Amy"])

A) “Roland”

37) What is the resulting content of the list after this code snippet?

min(["Roland", "Kent", "Ravi", "Amy"])

A) “Amy”

38) What is the resulting content of the list values after this code snippet?

values = [1991, 1875, 1980, 1965, 2000]
values.sort()

A) [1875, 1965, 1980, 1991, 2000]

39) What is the resulting content of the list letters after this code snippet?

letters = list("abcdefg")

A) letters contains a list with the contents: ["a", "b", "c", "d", "e", "f", "g"]

40) Which of the following code segments will result in values containing the list ["Hydrogen", "Helium", "Lithium"]?
A) values = []
values.append(“Hydrogen”)
values.append(“Helium”)
values.append(“Lithium”)

41) Which of the following code segments will result in values containing the list ["X", "Y", "Z"]?
A) values = [“X”]
values.insert(1, “Y”)
values.insert(2, “Z”)

42) What is stored in pos when the following code segment executes?

values = ["J", "Q", "X", "Z"]
pos = values.find("Y")

A) No value is stored because a runtime exception occurs

43) What is in grades after the follow code segment executes?

grades = ["A", "B+", "C", "C+", "C", "B-"]
grades.remove("C")

A) [“A”, “B+”, “C+”, “C”, “B-“]

44) What is displayed by the following code segment?

values = ["Q", "W", "E", "R", "T", "Y"]
print(min(values))

A) E

45) What is output by the following code segment?

x = list("QWERTY")
print(x)


 A) [“Q”, “W”, “E”, “R”, “T”, “Y”]

46) What is output by the following code segment?

values = ["Q", "W", "E", "R", "T", "Y"]
print(values[2 : 4])

A) [“E”, “R”]

47) What is the value of names after the following code segment has run?

names = []
names.append("Amy")
names.append("Bob")
names.append("Peg")
names[0] = "Cy"
names.insert(0, "Ravi")

A) [‘Ravi’, ‘Cy’, ‘Bob’, ‘Peg’]

48) What is the value of names after the following code segment has run?

names = []
names.append("Amy")
names.append("Bob")
names.append("Peg")
names[0] = "Cy"
names.insert(0, "Ravi")
names.insert(4, "Savannah")

A) [‘Ravi’, ‘Cy’, ‘Bob’, ‘Peg’, ‘Savannah’]

49) What is the value of the variable indexValue after the following code snippet runs?

states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
indexValue = states.index("Pennsylvania")

A) A ValueError exception is raised.

50) What is the value of states after the following code segment has run?

element = "Ohio"
states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]
states.remove(element)

A) [“Alaska”, “Hawaii”, “Florida”, “Maine”, “Florida”]

51) Which statement correctly compares these two lists for equality?

nums = [1, 2, 3, 4, 5]
nums2 = [5, 4, 3, 2, 1]

A) if nums == nums2

52) What is in values after the following code segment executes?

values = [1, 2, 3]
values = values * 4

A) [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

53) Consider the following code segment:

x = [5, 3, 7]
y = sorted(x)
print(x)

What is displayed when it executes?

A) [5, 3, 7]

54) Consider the following code segment:

x = [5, 3, 7]
y = x.pop()
print(y)

What is displayed when it executes?
A) 7

55) Consider the following code segment:

x = [5, 3, 7, 9, 1]
y = x[2 : 4]
print(y)

What is displayed when it executes?

A) [7, 9]

56) Consider the following code segment:

x = [5, 3, 7, 9, 1]
y = x[3 : ]
print(y)

What is displayed when it executes?
A) [9, 1]

57) Consider the following code segment:

x = [5, 3, 7, 9, 1]
y = x[ : 2]
print(y)

What is displayed when it executes?
A) [5, 3]

58) What is the resulting contents of the list values in this code snippet?

values = []
for i in range(10) :
   values.append(i)

A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

59) What is the resulting value of the variable sentence in this code snippet?

words = ["Mary", "had", "a", "little", "lamb"]
sentence = ""
for word in words :
   sentence = sentence + word

A) Maryhadalittlelamb

60) What is the resulting value of the variable sentence in this code snippet?

words = ["Mary", "had", "a", "little", "lamb"]
sentence = str(words)

A) [‘Mary’, ‘had’, ‘a’, ‘little’, ‘lamb’]

61) What is missing from this code snippet to find the largest value in the list?

largest = values[0]
for i in range(1, len(values)) :
   if values[i] > largest :
      ____________________

A) largest = values[i]

62) What code completes this code snippet to swap the first and last element in the list?

states = ["Alaska", "Hawaii", "Florida", "Maine"]
i = 0
________________________
temp = states[j]
states[j] = states[0]
states[i] = temp

A) j = len(states) – 1

63) The following code segment is supposed to display all of the elements in a list with dashes between them. For example, if values contains [1, 2, 3, 4, 5] then the program should display 1-2-3-4-5.

result = ""
for i in range(len(values) :
   if i > 0 :
      _____________________
   result = result + str(values[i])
 
print(result)

What line of code should be placed in the blank to achieve this goal?

A) result = result + “-“

64)  Consider the following code segment:

x = values[0]
for i in range(1, len(values)) :
   if values[i] > x :
      x = values[i]

Which of the following statements assigns the same value to x as the code segment above?
A) x = max(values)

65) The following code segment is supposed to count the number of times that the number 5 appears in the list values.

counter = 0
____________________
   if (element == 5) :
      counter = counter + 1

What line of code should be placed in the blank in order to achieve this goal?
A) for element in values :

66) What does the following code segment do?

i = 0
while i < len(values) :
   if values[i] >= 4 and values[i] <= 6 :
      values.pop(i)
   else :
      i = i + 1

A) It removes all 4s, 5s and 6s from values

67) Which code segment swaps the elements at position 0 and position 2 within the values list?
A) temp = values[2]
values[2] = values[0]
values[0] = temp

68) The following code segment is supposed to read all of the integers entered by the user until a blank line is entered and store them in values.

values = []
inputStr = input("Enter a value (blank line to quit): ")
____________________
   values.append(int(inputStr))
   inputStr = input("Enter a value (blank line to quit): ")

What line of code should be placed in the blank to achieve this goal?
A) while inputStr != “” :

69) What is missing from this code snippet to find the longest value in the list?

colors = ["red", "purple", "blue", "green", "yellow", "light green"]
longest = colors[0]
for i in range(1, len(colors)) :
   _____________________
      longest = colors[i]

A) if len(colors[i]) > len(longest) :

70) What type of search inspects every element sequentially?
A) linear search
71) What is printed by the following code snippet?

words = ["Today", "is", "Wednesday", "tomorrow", "is", "Thursday"]
for i in range(len(words)) :
  word = words[i]
  if len(word) < 8 :
     words.pop(i)
print(words)

A) Nothing, this code snippet causes an IndexError: list index out of range error.

72) What is printed by the following code snippet?

words = ["Today", "is", "Wednesday", "tomorrow", "is", "Thursday"]
i = 0
while i < (len(words)) :
   word = words[i]
   if len(word) < 8 :
      words.pop(i)
   else :
      i = i  + 1
print(words)

A) [“Wednesday”, “tomorrow”, “Thursday”]

73) Which of the following statements is NOT true about functions and lists:
A) when calling a function with a list argument, the function receives a copy of the list

74) The following function is supposed to add 1 to every element in a list of integers.

def addOne(values) :
   for i in range(len(values)) :
      values[i] = values[i] + 1

What is wrong with the following function?

A) There is nothing wrong with the function. It works as intended.

75) Given a list containing prices, how do you find the highest priced item and remove it from the list:
A) find the maximum, remove it from the list

76) What does the following code segment do?

x = 0
for i in range(1, len(values)) :
   if values[i] > values[x] :
      x = i

A) It finds the position of the largest item in values and stores the position in x
77) What does the following code segment do?

x = 0
for i in range(1, len(values)) :
   if values[i] < values[x] :
      x = i

 A) It finds the position of the smallest item in values and stores it in x

78) What library is used to read and write sound files?
A) scipy.io.wavfile

79) What is stored in contents when a sound file is read by the scipy library using the following statement?

contents = scipy.io.wavfile.read("meow.wav")

A) A tuple containing the sample rate and a NumPy array

80) Which of the following items would not be a good object to use while solving algorithms such as swapping the first half of a list with the second half:
A)  toothpicks

81) What is the purpose of the following pseudocode:

i = 0
j = length / 2
While i < length / 2
   #  Swap elements at positions i and j
   temp = a[i]
   a[i] = a[j]
   a[j] = temp
 
   i = i + 1
   j = j + 1

A) flip the first half of a list with the second half

82) What does the following code segment do?

x = []
for i in range(5) :
   x.append([])
   for j in range(3) :
      x[i].append(0)

A) It creates a table with 3 columns and 5 rows and stores it in x.
 83) What output is generated by the following code segment?

table = [["T", "U", "V"], ["W", "X", "Y"]]
print(table[0])

A) [“T”, “U”, “V”]

84) What output is generated by the following code segment?

table = [["T", "U", "V"], ["W", "X", "Y"]]
print(table[1][1])

A) X

85) Consider the following table:

table = [["T", "U", "V"], ["W", "X", "Y"]]

Which statement will display Y?
A) print(table[len(table) – 1][len(table[1]) – 1])

86) Which of the following statements best represents a deck of cards?
A) cards = [[“Ace of Hearts”, “Ace of Spades”, “Ace of Diamonds”, “Ace of Clubs”],
[“2 of Hearts”, “2 of Spades”, “2 of Diamonds”, “2 of Clubs”], etc.]

87) What is printed from the following code snippet?

prices = [[ 1.0, 3.50, 7.50 ],
   [ 10.0, 30.50, 70.50 ],
   [ 100.0, 300.50, 700.50 ],
   [ 1000.0, 3000.50, 7000.50 ]]
print(prices[1][2])

A) 70.5

88) Given the list values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], which statement fills the list with these numbers:

1 4 9 16 25 36 49 64 81 100

A) for i in range(10) :
values[i] = (i + 1) * (i + 1)

89) What list is stored in x after this code segment has run?

x = []
for i in range(3) :
   x.append([])
   for j in range(i) :
      x[j].append(0)

A) [[0, 0], [0], []]

90) How many values does a stereo sound file have for each sample?
A) 2
91) What is returned when scipi.io.wavfile.read is called?
A) A tuple containing the sample rate and the sound data

Leave a Reply

Your email address will not be published. Required fields are marked *