I Hate CBT's

View Original

7.1.9 Diving Contest

Question: 8.1 What will the following code display?
numbers = [1, 2, 3, 4, 5]
numbers[2] = 99
print(numbers)
Answer: [1, 2, 99, 4, 5]
==================================================
Question: 8.2 What will the following code display?
numbers = list(range(3))
print(numbers)
Answer: [0, 1, 2]
==================================================
Question: 8.3 What will the following code display?
numbers = [10] * 5
print(numbers)
Answer: [10, 10, 10, 10, 10]
==================================================
Question: 8.4 What will the following code display?
numbers = list(range(1, 10, 2))
for n in numbers:
print(n)
Answer: 1
3
5
7
9
==================================================
Question: 8.5 What will the following code display?
numbers = [1, 2, 3, 4, 5]
print(numbers[-2])
Answer: 4
==================================================
Question: 8.6 How do you find the number of elements in a list?
Answer: Use the built-in len function.
==================================================
Question: 8.7 What will the following code display?
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers3 = numbers1 + numbers2
print(numbers1)
print(numbers2)
print(numbers3)
Answer: [1, 2, 3]
[10, 20, 30]
[1, 2, 3, 10, 20, 30]
==================================================
Question: 8.8 What will the following code display?
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers2 += numbers1
print(numbers1)
print(numbers2)
Answer: [1, 2, 3]
[10, 20, 30, 1, 2, 3]
==================================================
Question: 8.9 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:3]
print(my_list)
Answer: What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:3]
print(my_list)
==================================================
Question: 8.10 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:]
print(my_list)
Answer: [2, 3]
==================================================
Question: 8.11 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[:1]
print(my_list)
Answer: [1]
==================================================
Question: 8.12 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[:]
print(my_list)
Answer: [1, 2, 3, 4, 5]
==================================================
Question: 8.13 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[-3:]
print(my_list)
Answer: [3, 4, 5]
==================================================
Question: 8.15 What is the difference between calling a list's remove method and using the del
statement to remove an element?
Answer: The remove method searches for and removes an element containing a
specific value. The del statement removes an element at a specific index.
==================================================
Question: 8.16 How do you find the lowest and highest values in a list?
Answer: You can use the built-in min and max functions.
==================================================
Question: outfile = open('cities.txt', 'w')
outfile.writelines(cities)
outfile.close()
Answer: opens file and sets to write
writes the list
closes file
==================================================
Question: 8.22 What is the primary difference between a list and a tuple?
Answer: The primary difference between tuples and lists is that tuples are immutable.
That means that once a tuple is created, it cannot be changed.
==================================================
Question: 8.23 Give two reasons why tuples exist.
Answer: Here are three reasons:
• Processing a tuple is faster than processing a list, so tuples are good choices when you are processing lots of data and that data will not be modified.
• Tuples are safe. Because you are not allowed to change the contents of a tuple, you can store data in one and rest assured that it will not be modified (accidentally or otherwise) by any code in your program.
• There are certain operations in Python that require the use of a tuple.
==================================================
Question: 8.24 Assume that my_list references a list. Write a statement that converts it to a
tuple.
Answer: my_tuple = tuple(my_list)
==================================================
Question: 8.25 Assume that my_tuple references a tuple. Write a statement that converts it to a
list.
Answer: my_list = list(my_tuple)
==================================================
Question: append(item)
Answer: Adds item to the end of the list.
==================================================
Question: index(item)
Answer: Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list.
==================================================
Question: insert(index, item)
Answer: Inserts item into the list at the specified index.
==================================================
Question: sort()
Answer: Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value).
==================================================
Question: remove(item)
Answer: Removes the first occurrence of item from the list. A ValueError exception is raised if item is not found in the list.
==================================================
Question: reverse()
Answer: Reverses the order of the items in the list.
==================================================
Question: This term refers to an individual item in a list
Answer: element
==================================================
Question: This is a number that identifies a character in a string or an item in a list.
Answer: index
==================================================
Question: This is the last index in a string or a list.
Answer: The size of the string or list minus one.
==================================================
Question: This function returns the length of a string or a list.
Answer: length, len()
==================================================
Question: T/F: Strings in Python are immutable
Answer: True
==================================================
Question: This built-in function returns the highest value in a list.
Answer: max
==================================================
Question: This file object method returns a list containing the file's contents.
Answer: readlines
==================================================
Question: Which of the following statements creates a tuple?
Answer: values = (1,)
==================================================
Question: How many elements are in the list numbers = [10, 20, 30, 40, 50] ?
Answer: 5, can also be found by len(numbers) in a python program.
==================================================
Question: Out of Lists, Tuples, and Strings, which are immutable? (meaning you can't change them)
Answer: Tuples and Strings
==================================================
Question: What is the index of the first element in list numbers = [10, 20, 30, 40, 50] ?
Answer: 0
==================================================
Question: What is the index of the last element in list numbers = [10, 20, 30, 40, 50] ?
Answer: 4
==================================================
Question: What will the following code display?
values = [2, 4, 6, 8, 10] print(values[1:3])
Answer: [4, 6]
==================================================
Question: What method do you use to add an item to the end of an existing list?
Answer: append
==================================================
Question: Create a blank list
Answer: myList = []
==================================================
Question: How to add integer 10 to a blank list
Answer: myList.append(10)
==================================================
Question: What's the difference between infile.readline() and infile.readlines()
Answer: infile.readline() will read the next line only and infile.readlines() will take all the lines in a file and put them as string elements in a list.
==================================================
Question: When the * operator's left operand is a list and its right operand is an integer, the operator becomes this.
a. The multiplication operator
b. The repetition operator
c. The initialization operator
d. Nothing—the operator does not support those types of operands.
Answer: b. The repetition operator
==================================================