Skip to content
QuiznExam

Objects and Classes in C++ Quiz

Test your knowledge of C++ objects and classes, including class definitions, object creation, access specifiers, constructors, destructors, member functions, encapsulation, static members, and the fundamentals of object-oriented programming.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is a class in C++?

    • A. A function that executes automatically
    • B. A user-defined type that groups data members and member functions
    • C. A loop used to repeat instructions
    • D. A built-in data type for storing numbers
    Show answer

    B. A user-defined type that groups data members and member functions

    A class is a user-defined type that combines data and functions into a single unit.

  2. 2. Which keyword is used to declare a class in C++?

    • A. object
    • B. structclass
    • C. class
    • D. define
    Show answer

    C. class

    The class keyword introduces a class definition in C++.

  3. 3. Which statement correctly creates an object of a class named Car?

    • A. class Car;
    • B. Car object;
    • C. object Car();
    • D. new class Car;
    Show answer

    B. Car object;

    The declaration Car object; creates an object named object of the class type Car.

  4. 4. What is the default access specifier for members of a C++ class?

    • A. public
    • B. protected
    • C. private
    • D. internal
    Show answer

    C. private

    Members of a class are private by default, so they cannot normally be accessed directly from outside the class.

  5. 5. Which access specifier allows class members to be accessed from outside the class, subject to normal access rules?

    • A. private
    • B. public
    • C. protected
    • D. hidden
    Show answer

    B. public

    Public members can be accessed from outside the class through an object or other valid access path.

  6. 6. What is a constructor in C++?

    • A. A special member function used to initialize objects
    • B. A function that always returns an integer
    • C. A keyword used to destroy objects
    • D. A variable shared by all functions
    Show answer

    A. A special member function used to initialize objects

    A constructor is called when an object is initialized and can establish its initial state.

  7. 7. Which statement about a C++ constructor is correct?

    • A. It must have the return type void
    • B. Its name must match the class name
    • C. It must always be declared private
    • D. It can only be called after the object is destroyed
    Show answer

    B. Its name must match the class name

    A constructor has the same name as its class and does not declare a return type.

  8. 8. What is the purpose of a destructor in C++?

    • A. To create additional classes
    • B. To overload every operator
    • C. To perform cleanup when an object's lifetime ends
    • D. To initialize static variables only
    Show answer

    C. To perform cleanup when an object's lifetime ends

    A destructor can release resources owned by an object when its lifetime ends.

  9. 9. Which syntax represents the destructor of a class named Student?

    • A. Student()
    • B. delete Student()
    • C. ~Student()
    • D. destructor Student()
    Show answer

    C. ~Student()

    A destructor uses the class name preceded by a tilde, such as ~Student().

  10. 10. How can a member function be called using an object named obj?

    • A. obj->function()
    • B. obj.function()
    • C. obj::function()
    • D. function.obj()
    Show answer

    B. obj.function()

    The dot operator accesses a member through an object. The arrow operator is typically used with a pointer to an object.

  11. 11. What does encapsulation mean in object-oriented programming?

    • A. Combining data and related functions while controlling access to them
    • B. Executing a loop repeatedly
    • C. Converting all variables into constants
    • D. Creating objects without defining a class
    Show answer

    A. Combining data and related functions while controlling access to them

    Encapsulation groups related data and behavior and uses access controls to protect an object's internal state.

  12. 12. What is a static data member of a C++ class?

    • A. A separate variable created for every object
    • B. A variable that cannot be initialized
    • C. A member associated with the class rather than a separate copy for each object
    • D. A local variable inside every member function
    Show answer

    C. A member associated with the class rather than a separate copy for each object

    A static data member belongs to the class and is shared by its objects rather than having a separate instance for each object.

  13. 13. Which operator is commonly used to access a static member through the class name?

    • A. .
    • B. ->
    • C. ::
    • D. &
    Show answer

    C. ::

    The scope resolution operator :: can access a static member through the class name, as in Counter::count.

  14. 14. What happens when an object is passed to a function by value in C++?

    • A. The original object is always destroyed
    • B. A separate parameter object is initialized from the argument
    • C. The function receives only the object's class name
    • D. The object automatically becomes static
    Show answer

    B. A separate parameter object is initialized from the argument

    Passing an object by value initializes a separate parameter object, usually through a copy or move operation. The original object is not automatically destroyed.

  15. 15. Which statement about objects of the same C++ class is correct?

    • A. They must always have identical data member values
    • B. They cannot call the same member function
    • C. They share all non-static data members automatically
    • D. Each object normally has its own non-static data members
    Show answer

    D. Each object normally has its own non-static data members

    Each object has its own instance state through non-static data members, while static data members are shared at the class level.