ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
a = [1, 2, 3, 4]
try:
print(a[len(a) / 2])
except TypeError:
print('TypeError')
print(a[len(a) // 2])- A. 2 | 3
- B. TypeError | 2
- C. TypeError | 3
- D. 2 | 2
Correct answer: C. TypeError | 3
len(a) / 2 is the float 2.0 in Python 3, and list indices must be integers, so the first print raises TypeError. The floor division gives 2, and a[2] is 3.
More Python Programming questions
- What is printed? python s = 'u00b2' print(s.isdigit(), s.isdecimal(), s.isnumeric())
- What is printed? python d = {1: 'a'} d[1.0] = 'b' d[True] = 'c' print(d)
- What is printed? python def acc(): total = 0 while True: x = yield total if x is None: return total total += x g = acc() next(g) g.send(5)…
- What is printed? python class P: def init(self, x): self.x = x def eq(self, o): return self.x == o.x try: s = {P(1)} except TypeError…
- What is printed? python class S: slots = ('a',) s = S() s.a = 1 try: s.b = 2 except AttributeError: print('no dict', s.a)
- What is printed? python print(len(set(map(lambda x: x % 3, range(10)))), sum(1 for in filter(None, [0, 1, '', 'a', None])))