ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
s = "abcdefgh" print(s[1:6:2], s[::-3], s[-3:-1], len(s[5:2]))
- A. bdf hea fg 0
- B. bdf heb fg 0
- C. bdf heb fgh 0
- D. ace heb fg 0
Correct answer: B. bdf heb fg 0
s[1:6:2] takes indices 1, 3, 5 → bdf. s[::-3] starts at the last index 7 and steps back by 3: h (7), e (4), b (1). s[-3:-1] covers indices 5 and 6 → fg (the stop is excluded). A forward slice with start > stop is simply empty – no error.
More Python Programming questions
- What is printed? python def gen(): for i in range(3): x = yield i print("got", x, end="; ") g = gen() print(next(g), end="; ")…
- What is printed? python a = 256; b = 256 c = [1, 2]; d = [1, 2] print(a == b, c == d, c is d, c[:] is c)
- What is printed? python t = (1, [2, 3], 4) try: t[1] += [5] except TypeError as e: print("error", end=" ") print(t)
- What is printed? python def f(): try: return "try" finally: print("finally", end=" ") def g(): try: return "try" finally: return "finally"…
- What is printed? python nums = [3, 1, 2] r = nums.sort() s = sorted(nums, reverse=True) print(r, nums, s)
- What is printed? python x = 10 def outer(): x = 20 def inner(): nonlocal x x += 1 return x return inner() + x print(outer(), x)