Below are some practice problems for working with objects in python. These are completely OPTIONAL but are highly recommended as extra practice. Solutions for each can be found at the bottom of the page.
Given the code listing below, draw an environment diagram then answer the questions that follow. In each frame on the stack, remember to include a space for the return address and return value.
"""Nested class environment diagram."""
from __future__ import annotations
from typing import List
class Review:
    """Review class."""
    score: float
    def __init__(self, s: float):
        """Review Constructor."""
        self.score = s
    
class Rating:
    """Rating class."""
    reviews: List[Review]
    def __init__(self, a1: Review, a2: Review):
        """Rating constructor."""
        self.reviews = [a2, a1]
    
    def biased_average(self) -> float:
        """Calculate biased average."""
        output: float = 20
        for i in range(0, len(self.reviews)):
            output += self.reviews[i].score
        return output / (len(self.reviews) + 2)
    
def main() -> None:
    """Main functinon."""
    a = Review(6.5)
    alpine = Rating(a, Review(8.3))
    print(alpine.biased_average())
    
main()The following questions are based on the environment diagram for the code listing. If a question is asked about a name that has not been initalized, respond with undefined.
Review’s constructor frame, what is the type of self.score?biased_average’s frame, what is the type of of the value of self?alpine.reviews?Given the code listing below, draw an environment diagram then answer the questions that follow. In each frame on the stack, remember to include a space for the return address and return value.
"""Vec environment diagram."""
from __future__ import annotations
class Vec:
    """Vec class."""
    x: float = 0
    y: float = 0
    z: float = 0
    def __init__(self, a: float, b: float, c: float):
        """Vec constructor."""
        self.x = a
        self.y = b
        self.z = c
    def dot(self, a: Vec) -> float:
        """Dot product of two vectors."""
        print(self.x)
        return self.x * a.x + self.y * a.y + self.z * a.z
    
def main() -> None:
    """Main function."""
    a = Vec(1, 3, -2)
    b = Vec(5, 1, 4)
    
    a_dot = a.dot(b)
    another_dot = b.dot(a)
    print(a_dot)
    print(another_dot)
main()The following questions are based on the environment diagram for the code listing. If a question is asked about a name that has not been initalized, respond with undefined.
Vec objects are on the heap?main’s frame, what is the value of a? What is its type?main’s frame, what is the value of a.z? What is its type?main’s frame, what is the value of x? What is its type?Given the class definition of a Sim provided below, complete the following exercises.
Sim class that takes in and sets up the name and age values.Sim class called bday that increases the Sim’s age by one, turns its is_happy value to True and decreases its hunger value by 30.bday that takes in a Sim and increases its age by one, turns its is_happy to True and decreases its hunger by 30.floatRatingList[Review]a is a Vec with the following attributes: x: 1, y: 3, z: -2a.z is -2. It is a floatclass Sim:
    """Sim class definiton."""
    name: str
    age: int
    is_happy: bool = True
    hunger: int = 50
    def __init__(self, s: str, n: int): 
        """Sim constructor."""
        self.name = s
        self.age = n
    def bday(self) -> None:
        """Bday method."""
        self.age += 1
        self.is_happpy = True
        self.hunger -= 30
def bday(a: Sim) -> None:
    """Bday function."""
    a.age += 1
    a.is_happy = True
    a.hunger -= 30