Chapter 6

Ace your homework & exams now with Quizwiz!

What does the following code segment do? x = [] for i in range(5) : x.append([]) for j in range(3) : x[i].append(0) 1. It creates a table with 3 columns and 5 rows and stores it in x. 2. It creates a table with 5 columns and 3 rows and stores it in x. 3. It creates a list of 15 elements and stores it in x. 4. It creates an empty list and stores it in x.

1. It creates a table with 3 columns and 5 rows and stores it in x.

Given the following table, what is the value at randomArray [3][[3] myArray = [ [25, 5, 27, 24], [25, 16, 27, 10], [11, 29, 1, 15], [18, 19, 12, 15], [11, 24, 30, 29], [17, 13, 6, 1] ]

Correct Answer(s): 15

the following table, what is the value at randomArray [0][[3] myArray = [ [25, 5, 27, 24], [25, 16, 27, 10], [11, 29, 1, 15], [18, 19, 12, 15], [11, 24, 30, 29], [17, 13, 6, 1] ]

Correct Answer(s): 24

Given the following table, what is the value for: myArray[1][3] + myArray[5][2] myArray = [ [25, 5, 27, 24], [25, 16, 27, 10], [11, 29, 1, 15], [18, 19, 12, 15], [11, 24, 30, 29], [17, 13, 6, 1] ]

Your Answer: 16

Given the following table, what is the value for: myArray[0][3] - myArray[5][3] myArray = [ [25, 5, 27, 24], [25, 16, 27, 10], [11, 29, 1, 15], [18, 19, 12, 15], [11, 24, 30, 29], [17, 13, 6, 1] ]

Your Answer: 23

Which line of code is equivalent to this code segment? 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? 1. x = max(values) 2. x = min(values) 3. x = sum(values) 4. x = sorted(values)

1. x = max(values)

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

2. for i in range(10) : print(i, values[i])

Which of the following statements looks for the name 'Bob' in the list names? 1. if "Bob" found in names : print("found Bob") 2. if "Bob" in names : print("found Bob") 3. if names contains "Bob" : print("found Bob") 4. if names.indexOf("Bob") >= 0 : print("found Bob")

2. if "Bob" in names : print("found Bob")

Given the following list, what value is at index 5? values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1. 5 2. 6 3. 7 4. 4

2. 6

What type of search inspects every element sequentially? 1. binary search 2. linear search 3. simple search 4. recursive search

2. linear search

Which statement gets the length of the list values? 1. numValues = length(values) 2. numValues = len(values) 3. numValues = size(values) 4. numValues = values.length

2. numValues = len(values)

Given the following code snippet, which statement correctly copies the contents of the list prices? prices = [10.00, 15.50, 13.25, 20.15] 1. values[10] = prices 2. values = list(prices) 3. for element in prices : values = element 4. values[10] = prices[10]

2. values = list(prices)

Given the following code snippet, which statement correctly creates an alias for the list prices? prices = [10.00, 15.50, 13.25, 20.15] 1. values[10] = prices 2. values = prices 3. for element in prices : values = element 4. values[10] = prices[10]

2. values = prices

What is the resulting value of this code snippet? What is the resulting value of this code snippet? sum([1, 2, 3, 4, 5]) 1. 121 2. 16 3. 15 4. 14

3. 15

Trace code that uses a slice What is output by the following code segment? values = ["Q", "W", "E", "R", "T", "Y"] print(values[2 : 4]) 1. ["W", "E"] 2. ["W", "E", "R"] 3. ["E", "R"] 4. ["E", "R", "T"]

3. ["E", "R"]

Trace code that uses a slice Given following code snippet, what is printed? letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] print ("letters[:4] ",letters[:4]) 1. ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 2. ['e', 'f'] 3. ['a', 'b', 'c', 'd'] 4. ['e', 'f', 'g']

3. ['a', 'b', 'c', 'd']

Given the following array representing a tic tac toe board, Where would you insert an "X" to win the game? myTicTacToe = [ ["X","O","X"], ["X","O","O"], ["?","?","?"] ] 1. myTicTacToe [1] [3] 2. myTicTacToe [3] [1] 3. myTicTacToe [0] [2] 4. myTicTacToe [2] [0]

4. myTicTacToe [2] [0]

Given the definition of a list: names = [ ] which statement correctly adds "Harry" to the list? 1. names[] = "Harry" 2. names[len(names) + 1] = "Harry" 3. names.add("Harry") 4. names.append("Harry")

4. names.append("Harry")

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? 1. somelist(21) 2. somelist[21] 3. print(somelist(21)) 4. print(somelist[21])

4. print(somelist[21])

What is the resulting contents of the list values in this code snippet? values = [] for i in range(10) : values.append(i) 1. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 3. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 4. [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]

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

What is the valid range of index values for a list? What is the valid range of index values for a list? 1. at least zero and less than the number of elements in the list 2. at least one and less than the number of elements in the list 3. at least zero and less than or equal to the number of elements in the list 4. at least one and less than or equal to the number of elements in the list

1. at least zero and less than the number of elements in the list

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 1. flip the first half of a list with the second half 2. sort the list from smallest to largest 3. sort the list from largest to smallest 4. reverse the entire list

1. flip the first half of a list with the second half

What is the resulting value of this code snippet? What is the resulting content of the list letters after this code snippet? letters = list("abcdefg") 1. letters contains a list with the contents: ["a", "b", "c", "d", "e", "f", "g"] 2. letters contains a list with the contents: ["abcdefg"] 3. Invalid Type Error 4. letters contains a list with the contents: ("abcdefg")

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

What is the resulting value of this code snippet? hat is the resulting value of this code snippet? max(["Roland", "Kent", "Ravi", "Amy"]) 1. Invalid Type Error 2. "Roland" 3. "Amy" 4. "Ravi"

2. "Roland"

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]) 1. 30.5 2. 70.5 3. 700.5 4. 10.0

