ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
nums = [1, 2, 3, 4, 5] print([nums[i] for i in range(len(nums) - 1, -1, -2)], nums[::-2][0])
- A. [5, 3, 1] 5
- B. [5, 3, 1] 1
- C. [4, 2] 5
- D. [5, 4, 3, 2, 1] 5
Correct answer: A. [5, 3, 1] 5
range from the last index stepping by -2 gives indices 4, 2 and 0, which are 5, 3 and 1. nums[::-2] starts from the last element, so its first item is 5.
More Python Programming questions
- What is printed? python def walk(s): total = 0 for i, ch in enumerate(s, start=1): if ch.isupper(): total += i else: total -= i return…
- What is printed? python print(sum(x for x in range(1, 11) if x & 1), sum(x for x in range(1, 11) if x & 2))
- What is printed? python x = 'AbC' print(x.swapcase(), x.lower() < x.upper(), sorted(x) == ['A', 'C', 'b'])
- What is printed? python print(max(range(5), key=lambda x: -abs(x - 2)), min(range(5), key=lambda x: abs(x - 2)))
- What is printed? python print(sum(divmod(17, 5)), divmod(-17, 5)[1], 17 % 5 - (-17) % 5)
- What is printed? python def prodexcept(a): n = len(a) out = [1] n left = 1 for i in range(n): out[i] = left left = a[i] right = 1 for i in…