C++ Constructors, Destructors, and Operator Overloading Quiz Quiz

Explore essential concepts of C++ constructors, destructors, and operator overloading with beginner-friendly questions covering syntax, purpose, and key rules. Strengthen your C++ fundamentals and learn how object behavior is controlled in classes.

  1. Default Constructor Identification

    Which of the following declares a default constructor in a C++ class named Example?

    1. Example();
    2. ~Example();
    3. void Example();
    4. Example(1);

    Explanation: The statement Example(); correctly declares a default constructor for the Example class. void Example(); would be interpreted as a regular member function, not a constructor. ~Example(); declares a destructor, not a constructor. Example(1); defines a parameterized constructor, not a default one.

  2. Destructor Syntax Recognition

    In C++, what is the correct way to define a destructor for the class Animal?

    1. delete Animal();
    2. Animal::~();
    3. Animal();
    4. ~Animal();

    Explanation: ~Animal(); is the proper destructor declaration for the Animal class. Animal(); is a constructor, not a destructor. delete Animal(); attempts to deallocate an object, not declare a destructor. Animal::~(); is not valid C++ syntax.

  3. Purpose of Copy Constructor

    What is the main purpose of a copy constructor in a C++ class?

    1. To destroy an object and free resources
    2. To initialize static members
    3. To assign new values after construction
    4. To create a new object as a copy of an existing object

    Explanation: A copy constructor initializes a new object using the contents of an existing object of the same class. It does not initialize static members, which belong to the class itself. Destructors are responsible for destroying objects, and assigning values after construction is handled by assignment operators, not copy constructors.

  4. Destructor Invocation Timing

    When is the destructor called for a local object in C++?

    1. During the object's construction
    2. When the function scope in which it was created ends
    3. When the program starts
    4. When the header file is included

    Explanation: The destructor for a local object is automatically called when the scope in which the object was created ends, such as at the end of a function. Including a header file does not trigger the destructor. The constructor, not the destructor, is called during object construction. The destructor is not called at program start.

  5. Overloading the Assignment Operator

    Which operator is typically overloaded to handle object assignment in C++? For example: obj1 = obj2;

    1. operator()
    2. operator+
    3. operator[]
    4. operator=

    Explanation: operator= specifically handles object assignment by defining how one object's values are assigned to another. operator+ is overloaded for addition, operator() for function call objects, and operator[] for array-like access. Only operator= is correct for assignment.

  6. Syntax for Operator Overloading

    How is operator overloading typically implemented inside a C++ class?

    1. By declaring a friend structure
    2. By defining a function named operator followed by the operator symbol, such as operator+
    3. By creating a macro for the operator
    4. By using a function named overload+

    Explanation: Operator overloading uses a special function with the name operator followed by the symbol being overloaded, like operator+. overload+ is not a recognized function name in C++. Macros and structures are unrelated to operator overloading.

  7. Use of Destructor in Resource Management

    If a C++ class allocates memory using new in its constructor, where should the corresponding delete statement be placed?

    1. In the destructor
    2. Only in the main function
    3. In the constructor
    4. Inside a private method called free()

    Explanation: The destructor should contain the delete statement to ensure memory allocated by new in the constructor is correctly released when the object is destroyed. Placing delete in the constructor would deallocate memory immediately after allocation. The main function or a separate private method may not automatically be called, risking memory leaks.

  8. Rule of Three in C++

    According to the Rule of Three in C++, which three member functions should usually be defined together in a class managing resources?

    1. Assignment operator, operator++, operator--
    2. Constructor, destructor, size operator
    3. Move constructor, default constructor, destructor
    4. Destructor, copy constructor, assignment operator

    Explanation: The Rule of Three suggests defining the destructor, copy constructor, and assignment operator together if your class manages resources to avoid resource-management bugs. The first distractor is the correct set. The other options mix in operators or constructors that are not directly related to resource management.

  9. Invoking Base Class Constructors

    In inheritance, how is the base class constructor called from the derived class constructor in C++?

    1. Calling the base constructor inside the derived constructor body
    2. Using the member initializer list syntax
    3. By writing 'super();' in the derived constructor
    4. The base constructor is never called automatically

    Explanation: The base class constructor is called using the member initializer list in the derived class's constructor. Explicitly calling the base constructor inside the derived constructor body is not allowed. The 'super();' syntax does not exist in C++. The base constructor is, in fact, called automatically if not managed otherwise.

  10. Operator Overloading Limitation

    Which of the following operators cannot be overloaded in C++?

    1. [] (subscript operator)
    2. = (assignment operator)
    3. + (addition operator)
    4. ?: (ternary conditional operator)

    Explanation: The ternary conditional operator '?:' cannot be overloaded in C++. The addition operator '+', the assignment operator '=', and the subscript operator '[]' can all be overloaded within C++ classes. As such, the distractors are all common operator overloads, but '?:' is a built-in operator that cannot be customized.