wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python quiz

Total questions: 10

Worksheet time: 8mins

Name
Class
Date
1.

Which one is NOT a legal variable name?

a)

my_var

b)

Myvar

c)

_myvar

d)

my-var

2.

What is the correct syntax to output the type of a variable or object in Python?

a)

print(typeof(x))

b)

print(type(x))

c)

print(typeof x)

d)

print(typeOf(x))

3.

Which method can be used to remove any whitespace from both the beginning and the end of a string?

a)

ptrim()

b)

len()

c)

strip()

d)

trim()

4.

A while loop in Python is used for what type of iteration?

a)

indefinite

b)

discriminant

c)

definite

d)

indeterminate

5.

Which of these collections defines a LIST?

a)

["apple", "banana", "cherry"]

b)

{"apple", "banana", "cherry"}

c)

("apple", "banana", "cherry")

d)

{"name": "apple", "color": "green"}

6.

What is the output of the following code snippet:


a = ['foo', 'bar', 'baz', 'qux', 'corge']

while a:

if len(a) < 3:

break

print(a.pop())

print('Done.')

a)

corge

qux

baz

b)

corge

qux

baz

Done.

c)

The snippet doesn’t generate any output.

d)

corge

qux

baz

bar

foo

Done.

7.

Will the print() statement on line 5 be executed in this case:


a = ['foo','bar','baz','qux','corge']

while a:

print(a.pop())

else:

print('Done.')

a)

No

b)

Yes

8.

After execution of the following code, what is the value of s?


s = ""

n = 5

while n > 0:

n -= 1

if (n % 2) == 0:

continue

a = ['foo', 'bar', 'baz']

while a:

s += str(n) + a.pop(0)

if len(a) < 2:

break

(a)  

9.

What is the output of the following code snippet:

d = {'foo': 1, 'bar': 2, 'baz': 3}

 while d:

        print(d.popitem())

 print('Done.')

a)

The snippet doesn’t generate any output.

b)

Done.

c)

foo bar baz

d)

('baz', 3) 
('bar', 2) 
('foo', 1) 
Done.

10.

What is the output of this code snippet:


d = {'foo': 1, 'bar': 2, 'baz': 3}

while len(d) > 3:

print(d.popitem())

print('Done.')

a)

Done.

b)

('baz', 3)

('bar', 2)

('foo', 1)

Done.

c)

The snippet doesn’t generate any output.

d)

foo

bar

baz