ProgrammingHardTCS NQT · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? def power(base, exp=2): return base ** exp print(power(3), power(3, 3), power(exp=3, base=2))
- A. 9 9 9
- B. 9 27 8
- C. 6 9 6
- D. 9 27 9
Correct answer: B. 9 27 8
power(3) uses the default exp=2 -> 9; power(3,3) -> 27; keyword call power(exp=3, base=2) -> 2**3=8.
More Python Programming questions
- What does the following Python 3 code print? class Stack: def init(self): self.items = [] def len(self): return len(self.items) def…
- What does the following Python 3 code print? class Range2: def init(self, n): self.n = n def getitem(self, i): if i >= self.n: raise…
- What does the following Python 3 code print? import itertools print(list(itertools.chain.fromiterable([[1, 2], [3], [4, 5, 6]])))
- What does the following Python 3 code print? import itertools print(list(itertools.product([1, 2], [3, 4])))
- What does the following Python 3 code print? import itertools print(list(itertools.combinationswithreplacement([1, 2, 3], 2)))
- What does the following Python 3 code print? import itertools print(list(itertools.compress([1, 2, 3, 4, 5], [1, 0, 1, 0, 1])))