ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class A:
def __getitem__(self, k):
return k
a = A()
print(a[1], a[1:3], a[::2], a[1, 2:3], a[...])- A. 1 [1, 2] [0, 2] (1, [2]) Ellipsis
- B. 1 slice(1, 3) slice(None, None, 2) (1, slice(2, 3)) ...
- C. 1 slice(1, 3, None) slice(None, None, 2) (1, slice(2, 3, None)) Ellipsis
- D. 1 (1, 3) (None, None, 2) (1, (2, 3)) Ellipsis
Correct answer: C. 1 slice(1, 3, None) slice(None, None, 2) (1, slice(2, 3, None)) Ellipsis
Subscription passes whatever is inside the brackets to __getitem__: slice syntax becomes a slice object (missing parts are None), commas make a tuple, and ... is the Ellipsis singleton.
More Python Programming questions
- What is printed? python print([int(t, 0) for t in " 12000 t-0b101 +7 ".split()])
- What is printed? python import sys print(sys.getintmaxstrdigits()) try: s = str(10 4300) except ValueError: print("too long")
- What is printed? python print(len(str(2 1000 5 1000)))
- What is printed? python rows = [(2, 'b'), (1, 'z'), (2, 'a'), (1, 'y')] print(sorted(rows, key=lambda t: (-t[0], t[1])))
- What is printed? python print(sorted(['bb', 'a', 'cc', 'd', 'ee'], key=len, reverse=True))
- What is printed? python import bisect a = [1, 2, 2, 2, 3] print(bisect.bisectleft(a, 2), bisect.bisectright(a, 2), bisect.bisectright(a…