ProgrammingHardIBM · Deloitte · Product Company SDE
What does the following Python 3 code print? a = {1, 2, 3} b = {2, 3, 4} print(a & b, a | b, a - b, a ^ b)
- A. {2, 3} {1, 2, 3, 4} {4} {1, 4}
- B. {2, 3} {1, 2, 3, 4} {1} {1, 2, 3, 4}
- C. {2, 3} {1, 2, 3, 4} {1} {1, 4}
- D. {1, 2, 3, 4} {2, 3} {1} {1, 4}
Correct answer: C. {2, 3} {1, 2, 3, 4} {1} {1, 4}
& is intersection {2,3}; | is union {1,2,3,4}; - is set difference (in a not b) {1}; ^ is symmetric difference (in exactly one) {1,4}.
More Python Programming questions
- What does the following Python 3 code print? a = frozenset([1, 2, 3]) try: a.add(4) except AttributeError: print("AttributeError")
- 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())