Unit 3: String, Lists, Tuples and Dictionaries - Practice Quiz

INT108 — Python Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Why is a Python string called a compound data type?

Strings as a compound data type Easy
A. It combines every variable in a program into one object
B. It contains a sequence of characters
C. It automatically changes its characters
D. It stores only numeric values

2 What is the value of len("Python")?

String length Easy
A. 7
B. 8
C. 6
D. 5

3 Which statement visits every character in the string word?

String traversal Easy
A. for ch in word:
B. while word == ch:
C. if ch in word:
D. for word in range(ch):

4 What does "Programming"[0:3] return?

String slices Easy
A. "Programming", because slicing always returns the complete original string
B. "rog"
C. "Prog"
D. "Pro"

5 What is the result of "cat" == "cat" in Python?

String comparison Easy
A. "cat"
B. True
C. None
D. False

6 What does "banana".find("na") return?

Find function Easy
A. 1
B. A list containing every index where the substring "na" occurs
C. 2
D. 3

7 Which initial value should usually be assigned to a variable used to count matching characters?

Looping and counting Easy
A. -1
B. None
C. 0
D. 1

8 Which expression creates a Python list containing the values 1, 2, and 3?

List values Easy
A. (1, 2, 3)
B. [1, 2, 3]
C. <1, 2, 3>
D. {1, 2, 3}

9 What is the value of len([10, 20, 30, 40])?

List length Easy
A. 40
B. 3
C. 100, because len() adds all numeric elements in the list
D. 4

10 What is the result of 3 in [1, 2, 3, 4]?

Membership Easy
A. False
B. None
C. 3
D. True

11 What does [1, 2] + [3, 4] produce?

List operations Easy
A. [[1, 2], [3, 4]]
B. An error because the + operator can only combine numeric variables
C. [4, 6]
D. [1, 2, 3, 4]

12 What does [10, 20, 30, 40][1:3] return?

List slices Easy
A. [10, 20, 30]
B. [20, 30]
C. [10, 20]
D. [20, 30, 40]

13 Which statement deletes the element at index 1 from a list named items?

Deletion Easy
A. del items, which removes only the element stored at index 1
B. del items[1]
C. delete items[1]
D. items.delete(1)

14 Given colors = ["red", "green", "blue"], which expression accesses "green"?

Accessing elements Easy
A. colors[3]
B. colors[2]
C. colors[1]
D. colors[0]

15 What is printed by for value in [2, 4, 6]: print(value)?

Lists and for loops Easy
A. The indices 0, 1, and 2
B. Only the final value 6
C. One combined value equal to the sum of all three list elements
D. The values 2, 4, and 6

16 When a list is passed to a function and the function changes one of its elements, what can happen to the original list?

List parameters Easy
A. The function receives a completely independent copy that can never affect the original
B. The original list always becomes a tuple
C. The original list is automatically deleted
D. The original list can also change

17 Given matrix = [[1, 2], [3, 4]], what is the value of matrix[1][0]?

Nested lists Easy
A. 2
B. 3
C. 1
D. 4

18 Which statement about tuples is correct?

Mutability and tuples Easy
A. Tuple elements can always be replaced after the tuple is created
B. Tuples can only store strings
C. Tuples are immutable
D. Tuples use key-value pairs

19 After x, y = (5, 8), what values do x and y contain?

Tuple assignment Easy
A. x = 5, y = 8
B. x = 5, y = (5, 8)
C. x = 8, y = 5
D. x = (5, 8), y = 0

20 Given student = {"name": "Asha", "age": 18}, which expression returns "Asha"?

Dictionary operations and methods Easy
A. student.values("name"), because values() accepts a key and returns its value
B. student["name"]
C. student[0]
D. student["Asha"]

21 What is printed by the following code?

word = "Python"
result = word[1] + word[-2]
print(result)

Strings as a compound data type Medium
A. Pt
B. ot
C. yh
D. yo

22 What value is assigned to result?

text = "data science"
result = len(text[2:9])

