ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
def shift(s, k):
return ''.join(chr((ord(c) - 97 + k) % 26 + 97) if c.islower() else c for c in s)
print(shift('xyz ABC', 3), shift('abc', -1))- A. abd ABC zab
- B. abc ABC abc
- C. abc ABC zab
- D. xyz ABC zab
Correct answer: C. abc ABC zab
Python's % keeps the shifted index between 0 and 25 even when k is negative, so a moves to z. Uppercase letters are left alone because only lowercase letters are shifted.
More Python Programming questions
- What is printed? python def closest3(a, t): a = sorted(a) best = None for i in range(len(a) - 2): l, r = i + 1, len(a) - 1 while l < r: s…
- What is printed? python print(0.0 == -0.0, -0.0, 1e16 + 1 == 1e16, 2 53 + 1 == 2 53)
- What is printed? python import bisect xs = [1, 4, 4, 9] i = bisect.bisectleft(xs, 4) xs.insert(i, 4) print(xs, bisect.bisectright(xs, 4)…
- What is printed? python from collections import deque d = deque(range(5)) d.rotate(-2) print(list(d), d[0], d[-1])
- What is printed? python import heapq h = [] for x in [5, 1, 3]: heapq.heappush(h, x) heapq.heappush(h, 0) print([heapq.heappop(h) for in…
- What is printed? python s = 'hello' print(s.index('l'), s.rfind('l'), s.find('l', 3), s.index('lo'))