N

COMP 110 QUIZ04

Section 1: Core OOP Concepts
  1. Class
    A blueprint defining attributes and methods for objects.
    Example: class Node: defines a linked list node template.

  2. Object (Instance)
    Concrete creation from a class with unique attribute values.
    Example: one = Node(1) creates a Node object.

  3. __init__ (Constructor)
    Special method called when creating an object to initialize attributes.
    Signature: def __init__(self, value, next=None)

  4. self Parameter
    Reference to the current object instance (first parameter in methods).
    Purpose: Access object's attributes like self.value.

  5. Attributes
    Variables owned by an object (e.g., Node.value, Node.next).
    Defined in constructor via self.attribute = ...


Section 2: Recursion & Linked Lists
  1. Base Case
    Condition where recursion stops (no further calls).
    Example: if head is None: return "None"

  2. Recursive Case
    Function calls itself with modified arguments to progress toward base case.
    Example: return f"{head.value} -> {to_str(head.next)}"

  3. Linked List
    Recursive data structure where each Node points to the next node or None.
    Components: head (first node), tail (last node, next=None)

  4. Recursive Traversal
    Process: Handle current node → recursively process next node.
    Pattern: Access head.value, then call function on head.next.


Section 3: Key Principles
  1. Abstraction
    Hiding complex implementation while exposing essential features.
    OOP Aspect: Objects provide methods for interaction without revealing internals.

  2. Instantiation
    Creating an object from a class using ClassName().
    Triggers __init__ to set initial attributes.

  3. Stack Frame
    Memory space for a function call. Recursion creates stacked frames.
    Base case resolves first (top of stack).

  4. RecursionError
    Occurs if base case isn’t reached (infinite recursion).
    Fix: Ensure recursive case progresses toward base case.
    1. Class Definition

    Front: What does this code define?

    python

    class Dog:

    Back: A class (blueprint for objects).

    Key: Look for class keyword.


    2. Instance Attribute

    Front: Identify the instance attribute:

    python

    def __init__(self):
        self.name = "Fido"

    Back: self.name (unique to each object).

    Key: Any variable prefixed with self..


    3. Class Attribute

    Front: What’s the class attribute?

    python

    class Dog:
        species = "Canine"

    Back: species (shared by all Dog objects).

    Key: Variables declared inside class but outside methods.


    4. Constructor

    Front: Which method is the constructor?

    python

    def __init__(self, age):
        self.age = age

    Back: __init__ (auto-called during instantiation).

    Key: Always named __init__.


    5. Method vs. Function

    Front: Is this a method or function?

    python

    def bark(self):
        print("Woof!")

    Back: Method (defined inside a class, takes self).

    Key: Functions are standalone; methods belong to classes.


    6. Magic Method

    Front: What does __str__ do?

    python

    def __str__(self):
        return f"Dog: {self.name}"

    Back: Defines how the object is printed (e.g., print(dog)).

    Key: Look for __method__ names.


    7. Local Variable

    Front: Find the local variable:

    python

    def calculate(self):
        total = self.price * 2

    Back: total (exists only inside the method).

    Key: Variables not attached to self.


    8. Class Method

    Front: What’s special here?

    python

    @classmethod
    def change_species(cls, new_species):
        cls.species = new_species

    Back: Class method (modifies class-level attributes via cls).

    Key: Decorator @classmethod and cls parameter.


    9. Instantiation

    Front: How do you create a Dog object?
    Back: my_dog = Dog() (calls __init__).

    Key: ClassName() syntax.


    10. Self Parameter

    Front: Why does every method need self?
    Back: self refers to the current object instance.

    Key: First parameter in instance methods.