PYTHON Iterators and Generators MCQs

  1. Which of the following is used to create an iterator?
    a) iter()
    b) next()
    c) generator()
    d) yield
    View Answer

    Correct answer: A — iter()

  2. What will be printed?
    1 python
    2 x = [1,2,3]
    3 it = iter(x)
    4 print(next(it))
    a) 1
    b) 2
    c) 3
    d) Error
    View Answer

    Correct answer: A — 1

  3. Which of the following is used to get next element from iterator?
    a) next()
    b) iter()
    c) yield
    d) gen()
    View Answer

    Correct answer: A — next()

  4. What is the output of print(isinstance(iter([]),object))?
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  5. Which of the following is used to define generator function?
    a) yield
    b) return
    c) gen()
    d) next()
    View Answer

    Correct answer: A — yield

  6. What will be printed?
    1 python
    2 def gen():
    3 yield "Eduinq"
    4 print(next(gen()))
    a) Eduinq
    b) Error
    c) None
    d) yield
    View Answer

    Correct answer: A — Eduinq

  7. Which of the following is used to convert iterator to list?
    a) list(iterator)
    b) iter(list)
    c) next(list)
    d) yield list
    View Answer

    Correct answer: A — list(iterator)

  8. What is the output of print(list(range(3)))?
    a) [0,1,2]
    b) [1,2,3]
    c) Error
    d) None
    View Answer

    Correct answer: A — [0,1,2]

  9. Which of the following is used to stop iteration?
    a) StopIteration
    b) EndIteration
    c) BreakIteration
    d) None
    View Answer

    Correct answer: A — StopIteration

  10. What will be printed?
    1 python
    2 def gen():
    3 yield 1
    4 yield 2
    5 for i in gen():
    6 print(i)
    a) 1 2
    b) 2 1
    c) Error
    d) None
    View Answer

    Correct answer: A — 1 2

  11. Which of the following is used to check if object is iterable?
    a) hasattr(obj,"iter")
    b) isinstance(obj,iter)
    c) type(obj)==iter
    d) None
    View Answer

    Correct answer: A — hasattr(obj,"iter")

  12. What is the output of print(hasattr([], "iter"))?
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  13. Which of the following is used to create generator expression?
    a) (x for x in range(3))
    b) [x for x in range(3)]
    c) {x for x in range(3)}
    d) None
    View Answer

    Correct answer: A — (x for x in range(3))

  14. What will be printed?
    1 python
    2 gen = (x*x for x in range(3))
    3 print(list(gen))
    a) [0,1,4]
    b) [1,4,9]
    c) Error
    d) None
    View Answer

    Correct answer: A — [0,1,4]

  15. Which of the following is used to define infinite generator?
    a) while True: yield
    b) for True: yield
    c) loop yield
    d) None
    View Answer

    Correct answer: A — while True: yield

  16. What is the output of print(next((x for x in range(3))))?
    a) 0
    b) 1
    c) Error
    d) None
    View Answer

    Correct answer: A — 0

  17. Which of the following is used to chain iterators?
    a) itertools.chain()
    b) iter.chain()
    c) next.chain()
    d) yield.chain()
    View Answer

    Correct answer: A — itertools.chain()

  18. What will be printed?
    1 python
    2 import itertools
    3 print(list(itertools.chain([1,2],[3,4])))
    a) [1,2,3,4]
    b) [1,2],[3,4]
    c) Error
    d) None
    View Answer

    Correct answer: A — [1,2,3,4]

  19. Which of the following is used to repeat values infinitely?
    a) itertools.cycle()
    b) itertools.repeat()
    c) itertools.infinite()
    d) None
    View Answer

    Correct answer: A — itertools.cycle()

  20. What is the output of print(next(itertools.cycle("Eduinq")))?
    a) E
    b) d
    c) u
    d) q
    View Answer

    Correct answer: A — E

  21. Which of the following is used to repeat a value n times?
    a) itertools.repeat()
    b) itertools.cycle()
    c) itertools.chain()
    d) None
    View Answer

    Correct answer: A — itertools.repeat()

  22. What will be printed?
    1 python
    2 import itertools
    3 print(list(itertools.repeat("Eduinq",3)))
    a) ['Eduinq','Eduinq','Eduinq']
    b) ['Eduinq']
    c) Error
    d) None
    View Answer

    Correct answer: A — ['Eduinq','Eduinq','Eduinq']

  23. Which of the following is used to accumulate values?
    a) itertools.accumulate()
    b) itertools.sum()
    c) itertools.add()
    d) None
    View Answer

    Correct answer: A — itertools.accumulate()

  24. What is the output of print(list(itertools.accumulate([1,2,3])))?
    a) [1,3,6]
    b) [1,2,3]
    c) Error
    d) None
    View Answer

    Correct answer: A — [1,3,6]

  25. Which of the following is used to create infinite counter?
    a) itertools.count()
    b) itertools.counter()
    c) itertools.infinite()
    d) None
    View Answer

    Correct answer: A — itertools.count()

  26. What will be printed?
    1 python
    2 import itertools
    3 c = itertools.count(5,2)
    4 print(next(c),next(c))
    a) 5 7
    b) 5 6
    c) Error
    d) None
    View Answer

    Correct answer: A — 5 7

  27. Which of the following is used to slice iterator?
    a) itertools.islice()
    b) itertools.slice()
    c) itertools.cut()
    d) None
    View Answer

    Correct answer: A — itertools.islice()

  28. What is the output of print(list(itertools.islice(range(10),3)))?
    a) [0,1,2]
    b) [1,2,3]
    c) Error
    d) None
    View Answer

    Correct answer: A — [0,1,2]

  29. Which of the following is used to filter elements with condition?
    a) itertools.filterfalse()
    b) itertools.filter()
    c) itertools.select()
    d) None
    View Answer

    Correct answer: A — itertools.filterfalse()

  30. What will be printed?
    1 python
    2 import itertools
    3 print(list(itertools.filterfalse(lambda x:x%2==0,range(5))))
    a) [1,3]
    b) [0,2,4]
    c) Error
    d) None
    View Answer

    Correct answer: A — [1,3]

  31. Which of the following is used to group elements?
    a) itertools.groupby()
    b) itertools.group()
    c) itertools.cluster()
    d) None
    View Answer

    Correct answer: A — itertools.groupby()

  32. What is the output of print([(k,list(g)) for k,g in itertools.groupby("Eduinq")])?
    a) [('E',['E']),('d',['d']),('u',['u']),('i',['i']),('n',['n']),('q',['q'])]
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — [('E',['E']),('d',['d']),('u',['u']),('i',['i']),('n',['n']),('q',['q'])]

  33. Which of the following is used to zip longest iterables?
    a) itertools.zip_longest()
    b) itertools.zip()
    c) itertools.longzip()
    d) None
    View Answer

    Correct answer: A — itertools.zip_longest()

  34. What will be printed?
    1 python
    2 import itertools
    3 print(list(itertools.zip_longest([1,2],[3],fillvalue=0)))
    a) [(1,3),(2,0)]
    b) [(1,3)]
    c) Error
    d) None
    View Answer

    Correct answer: A — [(1,3),(2,0)]

  35. Which of the following is used to create permutations?
    a) itertools.permutations()
    b) itertools.combinations()
    c) itertools.arrangements()
    d) None
    View Answer

    Correct answer: A — itertools.permutations()

  36. What is the output of print(list(itertools.permutations([1,2],2)))?
    a) [(1,2),(2,1)]
    b) [(1,2)]
    c) Error
    d) None
    View Answer

    Correct answer: A — [(1,2),(2,1)]

  37. Which of the following is used to create combinations?
    a) itertools.combinations()
    b) itertools.permutations()
    c) itertools.arrangements()
    d) None
    View Answer

    Correct answer: A — itertools.combinations()

  38. What will be printed?
    1 python
    2 import itertools
    3 print(list(itertools.combinations([1,2,3],2)))
    a) [(1,2),(1,3),(2,3)]
    b) [(1,2),(2,1)]
    c) Error
    d) None
    View Answer

    Correct answer: A — [(1,2),(1,3),(2,3)]

  39. Which of the following is used to create combinations with replacement?
    a) itertools.combinations_with_replacement()
    b) itertools.combinations()
    c) itertools.permutations()
    d) None
    View Answer

    Correct answer: A — itertools.combinations_with_replacement()

  40. What is the output of print(list(itertools.combinations_with_replacement([1,2],2)))?
    a) [(1,1),(1,2),(2,2)]
    b) [(1,2),(2,1)]
    c) Error
    d) None
    View Answer

    Correct answer: A — [(1,1),(1,2),(2,2)]

  41. Which of the following is used to chain generator functions?
    a) yield from
    b) yield chain
    c) yield next
    d) None
    View Answer

    Correct answer: A — yield from

  42. What will be printed?
    1 python
    2 def gen1():
    3 yield "Eduinq"
    4 def gen2():
    5 yield from gen1()
    6 print(list(gen2()))
    a) ['Eduinq']
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — ['Eduinq']

  43. Which of the following is used to send value into generator?
    a) generator.send()
    b) generator.next()
    c) generator.value()
    d) None
    View Answer

    Correct answer: A — generator.send()

  44. What is the output of below code?
    1 python
    2 def gen():
    3 x = yield
    4 yield x*2
    5 g = gen()
    6 next(g)
    7 print(g.send(5))
    a) 10
    b) 5
    c) Error
    d) None
    View Answer

    Correct answer: A — 10

  45. Which of the following is used to throw exception into generator?
    a) generator.throw()
    b) generator.raise()
    c) generator.error()
    d) None
    View Answer

    Correct answer: A — generator.throw()

  46. What will be printed?
    1 python
    2 def gen():
    3 try:
    4 yield 1
    5 except ValueError:
    6 yield "Eduinq Error"
    7 g = gen()
    8 print(next(g))
    9 print(g.throw(ValueError))
    a) 1 Eduinq Error
    b) 1 Error
    c) Error
    d) None
    View Answer

    Correct answer: A — 1 Eduinq Error

  47. Which of the following is used to close generator?
    a) generator.close()
    b) generator.stop()
    c) generator.end()
    d) None
    View Answer

    Correct answer: A — generator.close()

  48. What is the output of below code?
    1 python
    2 def gen():
    3 yield 1
    4 g = gen()
    5 print(next(g))
    6 g.close()
    7 print(next(g))
    a) 1 Error
    b) 1 1
    c) Error
    d) None
    View Answer

    Correct answer: A — 1 Error

  49. Which of the following is used to check if object is iterator?
    a) hasattr(obj,"next")
    b) hasattr(obj,"iter")
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  50. What will be printed?
    1 python
    2 x = [1,2,3]
    3 it = iter(x)
    4 print(hasattr(it,"__next__"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

Quick Links to Explore