ProgrammingHardGATE CS · Product-company placement
Which statement about iterables and iterators is correct?
- A. Every iterable is an iterator, because both define __iter__
- B. Every iterator is an iterable (its __iter__ returns itself), but a list is an iterable that is not an iterator
- C. A list is an iterator, because next() can be called on it directly
- D. A generator is an iterable but not an iterator, because it has no __next__
Correct answer: B. Every iterator is an iterable (its __iter__ returns itself), but a list is an iterable that is not an iterator
An iterator implements __next__ and an __iter__ that returns itself, so it is also iterable. A list only implements __iter__ (returning a fresh list_iterator); next([1]) raises TypeError. Generators are iterators.
More Python Programming questions
- Assertion (A): After t = (1, 2, [3]), the statement t[2].append(4) succeeds and changes what print(t) shows. Reason (R): A tuple's…
- Which of these expressions raises an exception in Python 3?
- Which statement about the C3 method resolution order is correct for class D(B, C) where both B and C inherit from A?
- Which is the safest way to remove every even number from a list nums in place (so that other references to the same list see the change)?
- What is printed? python import sys a = [] b = a c = [a, a] print(sys.getrefcount(a) - sys.getrefcount([]) >= 3, c[0] is c[1], [a] == [b])
- What is printed? python x = (1, 2, 3) y = x + (4,) z = x 0 print(len(y), z, x.count(2), x.index(3), 3 in x)