Which statement correctly describes the relationship between a C++ class and an object?
08 Classes and Objects Online Quiz Questions
Use this free practice quiz with 20 questions to review 08 Classes and Objects, test your knowledge, and prepare for your next test or exam.
Which C++ access levels permit a class's friends to access members? Select all correct choices.
- A
public
- B
private
- C
protected
- D
None of these access levels
True or false: A const member function can generally be called on a const object, while a non-const member function cannot generally be called on one.
- A
True
- B
False
What C++ design principle combines an object's state and behavior while controlling access to that state?
Complete the statement: A initializes an object, has the same name as its class, and has no return type.
Given a class Rectangle with a public data member width, suppose Rectangle first; and Rectangle second; are declared, followed by first.width = 4.0;. What happens to second.width, and why?
What must follow the closing brace of a C++ class definition?
- A
A semicolon after the closing brace
- B
A colon before the class name
- C
The keyword end
- D
A comma after every member
Which two statements accurately describe the roles of members in a C++ class? Select all correct choices.
- A
Data members represent object state.
- B
Constructors are the only members that can access data.
- C
Member functions define object behavior.
- D
Private members are always stored outside the object.
A BankAccount starts with a balance of 100.0. Its withdraw function returns false when the requested amount is greater than the balance. How many of these calls return true: withdraw(40.0), withdraw(70.0)?
Complete the statement: In a constructor, the initializes data members before the constructor body runs.
What determines the initialization order of non-static data members in a C++ object?
- A
The order of constructor overloads
- B
The alphabetical order of member names
- C
The order in which members are declared in the class
- D
The order in which arguments are passed at the call site
True or false: In the example where BankAccount account(100.0); is declared inside process(), account is destroyed when process()'s block ends.
- A
True
- B
False
A C++ class makes its members private by default, whereas a C++ struct makes its members public by default.
- A
True
- B
False
Suppose Rectangle first; and Rectangle second; are separate objects, and second.width is 7.0. After first.width = 4.0;, what is the value of second.width?
- A
second.widthchanges to4.0because both objects have the same type. - B
second.widthremains7.0becausefirstandsecondstore separate data. - C
The program changes both objects because
widthis a class member. - D
second.widthbecomes uninitialized wheneverfirst.widthis assigned.
Given class Pair { int first; int second; public: Pair() : second(2), first(1) {} };, which data member is initialized first when a Pair object is constructed?
- A
firstis initialized beforesecond. - B
secondis initialized beforefirstbecause it appears first in the initializer list. - C
Both are initialized simultaneously.
- D
The order is selected alphabetically by the compiler.
The class Meters has explicit Meters(double value). Which statement is valid without an implicit conversion from double?
- A
Meters distance(5.0); - B
Meters distance = 5.0; - C
void use(Meters); use(5.0); - D
Meters distance{}followed by assigning5.0directly to the private member.
For const Temperature room_temperature;, which member can be called on room_temperature?
- A
A non-
constmember function that changes the object. - B
celsius() const, which only reads the stored value. - C
A constructor that changes the object during creation.
- D
A private data member accessed directly from outside the class.
A BankAccount is constructed with an initial balance of 100.0. What happens when withdraw(150.0) is called?
- A
It returns
trueand changes the balance to-50.0. - B
It returns
falseand changes the balance to0.0. - C
It returns
falseand leaves the balance at100.0. - D
It throws an exception and leaves the balance unspecified.
In the C++ expression Rectangle::area, which operator connects the class name Rectangle to the member function area? Enter the operator token only.
In the Student class from the example, a new student starts with grade = 0.0. After student.set_grade(120.0);, what grade will student.print() display?