Data Structures & AlgorithmsModerateWipro · Accenture pattern
What is printed? (Python string immutability and methods)
What is printed? (Python string immutability and methods)
s = "Hello World"
t = s.replace("o", "0", 1)
u = s.swapcase()[::2]
print(s, t, u)- A. Hell0 World Hell0 World hLOwRD
- B. Hello World Hell0 W0rld hLOwRD
- C. Hello World Hell0 World hLOwRD
- D. Hello World Hell0 World hLO RD
Correct answer: C. Hello World Hell0 World hLOwRD
Strings are immutable, so s is unchanged. replace(..., 1) replaces only the first 'o'. swapcase gives "hELLO wORLD" and [::2] takes indices 0, 2, 4, …: h, L, O, w, R, D.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is printed? (queue simulation – hot potato with step k) python from collections import deque q = deque(range(1, 8)); out = [] while…
- What is printed? (Josephus recurrence) python def josephus(n, k): r = 0 for m in range(2, n + 1): r = (r + k) % m return r + 1…
- What is printed? (hash set for pairs with a given difference) python def pairsdiff(a, k): s = set(a) return sorted((x, x + k) for x in s…
- What is printed? (Python dict ordering and update) python d = {"b": 1, "a": 2} d["c"] = 3 d["b"] = 4 del d["a"] d["a"] = 5…
- What is printed? (prefix sums for range queries) python a = [3, -2, 5, 1, -4, 6] P = [0] for x in a: P.append(P[-1] + x) q = [(0, 2), (1…
- What is printed? (difference array for range increments) python n = 6 diff = [0] (n + 1) for l, r, v in [(0, 2, 5), (1, 4, 3), (3, 5…