Unit 3: Strings and Lists; Tuples and Dictionaries - Practice Quiz
1 Why is a string considered a compound data type in Python?
2 Which function returns the number of characters in a string?
length()
len()
size()
count()
3 What is the index of the first character in a Python string?
length
-1
1
0
4
What is the result of "python"[0:3]?
"tho"
"pyt"
"python"
"pyth"
5 Which loop is commonly used to traverse each character of a string?
with statement
try block
for loop
if statement
6
What does the expression "apple" == "apple" evaluate to?
True
None
False
0
7
What does "hello".find("l") return?
1
2
3
-1
8
What does the find() method return when the substring is not present?
-1
0
None
9 What is a common use of a counter variable while looping through a string?
10 Which of the following correctly creates a list of three numbers?
<1, 2, 3>
{1, 2, 3}
(1, 2, 3)
[1, 2, 3]
11
What does len([10, 20, 30, 40]) return?
0
5
3
4
12
What does the expression 3 in [1, 2, 3] evaluate to?
False
True
None
3
13
What is the result of [1, 2] + [3, 4]?
[4, 6]
[3, 4, 1, 2]
[1, 2, 3, 4]
[1, 2, [3, 4]]
14
What is the result of [10, 20, 30, 40][1:3]?
[10, 20]
[30, 40]
[20, 30]
[20, 30, 40]
15 Which keyword is used to remove an item from a list by its index?
del
remove
erase
drop
16
What is printed by for x in [1, 2, 3]: print(x)?
1, 2, 3 on separate lines
[1, 2, 3]
3
6
17 What is a nested list?
18 Which symbol pair is typically used to create a tuple?
{}
<>
[]
()
19 What key property makes tuples different from lists?
20 Which of the following correctly creates a dictionary?
("a", 1, "b", 2)
{"a": 1, "b": 2}
{"a", 1, "b", 2}
["a": 1, "b": 2]
21
What is the output of the following code?
s = "Programming"
print(s[2:8:2])
orm
oai
ogam
rgamn
22
What does the following loop print?
word = "abc"
for ch in word:
print(ch * 2, end="")
abc
abcabc
aabbcc
aaabbbccc
23
What is the result of the following code?
s = "banana"
print(s.find("na"))
-1
3
4
2
24
What is the output of the following comparison?
print("apple" < "banana")
False
True
0
Error
25
How many times does the letter s appear based on this code?
text = "mississippi"
count = 0
for c in text:
if c == "s":
count += 1
print(count)
5
2
4
3
26
What is the output of the following code?
s = "Hello, World!"
print(len(s))
12
14
11
13
27 Which statement about Python strings is correct?
s[0] = "X" raises an error
28
What is the output of the following code?
result = [1, 2] + [3, 4] * 2
print(result)
[4, 8, 3, 4]
[1, 2, 6, 8]
[1, 2, 3, 4, 3, 4]
[1, 2, 3, 4, 1, 2, 3, 4]
29
What does the following code print?
nums = [10, 20, 30, 40, 50]
print(nums[-3:-1])
[40, 50]
[30, 40, 50]
[20, 30]
[30, 40]
30
What is the output of the following code?
data = [[1, 2], [3, 4]]
print(3 in data)
True
Error
False
[3, 4]
31
What is the value of nums after this code runs?
nums = [1, 2, 3, 4, 5]
del nums[1:3]
print(nums)
[1, 2, 5]
[1, 4, 5]
[2, 3, 4, 5]
[1, 3, 4, 5]
32
What does the following code print?
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[1][2])
6
4
5
3
33
What is the output of the following code?
nums = [1, 2, 3, 4]
total = 0
for n in nums:
total += n * n
print(total)
20
30
10
100
34
What does the following code print?
def modify(lst):
lst.append(99)
nums = [1, 2, 3]
modify(nums)
print(nums)
[99]
[1, 2, 3, 99]
[99, 1, 2, 3]
[1, 2, 3]
35
What happens when the following code is executed?
t = (1, 2, 3)
t[0] = 10
print(t)
(10, 2, 3)
(1, 2, 3)
TypeError
[10, 2, 3]
36
What is the output of the following code?
t = (1, 2) * 3
print(len(t))
5
6
3
2
37
What is the output of the following code?
d = dict([("a", 1), ("b", 2)])
print(d["b"])
("b", 2)
b
1
2
38
What does the following code print?
d = {"x": 1}
d["y"] = 2
d["x"] = 5
print(d)
{'x': 5}
{'x': 1, 'y': 2}
{'x': 1, 'y': 2, 'x': 5}
{'x': 5, 'y': 2}
39
What is the output of the following code?
d = {"a": 1, "b": 2, "c": 3}
del d["b"]
print(list(d.keys()))
['b']
['a', 'b', 'c']
['a', 'c']
['a', 'c', 'b']
40
What is the output of the following code?
d = {"a": 1, "b": 2}
print(d.get("c", 0))
c
Error
0
None
41
What is the output of the following code?
s = "programming"
print(s[::-2])
gimargo
pormig
gnimargorp
gimrgm
42
Which expression evaluates to True in Python 3?
"ABC" > "abc"
"apple" < "Apple"
"Apple" < "apple"
"Z" < "A"
43
What does the following print?
s = "mississippi"
print(s.find("ss", 3))
2
4
-1
5
44
What is the value of count after this loop?
s = "aAbBcCaA"
count = 0
for ch in s:
if ch.lower() == 'a':
count += 1
print(count)
2
4
8
3
45
What is the output?
s = "abcde"
result = ""
for i in range(len(s)):
result += s[i] * i
print(result)
aabbbcccc
abbcccdddd
abcde
bccdddeeee
46
What is printed?
a = [1, 2, 3]
b = a * 2
b[0] = 99
print(a[0], b[0])
1 99
1 1
99 99
99 1
47
What is the output?
def modify(lst):
lst = lst + [4]
lst.append(5)
x = [1, 2, 3]
modify(x)
print(x)
[1, 2, 3, 5]
[1, 2, 3]
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
48
What is the result?
lst = [0, 1, 2, 3, 4, 5]
lst[1:4] = [10, 20]
print(lst)
[10, 20, 3, 4, 5]
[0, 10, 20, 2, 3, 4, 5]
[0, 10, 20, 3, 4, 5]
[0, 10, 20, 4, 5]
49
What is the output?
lst = [10, 20, 30, 40, 50]
del lst[1:4:2]
print(lst)
[10, 30, 40, 50]
[20, 40]
[10, 40, 50]
[10, 30, 50]
50
What does this print?
matrix = [[1, 2], [3, 4], [5, 6]]
total = 0
for row in matrix:
total += row[-1]
print(total)
21
9
12
6
51
What is the output?
nested = [[1, 2], [3, 4]]
print(3 in nested, [3, 4] in nested)
False True
False False
True False
True True
52
What is printed?
grid = [[0]*3]*3
grid[0][0] = 1
print(grid)
[[1, 0, 0], [0, 0, 0], [1, 0, 0]]
[[1, 1, 1], [0, 0, 0], [0, 0, 0]]
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
53 Which of the following creates a tuple containing a single integer element?
t = (5)
t = tuple(5)
t = (5,)
t = 5
54
What is the output?
t = (1, [2, 3], 4)
t[1].append(5)
print(t)
(1, [2, 3], 4)
(1, [2, 3], 4, 5)
TypeError raised
(1, [2, 3, 5], 4)
55
What does the following produce?
d = dict(zip("abc", [1, 2, 3, 4]))
print(d)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
ValueError raised
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2, 'c': 3, None: 4}
56
What is printed?
d = {'x': 1}
print(d.get('y', 0) + d.setdefault('y', 5))
print(d)
0 then {'x': 1}
5 then {'x': 1, 'y': 5}
10 then {'x': 1, 'y': 5}
5 then {'x': 1}
57
What is the output?
d = {'a': 1, 'b': 2, 'c': 3}
val = d.pop('b')
res = d.pop('z', -1)
print(val, res, len(d))
2 -1 3
None -1 2
2 -1 2
2 None 2
58
What does this print?
d = {'a': 1, 'b': 2}
d.update({'b': 20, 'c': 30})
print(sum(d.values()))
23
33
51
53
59
What is the output?
d = {1: 'a', 2: 'b'}
for k in list(d.keys()):
d[k*10] = d[k]
print(len(d))
RuntimeError raised
4
2
6
60
What is printed?
s = "a\tb\nc"
print(len(s))
3
5
6
7
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →