Embercode
✕
References and MemoryQuick quiz

None and stale references

6 questions. Covers None bugs and stale references in Python.

Questions 1 to 5 of 6

  1. 1. The program below crashes. Which line causes the crash, and why? ``python stock = {'apple': 4} count = stock.get('pear') print(count + 1) ``
  2. 2. The code below calls a method on a value that may be None. Which change makes the check safe? ``python name = None if name.lower() == 'ana': print('hi Ana') ``
  3. 3. A crash prints this message: AttributeError: 'NoneType' object has no attribute 'upper'. What does the message tell you?
  4. 4. What does this check do when name is None? ``python def label(name): if name is None: raise ValueError('name must not be None') return 'Item: ' + name ``
  5. 5. What does this program print? ``python text = None print(text or '(missing)') name = None print('empty' if name is None else name.strip()) try: print(text.upper()) except AttributeError: print('caught') ``

5 question(s) left on this page.