ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
i = 10
squares = [i * i for i in range(3)]
for j in range(3):
pass
print(i, squares, j)- A. 10 [0, 1, 4] 2
- B. 2 [0, 1, 4] 2
- C. 10 [0, 1, 4] 3
- D. 10 [0, 1, 4] NameError
Correct answer: A. 10 [0, 1, 4] 2
A list comprehension has its own scope in Python 3, so its i does not overwrite the outer i (still 10). A for loop variable does leak: j keeps its last value 2 after the loop.
More Python Programming questions
- What is printed? python s = " Hello, World " print(s.strip().lower().split(","), s.count("l"), s.find("z"), "-".join("abc"))
- What is printed? python def f(args, kw): return len(args), sorted(kw) print(f(1, 2, [3, 4], a=5, {"b": 6}))
- What is printed? python class A: count = 0 def init(self): A.count += 1 self.count = 10 a = A(); b = A() b.count += 5 print(A.count…
- What is printed? python it = iter([1, 2, 3, 4, 5]) pairs = list(zip(it, it)) print(pairs, list(it))
- What is printed? python import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(b, c)
- What is printed? python print(bool([]), bool([0]), bool(""), bool(" "), None == False, 0 == False)