ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class R:
def __iter__(self):
print("it", end=" ")
return iter([1, 2, 3])
r = R()
print(2 in r, 5 in r)- A. True False
- B. it True False
- C. it it True it False
- D. it it True False
Correct answer: D. it it True False
R has no __contains__, so the in operator falls back to iterating via __iter__. Each in creates a fresh iterator, so "it" is printed twice, and both are printed while print's arguments are being evaluated, i.e. before the final line's values appear. 2 is found, 5 is not: True False.
More Python Programming questions
- What is printed? python d = {1: "a", 2: "b"} for k in d: d[k] = d[k] 2 try: for k in d: d[k 10] = "x" except RuntimeError: print("RE"…
- What is printed? python s = "abcbaXabcba" print(s.strip("ab"), s.removeprefix("ab"), s.lstrip("cba"))
- What is printed? python print(-7.5 // 2, -7.5 % 2, 7 // -2.0, 7 % -2.0)
- What is printed? python n = [1] print(1 in n == True, (1 in n) == True, 0 < 1 in n)
- What is printed? python class P: slots = ("x",) def init(self): self.x = 1 p = P() try: p.y = 2 except AttributeError: print("AE", end="…
- What is printed? python class T: def init(self): self.c = 0 @property def c(self): return self.c @c.setter def c(self, v): self.c = max(0…