ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
s = "banana"
print(s.replace("a", "o", 2), s.upper().count("A"), s[::2], s.startswith(("x", "ba")))- A. bonono 3 bnn True
- B. bonona 3 aaa True
- C. bonona 3 bnn True
- D. bonona 3 bnn False
Correct answer: C. bonona 3 bnn True
The count argument limits replace to the first 2 a's. s[::2] takes indices 0, 2, 4 ('b', 'n', 'n'). startswith accepts a tuple of prefixes and is True if any matches.
More Python Programming questions
- What is printed? python d = {"x": [1, 2], "y": [3]} flat = [v for vs in d.values() for v in vs] lengths = {k: len(v) for k, v in…
- What is printed? python def f(n): out = [] while n: n, r = divmod(n, -2) if r < 0: n, r = n + 1, r + 2 out.append(str(r)) return…
- What is printed? python def gen(n): if n == 0: yield "" return for s in gen(n - 1): yield s + "0" if not s.endswith("1"): yield s + "1"…
- What is printed? python cache = {} def ways(n): if n < 0: return 0 if n == 0: return 1 if n not in cache: cache[n] = ways(n - 1) + ways(n…
- What is printed? python def perms(s): if len(s) <= 1: return [s] out = [] for i, c in enumerate(s): for p in perms(s[:i] + s[i + 1:])…
- What is printed? python def makeops(): ops = {} for name, sym in [("add", "+"), ("mul", "")]: ops[name] = eval(f"lambda a, b: a {sym} b")…