ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita
What does the following Python 3 code print? a = frozenset([1, 2, 3]) try: a.add(4) except AttributeError: print("AttributeError")
- A. frozenset({1, 2, 3, 4})
- B. TypeError
- C. None
- D. AttributeError
Correct answer: D. AttributeError
frozenset is immutable and has no add() method at all, so calling it raises AttributeError, not a mutation error.
More Python Programming questions
- What does the following Python 3 code print? s = set([1, 2, 2, 3, 3, 3]) print(sorted(s), len(s))
- 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")