wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Quiz

Total questions: 40

Worksheet time: 15mins

Name
Class
Date
1.

What will print([1, 2, 3] + [4, 5]) output?

a)

[1, 2, 3, 4, 5]

b)

[5, 4, 3, 2, 1]

c)

Error

d)

[1, 2, 3, [4, 5]]

2.

Which of the following will create a shallow copy of a list a?

a)

b = a[:]

b)

b = a.copy()

c)

b = a[:]
and
b = a.copy()
both options correct

d)

None of the above

3.

What will print(len([[1, 2], [3, 4]])) return?

a)

2

b)

4

c)

8

d)

Error

4.

Which of the following will raise an error for tuple a?

a)

a = (1, 2)

b)

a[0] = 3

c)

len((1, 2, 3))

d)

(1, 2) + (3,)

5.

What is the output of print((1, 2) * 2)?

a)

(1, 2, 1, 2)

b)

(1, 2, 2)

c)

Error

d)

[1, 2, 1, 2]

6.

Which method is used to get the value associated with a key in a dictionary?

a)

value()

b)

get()

c)

key()

d)

items()

7.

What will print(len({1: "a", 2: "b"})) return?

a)

1

b)

2

c)

3

d)

4

8.

What will print({}.get("key", "default")) output?

a)

None

b)

default

c)

key

d)

Error

9.

Which of the following will raise an error?

a)

{1: "a", 2: "b"}[1]

b)

{1: "a", 2: "b"}["c"]

c)

{1: "a", 2: "b"}.get(3)

d)

{}

10.

What will print({1, 2, 3} | {3, 4, 5}) output?

a)

Error

b)

{1, 2, 4, 5}

c)

{3}

d)

{1, 2, 3, 4, 5}

11.

Which method removes an element from a set without raising an error if the element is absent?

a)

remove()

b)

pop()

c)

discard()

d)

clear()

12.

What will print({1, 2, 3} & {2, 3, 4}) output?

a)

{1, 2, 3, 4}

b)

{2, 3}

c)

{1, 4}

d)

Error

13.

What is the result of print(len({1, 2, 2, 3}))?

a)

3

b)

4

c)

Error

d)

2

14.

What happens when print({1, 2, [3, 4]}) is executed?

a)

{1, 2, [3, 4]}

b)

{1, 2}

c)

Error

d)

None of the above

15.

Evaluate the code:
a = {1, 2, 3}
b = a.pop(1)

a)

Sets do not support pop()

b)

pop() does not accept arguments

c)

Sets are immutable

d)

None of the above

16.

Evaluate the following statement in Python and correct the error:
my_dict = {[1, 2]: "value"}

a)

Replace [1, 2] with (1, 2)

b)

Replace [1, 2] with 1, 2

c)

Use dict() instead

d)

None of the above

17.

What will print({x**2 for x in range(3)}) output?

a)

{1, 4, 0}

b)

{4, 0, 1}

c)

{0, 1, 4}

d)

Error

18.

What is the output of print(2 in {1, 2, 3})?

a)

True

b)

False

c)

None

d)

Error

19.

What will the following code output?
for i in range(3):
for j in range(2):
print(i * j, end=" ")

a)

0 0 0 1 0 2

b)

0 0 0 0 0 1

c)

0 0 0 1

d)

0 0 0 0 1 2

20.

Error Correction: Fix the code to break the loop:
i = 0

while True:

print(i)

if i == 5:

break

a)

Add i += 1 before break

b)

Use for instead of while

c)

Add i += 1 after print(i)

d)

Replace break with continue

21.

What will be the output of the following code?
s = "hello"
print(s[-1:-5:-1])

a)

olleh

b)

hello

c)

olle

d)

Error

22.

What is the result of
''.join(sorted("cabac"))?

a)

cabac

b)

aabcc

c)

abacc

d)

Error

23.

Which of the following results in a valid deep copy of a list?

a)

b = a.copy()

b)

b = copy.deepcopy(a)

c)

b = a[:]

d)

b = list(a)

24.

