CNIT 155 PURDUE FINAL EXAM (PYTHON)
What is a List? - Answers -A data structure that can store many values of under one
name
Which is an example of how to create an empty list? - Answers -Variable = []
Variable = list()
Which is an example of how to create a list with predefined values? - Answers -colors =
["red, "yellow", "blue"]
To create a list with predefined values, you have to.... - Answers -Put a sequence of
items in []
What does list1.append(elem) do? - Answers -Adds a single element to the end of the
list
What does list1.insert(index, elem) do? - Answers -Inserts the element elem at the
given index, shifting elements to the right
What does list1.extend(list2) do? - Answers -Add the elements in list2 to the end of the
list.
T or F: The extend method is not only used with a list. - Answers -False
T or F: Using + or += on a list is similar to using the extend method. - Answers -True
How do you access the individual element in a list using an index? - Answers -
list1[index]
What are characteristics of Positive Indexes? - Answers -First element in the list has
index 0, second element has index 1, and n'th element is n-1
What are characteristics of Negative Indexes? - Answers -Identify positions relative to
the end of the list, index -1 identifies the last element, and -2 identifies the next to last
element, etc.
What does Function do? - Answers -Is a description
What does len do? - Answers -Returns the number of items in the list
What does min do? - Answers -Returns the minimum among the elements in the list
What does max do? - Answers -Returns the maximum among the elements in the list
,What does sum do? - Answers -Returns the sum of all the elements in the list. (applies
to list of numerical values only)
Given a list, you may use ________ to randomize the order of the elements in a list. -
Answers -Shuffle
What do you type to import the shuffle function and use the shuffle function with a list? -
Answers -from random import shuffle
shuffle(list1)
You can refer to elements of a list using the... - Answers -Subscript syntax
T or F: The code below makes third have a value of 83.0
scores = [75.0, 68.5, 83.0]
third = scores[2] - Answers -True
T or F: Negative indices count from the right end. - Answers -True
What is the value of third after running the code below?
scores = [75.0, 68.5, 83.0]
third = scores[-1] - Answers -83.0
Which is an example of negative indices? - Answers -scores = [75.0, 68.5, 83.0]
third = scores[-1]
Which is an example of positive indices? - Answers -scores = [75.0, 68.5, 83.0]
third = scores[2]
Slicing gives a... - Answers -List of elements
Subscripting gives a... - Answers -Single element
Which is an example of slicing a list? - Answers -scores = [68.5, 83.0]
exams = scores[1:3]
T or F: Each list is an object. - Answers -True
Which is an example of how you call a list method? - Answers -
list.methodName(arguments)
T or F: The in operator does not check for that exact method, and looks "inside"
elements. - Answers -False
, To search for an element in a list, you can use the... - Answers -in operator and index
method
Which is an example of using the in operator to search for an element in a list? -
Answers -Names = ["William", "Scott", "Sophia"]
if "William" in Names:
Which is an example of using the index method to search for an element's position in a
list? - Answers -Names = ["William", "Scott", "Sophia"]
loc = Names.index("Scott")
# loc is 1
Which is an example of using the index method to search for an element from the start
to the end of a list? - Answers -List.index(elem, start, end)
T or F: If the index method finds the element, it return its index (position) inside the list. -
Answers -True
T or F: A ValueError is given when the index method searches for the element and the
element is not between start and end in the list. - Answers -True
T or F: A Searchgive Error is not given when the index method is used and the element
is NOT found. - Answers -False
Which is an example of transversing a list with a for loop? - Answers -scores = [ 85, 72,
56, 98, 84, 72]
for grade in scores:
print(grade)
To get both indices and elements in transversing a list, use... - Answers -A loop
controlled with range(len(list)) with subscripting
for index in range(len(scores)):
print (index,"\t"
,grade[i])
Enumerate(mylist) to control the
for loop
for index, grade in enumerate(scores):
print (index,"\t"
,grade)
What is a List? - Answers -A data structure that can store many values of under one
name
Which is an example of how to create an empty list? - Answers -Variable = []
Variable = list()
Which is an example of how to create a list with predefined values? - Answers -colors =
["red, "yellow", "blue"]
To create a list with predefined values, you have to.... - Answers -Put a sequence of
items in []
What does list1.append(elem) do? - Answers -Adds a single element to the end of the
list
What does list1.insert(index, elem) do? - Answers -Inserts the element elem at the
given index, shifting elements to the right
What does list1.extend(list2) do? - Answers -Add the elements in list2 to the end of the
list.
T or F: The extend method is not only used with a list. - Answers -False
T or F: Using + or += on a list is similar to using the extend method. - Answers -True
How do you access the individual element in a list using an index? - Answers -
list1[index]
What are characteristics of Positive Indexes? - Answers -First element in the list has
index 0, second element has index 1, and n'th element is n-1
What are characteristics of Negative Indexes? - Answers -Identify positions relative to
the end of the list, index -1 identifies the last element, and -2 identifies the next to last
element, etc.
What does Function do? - Answers -Is a description
What does len do? - Answers -Returns the number of items in the list
What does min do? - Answers -Returns the minimum among the elements in the list
What does max do? - Answers -Returns the maximum among the elements in the list
,What does sum do? - Answers -Returns the sum of all the elements in the list. (applies
to list of numerical values only)
Given a list, you may use ________ to randomize the order of the elements in a list. -
Answers -Shuffle
What do you type to import the shuffle function and use the shuffle function with a list? -
Answers -from random import shuffle
shuffle(list1)
You can refer to elements of a list using the... - Answers -Subscript syntax
T or F: The code below makes third have a value of 83.0
scores = [75.0, 68.5, 83.0]
third = scores[2] - Answers -True
T or F: Negative indices count from the right end. - Answers -True
What is the value of third after running the code below?
scores = [75.0, 68.5, 83.0]
third = scores[-1] - Answers -83.0
Which is an example of negative indices? - Answers -scores = [75.0, 68.5, 83.0]
third = scores[-1]
Which is an example of positive indices? - Answers -scores = [75.0, 68.5, 83.0]
third = scores[2]
Slicing gives a... - Answers -List of elements
Subscripting gives a... - Answers -Single element
Which is an example of slicing a list? - Answers -scores = [68.5, 83.0]
exams = scores[1:3]
T or F: Each list is an object. - Answers -True
Which is an example of how you call a list method? - Answers -
list.methodName(arguments)
T or F: The in operator does not check for that exact method, and looks "inside"
elements. - Answers -False
, To search for an element in a list, you can use the... - Answers -in operator and index
method
Which is an example of using the in operator to search for an element in a list? -
Answers -Names = ["William", "Scott", "Sophia"]
if "William" in Names:
Which is an example of using the index method to search for an element's position in a
list? - Answers -Names = ["William", "Scott", "Sophia"]
loc = Names.index("Scott")
# loc is 1
Which is an example of using the index method to search for an element from the start
to the end of a list? - Answers -List.index(elem, start, end)
T or F: If the index method finds the element, it return its index (position) inside the list. -
Answers -True
T or F: A ValueError is given when the index method searches for the element and the
element is not between start and end in the list. - Answers -True
T or F: A Searchgive Error is not given when the index method is used and the element
is NOT found. - Answers -False
Which is an example of transversing a list with a for loop? - Answers -scores = [ 85, 72,
56, 98, 84, 72]
for grade in scores:
print(grade)
To get both indices and elements in transversing a list, use... - Answers -A loop
controlled with range(len(list)) with subscripting
for index in range(len(scores)):
print (index,"\t"
,grade[i])
Enumerate(mylist) to control the
for loop
for index, grade in enumerate(scores):
print (index,"\t"
,grade)