ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print([x % 3 for x in [-7, -3, 0, 4]])
- A. [2, 0, 0, 1]
- B. [-1, 0, 0, 1]
- C. [1, 0, 2, 1]
- D. [2, 0, 0, -2]
Correct answer: A. [2, 0, 0, 1]
Python's % takes the sign of the divisor, so every result lies in 0, 1, 2. -7 % 3 is 2, -3 % 3 is 0, 0 % 3 is 0 and 4 % 3 is 1.
More Python Programming questions
- What is printed? python def counter(): count = 0 def step(): nonlocal count count += 2 return count return step c1 = counter() c2 =…
- What is printed? python values = {'x': [1, 2], 'y': [3]} total = sum(len(v) for v in values.values()) keys = sorted(values, key=lambda k…
- What is printed? python print(int(' -0012 '), int('+5'), float('-.5'), float('5.'))
- What is printed? python x = 10 y = x y += 1 z = [x] z[0] += 5 print(x, y, z)
- What is printed? python a = list('abc') print(a.index('b'), ''.join(a[::-1]), a.pop(0), a)
- What is printed? python x = 5 print(f'{x=}', f'{x 2 = }', f'{"a" 2!r}')