Which statement best distinguishes a class from an object in object-oriented programming?
2 Classes and Objects Online Quiz Questions
Use this free practice quiz with 20 questions to review 2 Classes and Objects, test your knowledge, and prepare for your next test or exam.
What happens when Python evaluates checking = BankAccount("Maya", 500) and savings = BankAccount("Maya", 2000)?
- A
They create one BankAccount object with two variable names.
- B
They create two separate BankAccount instances.
- C
They create two classes that share the same balance.
- D
They create one class attribute for each variable.
In the expression checking.withdraw(100), what kind of class-defined element is withdraw?
- A
attribute
- B
constructor
- C
method
- D
reference
Select all statements that correctly describe attributes in a class-based program.
- A
An instance attribute can hold a value specific to one object.
- B
A class attribute is necessarily copied into a separate independent value for every instance.
- C
A class attribute can represent a value shared by instances.
- D
An instance attribute must have the same value in every object of the class.
After checking = BankAccount("Maya", 500) followed by checking.deposit(150), which two results are supported by the material? Select all that apply.
- A
The class definition is replaced.
- B
The checking object's state changes.
- C
The owner attribute is automatically changed.
- D
The balance attribute is updated.
In Python, __init__() is called automatically after a new instance has been created to establish its initial state.
- A
True
- B
False
The assignment alias = account creates an independent copy of the object referenced by account.
- A
True
- B
False
What is the technical term for the collection of an object's attribute values at a particular moment?
What term describes an object's individuality or whether two references designate the exact same object?
An object is an of a class, and creating that object is called .
In the example where currency = "USD" is declared in the class body, currency is a .
Consider this code:
first = BankAccount("Maya", 500)
second = BankAccount("Maya", 500)
third = first
Explain why first is second is False while first is third is True.
For the Rectangle class, what does small.area() return after small = Rectangle(3, 4) and then small.scale(2)?
- A
12
- B
24
- C
48
- D
96
In Python, an instance method's conventionally named self parameter represents the instance on which the method is called.
- A
True
- B
False
What programming term describes the collection of an object's attribute values at a particular moment?
Given the class definition Temperature with to_fahrenheit() returning self.celsius * 9 / 5 + 32, what value does Temperature(20).to_fahrenheit() return?
What does the expression BankAccount("Maya", 500) produce when the class has the initializer shown in the material?
- A
A new class definition named
BankAccount - B
A concrete
BankAccountobject with ownerMayaand balance 500 - C
A method named
BankAccountwithout any stored data - D
A shared class attribute containing the value 500
A BankAccount class uses currency = "USD" and assigns self.owner and self.balance in __init__(). Which design best matches the roles of these attributes?
- A
Store
currency,owner, andbalanceonly as class attributes - B
Store
currencyas a class attribute andownerandbalanceas instance attributes - C
Store
currencyas an instance attribute andownerandbalanceonly as class attributes - D
Store all three values outside the class because attributes cannot hold account data
Using the Rectangle class from the material, what does small.area() return after small = Rectangle(3, 4) followed by small.scale(2)?
- A
12
- B
24
- C
48
- D
96
Given first = BankAccount("Maya", 500), second = BankAccount("Maya", 500), and third = first, what are the results of first is second and first is third?
- A
Both expressions evaluate to
Truebecause the objects have the same owner and balance - B
Both expressions evaluate to
Falsebecause each variable has a different name - C
first is secondisFalse, whilefirst is thirdisTrue - D
first is secondisTrue, whilefirst is thirdisFalse