String length Medium
A. 8
B. 9
C. 7
D. 6

23 What is printed by the following code?

text = "planet"
result = ""
for i in range(0, len(text), 2):
result += text[i]
print(result)

String traversal Medium
A. pnt
B. plt
C. lne
D. pae

24 What is the value of result after this statement?

text = "abcdefgh"
result = text[-2:1:-2]

String slices Medium
A. gdb
B. hfdb
C. gec
D. geca

25 Which expression evaluates to True in Python?

String comparison Medium
A. "apple" > "Banana"
B. "20" > "3"
C. "cat" < "catalog"
D. "Zoo" > "apple"

26 What is printed by the following code?

text = "abracadabra"
print(text.find("abra", 1))

Find function Medium
A. -1
B. 7
C. 0
D. 1

27 What is printed by this code?

text = "banana"
count = 0
for i in range(len(text) - 1):
if text[i:i + 2] == "an":
count += 1
print(count)

Looping and counting Medium
A. 4
B. 3
C. 1
D. 2

28 What does the following expression evaluate to?

values = [[1, 2], [3, 4], 5]
result = 3 in values

Membership Medium
A. TypeError
B. 3
C. True
D. False

29 What is the value of result?

a = [1, 2]
b = [3]
result = a * 2 + b

List operations Medium
A. [2, 4, 3]
B. [1, 2, 1, 2, 3]
C. [1, 2, 3, 3]
D. [1, 2, 2, 3]

30 What is printed by the following code?

nums = [0, 1, 2, 3, 4, 5]
nums[1:5:2] = [9, 8]
print(nums)

List slices Medium
A. [9, 1, 8, 3, 4, 5]
B. [0, 9, 2, 8, 4, 5]
C. [0, 9, 2, 3, 8, 5]
D. [0, 9, 8, 3, 4, 5]

31 What is printed by this code?

items = ['a', 'b', 'c', 'd', 'e']
del items[1:4:2]
print(items)

Deletion Medium
A. ['a', 'd', 'e']
B. ['a', 'c', 'e']
C. ['a', 'b', 'd', 'e']
D. ['b', 'd']

32 What is printed by the following code?

nums = [1, 2, 3]
for value in nums:
value *= 2
print(nums)

Lists and for loops Medium
A. [2, 4, 6]
B. [1, 2, 3]
C. [1, 4, 9]
D. [2, 2, 3]

33 What is printed by this program?

def update(values):
values.append(4)
values = [9, 9]

nums = [1, 2, 3]
update(nums)
print(nums)

List parameters Medium
A. [1, 2, 3, 4]
B. [9, 9]
C. [9, 9, 4]
D. [1, 2, 3]

34 What is printed by the following code?

matrix = [[1, 2], [3, 4], [5, 6]]
total = 0
for row in matrix:
total += row[1]
print(total)

Nested lists Medium
A. 9
B. 15
C. 12
D. 10

35 What is printed by the following code?

data = ([1, 2], 3)
data[0].append(4)
print(data)

Mutability and tuples Medium
A. ([1, 2, 4], 3)
B. ([1, 2], 3, 4)
C. ([1, 2], 4)
D. TypeError

36 What is printed by this code?

x, y, z = 2, 4, 6
x, y, z = z, x + y, y - x
print(x, y, z)

Tuple assignment Medium
A. 6 10 -4
B. 6 8 2
C. 2 6 6
D. 6 6 2

37 What is printed by the following program?

def bounds(values):
return min(values), max(values)

low, high = bounds([8, 3, 11, 5])
print(high - low)

Tuples as return values Medium
A. 14
B. 6
C. 8
D. 11

38 What is printed by this code?

counts = {'a': 2, 'b': 1}
for ch in "abac":
counts[ch] = counts.get(ch, 0) + 1
print(counts['a'], counts['c'])

Dictionary operations and methods Medium
A. 4 0
B. 4 1
C. 2 1
D. 3 1

