ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
s = slice(1, None, 2) t = "abcdefg" print(t[s], s.indices(len(t)), t[::-1][s])
- A. bdf (1, 7, 2) fdb
- B. bdf (1, None, 2) fdb
- C. bdf (1, 7, 2) gec
- D. bdf (1, 6, 2) fdb
Correct answer: A. bdf (1, 7, 2) fdb
slice(1, None, 2) picks indices 1, 3, 5 = 'bdf'. indices(7) resolves None to the length, giving (1, 7, 2). Reversed, the string is 'gfedcba', whose indices 1, 3, 5 are 'f', 'd', 'b'.
More Python Programming questions
- What is printed? python a = [1, 2, 3, 4, 5] a[1:4] = "xy" b = a[:] b[::-1] = b print(a, b[0])
- What is printed? python nums = [10, 20, 30, 40, 50] print(nums[-2::-2], nums[1:-1][::-1], nums[::-1][1:-1])
- What is printed? python s = "abcdef" print(s[5:1:-1], s[-1:-7:-1] == s[::-1], s[:-7:-1], s[2::-1])
- What is printed? python d = {"a": 1, "b": 2, "c": 3} d["a"] = 10 del d["b"] d["b"] = 20 d.update({"a": 0, "z": 9}) print(list(d)…
- What is printed? python d = {0: "zero", False: "F", 0.0: "f", "": "e"} s = {1, True, 1.0, -0.0, 0} print(d, len(s))
- What is printed? python nan = float("nan") L = [nan] print(nan == nan, nan in L, L == [nan], [float("nan")] == [float("nan")])