ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
x = 1
def f():
print(x, end=" ")
def g():
x = 2
f()
g()
h = lambda: x
x = 3
print(h())- A. 2 3
- B. 2 2
- C. 1 3
- D. 1 1
Correct answer: C. 1 3
Python is lexically scoped: f looks up x in the global scope where it was defined, not in its caller g. The lambda reads the global x when called, by then 3.
More Python Programming questions
- What is printed? python def outer(): v = "a" def inner(): return v v = "b" r1 = inner() del v try: return r1, inner() except NameError as…
- What is printed? python print(eval("[i for i in range(3)]"), [x for x in "ab"] 2, list(map(str.upper, "ab")), {"aba"} == {"a", "b"})
- What is printed? python class A: def init(self): self.v = 1 def eq(self, o): return isinstance(o, A) and self.v == o.v hash = None class…
- What is printed? python print(isinstance(True, int), issubclass(bool, int), type(True) is int, True == 1, hash(True) == hash(1.0))
- What is printed? python s = {1, 2, 3} s.add(2) s.discard(9) s.update([3, 4], (5,)) r = s.pop() if False else len(s) try: s.remove(9)…
- What is printed? python d = {"a": 1, "b": 2} items = d.items() print(("a", 1) in items, ("a", 2) in items, items & {("b", 2), ("c", 3)} ==…