Deck 5: Interfaces, Implementations, and Polymorphism

Full screen (f)
exit full mode
Question
You can use a for loop on any collection.
Use Space or
up arrow
down arrow
to flip the card.
Question
Once the designer of a collection class has obtained its interface, the implementation of the class includes completing code for the methods.
Question
When Python sees the in operator used with a collection, it runs the __contains__ method in the collection's class.
Question
When used with a bag object, the in operator returns an integer value.
Question
Software bags can grow as more items are added, or they can shrink as items are removed.
Question
The logical size of an array-based bag will always be the same as the array's capacity.
Question
A precondition is a statement of what must be true for a method to perform its actions correctly.
Question
When the LinkedBag structure is not empty, self.items refers to the last node in the linked structure.
Question
Whenever you need to use the logical size of the bag within a class definition, run len(self) instead of referring directly to the instance variable self.size .
Question
Each time you run Python's document function to obtain information about a module, data type, method, or function, you are accessing documentation about that resource's interface.
Question
The clear method empties a bag.
Question
The statement in the ArrayBag constructor to copy an item from a source collection to a new ArrayBag object is self.init(item) .
Question
A bag is a type of ordered collection.
Question
When defining the Bag class, the __init__ operation is the constructor for the class.
Question
An interface's documentation gives you enough information to know how to use or call a method and what to expect it to accomplish and return.
Question
Data that is needed to represent the state of a collection are assigned instance variables in the __help__ method of the class.
Question
You should always try to hide the implementing data structures behind a wall of method calls on the object being implemented.
Question
A docstring is a string enclosed in double quotes that will be displayed when Python's help function is run on a resource.
Question
When Python sees a for loop on an iterable object, it runs that object's __add__ method.
Question
The __iter__ method uses a yield statement to send each item to the calling for loop.
Question
Which of the following is one of the hallmarks of well-designed software?

A) a random grouping of interfaces to ensure secure code
B) an interface that is tightly coupled to the implementation
C) the clean separation of interfaces from implementations
D) a sophisticated interface with a high learning curve
Question
In the Bag class defined in the chapter, what is one of the purposes of the sourceCollection argument in the __init__ method?

A) to create a bag with the contents of another collection
B) to delete the contents of an existing collection
C) to define source code for the method
D) to add data to an existing bag collection
Question
In the ArrayBag class, why must the ArrayBag object track its logical size in a separate instance variable?

A) because the physical size is always smaller than the logical size
B) because the initial size of the object can't be changed
C) because the logical size will likely differ from the array's capacity
D) because the array size and the logical size are identical
Question
The running times of the operations of ArrayBag and LinkedBag are similar.
Question
In the remove method for LinkedBag, if probe points to the node at the head of the linked structure, trailer will point to the tail.
Question
What word is best defined as the use of common names for operations on different types of collections?

A) anachronism
B) polymorphism
C) concatenation
D) modularization
Question
The design and implementation of a collection class consists of two steps. What is the first step?

A) document the class using docstrings
B) choose an appropriate data structure
C) write the code for the defined methods
D) test the method using sample data
Question
What is the name of the method that performs the constructor operation for a class?

A) __eq__
B) __iter__
C) __add__
D) __init__
Question
The code for the add method for the ArrayBag class is shown below. What is the missing code?
Def add(self, item):
Self.items[len(self)] = item
< missing code >

A) self.items +=1
B) self = self + 1
C) self.size += 1
D) self.len = self.items
Question
In the ArrayBag class, what must the __init__ method do if a source collection is specified?

A) the new instantiation of the bag must be copied to the source collection.
B) the source collection must be initialized with the clear method
C) the data in the source collection must be deleted
D) the data in the source collection must be copied
Question
What does Python do when it sees a for loop on an iterable object?

A) executes a do loop
B) runs the __iter__ method
C) runs the clear method
D) executes a return statement
Question
Which Python function allows you to obtain information about a module, data type, method, or function?

A) help
B) list
C) doc
D) info
Question
The __iter__ method is identical in the ArrayBag and LinkedBag classes.
Question
Which of the following is NOT a goal of the barrier that separates an interface from implementation?

A) allows users to quickly glue resources together
B) allows alternative implementations of the same resource
C) changes to a resource's implementations do not disturb user code
D) increases the learning curve for resource's users
Question
Class diagrams show the relationships among classes at various levels of detail.
Question
Which is true about a bag collection?

