This Quiz contains totally 10 Questions each carry 1 point for you.
2. Which of the following is the generator expression?
[x**2 for x in my_list]
(x**2 for x in my_list)
{x**2 for x in my_list}
x**2 for x in my_list
Correct!
Wrong!
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)
list object address and [1, 9, 36, 100]
[1, 9, 36, 100] and list object address
[1, 9, 36, 100] and generator object address
generator object address and [1, 9, 36, 100]
Correct!
Wrong!
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
586
46
Error
Nothing is printed
Correct!
Wrong!
5. What would be the output of the below Python code?
def my_gen():
n = 1
yield n
n += 1
yield n
n += 1
yield n
a = my_gen()
print(next(a))
print(next(a))
print(next(a))
print(next(a))
StopIteration
1,2,3
1,2,3 and StopIteration
Error
Correct!
Wrong!
6. What would be the output of the below Python code?
def rev_str(my_str):
length = len(my_str)
for i in range(length - 1, -1, -1):
yield my_str[i]
# For loop to reverse the string
for char in rev_str("hello"):
print(char)
olleh
hello
Error
None of the above
Correct!
Wrong!
7. A generator in Python makes use of ________ keyword.
Return
Gen
Yield
All the above
Correct!
Wrong!
8. Generator in Python is not a subclass of Iterator.
True
False
Correct!
Wrong!
9. State whether the below statement is true or false.
issubclass(collections.Iterator,types.GeneratorType)
False
True
Correct!
Wrong!
10. Select the true statement from the below about generators and iterators.
Iterators are implemented using a function, whereas Generators are implemented using a class.
Generators are implemented using a function as well as class.
Iterators are implemented using a function as well as class.
Generators are implemented using a function, whereas iterators are implemented using a class.
Correct!
Wrong!
Share the quiz to show your results !
Subscribe to see your results
Ignore & go to results
Python-Generators-Quiz
You got %%score%% of %%total%% right
%%description%%
%%description%%
Loading...
