Q-1: In which of the following does the CricketFan class correctly inherit from the PartyAnimal class?
from party import PartyAnimal
This is a necessary step to import the PartyAnimal class, but it does not define a class CricketFan that inherits from PartyAnimal
class CricketFan(PartyAnimal)
Correct! CricketFan inherits from PartyAnimal.
an = PartyAnimal()
This only creates an instance of the PartyAnimal class called an.
CricketFan = PartyAnimal()
This would create an instance of the PartyAnimal class called CricketFan.
2.
Q-2: Which of the following does not correctly create an object instance?
puppy = Dog("Jamie")
This uses the constructor function to create an object with arguments.
dog = Dog("Jamie")
The instance can have a similar name to the class. This uses the constructor function to create an object with arguments.
jamie = Dog()
This is correct and does not have arguments.
pupper = new Dog("Jamie")
The 'new' is incorrect
3.
Q-3: What does the following code output?
class People():
def __init__(self, name):
self.name = name
def namePrint(self):
print(self.name)
person1 = People("Sally")
person2 = People("Louise")
person1.namePrint()
Sally
This prints the value of the name attribute for person1.
Louise
'Louise' is the 'name' of person2 but we are calling the person1 instance.
Sally Louise
It only prints the value of name for one instance.
person1
The output is not the name of the instance.
4.
Q-4: Which of the following statements is incorrect about the following code?
class People():
def __init__(self, name):
self.name = name
def namePrint(self):
print(self.name)
person1 = People("Sally")
person2 = People("Louise")
person1.namePrint()
person1 and person2 are two different instances of the People class.
Since two different objects were created, this is correct.
The __init__ method is used to set initial values for attributes.
__init__ is an optional method in classes that is used to set initial values for objects.
'self' is not needed in def namePrint(self):
'self' is used to represent the current instance of the class.
person2 has a different value for 'name' than person1.
Since they are two different instances, they cannot access each other and have different initial values too
5.
Q-5: 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 class but not how it works internally.
A class contains functions as well as the data that is used by those functions.
This is true as an class can contain functions as well as data that is used by those functions.
Constructor methods are required to initialize an object and destructor methods are required to destroy the object when no longer required.
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.
A powerful feature of object-oriented programming is the ability to create a new class by extending an existing class.
This is true, we can extend a 'parent' class to create a new 'child' class and the new class has access to its functions and can override them if needed.
6.
Q-6: The _________ keyword defines a template indicating the data that will be in an object of the class and the functions that can be called on an object of the class.
class
The class keyword defines the data that is in an object of a class and the functions that can be called on an object of the class.
object
An object is created using this template.
Class
The keyword is case-sensitive
instance
An instance is a single occurrence of an object created when the object is initialized.
7.
Q-7: _________ is not a keyword, but by convention it is used to refer to the current instance (object) of a class.
class
'class' is a keyword that defines the template for objects of the class.
def
'def' is used to define a function
self
'self' is not a keyword, but by convention is used to refer to the current object
init
'init' is not a keyword, but the __init__ method is created to initialize class variables.
8.
Q-8: What is the output of the following code?
class Pokemon():
def __init__(self, name, type):
self.name = name
self.type = type
def stringPokemon(self):
print(f"Pokemon name is {self.name} and type is {self.type}")
class GrassType(Pokemon):
# overrides the stringPokemon() function on 'Pokemon' class
def stringPokemon(self):
print(f"Grass type pokemon name is {self.name}")
poke1 = GrassType('Bulbasaur', 'Grass')
poke1.stringPokemon
poke1.stringPokemon()
poke2 = Pokemon('Charizard', 'Fire')
poke2.stringPokemon
poke2.stringPokemon()
Grass type pokemon name is Bulbasaur Pokemon name is Charizard and type is Fire
A child class can inherit functions from parent class and also override them.
Pokemon name is Bulbasaur and type is Grass Pokemon name is Charizard and type is Fire
The stringPokemon() functions is changed inside the GrassType class.
Grass type pokemon name is Bulbasaur Grass type pokemon name is Charizard
The stringPokemon() functions is only changed for GrassType class but remains unchanged in the original class.
Error because the extending class has a stringPokemon() function which already exists.
A class inherits functions from another class and override them in any way. Only the constructor class cannot be changed.
9.
Q-9: True or False? In order to extend a class, the new class should have access to all the data and inner workings of the parent class.
True
The child class does not need access to the all the inner workings in parent class.
False
The child class knows how to use the parent class and its functions but not its data and the inner workings. The "super" command can come in handy here.
10.
Q-10: Which of the following is the correct way to define an initializer method?
def __init__(title, author):
Unless a class method is static, 'self' is explicitly used to represent current instance.
def __init__(self, title, author):
"self" is used to represent an instance of the given class and the parameters will be used to initialize variables.
def __init__():
At minimum, the "self" parameter is needed to represent an instance of the given class.
__init__(self, title, author):
The reserved word "def" is necessary to define a method.