Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

Which statement correctly describes the relationship between a C++ class and an object?

  1. A

    A class is an instance of an object.

  2. B

    An object is an instance of a class.

  3. C

    A class can contain data but never functions.

  4. D

    An object exists only at namespace scope.

02
Choose all
1 point

Which C++ access levels permit a class's friends to access members? Select all correct choices.

  1. A

    public

  2. B

    private

  3. C

    protected

  4. D

    None of these access levels

03
True or false
1 point

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.

  1. A

    True

  2. B

    False

04
Written response
1 point

What C++ design principle combines an object's state and behavior while controlling access to that state?

05
Fill in the blank
1 point

Complete the statement: A initializes an object, has the same name as its class, and has no return type.

06
Open ended
1 point

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?

07
Choose one
1 point

What must follow the closing brace of a C++ class definition?

  1. A

    A semicolon after the closing brace

  2. B

    A colon before the class name

  3. C

    The keyword end

  4. D

    A comma after every member

08
Choose all
1 point

Which two statements accurately describe the roles of members in a C++ class? Select all correct choices.

  1. A

    Data members represent object state.

  2. B

    Constructors are the only members that can access data.

  3. C

    Member functions define object behavior.

  4. D

    Private members are always stored outside the object.

09
Written response
1 point

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)?

10
Fill in the blank
1 point

Complete the statement: In a constructor, the initializes data members before the constructor body runs.

11
Choose one
1 point

What determines the initialization order of non-static data members in a C++ object?

  1. A

    The order of constructor overloads

  2. B

    The alphabetical order of member names

  3. C

    The order in which members are declared in the class

  4. D

    The order in which arguments are passed at the call site

12
True or false
1 point

True or false: In the example where BankAccount account(100.0); is declared inside process(), account is destroyed when process()'s block ends.

  1. A

    True

  2. B

    False

13
True or false
1 point

A C++ class makes its members private by default, whereas a C++ struct makes its members public by default.

  1. A

    True

  2. B

    False

14
Choose one
1 point

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?

  1. A

    second.width changes to 4.0 because both objects have the same type.

  2. B

    second.width remains 7.0 because first and second store separate data.

  3. C

    The program changes both objects because width is a class member.

  4. D

    second.width becomes uninitialized whenever first.width is assigned.

15
Choose one
1 point

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?

  1. A

    first is initialized before second.

  2. B

    second is initialized before first because it appears first in the initializer list.

  3. C

    Both are initialized simultaneously.

  4. D

    The order is selected alphabetically by the compiler.

16
Choose one
1 point

The class Meters has explicit Meters(double value). Which statement is valid without an implicit conversion from double?

  1. A

    Meters distance(5.0);

  2. B

    Meters distance = 5.0;

  3. C

    void use(Meters); use(5.0);

  4. D

    Meters distance{} followed by assigning 5.0 directly to the private member.

17
Choose one
1 point

For const Temperature room_temperature;, which member can be called on room_temperature?

  1. A

    A non-const member function that changes the object.

  2. B

    celsius() const, which only reads the stored value.

  3. C

    A constructor that changes the object during creation.

  4. D

    A private data member accessed directly from outside the class.

18
Choose one
1 point

A BankAccount is constructed with an initial balance of 100.0. What happens when withdraw(150.0) is called?

  1. A

    It returns true and changes the balance to -50.0.

  2. B

    It returns false and changes the balance to 0.0.

  3. C

    It returns false and leaves the balance at 100.0.

  4. D

    It throws an exception and leaves the balance unspecified.

19
Written response
1 point

In the C++ expression Rectangle::area, which operator connects the class name Rectangle to the member function area? Enter the operator token only.

20
Written response
1 point

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?