person1 and person2 are two different instances of the People class.
Since two different objects were created, this is correct. Try again.
The __init__ class is used to create instances and set initial values for its attributes.
__init__ is an optional method in classes that is used to set initial values for objects. Try again.
As we are not updating any values, 'self' does not need to be passed to the __str__ method.
Correct! Here 'self' is used to represent the current instance of the class and the current object must be passed into the __str__ method.
person2 cannot access the 'name' of person1.
Since they are two different instances, they cannot access each other and have different initial values too. Try again.
7.
Q-7: Which of the following statements is not true about object-oriented programming?
One of the benefits of object-oriented programming is that it can hide complexity.
This is true, while using an object, we need to know how to use the object, but not how it works internally. Try again.
A class defines a number of functions as well as the data that is used by those functions.
This is true as a class defines functions as well as data that is used by those functions. Try again.
Constructor methods are required to initialize an object and destructor methods are required to destroy the object when no longer required.
Correct! This is false, a constructor is optional can be used to set initial values for an object and python automatically destroys any object if its reference count changes to a zero without needing a destructor.
The __str__ method is responsible for returning a string representation for an object.
This is true, we can print information about an object with the __str__ method. Try again.
8.
Q-8: Which of the following is the correct way to define the __str__ method which returns a string with the title and author?
def __str__(title, author):
Incorrect! Unless a class method is static, 'self' is used to represent the current instance. Try again.
def __str__(self):
Correct! The "self" is used to represent an instance of the given class.
def __str__():
Incorrect! The "self" parameter is needed to represent an instance of the given class. Try again.
def str(self):
Incorrect! The method is __str__. Try again.
9.
Q-9: The _________ keyword defines a template for objects of a class.
Class
Incorrect! Python is case sensitive. It should be class not Class.
object
Incorrect! An object is created by a class. Try again.
class
Correct! The keyword is class.
instance
Incorrect! An instance is created from a class. Try again.
10.
Q-10: _________ is by convention used to represent the current instance of a class and to access the attributes and methods of the class.
class
Incorrect! The 'class' keyword defines a template for all objects of the class. Try again.
def
Incorrect! The 'def' keyword defines a function. Try again.
self
Correct! While 'self' is not a keyword, the convention is to use it to represent the current object.
init
Incorrect! The 'init' is not a keyword, but the __init__ method is created to initialize object attributes. Try again.