PYTHON multiple choice questions
1a) Given the following list, what value is at index 5?
values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1b) 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?
1c) Which statement correctly identifies the error in this code snippet?
scores = [80, 85, 60.5, 95, 70] print(scores[5])
| DataTypeError: cannot mix integer and floating point numbers |
| There is no error. The code runs and prints 70. |
| NameError: name 'scores' is not defined |
| IndexError: list index out of range |
1d) What is the difference between a list and a string?
| lists are sequences but strings are not and therefore you cannot access a string element using [] |
| lists are immutable, but strings can be changed |
| lists can hold values of any type, whereas strings are only sequences of characters |
| elements in a list can be accessed using an integer as the index value, but strings can use any numeric data type for the index |
1e) Given a list values that contains the first ten prime numbers, which statement prints the index and value of each element in the list?
| for i in range(11): print(i, values[i]) |
| for i in range(10): print(i, values[i]) |
| for i in range(1, 10): print(i, values[i]) |
| for i in range(1, 11): print(i, values[i]) |
1f) 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
| [10.00, 15.50, 13.50, 20.15] |
| [10.60, 16.43, 14.31, 21.36] |