ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
print(-7 // 2, -7 % 2, 7 // -2, 7 % -2, divmod(-7, 2))
- A. -3 1 -3 -1 (-3, 1)
- B. -4 1 -4 -1 (-4, 1)
- C. -3 1 -4 -1 (-4, 1)
- D. -4 1 -3 -1 (-4, 1)
Correct answer: B. -4 1 -4 -1 (-4, 1)
Python floors toward minus infinity, so -7 // 2 is -4. The remainder takes the sign of the divisor: -7 % 2 is 1 and 7 % -2 is -1. divmod follows the same rule.
More Python Programming questions
- What is printed? python print(round(0.5), round(1.5), round(2.5), round(-0.5), round(2.675, 2))
- What is printed? python s = 'aaaa' print(s.count('aa'), s.replace('aa', 'b'), 'a,,b'.split(','), ' x y '.split(' '))
- What is printed? python import itertools print([''.join(p) for p in itertools.permutations('abc', 2)][:4])
- What is printed? python import itertools print(len(list(itertools.combinationswithreplacement(range(5), 3))))
- What is printed? python import itertools print(sum(1 for t in itertools.product('01', repeat=10) if t.count('1') == 3))
- What is printed? python import itertools g = itertools.groupby('aab') k, grp = next(g) next(g) print(k, list(grp))