ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = "hello"
try:
a[0] = "j"
except TypeError:
a = "j" + a[1:]
b = a.replace("l", "X", 1)
print(a, b, len(a.replace("", "-")))- A. jello jeXXo 11
- B. hello heXlo 11
- C. jello jeXlo 11
- D. jello jeXlo 9
Correct answer: C. jello jeXlo 11
Strings are immutable, so item assignment fails and a new string is built. replace's third argument limits it to one replacement. Replacing '' inserts '-' at all len+1 = 6 gaps, giving 5 + 6 = 11 characters.
More Python Programming questions
- What is printed? python print(1000 == 1000, 0.1 + 0.7, 7 / 2 == 7 // 2 + 0.5, 2 0.5 2, (-8) (1 / 3) == -2)
- What is printed? python s = "racecar" print(s.index("c"), s.rindex("c"), s.find("c", 3), s[::-1] is s, s.count("a", 2))
- 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)…