- Which of the following is used to create an iterator?
View Answer
Correct answer: A — iter()
- What will be printed?
1 python 2 x = [1,2,3] 3 it = iter(x) 4 print(next(it)) View Answer
Correct answer: A — 1
- Which of the following is used to get next element from iterator?
View Answer
Correct answer: A — next()
- What is the output of print(isinstance(iter([]),object))?
View Answer
Correct answer: A — True
- Which of the following is used to define generator function?
View Answer
Correct answer: A — yield
- What will be printed?
1 python 2 def gen(): 3 yield "Eduinq" 4 print(next(gen())) View Answer
Correct answer: A — Eduinq
- Which of the following is used to convert iterator to list?
View Answer
Correct answer: A — list(iterator)
- What is the output of print(list(range(3)))?
View Answer
Correct answer: A — [0,1,2]
- Which of the following is used to stop iteration?
View Answer
Correct answer: A — StopIteration
- What will be printed?
1 python 2 def gen(): 3 yield 1 4 yield 2 5 for i in gen(): 6 print(i) View Answer
Correct answer: A — 1 2
- Which of the following is used to check if object is iterable?
View Answer
Correct answer: A — hasattr(obj,"iter")
- What is the output of print(hasattr([], "iter"))?
View Answer
Correct answer: A — True
- Which of the following is used to create generator expression?
View Answer
Correct answer: A — (x for x in range(3))
- What will be printed?
1 python 2 gen = (x*x for x in range(3)) 3 print(list(gen)) View Answer
Correct answer: A — [0,1,4]
- Which of the following is used to define infinite generator?
View Answer
Correct answer: A — while True: yield
- What is the output of print(next((x for x in range(3))))?
View Answer
Correct answer: A — 0
- Which of the following is used to chain iterators?
View Answer
Correct answer: A — itertools.chain()
- What will be printed?
1 python 2 import itertools 3 print(list(itertools.chain([1,2],[3,4]))) View Answer
Correct answer: A — [1,2,3,4]
- Which of the following is used to repeat values infinitely?
View Answer
Correct answer: A — itertools.cycle()
- What is the output of print(next(itertools.cycle("Eduinq")))?
View Answer
Correct answer: A — E
- Which of the following is used to repeat a value n times?
View Answer
Correct answer: A — itertools.repeat()
- What will be printed?
1 python 2 import itertools 3 print(list(itertools.repeat("Eduinq",3))) View Answer
Correct answer: A — ['Eduinq','Eduinq','Eduinq']
- Which of the following is used to accumulate values?
View Answer
Correct answer: A — itertools.accumulate()
- What is the output of print(list(itertools.accumulate([1,2,3])))?
View Answer
Correct answer: A — [1,3,6]
- Which of the following is used to create infinite counter?
View Answer
Correct answer: A — itertools.count()
- What will be printed?
1 python 2 import itertools 3 c = itertools.count(5,2) 4 print(next(c),next(c)) View Answer
Correct answer: A — 5 7
- Which of the following is used to slice iterator?
View Answer
Correct answer: A — itertools.islice()
- What is the output of print(list(itertools.islice(range(10),3)))?
View Answer
Correct answer: A — [0,1,2]
- Which of the following is used to filter elements with condition?
View Answer
Correct answer: A — itertools.filterfalse()
- What will be printed?
1 python 2 import itertools 3 print(list(itertools.filterfalse(lambda x:x%2==0,range(5)))) View Answer
Correct answer: A — [1,3]
- Which of the following is used to group elements?
View Answer
Correct answer: A — itertools.groupby()
- What is the output of print([(k,list(g)) for k,g in itertools.groupby("Eduinq")])?
View Answer
Correct answer: A — [('E',['E']),('d',['d']),('u',['u']),('i',['i']),('n',['n']),('q',['q'])]
- Which of the following is used to zip longest iterables?
View Answer
Correct answer: A — itertools.zip_longest()
- What will be printed?
1 python 2 import itertools 3 print(list(itertools.zip_longest([1,2],[3],fillvalue=0))) View Answer
Correct answer: A — [(1,3),(2,0)]
- Which of the following is used to create permutations?
View Answer
Correct answer: A — itertools.permutations()
- What is the output of print(list(itertools.permutations([1,2],2)))?
View Answer
Correct answer: A — [(1,2),(2,1)]
- Which of the following is used to create combinations?
View Answer
Correct answer: A — itertools.combinations()
- What will be printed?
1 python 2 import itertools 3 print(list(itertools.combinations([1,2,3],2))) View Answer
Correct answer: A — [(1,2),(1,3),(2,3)]
- Which of the following is used to create combinations with replacement?
View Answer
Correct answer: A — itertools.combinations_with_replacement()
- What is the output of print(list(itertools.combinations_with_replacement([1,2],2)))?
View Answer
Correct answer: A — [(1,1),(1,2),(2,2)]
- Which of the following is used to chain generator functions?
View Answer
Correct answer: A — yield from
- What will be printed?
1 python 2 def gen1(): 3 yield "Eduinq" 4 def gen2(): 5 yield from gen1() 6 print(list(gen2())) View Answer
Correct answer: A — ['Eduinq']
- Which of the following is used to send value into generator?
View Answer
Correct answer: A — generator.send()
- 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)) View Answer
Correct answer: A — 10
- Which of the following is used to throw exception into generator?
View Answer
Correct answer: A — generator.throw()
- 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)) View Answer
Correct answer: A — 1 Eduinq Error
- Which of the following is used to close generator?
View Answer
Correct answer: A — generator.close()
- 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)) View Answer
Correct answer: A — 1 Error
- Which of the following is used to check if object is iterator?
View Answer
Correct answer: C — Both A and B
- What will be printed?
1 python 2 x = [1,2,3] 3 it = iter(x) 4 print(hasattr(it,"__next__")) View Answer
Correct answer: A — True