A) it is an unordered collection
B) it is a standard collection type in Python
C) bags can contain only numeric objects
D) bags are of a fixed size
Question
What is found in the body of the method code that is used to document the method?

A) mutators
B) parameters
C) docstring
D) arguments
Question
While desirable, testing is not a critical part of software resource development.
Question
The code for the __iter__ method is shown below. What is the missing code?
Def __iter__(self):
< missing code >
While cursor < len(self):
Yield self.items[cursor]
Cursor += 1

A) cursor = 0
B) cursor = 1
C) cursor = self.size
D) cursor = len(self)
Question
What is it called when the contents of two bags are combined into a third bag?

A) iteration
B) appending
C) concatenation
D) reduction
Question
Why won't some of the ArrayBag methods require changes when implementing the LinkedBag class?

A) because the methods don't use the self variable
B) because the ArrayBag class is based on lists
C) because the LinkedBag object is not iterable
D) because the methods don't directly access the array variable
Question
Which of the following methods will require changes between the ArrayBag and the LinkedBag class?

A) isEmpty
B) remove
C) __eq__
D) __len__
Question
What testing tool can be used to help ensure that a resource meets its requirements?

A) unittest
B) pytest
C) pyunit
D) unitcheck
Question
The following code copies the data from a specified source collection in the LinkedBag __init__ method. What is the missing code? for item in sourceCollection:
< missing code >

A) add(item.self)
B) self.add(item)
C) self.item(add)
D) self.item = source.item
Question
What is the last step in the remove function in the ArrayBag class?

A) check the precondition
B) shift items to the right
C) resize the array
D) search for the target item index
Question
What method does Python run when it sees the in operator used with a collection?

A) __contains__
B) __iter__
C) __eq__
D) __add__
Question
In the ArrayBag class, what function does the __str__ method use to generate a sequence of strings from a bag?

A) count
B) len
C) add
D) map
Question
What is one of the pieces of data that must be initialized in the __init__ method in the LinkedBag class?

A) an array
B) a linked structure
C) a physical size
D) a string pointer
Question
How can the performance of the in and remove functions be described in the two bag implementations?

A) they take linear time
B) they take constant time
C) the performance is exponential according to the size of the bag
D) the performance is logarithmic according to the size of the bag
Question
Which of the following is a visual aid to help catalog resources in your software toolbox?

A) docstring
B) method mapper
C) unit modeler
D) class diagram
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Interfaces, Implementations, and Polymorphism
1
You can use a for loop on any collection.
True
2
Once the designer of a collection class has obtained its interface, the implementation of the class includes completing code for the methods.
True
3
When Python sees the in operator used with a collection, it runs the __contains__ method in the collection's class.
True
4
When used with a bag object, the in operator returns an integer value.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
Software bags can grow as more items are added, or they can shrink as items are removed.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
The logical size of an array-based bag will always be the same as the array's capacity.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
A precondition is a statement of what must be true for a method to perform its actions correctly.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
When the LinkedBag structure is not empty, self.items refers to the last node in the linked structure.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
Whenever you need to use the logical size of the bag within a class definition, run len(self) instead of referring directly to the instance variable self.size .
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
Each time you run Python's document function to obtain information about a module, data type, method, or function, you are accessing documentation about that resource's interface.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
The clear method empties a bag.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
The statement in the ArrayBag constructor to copy an item from a source collection to a new ArrayBag object is self.init(item) .
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
A bag is a type of ordered collection.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
When defining the Bag class, the __init__ operation is the constructor for the class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
An interface's documentation gives you enough information to know how to use or call a method and what to expect it to accomplish and return.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
Data that is needed to represent the state of a collection are assigned instance variables in the __help__ method of the class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
You should always try to hide the implementing data structures behind a wall of method calls on the object being implemented.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
A docstring is a string enclosed in double quotes that will be displayed when Python's help function is run on a resource.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
When Python sees a for loop on an iterable object, it runs that object's __add__ method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
The __iter__ method uses a yield statement to send each item to the calling for loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
Which of the following is one of the hallmarks of well-designed software?

A) a random grouping of interfaces to ensure secure code
B) an interface that is tightly coupled to the implementation
C) the clean separation of interfaces from implementations
D) a sophisticated interface with a high learning curve
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
In the Bag class defined in the chapter, what is one of the purposes of the sourceCollection argument in the __init__ method?

A) to create a bag with the contents of another collection
B) to delete the contents of an existing collection
C) to define source code for the method
D) to add data to an existing bag collection
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
In the ArrayBag class, why must the ArrayBag object track its logical size in a separate instance variable?

