Exam 3 Flashcards
What will the following code display?
dct = { ‘Monday’ : 1 , ‘Tuesday’ : 2, ‘Wednesday’ : 3}
print(dct[‘Tuesday’])
ANSWER would be 2 because it’s asking for the dct
What will the following code display?
dct = { ·Monday’ : 1 , ‘Tuesday’ : 2, ‘Wednesday’ : 3}
print(dct.get(‘Friday’, ‘not found’))
ANS: not found
dct = {1: [0, 1]. 2: [2, 3]. 3: [4, 51]}
print ( dct [ 3] )
ANS: [4, 51]
After the following statement executes, what elements will be stored in the myset set?
myset = set ( ‘Saturn’)
ANS: {‘S’, ‘a’, ‘t’, ‘u’, ‘r’, ‘n’}
After the following statement executes, what elements will be stored in the myset set?
myset = set( [2, 4, 4, 6, 6, 6, 6])
ANS: {2, 4, 6}
After the following statement executes, what elements will be stored in the myset set?
myset = set ( [ ‘a ‘ , ‘bb’ , ‘ccc ‘ , ‘dddd ‘ ] )
ANS: { ‘a ‘ , ‘bb’ , ‘ccc ‘ , ‘dddd ‘}
The difference of set1 and aet2 is a set that contains only the elements that appear in set1 but do not appear in set2. T/F
True
New attributes and methods may be added to a subclass
True
When a class inherits another class, it is required to use all the data attributes and methods of the superclass
False
All instances of a class share the same values of the data attributes in the class.
False
A class can be thought of as a blueprint that can be used to create an object
True
Which method is automatically executed when an instance of a class is created in memory?
ANS: __init__
Write the first line of code while creating a class named Worker?
ANS: class Worker:
What type of method provides a safe way to return a value from a class’s attribute without changing it
ANS: Accessor
In the following line of code, what is the name of the base class?
class Python(Course):
ANS: Course
Of the two classes, Cherry and Flavor, which would most likely be the subclass?
ANS: Cherry
What are the valid indexes for the string ‘New York’?
ANS: 0 through 7
What will be displayed after the following code executes?
mystr = ‘yes’
yourstr = ‘no’
mystr += yourstr * 2
print(mystr)
ANS: ‘yesnono’
What will be assigned to the variable some_nums after the following code executes?
special = ‘0123456789’, # ‘abcdfegh’
some_nums = special[0:10:2]
ANS: ‘02468’
The following expression is valid:
string[6] = ‘8’
ANS: Invalid
What is the return value of the string method lstrip()?
ANS: without leading spaces, \n, extra char,
Indexing works with both strings and lists
True
A subclass may not override any method other than the __init__ method.
false
A superclass inherits attributes and methods from its subclasses without any of them having to be rewritten.
false
An “is a” relashonship exists between a grasshopper and a bumblebee
false
base classes are also called
superclass
Each subclass has a method named __init__ that overrides the superclass’s __init__ method.
true
class clock: def \_\_init\_\_(self, shape, color, price): self._shape = shape self.color = color self.price = price
3 mutator, 3 accessor
In a UML diagram depicting inheritance, you only need to write the name of the subclass.
false
In an inheritance relationship, what is a specialized class called?
a subclass
in the following line of code, what is the name of the base class?
class Python(Course):
course
In the following line of code, what is the name of the subclass?
class Rose(Flower):
rose
Of the two classes, Cherry and Flavor, which would most likely be the subclass?
cherry
One problem with using a UML diagram is that there is no way to indicate inheritance.
false lol the whole point of the diagram is to show inheritance
Polymorphism works on any two class methods that have the same name.
true
what does a subclass inherit from a superclass?
atrributes and methods
what gives a program the ability to call the correct method depending on the type of object that is used to call it?
Pollymorphism
When a class inherits another class, it is required to use all the data attributes and methods of the superclass.
false
When there are several classes that have many common data attributes, it is better to write a(n) ________ to hold all the general data.
superclass
Which of the following is the correct syntax for defining a class, table, which inherits from the furniture class?
class table(furniture):
________ allows a new class to inherit members of the class it extends.
inheritance
________ has the ability to define a method in a subclass and then define a method with the same name in a superclass.
polymorphism
what does it mean to say there is an “is a” relationship between two objects?
When one object is a specialized version of another object, there is an “is a” relationship between them. The specialized object “ is a” version of the general object.
In an inheritance relationship, the _____ is the general class.
a. subclass
b. superclass
c. slave class
d. child class
superclass