NEW
Font size
WorksheetsPython Programming Quiz
Total questions: 22
Worksheet time: 11mins
What will this code output? numbers = [1, 2, 3] for n in numbers: print(n * 2)
1 2 3
2 4 6
1 4 9
Error
Which keyword is used to loop through a list?
loop
iterate
for
each
What is printed here? fruits = ["apple", "banana", "cherry"] print(fruits[1])
apple
banana
cherry
Error
How many times will this print "Hi"? for i in range(3): print("Hi")
1
2
3
4
What is the output of this code? nums = [2, 4, 6] for n in nums: print(n + 1)
2 4 6
3 5 7
1 3 5
2 5 7
What does this function return? def add(x, y): return x + y print(add(3, 4))
Error
7
34
"x + y"
What is the final value of total? numbers = [1, 2, 3, 4] total = 0 for n in numbers: total += n
9
10
24
0
What will this code print? nums = [5, 10, 15] for n in nums: if n > 8: print(n)
5 10 15
10 15
15
5 15
What does this loop print? names = ["Tom", "Sue", "Max"] for j in range(len(names)): print(names[i])
Error
Tom Sue Max
0 1 2
i i i
How many times will this loop run? for i in range(2, 10, 2): print(i)
3
4
5
8
What is the output? items = ["a", "b", "c"] for item in items: print(item.upper())
A B C
a b c
['A', 'B', 'C']
Error
What will the output be? nums = [1, 2, 3, 4] new = [] for n in nums: if n % 2 == 0: new.append(n) print(new)
[1, 3]
[2, 4]
[1, 2, 3, 4]
[]
What happens here? names = ["Ann", "Bob", "Cat"] for name in names: name = name.upper() print(names)
['ANN', 'BOB', 'CAT']
['Ann', 'Bob', 'Cat']
['A', 'B', 'C']
Error
What will this code print? fruits = ["apple", "banana", "cherry"] print(fruits[-1])
apple
banana
cherry
Error
What does this code output? for i in range(1, 4): print(i)
1 2 3
0 1 2
1 2 3 4
0 1 2 3
What is the length of this list? colors = ["red", "green", "blue", "yellow"] print(len(colors))
3
4
5
Error
What does this range generate? list(range(5, 0, -1))
[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]
[5, 0]
Error
What is printed here? lst = ["a", "b", "c"] for i in range(len(lst)): lst[i] = lst[i].upper() print(lst)
["a", "b", "c"]
["A", "B", "C"]
[‘upper’, ‘upper’, ‘upper’]
Error
What will this code output? numbers = [10, 20, 30] for n in numbers: print(n - 5)
15 25 35
10 20 30
5 10 15
5 15 25
What does this code print? numbers = [1, 2, 3, 4] for n in numbers: if n < 3: print(n)
Error
1 2 3
2 3
1 2
What will this code output? values = [1, 2, 3, 4, 5] print(values[0] + values[4])
5
6
Error
7
How many elements will be printed? for i in range(1, 5): print(i)
3
4
6
5