A) because the physical size is always smaller than the logical size
B) because the initial size of the object can't be changed
C) because the logical size will likely differ from the array's capacity
D) because the array size and the logical size are identical
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
The running times of the operations of ArrayBag and LinkedBag are similar.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
In the remove method for LinkedBag, if probe points to the node at the head of the linked structure, trailer will point to the tail.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
What word is best defined as the use of common names for operations on different types of collections?

A) anachronism
B) polymorphism
C) concatenation
D) modularization
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
The design and implementation of a collection class consists of two steps. What is the first step?

A) document the class using docstrings
B) choose an appropriate data structure
C) write the code for the defined methods
D) test the method using sample data
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
What is the name of the method that performs the constructor operation for a class?

A) __eq__
B) __iter__
C) __add__
D) __init__
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
The code for the add method for the ArrayBag class is shown below. What is the missing code?
Def add(self, item):
Self.items[len(self)] = item
< missing code >

A) self.items +=1
B) self = self + 1
C) self.size += 1
D) self.len = self.items
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
In the ArrayBag class, what must the __init__ method do if a source collection is specified?

A) the new instantiation of the bag must be copied to the source collection.
B) the source collection must be initialized with the clear method
C) the data in the source collection must be deleted
D) the data in the source collection must be copied
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
What does Python do when it sees a for loop on an iterable object?

A) executes a do loop
B) runs the __iter__ method
C) runs the clear method
D) executes a return statement
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
Which Python function allows you to obtain information about a module, data type, method, or function?

A) help
B) list
C) doc
D) info
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
The __iter__ method is identical in the ArrayBag and LinkedBag classes.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following is NOT a goal of the barrier that separates an interface from implementation?

A) allows users to quickly glue resources together
B) allows alternative implementations of the same resource
C) changes to a resource's implementations do not disturb user code
D) increases the learning curve for resource's users
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
Class diagrams show the relationships among classes at various levels of detail.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
Which is true about a bag collection?

A) it is an unordered collection
B) it is a standard collection type in Python
C) bags can contain only numeric objects
D) bags are of a fixed size
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
What is found in the body of the method code that is used to document the method?

A) mutators
B) parameters
C) docstring
D) arguments
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
While desirable, testing is not a critical part of software resource development.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
The code for the __iter__ method is shown below. What is the missing code?
Def __iter__(self):
< missing code >
While cursor < len(self):
Yield self.items[cursor]
Cursor += 1

A) cursor = 0
B) cursor = 1
C) cursor = self.size
D) cursor = len(self)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
What is it called when the contents of two bags are combined into a third bag?

A) iteration
B) appending
C) concatenation
D) reduction
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
Why won't some of the ArrayBag methods require changes when implementing the LinkedBag class?

A) because the methods don't use the self variable
B) because the ArrayBag class is based on lists
C) because the LinkedBag object is not iterable
D) because the methods don't directly access the array variable
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
Which of the following methods will require changes between the ArrayBag and the LinkedBag class?

A) isEmpty
B) remove
C) __eq__
D) __len__
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
What testing tool can be used to help ensure that a resource meets its requirements?

A) unittest
B) pytest
C) pyunit
D) unitcheck
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
The following code copies the data from a specified source collection in the LinkedBag __init__ method. What is the missing code? for item in sourceCollection:
< missing code >

A) add(item.self)
B) self.add(item)
C) self.item(add)
D) self.item = source.item
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
What is the last step in the remove function in the ArrayBag class?

A) check the precondition
B) shift items to the right
C) resize the array
D) search for the target item index
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
What method does Python run when it sees the in operator used with a collection?

A) __contains__
B) __iter__
C) __eq__
D) __add__
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
In the ArrayBag class, what function does the __str__ method use to generate a sequence of strings from a bag?

A) count
B) len
C) add
D) map
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
What is one of the pieces of data that must be initialized in the __init__ method in the LinkedBag class?

A) an array
B) a linked structure
C) a physical size
D) a string pointer
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
How can the performance of the in and remove functions be described in the two bag implementations?

A) they take linear time
B) they take constant time
C) the performance is exponential according to the size of the bag
D) the performance is logarithmic according to the size of the bag
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
Which of the following is a visual aid to help catalog resources in your software toolbox?

A) docstring
B) method mapper
C) unit modeler
D) class diagram
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 50 flashcards in this deck.