Deck 6: Inheritance and Abstract Classes
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
Play
Full screen (f)
Deck 6: Inheritance and Abstract Classes
1
A set of methods that could be used on other data structures besides the one they were created for could be used in a general abstract class.
True
2
When a class is customized using inheritance, the two classes should have a similar interface.
True
3
An abstract class captures the common features and behavior of a set of related classes.
True
4
The __init__ method in the parent class is automatically called by a subclass of the parent class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
By using inheritance to create new classes, you increase code redundancy.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
An abstract class is always used to create objects in client applications.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
To ensure that inheritance occurs, you need to place the name of the subclass in parentheses of the class header.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
A concrete class is often a subclass of an abstract class and is used to create objects in client applications.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
When you use inheritance to customize a class, the existing and new class have identical behavior.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
When calling the __init__ method from a subclass, you need to include the self argument.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
A class that inherits properties from another class is called a superclass.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
To begin creating a subclass, copy the parent class's file and delete the methods that will not change.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
The implementations of the __str__ and __eq__ methods in the AbstractCollection class can be used in both unordered and linear collections.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
A class that uses lists can use the methods defined in the AbstractCollection class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
The easiest way to take advantage of inheritance is to use it to customize an existing class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
When looking at a hierarchy of abstract classes, the classes trend from the more specific to the more general going from the top to the bottom of the hierarchy.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
To distinguish a method in the parent class from a method with the same name in a subclass, prefix the method name with self .
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
The root class of the Python hierarchy is called object .
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
An instance variable is considered redundant if two different classes use it and it refers to a different type of data structure in each class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
An instance variable that refers to an integer in two different related classes is a likely candidate to be moved to an abstract class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
A primary purpose of using inheritance is to eliminate redundant code.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
When a programmer calls the iter function on a collection, the collection's iterator object is returned.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
What is the root class of Python's built-in class hierarchy?
A) self
B) list
C) dict
D) object
A) self
B) list
C) dict
D) object
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
Under which circumstance can an instance variable be moved to an abstract class?
A) when it refers to the same type of data structure
B) when it is unique to a particular concrete class
C) when different data structure types are referenced by the variable
D) when it directly modifies the underlying data structure
A) when it refers to the same type of data structure
B) when it is unique to a particular concrete class
C) when different data structure types are referenced by the variable
D) when it directly modifies the underlying data structure
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following is true about abstract classes?
A) they are the same as a class interface
B) the are always a subclass of a superclass
C) they are not normally instantiated in client applications
D) they are used as concrete classes by client applications
A) they are the same as a class interface
B) the are always a subclass of a superclass
C) they are not normally instantiated in client applications
D) they are used as concrete classes by client applications
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
Which of the following is true about the sorted bag collection compared to the regular bag collection?
A) the add method is identical in both
B) the __init__ method is identical in both
C) the sorted bag's in operator runs in linear time
D) the items in a sorted bag must be of the same type
A) the add method is identical in both
B) the __init__ method is identical in both
C) the sorted bag's in operator runs in linear time
D) the items in a sorted bag must be of the same type
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
How is creating a subclass of an existing class different from copying the code from the existing class to a new file?
A) delete all the methods that don't have to change
B) you don't need an __init__ method
C) the name of the subclass is in parentheses of the class header
D) no methods need be created or modified
A) delete all the methods that don't have to change
B) you don't need an __init__ method
C) the name of the subclass is in parentheses of the class header
D) no methods need be created or modified
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
The __eq__ method in the AbstractCollection class compares pairs of items in two collections.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
What is the syntax for the calling the __init__ method in the ArrayBag class from the ArraySortedBag class?
A) self.init(ArrayBag, sourceCollection)
B) ArrayBag.self(__init__, sourceCollection)
C) ArrayBag.__init__(self, sourceCollection)
D) __init__.ArrayBag(self, sourceCollection)
A) self.init(ArrayBag, sourceCollection)
B) ArrayBag.self(__init__, sourceCollection)
C) ArrayBag.__init__(self, sourceCollection)
D) __init__.ArrayBag(self, sourceCollection)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
Which operator, when used with two bags, causes Python to run the __add__ method?
A) =
B) *
C) +
D) %
A) =
B) *
C) +
D) %
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
If a programmer calls the next function on an iterator object and there is no current item in the sequence, the next function returns the Null item.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
Which of the following is NOT a step in using inheritance to create a subclass?
A) delete the methods that do not change
B) duplicate redundant instance variables
C) modify the code for methods that change
D) add any new methods
A) delete the methods that do not change
B) duplicate redundant instance variables
C) modify the code for methods that change
D) add any new methods
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
When considering the ArrayBag class and the ArraySortedBag class, which of the following is true?
A) the ArraySortedBag class is the parent of the ArrayBag class
B) the ArraySortedBag class is a subclass of ArrayBag
C) the ArrayBag class is a subclass of ArraySortedBag
D) the ArrayBag class is the child class of ArraySortedBag
A) the ArraySortedBag class is the parent of the ArrayBag class
B) the ArraySortedBag class is a subclass of ArrayBag
C) the ArrayBag class is a subclass of ArraySortedBag
D) the ArrayBag class is the child class of ArraySortedBag
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
In the ArraySortedBag class, why must the self argument be passed to the __init__ method of ArrayBag?
A) to ensure the add method in ArraySortedBag is called if a source collection is specified
B) to ensure that ArraySortedBag calls the correct __init__ method
C) to ensure that ArrayBag calls its own __init__ method
D) to ensure the add method in ArrayBag is called if a source collection is specified
A) to ensure the add method in ArraySortedBag is called if a source collection is specified
B) to ensure that ArraySortedBag calls the correct __init__ method
C) to ensure that ArrayBag calls its own __init__ method
D) to ensure the add method in ArrayBag is called if a source collection is specified
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
The following code is part of the add method in the ArraySortedBag class. What is the missing code? if self.isEmpty() or item >= self.items[len(self) - 1]:
< missing code >
A) add.self(item)
B) ArrayBag.add(item)
C) ArrayBag.add(self, item)
D) add.ArrayBag(self, item)
< missing code >
A) add.self(item)
B) ArrayBag.add(item)
C) ArrayBag.add(self, item)
D) add.ArrayBag(self, item)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
Programming languages such as Java include a collection framework more extensive than that of Python.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
What method is called when the in operator is used on a sorted bag?
A) __iter__
B) __contains__
C) __init__
D) add
A) __iter__
B) __contains__
C) __init__
D) add
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
What is one of the primary purposes of using inheritance?
A) to customize an existing class
B) to create code redundancy
C) to avoid creating an __init__ method
D) to duplicate methods in an interface
A) to customize an existing class
B) to create code redundancy
C) to avoid creating an __init__ method
D) to duplicate methods in an interface
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
In which situation is a search unnecessary when performing the add method on a sorted bag?
A) when the bag is full
B) when the new item is less than the last item
C) when the new item is greater than the first item
D) when the bag is empty
A) when the bag is full
B) when the new item is less than the last item
C) when the new item is greater than the first item
D) when the bag is empty
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
What is a class called when the purpose of it is to eliminate redundant methods in other classes?
A) concrete class
B) abstract class
C) subclass
D) child class
A) concrete class
B) abstract class
C) subclass
D) child class
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
In the constructor for the ArrayBag class shown below, what is the missing code?
Def __init__(self, sourceCollection = None):
< missing code >
AbstractBag.__init__(self, sourceCollection)
A) items.add(self)
B) self.items = Array(ArrayBag.DEFAULT_CAPACITY)
C) self.items = ArrayBag.add(sourceCollection)
D) add.items(self, sourceCollection)
Def __init__(self, sourceCollection = None):
< missing code >
AbstractBag.__init__(self, sourceCollection)
A) items.add(self)
B) self.items = Array(ArrayBag.DEFAULT_CAPACITY)
C) self.items = ArrayBag.add(sourceCollection)
D) add.items(self, sourceCollection)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
Which of the following is true about the three bag classes (LinkedBag, ArrayBag, ArraySortedBag)?
A) they are abstract classes
B) they implement the bag interface
C) the are all superclasses
D) LinkedBag is a subclass of ArraySortedBag
A) they are abstract classes
B) they implement the bag interface
C) the are all superclasses
D) LinkedBag is a subclass of ArraySortedBag
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
When creating the AbstractCollection class, which methods must you modify to provide default behavior?
A) __len__ and count
B) __str__ and __eq__
C) isEmpty and add
D) __init__ and __len__
A) __len__ and count
B) __str__ and __eq__
C) isEmpty and add
D) __init__ and __len__
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
In the following code for the __add__ method in the ArraySortedBag class, what is the missing code? def __add__(self, other):
Result = ArraySortedBag(self)
For item in other:
< missing code >
Return result
A) result = result + 1
B) item.add(result)
C) result.add(item)
D) add.item(result)
Result = ArraySortedBag(self)
For item in other:
< missing code >
Return result
A) result = result + 1
B) item.add(result)
C) result.add(item)
D) add.item(result)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
When a programmer calls the next function on an iterator object, what happens if there is no current item in the sequence?
A) the previous item is returned
B) the compiler generates an error
C) a StopIteration exception is raised
D) the None item is returned
A) the previous item is returned
B) the compiler generates an error
C) a StopIteration exception is raised
D) the None item is returned
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
In the case of the AbstractCollection class, which of the following methods should NOT be included in this class?
A) add
B) isEmpty
C) __len__
D) count
A) add
B) isEmpty
C) __len__
D) count
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
In the following code for the ArrayBag class __contains__ method, what is the missing code?
Def __contains__(self, item):
Left = 0
Right = len(self) - 1
While left <= right:
MidPoint = (left + right) // 2
If self.items[midPoint] == item:
Return True
Elif self.items[midPoint] > item:
Right = midPoint - 1
Else:
< missing code >
Return False
A) right = midPoint + 1
B) left = midPoint - 1
C) right = left + 1
D) left = midPoint + 1
Def __contains__(self, item):
Left = 0
Right = len(self) - 1
While left <= right:
MidPoint = (left + right) // 2
If self.items[midPoint] == item:
Return True
Elif self.items[midPoint] > item:
Right = midPoint - 1
Else:
< missing code >
Return False
A) right = midPoint + 1
B) left = midPoint - 1
C) right = left + 1
D) left = midPoint + 1
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
When implementing the __init__ method in the AbstractBag class, what task should you leave out because it is the responsibility of the subclasses?
A) setting the self.size variable to 0
B) adding items from the source collection
C) initializing the self.items variable
D) checking if a source collection exists
A) setting the self.size variable to 0
B) adding items from the source collection
C) initializing the self.items variable
D) checking if a source collection exists
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
When you finish writing the abstract class, what must the subclasses do to use it?
A) reference the abstract class in their __init__ method.
B) copy the code to the subclass file
C) nothing, the abstract class is automatically included in the subclass
D) import the abstract class
A) reference the abstract class in their __init__ method.
B) copy the code to the subclass file
C) nothing, the abstract class is automatically included in the subclass
D) import the abstract class
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
What would be the purpose of creating a more general abstract class called AbstractCollection ?
A) to group methods used by a single subclass
B) to hide the details of the methods used in your abstract classes
C) to create objects from a client application
D) to group methods that can be used for other types of collections
A) to group methods used by a single subclass
B) to hide the details of the methods used in your abstract classes
C) to create objects from a client application
D) to group methods that can be used for other types of collections
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck