Deck 6: Inheritance and Abstract Classes

ملء الشاشة (f)
exit full mode
سؤال
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.
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
When a class is customized using inheritance, the two classes should have a similar interface.
سؤال
An abstract class captures the common features and behavior of a set of related classes.
سؤال
The __init__ method in the parent class is automatically called by a subclass of the parent class.
سؤال
By using inheritance to create new classes, you increase code redundancy.
سؤال
An abstract class is always used to create objects in client applications.
سؤال
To ensure that inheritance occurs, you need to place the name of the subclass in parentheses of the class header.
سؤال
A concrete class is often a subclass of an abstract class and is used to create objects in client applications.
سؤال
When you use inheritance to customize a class, the existing and new class have identical behavior.
سؤال
When calling the __init__ method from a subclass, you need to include the self argument.
سؤال
A class that inherits properties from another class is called a superclass.
سؤال
To begin creating a subclass, copy the parent class's file and delete the methods that will not change.
سؤال
The implementations of the __str__ and __eq__ methods in the AbstractCollection class can be used in both unordered and linear collections.
سؤال
A class that uses lists can use the methods defined in the AbstractCollection class.
سؤال
The easiest way to take advantage of inheritance is to use it to customize an existing class.
سؤال
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.
سؤال
To distinguish a method in the parent class from a method with the same name in a subclass, prefix the method name with self .
سؤال
The root class of the Python hierarchy is called object .
سؤال
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.
سؤال
An instance variable that refers to an integer in two different related classes is a likely candidate to be moved to an abstract class.
سؤال
A primary purpose of using inheritance is to eliminate redundant code.
سؤال
When a programmer calls the iter function on a collection, the collection's iterator object is returned.
سؤال
What is the root class of Python's built-in class hierarchy?

A) self
B) list
C) dict
D) object
سؤال
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
سؤال
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
سؤال
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
سؤال
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
سؤال
The __eq__ method in the AbstractCollection class compares pairs of items in two collections.
سؤال
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)
سؤال
Which operator, when used with two bags, causes Python to run the __add__ method?

A) =
B) *
C) +
D) %
سؤال
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.
سؤال
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
سؤال
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
سؤال
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
سؤال
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)
سؤال
Programming languages such as Java include a collection framework more extensive than that of Python.
سؤال
What method is called when the in operator is used on a sorted bag?

A) __iter__
B) __contains__
C) __init__
D) add
سؤال
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
سؤال
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
سؤال
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
سؤال
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)
سؤال
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
سؤال
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__
سؤال
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)
سؤال
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
سؤال
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
سؤال
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
سؤال
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
سؤال
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
سؤال
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
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/50
auto play flashcards
العب
simple tutorial
ملء الشاشة (f)
exit full mode
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
5
By using inheritance to create new classes, you increase code redundancy.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
6
An abstract class is always used to create objects in client applications.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
7
To ensure that inheritance occurs, you need to place the name of the subclass in parentheses of the class header.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
8
A concrete class is often a subclass of an abstract class and is used to create objects in client applications.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
9
When you use inheritance to customize a class, the existing and new class have identical behavior.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
10
When calling the __init__ method from a subclass, you need to include the self argument.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
11
A class that inherits properties from another class is called a superclass.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
12
To begin creating a subclass, copy the parent class's file and delete the methods that will not change.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
14
A class that uses lists can use the methods defined in the AbstractCollection class.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
15
The easiest way to take advantage of inheritance is to use it to customize an existing class.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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 .
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
18
The root class of the Python hierarchy is called object .
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
21
A primary purpose of using inheritance is to eliminate redundant code.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
22
When a programmer calls the iter function on a collection, the collection's iterator object is returned.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
23
What is the root class of Python's built-in class hierarchy?

A) self
B) list
C) dict
D) object
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
28
The __eq__ method in the AbstractCollection class compares pairs of items in two collections.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
30
Which operator, when used with two bags, causes Python to run the __add__ method?

A) =
B) *
C) +
D) %
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
36
Programming languages such as Java include a collection framework more extensive than that of Python.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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__
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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)
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 50 في هذه المجموعة.