ProgrammingHardAccenture pattern
What is printed?
What is printed?
x = 10
class C:
x = 20
def m(self):
return x
c = C()
print(c.m(), C.x)- A. 20 20
- B. 10 10
- C. 20 10
- D. 10 20
Correct answer: D. 10 20
Inside method m, the bare name x refers to the enclosing (module-level/global) scope, NOT the class attribute -- class bodies don't create an enclosing scope for their methods. So m() returns the global x, 10, while C.x (accessed via the class) is the class attribute, 20.
More Python Programming questions
- What is printed? python def f(x, y=10): def g(): return x + y y = 20 return g() print(f(1))
- What is printed? python nums = [1, 2, 3] doubled = list(map(lambda x: x 2, nums)) evens = list(filter(lambda x: x % 2 == 0, nums))…
- What is printed? python class A: def call(self, x): return x 2 a = A() print(a(5), callable(a))
- What is printed? python class Node: def init(self, val, children=None): self.val = val self.children = children if children is not None…
- What does the following Python 3 code print? lst = [x x for x in range(5)] gen = (x x for x in range(5)) print(type(lst), type(gen))
- What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)