ProgrammingVery hardAccenture · Cognizant · Python
What does the following Python 3 code print? class Node: def __init__(self, v, nxt=None): self.v = v; self.nxt = nxt n3 = Node(3) n2 = Node(2, n3) n1 = Node(1, n2) out = [] cur = n1 while cur: out.append(cur.v) cur = cur.nxt print(out)
- A. [3, 2, 1]
- B. [1, 2, 3]
- C. [1, 2]
- D. [1, 2, 3, None]
Correct answer: B. [1, 2, 3]
Walking the singly linked list from n1 through nxt pointers visits nodes with values 1, 2, 3 in order.
More Python Programming questions
- What does the following Python 3 code print? def fib(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib(n - 1…
- What does the following Python 3 code print? def outer(): x = 10 def inner(): x = 20 return x inner() return x print(outer())
- What does the following Python 3 code print? def outer(): x = [10] def inner(): x[0] = 20 inner() return x print(outer())
- What does the following Python 3 code print? x = 100 def f(): print(x) x = 5 try: f() except UnboundLocalError: print("UnboundLocalError")
- What does the following Python 3 code print? print("%d-%s-%.2f" % (5, "hi", 3.14159))
- What does the following Python 3 code print? print(list(filter(lambda x: x % 2 == 0, range(10))))