ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [1, 2, 3] a.append(a) print(len(a), a[-1][-1][0])
- A. 3 1
- B. 4 3
- C. RecursionError
- D. 4 1
Correct answer: D. 4 1
append(a) adds a reference to the list inside itself, so the list has length 4. a[-1] is the list itself, a[-1][-1] is the list again, and its first element is 1.
More Python Programming questions
- What is printed? python x = 'a,b;c d' print(x.replace(',', ' ').replace(';', ' ').split(), x.replace(',', ' ').split(' '))
- What is printed? python import string print(string.asciilowercase.index('z') - string.asciiuppercase.index('A'), len(string.digits +…
- What is printed? python a = [5, 1, 4] b = a a = sorted(a) c = a.copy() c.reverse() print(a, b, c)
- What is printed? python def f(a, b=[]): b.append(a) return len(b) print(f(1), f(2, []), f(3))
- What is printed? python values = [4, 8, 15, 16, 23, 42] print(bisect.bisect(values, 15) if False else 0, [v for v in values if v % 2 and v…
- What is printed? python import heapq h = [5, 1, 4] print(heapq.heappushpop(h, 0), heapq.heappushpop([], 7), sorted(h))