wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 22

Worksheet time: 11mins

Name
Class
Date
1.

What will this code output? numbers = [1, 2, 3] for n in numbers: print(n * 2)

a)

1 2 3

b)

2 4 6

c)

1 4 9

d)

Error

2.

Which keyword is used to loop through a list?

a)

loop

b)

iterate

c)

for

d)

each

3.

What is printed here? fruits = ["apple", "banana", "cherry"] print(fruits[1])

a)

apple

b)

banana

c)

cherry

d)

Error

4.

How many times will this print "Hi"? for i in range(3): print("Hi")

a)

1

b)

2

c)

3

d)

4

5.

What is the output of this code? nums = [2, 4, 6] for n in nums: print(n + 1)

a)

2 4 6

b)

3 5 7

c)

1 3 5

d)

2 5 7

6.

What does this function return? def add(x, y): return x + y print(add(3, 4))

a)

Error

b)

7

c)

34

d)

"x + y"

7.

What is the final value of total? numbers = [1, 2, 3, 4] total = 0 for n in numbers: total += n

a)

9

b)

10

c)

24

d)

0

8.

What will this code print? nums = [5, 10, 15] for n in nums: if n > 8: print(n)

a)

5 10 15

b)

10 15

c)

15

d)

5 15

9.

What does this loop print? names = ["Tom", "Sue", "Max"] for j in range(len(names)): print(names[i])

a)

Error

b)

Tom Sue Max

c)

0 1 2

d)

i i i

10.

How many times will this loop run? for i in range(2, 10, 2): print(i)

a)

3

b)

4

c)

5

d)

8

11.

What is the output? items = ["a", "b", "c"] for item in items: print(item.upper())

a)

A B C

b)

a b c

c)

['A', 'B', 'C']

d)

Error

12.

What will the output be? nums = [1, 2, 3, 4] new = [] for n in nums: if n % 2 == 0: new.append(n) print(new)

a)

[1, 3]

b)

[2, 4]

c)

[1, 2, 3, 4]

d)

[]

13.

What happens here? names = ["Ann", "Bob", "Cat"] for name in names: name = name.upper() print(names)

a)

['ANN', 'BOB', 'CAT']

b)

['Ann', 'Bob', 'Cat']

c)

['A', 'B', 'C']

d)

Error

14.

What will this code print? fruits = ["apple", "banana", "cherry"] print(fruits[-1])

a)

apple

b)

banana

c)

cherry

d)

Error

15.

What does this code output? for i in range(1, 4): print(i)

a)

1 2 3

b)

0 1 2

c)

1 2 3 4

d)

0 1 2 3

16.

What is the length of this list? colors = ["red", "green", "blue", "yellow"] print(len(colors))

a)

3

b)

4

c)

5

d)

Error

17.

What does this range generate? list(range(5, 0, -1))

a)

[5, 4, 3, 2, 1]

b)

[1, 2, 3, 4, 5]

c)

[5, 0]

d)

Error

18.

What is printed here? lst = ["a", "b", "c"] for i in range(len(lst)): lst[i] = lst[i].upper() print(lst)

a)

["a", "b", "c"]

b)

["A", "B", "C"]

c)

[‘upper’, ‘upper’, ‘upper’]

d)

Error

19.

What will this code output? numbers = [10, 20, 30] for n in numbers: print(n - 5)

a)

15 25 35

b)

10 20 30

c)

5 10 15

d)

5 15 25

20.

What does this code print? numbers = [1, 2, 3, 4] for n in numbers: if n < 3: print(n)

a)

Error

b)

1 2 3

c)

2 3

d)

1 2

21.

What will this code output? values = [1, 2, 3, 4, 5] print(values[0] + values[4])

a)

5

b)

6

c)

Error

d)

7

22.

How many elements will be printed? for i in range(1, 5): print(i)

a)

3

b)

4

c)

6

d)

5