Python Programming Quiz

Quiz on Generators in Python

Time left: 300 seconds

Q.1. Which keyword is asociated with generators?

Select Your Option

The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.

Q.2. What keyword is used to define a generator?

Select Your Option

Python generators are a simple way of creating iterators. A generator is simply a function that returns an object (iterator) which we can iterate over.

Q.3. On which generator cannot be used?

Select Your Option

Generators cannot be used to define classes.

Q.4. Which keyword is used to iterate through a generator?

Select Your Option

The next() function returns the next item in an iterator. You can add a default return value, to return if the iterable has reached to its end.

Q.5.What object is created when a generated is called?

Select Your Option

A generator is simply a function that returns an object (iterator) which we can iterate over.

Q.6. The keyword used for returning individual items of the generator is

Select Your Option

The yield statement returns a generator object (iterator). exception.

Q.7. Minimum number of yield statements required in a generator?

Select Your Option

A function becomes a generator function if it contains at least one yield statement. It may also have other yield or return statements.

Q.8. What is the output of following program?
          
            def gen():
                yield 'a'
                yield 'b'
                yield 'c'
            x=gen()

            for i in x:
                print(i,end="")
          
          

Select Your Option

gen() is a generator function that returns and iterator object when called.

Q.9. What is the output of following program?
          
            def gen():
                for i in range(5,0,-1):
                    yield i

            while next(gen()):
                print(next(gen()))
          
          

Select Your Option

Since the condition of the for loop is always satisfied, the function will always yield an object forever.

Q.10. What is the output of following program?
          
            def gen():
                yield 1
                yield 2
                yield 3

            x = gen()

            print(next(x),next(x),next(x))
          
          

Select Your Option

There are 3 values in the iterator returned by the function. These values are printed accordingly using the next() function to iterate over the object.

Q.11. What is the output of following program?
          
            def gen():
                yield 1
                yield 2
                yield 3

            x = gen()

            print(x[1])
          
          

Select Your Option

A generator object can be iterated over but it is not subscriptable (i.e, we cannot access a generator object at a specific index).

Q.12. What is the output of following program?
          
            l = [1,3,5,7,9]

            value = sum(x for x in l)
            print(value)
            
          
          

Select Your Option

The expression sum(x for x in l) is a generator expression. Python will sum the values as they’re generated, not saving all the values in memory.

Q.13. What is the output of following program?
          
            def gen():
                a = 5
                while a:
                    yield a
                    a -= 1

            for i in gen():
                print(i, end=" ")
          
          

Select Your Option

The value of a is yeilded and then decremented in the while loop. Hence, the sequence 5 4 3 2 1 will be printed when iterating over the generator object.

Q.14. What is the output of following program?
          
            def rev(str):
                length = len(str)
                for i in range(length - 1, -1, -1):
                    yield str[i]

            for char in rev("hello world"):
                print(char,end="")
          
          

Select Your Option

The string which is passed is yeilded backwards by use of the rev generator. Hence, the reverse of the string is printed.

Q.15. Generator in Python is not a subclass of Iterator.

Select Your Option

Generator in python is a subclass of Iterator.

Instructions:-

  1. This Quiz is based on Generators in Python
  2. Each correct answer will carry 2 points
  3. Once an option is selected you cannot select any other, So choose wisely
  4. Do not refresh the page as it will reset the quiz
  5. Once the time is up the quiz will automatically get submitted with no of Question responded
  6. Your total score alongwith your name will be shown after submission of the Quiz
  7. Wish you ALL THE BEST 👍👍

START

Results:-

Hi




Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post