What is the output of print((1, 2) * 0)?

a)

()

b)

(1, 2)

c)

[]

d)

Error

25.

What will be the result of the following code?
t = (1, [2, 3], 4)
t[1][0] = 0
print(t)

a)

(1, [2, 3], 4)

b)

(1, [0, 3], 4)

c)

Error

d)

(1, 2, 3, 4)

26.

What does the following code output?
d = {"a": 1, "b": 2}
print(d.get("c", d["a"]))

a)

None

b)

1

c)

KeyError

d)

2

27.

What will the following code output?
s1 = {1, 2, 3}
s2 = {3, 4, 5}
print(s1 - s2)

a)

{1, 2}

b)

{4, 5}

c)

{1, 2, 3, 4, 5}

d)

Error

28.

Which of the following will raise an error?

a)

{1, 2}.add(3)

b)

{1, 2}.update({3, 4})

c)

{1, 2}.remove(3)

d)

{1, 2}.discard(3)

29.

What will happen in the following code?
def func(a=[]):

a.append(1)

return a

print(func(), end=", ")

print(func())

a)

[1], [1]

b)

[1], [1, 1]

c)

Error

d)

[1], []

30.

What will the following code do?

x = 0
while x < 3:

print(x, end=" ")

x += 1

else:

print("Done")

a)

0 1 2

b)

0 1 2 Done

c)

Done

d)

Error

31.

What will the following code output?
result = [x**2 for x in range(5) if x % 2 == 0]
print(result)

a)

[0, 1, 4, 9, 16]

b)

[0, 4, 16]

c)

[1, 9]

d)

Error

32.

What does the following code produce?
result = [x + y for x in "abc" for y in "12"]
print(result)

a)

['a1', 'a2', 'b1', 'b2', 'c1', 'c2']

b)

['a1b2', 'c1']

c)

['a1', 'b2', 'c1', 'c2']

d)

Error

33.

Which of the following comprehensions creates a list of tuples (x, y) where x is odd and y is even for x, y ∈ range(4)? Consider 0 in Even.
A. [(x, y) for x in range(4) for y in range(4) if x % 2 == 1 and y % 2 == 0]

B. [(x, y) for x in range(4) if x % 2 == 1 for y in range(4) if y % 2 == 0]

C. [(x, y) for y in range(4) if y % 2 == 0 for x in range(4) if x % 2 == 1]

a)

A

b)

B

c)

C

d)

All of the above

34.

What will print([x for x in range(10) if x < 5 if x % 2 == 0]) output?

a)

[0, 1, 2, 3, 4]

b)

[0, 2, 4]

c)

[0, 4]

d)

Error

35.

What will print([x if x % 2 == 0 else -x for x in range(5)]) output?

a)

[0, 1, 2, 3, 4]

b)

[0, -1, 2, -3, 4]

c)

[0, -1, -2, -3, -4]

d)

Error

36.

What does this code output?
result = [[x * y for x in range(3)] for y in range(3)]
print(result)

a)

[[0, 0, 0], [0, 1, 2], [0, 2, 4]]

b)

[[0, 0, 0], [1, 2, 3], [4, 5, 6]]

c)

[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

d)

Error

37.

What will be the output of the following code?
result = [x for x in range(10) if x in [y for y in range(5, 15)]]

print(result)

a)

[5, 6, 7, 8, 9]

b)

[0, 1, 2, 3, 4]

c)

[10]

d)

Error

38.

What does this comprehension produce?

result = [x + y for x, y in zip(range(3), range(3, 6))]

print(result)

a)

[3, 5, 7]

b)

[3, 4, 5]

c)

[4, 5, 6]

d)

Error

39.

What does the del keyword do in Python?

a)

Deletes variables and objects from memory

b)

Deletes keys from dictionaries

c)

Deletes elements from a list

d)

All of the above

40.

Which of the following statements about Python keywords is true?

a)

Python keywords can be used as variable names

b)

The is keyword compares identity, not equality

c)

The def keyword is used for declaring classes

d)

The break keyword is used to skip iterations in loops