ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
x = [1, 2] g = (n * 10 for n in x) x.append(3) x = [7] print(list(g))
- A. [10, 20, 30]
- B. [10, 20]
- C. [70]
- D. []
Correct answer: A. [10, 20, 30]
A generator expression evaluates its first iterable (x) immediately and stores the list object, but runs the loop lazily. append(3) changes that same list, so 3 is included. Rebinding x to [7] afterwards does not affect the stored object.
More Python Programming questions
- What is printed? python class Bag: items = [] def add(self, v): self.items.append(v) a, b = Bag(), Bag() a.add(1); b.add(2) print(a.items…
- What is printed? python class A: def who(self): return "A" class B(A): def who(self): return "B" + super().who() class C(A): def…
- What is printed? python try: try: raise ValueError except TypeError: print("T", end=" ") finally: print("F1", end=" ") except ValueError…
- What is printed? python x = 10 def f(a=x): return a x = 20 print(f())
- What is printed? python print(sum(range(1, 10, 2)), list(range(5))[-1:-4:-1])
- What is printed? python print("abc" < "abd" < "ab", "10" < "9", 10 < 9)