Embercode
✕
Object-Oriented ProgrammingQuick quiz

Slicing and the object class

7 questions. Covers Object copying, the object base class and type checks in Python.

Questions 1 to 5 of 7

  1. 1. Predict the output of this program. ``python def describe(value): match value: case int(i) if i < 0: return f"negative {i}" case int(i): return f"integer {i}" case str(s): return f"text of length {len(s)}" case _: return f"other: {type(value).__name__}" print(describe(-3)) print(describe("abc")) print(describe(2.5)) ``
  2. 2. Predict the output of this program. ``python import copy class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound" class Dog(Animal): def __init__(self, name, toy): super().__init__(name) self.toy = toy def speak(self): return f"{self.name} barks" rex = Dog("Rex", "ball") plain = Animal(rex.name) same = rex print(plain.speak()) print(same.speak()) print(same is rex) clone = copy.copy(rex) print(type(clone).__name__, clone.toy) ``
  3. 3. A variable d holds Dog(), where Dog is a subclass of Animal. Which of these lines prints False?
  4. 4. copy.copy makes a new object whose attributes point to the same objects as the original. The objects inside those attributes are not copied. The lesson calls this kind of copy a ____ copy.

    Type your answer.

  5. 5. Which exception does Python raise for this code? ``python value = None value.speak() ``

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.