2. 70.5

What does the following code segment do? x = 0 for i in range(1, len(values)) : if values[i] > values[x] : x = i 1. It finds the largest item in values and stores it in x 2. It finds the position of the largest item in values and stores the position in x 3. It finds the smallest item in values and stores it in x 4. It finds the position of the smallest item in values and stores it in x

2. It finds the position of the largest item in values and stores the position in x

What does this code segment involving lists do? 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 1. It removes all 5 from values 2. It removes all 4s, 5s and 6s from values 3. It removes the element at position 5 from values 4. It removes the elements at positions 4, 5 and 6 from values

2. It removes all 4s, 5s and 6s from values

Given the following code snippet, what is the value of the variable removedValue? states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"] removedValue = states.pop(3) 1. 3 2. Maine 3. Florida 4. Hawaii

2. Maine

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 1. Mary had a little lamb 2. Maryhadalittlelamb 3. "Mary had a little lamb" 4. Mary, had, a, little, lamb

2. Maryhadalittlelamb

What is a list in Python? What is a list in Python? 1. a series of numbers 2. a container that stores a collection of elements that are arranged in a linear or sequential order 3. a series of strings 4. a series of references or locations of data in memory

2. a container that stores a collection of elements that are arranged in a linear or sequential order

Given a list containing prices, how do you find the highest priced item and remove it from the list: 1. find the minimum, remove it from the list 2. find the maximum, remove it from the list 3. find the maximum, create a second list without this value 4. find the minimum, create a second list without this value

2. find the maximum, remove it from the list

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 1. j = len(states) 2. j = len(states) - 1 3. j = len(states) + 1 4. j = 0

2. j = len(states) - 1

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 : ____________________ 1. i = i + 1 2. largest = values[i] 3. largest = values[0] 4. largest = values[i-1]

2. largest = values[i]

