ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = 'abc'
print(x.replace('', '-'))- A. -abc-
- B. -a-b-d-
- C. -a-b-c-
- D. a-b-e
Correct answer: C. -a-b-c-
An empty search string matches at every position, including both ends, so the replacement is inserted before each character and at the end. This is a classic trap for string-processing solutions.
More Python Programming questions
- What is printed? python try: 'abc'.split('') except ValueError as e: print(e)
- What is printed? python a = [1, 2, 3] for x in a: a.remove(x) print(a)
- What is printed? python print(10 -2, 10 2.0, 10 // 3.0)
- What is printed? python print(all([]), any([]), all(x > 0 for x in [1, 0]))
- What is printed? python print(list(map(lambda x, y: x y, [1, 2, 3], [4, 5])))
- What is printed? python print(sum([[1], [2, 3]], []), sum([[1], [2]], [0]) if False else 'skip')