ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = 0
total = 0
for i in range(1, 6):
if i % 2:
continue
x += i
if x > 4:
break
else:
total = -1
print(x, total)- A. 2 0
- B. 6 0
- C. 2 -1
- D. 6 -1
Correct answer: B. 6 0
The loop adds 2 and then 4, giving x = 6 after the second pass, which is greater than 4, so break runs. Since break was taken, the else block is skipped and total stays 0.
More Python Programming questions
- What is printed? python def f(n): if n <= 1: return n return f(n - 1) + f(n - 2) print(f(20), f.defaults)
- What is printed? python s = 'abcabc' print(s.index('c'), s.rindex('a'), s.find('bca', 1), s.count('bc', 1, 4))
- What is printed? python nums = [4, 9, 16] print(list(map(pow, nums, [0.5] 3)), [n 0.5 for n in nums])
- What is printed? python def gen(n): for i in range(n): yield i i g = gen(4) print(sum(g), sum(g), list(gen(0)))
- What is printed? python words = ['apple', 'Bob', 'cat'] print(sorted(words, key=str.casefold)[0], sorted(words)[0], max(words, key=len))
- What is printed? python import heapq h = [(2, 'x'), (1, 'y'), (1, 'a')] heapq.heapify(h) heapq.heappush(h, (0, 'z'))…