39 A sparse matrix stores only nonzero entries in a dictionary using (row, column) keys. What is printed?

matrix = {(0, 2): 5, (2, 1): 7}
total = 0
for col in range(3):
total += matrix.get((2, col), 0)
print(total)

Sparse matrices Medium
A. 12
B. 7
C. 0
D. 5

40 What is printed by the following code?

original = [[1], [2]]
copied = original[:]
copied[0].append(3)
copied.append([4])
print(original)

Aliasing and copying Medium
A. [[1, 3], [2], [4]]
B. [[1], [2]]
C. [[1], [2], [4]]
D. [[1, 3], [2]]

41 What is printed by the following code?

PYTHON
s = 'programming'
result = s[1] + s[-2] + s[len(s) // 2]
print(result)

Strings as a compound data type Hard
A. rgn
B. rnm followed by a newline character
C. rnm
D. pim

42 What value does out contain after this traversal?

PYTHON
s = 'abcdef'
out = []
for i, ch in enumerate(s):
    if i % 2 == 0:
        out.append(s[-i - 1])

String traversal Hard
A. ['f', 'c', 'a']
B. ['f', 'd', 'b']
C. ['e', 'c', 'a']
D. ['a', 'c', 'e']

43 What is printed by the following code?

PYTHON
s = '0123456789'
a = s[-2:1:-3]
b = s[1:-1][::-3]
print(a + ':' + b)

String slices Hard
A. 852:741
B. 258:258
C. 963:852
D. 852:852

44 Given Python's lexicographic string comparison, what is the value of the following expression?

PYTHON
words = ['9', '10', '2']
(min(words), max(words), '10' < '2')

String comparison Hard
A. ('10', '9', True)
B. ('10', '9', False)
C. ('2', '10', False)
D. ('9', '2', True)

45 What tuple is produced by this code?

PYTHON
s = 'ababaabababa'
a = s.find('ababa', 1)
b = s.rfind('ababa', 0, 10)
print((a, b))

Find function Hard
A. (5, 7)
B. (-1, 5)
C. (5, 5)
D. (7, 5)

46 What does count('aaaaa', 'aa') return?

PYTHON
def count(s, sub):
    total = 0
    start = 0
    while True:
        position = s.find(sub, start)
        if position == -1:
            return total
        total += 1
        start = position + 1

Looping and counting Hard
A. 5
B. 2
C. 4
D. 3

47 What is the value of the final expression?

PYTHON
a = [1, [2, 3], []]
a.append(a[1])
a[1].append(4)
(len(a), len(a[-1]), a[-1][-1])

List values Hard
A. (4, 2, 4)
B. (3, 3, 4)
C. (4, 2, 3)
D. (4, 3, 4)

48 What is the value of the final expression?

PYTHON
items = [[1, 2], [3, [4]], '12']
(
    2 in items,
    [1, 2] in items,
    4 in items[1],
    '1' in items[2]
)

Membership Hard
A. (False, True, False, True)
B. (True, True, True, False)
C. (True, False, False, True)
D. (False, True, True, True)

49 What is printed by this code?

PYTHON
a = [1, 2]
b = a
c = a + [3]
a += [4]
print((b, c, a is b))

List operations Hard
A. ([1, 2, 4], [1, 2, 3], True)
B. ([1, 2, 4], [1, 2, 3, 4], True)
C. ([1, 2], [1, 2, 3], True)
D. ([1, 2], [1, 2, 3], False)

50 What is the final value of a?

PYTHON
a = list(range(8))
a[1:7:2] = [9, 8, 7]
a[2:6] = [5]

List slices Hard
A. [0, 9, 5, 6, 7]
B. [0, 9, 5, 7, 6, 7]
C. [0, 5, 4, 7, 6, 7]
D. [0, 9, 5, 4, 7]

51 What is printed by the following code?

PYTHON
a = [0, 1, 2, 3, 4, 5]
del a[1::2]
x = a.pop(-2)
del a[:1]
print((x, a))

Deletion Hard
A. (2, [4])
B. (4, [2])
C. (3, [4, 5])
D. (2, [2, 4])

52 What value is assigned to result?

PYTHON
matrix = [[i + j for j in range(3)] for i in range(3)]
result = matrix[-1][-matrix[0][1]]

Accessing elements Hard
A. 3
B. 4
C. 2
D. 1

53 What is the value of a after the loop completes?

PYTHON
a = [2, 4, 6, 8]
for value in a:
    if value % 2 == 0:
        a.remove(value)

Lists and for loops Hard
A. [4, 6]
B. [2, 6]
C. []
D. [4, 8]

54 What is printed by this code?

PYTHON
def f(x, y=[]):
    y.append(x.pop())
    x = x + [0]
    return x, y

a = [1, 2]
r1 = f(a)
r2 = f(a)
print((a, r1, r2))

List parameters Hard
A. ([1, 2], ([1, 0], [2]), ([0], [1]))
B. ([], ([1, 0], [2, 1]), ([0], [2, 1]))
C. ([], ([1, 0], [2]), ([0], [1]))
D. ([], ([1, 0], [2]), ([0], [2, 1]))

55 What is printed by the following code?

PYTHON
grid = [[0] * 3] * 2
grid[0][1] = 7
grid[1] = grid[1][:]
grid[1][2] = 9
print((grid, grid[0] is grid[1]))

Nested lists Hard
A. ([[0, 7, 0], [0, 7, 9]], False)
B. ([[0, 7, 9], [0, 7, 9]], True)
C. ([[0, 7, 0], [0, 0, 9]], False)
D. ([[0, 7, 9], [0, 7, 9]], False)

56 What is printed after the exception is handled?

PYTHON
t = ([1, 2], 'x')
try:
    t[0] += [3]
except TypeError:
    pass
print(t)

Mutability and tuples Hard
A. ([1, 2, 3], 'x')
B. ([1, 2], 'x')
C. Nothing is printed
D. ([3], 'x')

57 What is printed by this code?

PYTHON
a = [0, 1, 2]
i = 0
i, a[i] = 1, 9
print((i, a))

Tuple assignment Hard
A. (1, [0, 9, 2])
B. (1, [9, 1, 2])
C. (1, [0, 1, 9])
D. (0, [0, 9, 2])

58 What is printed by the following code?

PYTHON
def stats(values):
    return min(values), max(values), sum(values)

low, *middle, high = stats([3, 1, 4, 2])
middle.append(high - low)
print((low, middle, high))

Tuples as return values Hard
A. (1, [10, 3], 4)
B. (1, [4, 9], 10)
C. (1, [4, 3], 10)
D. (1, [4, 10], 3)

59 What is printed in Python 3.7 or later, where dictionaries preserve insertion order?

PYTHON
d = {'a': 1, 'b': 2}
keys = d.keys()
x = d.setdefault('a', 9)
y = d.setdefault('c', 3)
d.update({'b': 5, 'd': 4})
p = d.pop('b')
print((list(keys), x, y, p))

Dictionary operations and methods Hard
A. (['a', 'b'], 9, 3, 2)
B. (['a', 'b', 'c'], 1, 3, 2)
C. (['a', 'c', 'd'], 1, 3, 5)
D. (['a', 'c', 'd'], 9, 3, 5)

60 A sparse matrix stores only nonzero entries in a dictionary. What is printed by this code?

PYTHON
matrix = {(0, 2): 4, (2, 1): -3}
alias = matrix
clone = matrix.copy()
alias[(1, 1)] = clone.get((1, 1), 0) + 5
del matrix[(0, 2)]
print((
    clone.get((0, 2), 0),
    alias.get((0, 2), 0),
    matrix.get((1, 1), 0),
    clone.get((1, 1), 0)
))

Sparse matrices Hard
A. (4, 0, 5, 0)
B. (4, 4, 5, 0)
C. (0, 0, 5, 5)
D. (4, 0, 0, 5)