Given the following code, what is the final value of letters? Remember when the argument is a list, the list reference is passed. letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] def testList(passList): newList = passList.pop() return passList testList(letters) print ("letters: {}".format(letters)) 1. letters: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 2. letters: ['a', 'b', 'c', 'd', 'e', 'f''] 3. letters: ['b', 'c', 'd', 'e', 'f', 'g']

2. letters: ['a', 'b', 'c', 'd', 'e', 'f'']

Given the following code, what is final value of the list letters? Remember when the argument is a list, the list reference is passed. letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] def testList(passList): newList = passList.pop() return passList anotherList = letters testList(anotherList) print ("letters: {}".format(letters)) 1. letters: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 2. letters: ['a', 'b', 'c', 'd', 'e', 'f']

2. letters: ['a', 'b', 'c', 'd', 'e', 'f']

What is stored in a list after performing append and insert operations? 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") 1. ['Ravi', 'Cy', 'Amy', 'Bob', 'Peg'] 2. ['Cy', 'Amy', 'Bob', 'Peg'] 3. ['Ravi', 'Cy', 'Bob', 'Peg'] 4. ['Ravi', 'Amy', 'Bob', 'Peg']

3. ['Ravi', 'Cy', 'Bob', 'Peg']

Which statement(s) allows us to initialize the list numbers with 10 elements all set to zero? 1. numbers = [0] 2. numbers[10] = 0 3. numbers = [0] * 10 4. numbers[10] = [0] * 10

3. numbers = [0] * 10

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

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

Which of the following statements is NOT true about functions and lists: 1. a function can return a list 2. a function can be called with a list as an argument 3. when calling a function with a list argument, the function receives a copy of the list 4. when calling a function with a list argument, the function receives a reference to the list not a copy of the list

3. when calling a function with a list argument, the function receives a copy of the list

Which code segment swaps the elements at position 0 and position 2 within the values list? 1. values[0] = values[2] 2. values[0] = values[2] values[2] = values[0] 3. values[0] = temp values[0] = values[2] temp = values[2] 4. temp = values[2] values[2] = values[0] values[0] = temp

4. temp = values[2] values[2] = values[0] values[0] = temp

What value is computed by a code segment that invokes the index method? 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") 1. -1 2. 0 3. 6 4. A ValueError exception is raised.

4. A ValueError exception is raised.

Which statement correctly identifies the error in this code snippet? scores = [80, 85, 60.5, 95, 70] print(scores[5]) 1. DataTypeError: cannot mix integer and floating point numbers 2. There is no error. The code runs and prints 70. 3. NameError: name 'scores' is not defined 4. IndexError: list index out of range

4. IndexError: list index out of range

Given the following code snippet, what are the contents of the list states? Given the following code snippet, what are the contents of the list states? states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"] states.remove("Florida") 1. ["Alaska", "Hawaii", "Maine", "Ohio", "Florida"] 2. ["Hawaii", "Hawaii", "Maine", "Ohio"] 3. ["Alaska", "Hawaii", "Florida", "Maine", "Ohio"] 4. ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]

4. ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"]

Trace code that uses a slice Given following code snippet, what is printed? letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] print ("letters[4:] ",letters[4:]) 1. ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 2. ['e', 'f'] 3. ['a', 'b', 'c', 'd'] 4. ['e', 'f', 'g']

4. ['e', 'f', 'g']

What is the resulting content of the list values after this code snippet? values = [1991, 1875, 1980, 1965, 2000] values.sort() 1. [1991, 1875, 1980, 1965, 2000] 2. [2000, 1991, 1980, 1965, 1875] 3. [2000, 1991, 1875, 1980, 1965] 4. [1875, 1965, 1980, 1991, 2000]

4. [1875, 1965, 1980, 1991, 2000]

Complete the code segment for counting the number of occurrences of a value in a list 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? 1. for counter in element : 2. for counter in values : 3. for element in counter : 4. for element in values :

4. for element in values :


Related study sets

POLI 107: Hannah Arendt: " Origins of Totalitarianism"

View Set

A/AS Level Business Studies - Chapter 2 - Business Structure (some A-Level material)

View Set

PTC - POSITIVE TRAIN CONTROL SYSTEM

View Set

Chapter 6 - Supporting Hard Drives and Other Storage DevicesHar

View Set

Parkinson's/Huntington's NCLEX-Khan Academy

View Set

Utah Life and Health Exam- Chapter 3: Life Insurance Policies

View Set