and Answers Latest Version Already
Passed
What is the correct syntax for creating an instance of a class named `Car`?
A) `Car = new Car()`
B) `car = Car()`
✔✔C) `car = Car()`
D) `car = new Car`
How do you access an attribute from an instance of a class?
✔✔A) Using dot notation, like `instance.attribute`
B) Using square brackets, like `instance["attribute"]`
C) Using the `get()` method
D) Using parentheses, like `instance(attribute)`
How would you define a class called `Dog` with an attribute `breed`?
A) `class Dog { breed = "Bulldog" }`
✔✔B) `class Dog: def __init__(self, breed): self.breed = breed`
1
,C) `def Dog(breed): breed = "Bulldog"`
D) `class Dog: def __init__(breed): self.breed = breed`
What is the result of calling `my_object.method()` if the method requires a parameter?
A) It will raise a `TypeError` if no argument is passed
✔✔B) It will raise a `TypeError` if no argument is passed
C) It will automatically pass `None` as the argument
D) It will work even if no argument is passed
How do you create a method inside a class?
A) By using a function definition outside of the class
✔✔B) By using `def` inside the class, with `self` as the first parameter
C) By defining a function inside a variable
D) By using `function()` inside the class
What is the purpose of the `self` keyword in a method inside a class?
A) To reference the class itself
B) To reference other classes
2
,✔✔C) To reference the instance of the object calling the method
D) To pass arguments to other functions
How would you add a method to a class that prints "Hello, world!"?
A) `def hello(): print("Hello, world!")`
✔✔B) `def hello(self): print("Hello, world!")`
C) `method hello(self): print("Hello, world!")`
D) `hello(self): print("Hello, world!")`
What is the correct way to call a method `greet()` from an instance `dog` of the class `Dog`?
✔✔A) `dog.greet()`
B) `greet(dog)`
C) `greet.dog()`
D) `dog("greet")`
How do you define a class variable that is shared by all instances of the class?
✔✔A) By defining the variable inside the class but outside any method
B) By defining the variable inside the `__init__()` method
3
, C) By passing the variable as an argument to the constructor
D) By using the `self` keyword
What does it mean to "instantiate" a class?
✔✔A) Creating an instance (or object) of the class
B) Defining the class itself
C) Calling a class method
D) Setting a class variable
How do you create a method that returns the value of an attribute?
A) By using a return statement in the `__init__()` method
✔✔B) By defining a method that accesses the attribute, like `def get_breed(self): return
self.breed`
C) By creating a getter function outside the class
D) By assigning the attribute value directly to a variable
How would you define a method that changes the value of an attribute in a class?
✔✔A) By using a setter method like `def set_breed(self, breed): self.breed = breed`
4