Skip to main content

Section 19.14 Multiple Choice Questions

Activity 19.14.1.

Which of the following does not correctly create an object instance?
  • student = Person("Charles")
  • This uses a constructor to initialize object attributes. Try again.
  • person = Person("Charles")
  • This uses a constructor to initialize object attributes. The instance can have a similar name to the class. Try again.
  • Charles = Person()
  • This is correct and does not have arguments. Try again.
  • student = new Person("Charles")
  • Correct! โ€™newโ€™ is incorrect.

Activity 19.14.2.

Which of the following is the correct way to define a constuctor (intializer)?
  • def __init__(title, author):
  • Incorrect! Unless a class method is static, โ€™selfโ€™ is needed to represent the current instance. Try again.
  • def __init__(self, title, author):
  • Correct! The "self" is used to represent an instance of the given class and the parameters will be used to initialize attributes.
  • def __init__():
  • Incorrect! At minimum, the "self" parameter is needed to represent an instance of the given class. Try again.
  • def init(self, title, author):
  • Incorrect! The method name is __init__. Try again.

Activity 19.14.3.

How many attributes does an object of the Book class have?
class Book:

  def __init__(self, title, author, pages, price, age_group):
    self.title = title
    self.author = author
    self.pages = pages
    self.price = price
    self.age_group = age_group

  def __str__(self):
    return "title: " + self.title + " author: " + self.author + " pages: " + self.pages + " price: " + self.price + " age_group: " + self.age_group
  • 2
  • Incorrect! This is the number of methods in the Book Class. Try again.
  • 3
  • Incorrect! How many items are initialized in the __init__ method?
  • 6
  • Incorrect! We do not count the โ€™selfโ€™ as an attribute. Try again.
  • 5
  • Correct! There are five attributes that are initialized in the __init__ method.

Activity 19.14.4.

What should be in the underlined section in the getX method of the Point class?
class Point:

  def __init__(self, initX, initY):
    self.x = initX
    self.y = initY

  def getX(self):
    return ______
  • x
  • Incorrect! We need to refer to the current instance of the class. Try again.
  • print(x)
  • Incorrect! We do not want print a value. We want to return the attribute value. Try again.
  • self.x
  • Correct! Here โ€™selfโ€™ is used to represent the current instance of the class.
  • init.x
  • Incorrect! There is nothing like init.x since we are using the โ€™selfโ€™ in the class. Try again.

Activity 19.14.5.

What does the following code output?
class Person():

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

    def __str__(self):
      print("Name: " + self.name)

  person1 = Person("James")
  person2 = Person("Tony")
  print(person1)
  • Name: James
  • Correct! This prints the string returned from the __str__ method.
  • Name: Tony
  • Incorrect! โ€™Tonyโ€™ is the โ€™nameโ€™ of person2 but we are printing the person1 instance. Try again.
  • Name: James Name: Tony
  • Incorrect! It does not print both objects.
  • person1
  • Incorrect! The output is not the name of the instance. Try again.

Activity 19.14.6.

Which of the following statements is incorrect about the following code?
class Person():

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

  def __str__(self):
    print("Name: " + self.name)

person1 = Person("James")
person2 = Person("Tony")
print(person1)
  • 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.

Activity 19.14.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.

Activity 19.14.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.

Activity 19.14.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.

Activity 19.14.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.
You have attempted of activities on this page.