ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
s = "racecar"
print(s.index("c"), s.rindex("c"), s.find("c", 3), s[::-1] is s, s.count("a", 2))- A. 2 4 2 True 1
- B. 2 4 4 False 1
- C. 2 4 4 True 2
- D. 2 5 4 False 1
Correct answer: B. 2 4 4 False 1
index finds the first 'c' (2), rindex the last (4); find with start 3 skips index 2 and finds 4. Slicing always builds a new string here, so `is` is False... and count from index 2 sees only the 'a' at index 5.
More Python Programming questions
- What is printed? python import sys print(type(10 20).name, 10 20 // 3 % 10, len(str(2 100)), (2 64) - 1 == 0xFFFFFFFFFFFFFFFF)
- What is printed? python x = [1, 2, 3] print(x 2, [x] 2 == [[1, 2, 3], [1, 2, 3]], x + [4] is x, [] == False, not [])
- What is printed? python t = 1, 2, u = (3) v = () w = (4,) 0 print(type(t).name, len(t), type(u).name, v == w, (1, 2) < (1, 2, -1))
- What is printed? python class Err(Exception): def init(self, code): super().init(f"code {code}") self.code = code try: try: raise Err(7)…
- What is printed? python def check(v): assert v > 0, f"bad {v}" return v try: check(-1) except AssertionError as e: print("AE", e, end=" ")…
- What is printed? python it = iter([1, 2]) try: while True: print(next(it), end=" ") except StopIteration as e: print("stop", e.value…