ProgrammingHardTCS CodeVita · GATE CS · Advanced
What does the following Python 3 code print? s = set([1, 2, 2, 3, 3, 3]) print(sorted(s), len(s))
- A. [1, 2, 2, 3, 3, 3] 6
- B. [1, 1, 2, 2, 3, 3] 3
- C. [1, 2, 3] 6
- D. [1, 2, 3] 3
Correct answer: D. [1, 2, 3] 3
A set automatically discards duplicates, keeping only the distinct values 1, 2, 3.
More Python Programming questions
- 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…
- 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))