Embercode
✕
References and MemoryQuick quiz

Stack and read-only values

7 questions. Covers Read-only values in Python.

Questions 1 to 5 of 7

  1. 1. The recursive total(n) below is called with n = 100000. At the deepest point, every pending call still holds a frame on the call stack. Which space complexity does this recursion use on the call stack? ``python def total(n): if n == 0: return 0 return n + total(n - 1) ``
  2. 2. total(100000) from the previous question fails with RecursionError under the default limit. Which change lets it finish, with the recursive code left as it is?
  3. 3. A function recurse(depth) adds one to depth, returns when depth == 10, and otherwise calls itself. print(recurse(0)) prints 10 with no error. Why?
  4. 4. What is the bug in this function? ``python def sum_to(n): if n == 1: return 1 return n + sum_to(n - 1) print(sum_to(0)) ``
  5. 5. What happens when this code runs? ``python import dataclasses @dataclasses.dataclass(frozen=True) class Person: name: str p = Person('x') p.name = 'y' ``

5 question(s) left on this page.