ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
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", end=" ")
print(len(d), d[2])- A. RE 4 bb
- B. RE 3 bb
- C. 4 bb
- D. RE 3 b
Correct answer: B. RE 3 bb
Replacing the value of an existing key does not change the dict's size, so the first loop is safe and doubles the values ("aa", "bb"). In the second loop the first iteration inserts key 10; when the loop asks for the next key, CPython notices the size changed and raises RuntimeError. Only one key was added, so len is 3.
More Python Programming questions
- 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…
- What is printed? python def acc(): total = 0 while True: try: x = yield total except ValueError: total = 0 continue if x is None: break…