wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python Generators

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

The generator function also works with other kinds of iterables like listtuple, etc

a)

True

b)

False

2.

Which of the following is the generator expression?

a)

[x**2 for x in my_list]

b)

(x**2 for x in my_list)

c)

{x**2 for x in my_list}

d)

x**2 for x in my_list

3.

What is the output of the following Python code?

my_list = [1, 3, 6, 10]

list_ = [x**2 for x in my_list]

generator = (x**2 for x in my_list)

print(list_)

print(generator)

a)

list object address and [1, 9, 36, 100]

b)

[1, 9, 36, 100] and list object address

c)

[1, 9, 36, 100] and generator object address

d)

generator object address and [1, 9, 36, 100]

4.

What is the output of the following Python code?

my_list = [2, 4, 9, 14, 17]

value = sum(x**2 for x in my_list)

value

a)

586

b)

59

c)

Error

d)

Nothing is printed

5.

A generator in Python makes use of ________ keyword.

a)

Gen

b)

Return

c)

Yield

d)

All the above

6.

Generator in Python is not a subclass of Iterator.

a)

True

b)

False

7.

State whether the below statement is True or False.

issubclass(collections.Iterator,types.GeneratorType)

a)

True

b)

False

8.

Select the true statement from the below about generators and iterators.

a)

Iterators are implemented using a function as well as class.

b)

Generators are implemented using a function as well as class.

c)

Iterators are implemented using a function, whereas Generators are implemented using a class.

d)

Generators are implemented using a function, whereas iterators are implemented using a class.

9.

Which one of the following is correct?

a)

A dictionary can have two same values with different keys

b)

A dictionary can have two same keys with different values.

c)

A dictionary can have two same keys or same values but cannot have two same key-value pair

d)

A python, a dictionary can neither have two same keys nor two same values.

10.

To obtain the number of entries in dictionary which command is used?

a)

d.size()

b)

size(d)

c)

len(d)

d)

z.len()