ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
data = [4, 7, 1, 9] best = 0 hi = [best := x for x in data if x > best] print(hi, best, "x" in globals())
- A. [4, 7, 9] 9 False
- B. [4, 7, 1, 9] 0 False
- C. [4, 7, 9] 0 True
- D. [4, 7, 9] 9 True
Correct answer: A. [4, 7, 9] 9 False
An assignment expression inside a comprehension binds the name in the ENCLOSING scope, so each kept x updates the outer best, and the next test compares against it: 4 > 0, 7 > 4, 1 fails, 9 > 7. best ends as 9. The loop variable x itself stays local to the comprehension, so it does not appear in globals().
More Python Programming questions
- What is printed? python class M: def init(self, v): self.v = v def add(self, o): return M(self.v + (o.v if isinstance(o, M) else o)) ms =…
- What is printed? python print(f"{3.14159:.2f}|{42:>5}|{'ab':^6}|{255:#x}|{1234567:,}")
- What is printed? python grid = [[0, 0]] 2 copy1 = list(grid) copy2 = [row[:] for row in grid] grid[0][0] = 1 grid[1] = [5, 5] print(copy1…
- What is printed? python vals = iter([3, 1, 0, 4, 0]) print(list(iter(lambda: next(vals), 0)), list(vals))
- What is printed? python class Acc: def init(self): self.items = [] def iadd(self, v): self.items.append(v) a = Acc() b = a a += 1 print(a…
- What is printed? python def f(): log = [] try: log.append("t") raise ValueError("v") except ValueError as e: log.append("e") raise…