ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [1, 2, 3, 4, 5, 6] print(a[1::2], a[::-2], a[4:1:-1])
- A. [2, 4, 6] [5, 3, 1] [5, 4, 3]
- B. [2, 4, 6] [6, 4, 2] [5, 4, 3]
- C. [2, 4, 6] [6, 4, 2] [5, 4]
- D. [1, 3, 5] [6, 4, 2] [5, 4, 3]
Correct answer: B. [2, 4, 6] [6, 4, 2] [5, 4, 3]
a[1::2] takes indices 1, 3, 5. a[::-2] starts at the last index and steps back two places. a[4:1:-1] goes from index 4 down to index 2, which includes 5, 4 and 3.
More Python Programming questions
- What is printed? python s = 'a1b22c333' import re print(re.findall(r'd+', s), sum(int(x) for x in re.findall(r'd+', s)))
- What is printed? python def solve(n, memo={}): if n in memo: return memo[n] if n < 2: return n memo[n] = solve(n - 1) + solve(n - 2)…
- What is printed? python x = 7 match x: case 1 | 2: r = 'small' case int(v) if v > 5: r = f'big{v}' case : r = 'other' print(r)
- What is printed? python a = [[1, 2], [3]] b = a[:] b[1].append(4) b.append([5]) print(a, b, len(a))
- What is printed? python print(bool([]), bool([0]), bool(0.0), bool('False'), bool(None) or 'x')
- What is printed? python import heapq events = [(3, 'end'), (1, 'start'), (2, 'mid')] heapq.heapify(events) out = [] while events: t, name…