ProgrammingModerateWipro NLTH · Python
What is printed?
What is printed?
print({x % 3 for x in range(10)}, {x: x * x for x in range(3)})- A. {0, 1, 2} {0: 0, 1: 1, 2: 4, 3: 9}
- B. {0, 1, 2} {0: 0, 1: 1, 2: 4}
- C. {0, 1, 2, 3} {0: 0, 1: 1, 2: 4}
- D. {0, 1, 2} {0: 0, 1: 1, 4: 2}
Correct answer: B. {0, 1, 2} {0: 0, 1: 1, 2: 4}
x % 3 takes only the values 0, 1, 2 and sets drop duplicates; the dict comprehension maps x to x*x for x in 0..2.
More Python Programming questions
- What is printed? python def f(): try: return 'a' finally: print('f', end=' ') print(f())
- What is printed? python def g(): x = yield 1 print('got', x) y = yield x 2 print('got', y) it = g() print(next(it)) print(it.send(5)) try…
- What is printed? python def mk(): n = 0 def inc(): nonlocal n n += 1 return n return inc a, b = mk(), mk() print(a(), a(), b(), a())
- What is printed? python class C: v = [] n = 0 def add(self, x): self.v.append(x) self.n += 1 a, b = C(), C() a.add(1); b.add(2); b.add(3)…
- What is printed? python print(1 < 2 < 3, 3 > 2 > 2, 1 == 1 in [1], not 1 == 2, 2 3 2)
- What is printed? python a = [[0] 2] 2 a[0][0] = 1 b = [[0] 2 for in range(2)] b[0][0] = 1 print(a, b)