Skip to content
QuiznExam

Operator Overloading in C++ Quiz

Test your knowledge of operator overloading in C++, including overloaded operators, member and friend functions, unary and binary operators, operator restrictions, and practical examples of operator overloading.

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 operator overloading in C++?

    • A. Creating a new operator that does not exist in C++
    • B. Giving an existing operator a specific meaning for user-defined types
    • C. Changing the precedence of an operator
    • D. Removing an operator from the language
    Show answer

    B. Giving an existing operator a specific meaning for user-defined types

    Operator overloading allows existing C++ operators to work with user-defined types, such as classes and structures.

  2. 2. Which keyword is used to declare an overloaded operator function?

    • A. operator
    • B. overload
    • C. operator_overload
    • D. define
    Show answer

    A. operator

    The operator keyword introduces an operator function, such as operator+ or operator==.

  3. 3. Which operator can be overloaded in C++?

    • A. Scope resolution operator (::)
    • B. Member access operator (.)
    • C. Addition operator (+)
    • D. Conditional operator (?:)
    Show answer

    C. Addition operator (+)

    The addition operator can be overloaded to define how values of a user-defined type should be added.

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

    • A. ==
    • B. []
    • C. ()
    • D. ::
    Show answer

    D. ::

    The scope resolution operator (::) cannot be overloaded in C++.

  5. 5. Which operator is commonly overloaded to compare two objects for equality?

    • A. +
    • B. ==
    • C. %
    • D. &&=
    Show answer

    B. ==

    Overloading operator== allows a class to define how two objects should be compared for equality.

  6. 6. What is a common way to overload a binary operator as a non-static member function?

    • A. Declare it only as a global variable
    • B. Declare it as a constructor with two return types
    • C. Define an operator function that takes one explicit operand parameter
    • D. Define an operator function without a class or function declaration
    Show answer

    C. Define an operator function that takes one explicit operand parameter

    For a binary operator overloaded as a non-static member function, the left operand is the calling object and the right operand is passed as a parameter.

  7. 7. When a binary operator is overloaded as a non-member function, how many explicit operand parameters does it normally take?

    • A. Two
    • B. Zero
    • C. One
    • D. Three
    Show answer

    A. Two

    A non-member binary operator function normally takes two parameters, one for each operand.

  8. 8. Why might a friend function be used to overload an operator?

    • A. It automatically changes operator precedence
    • B. It makes every class member public
    • C. It allows a function to become a constructor
    • D. It can access a class's private and protected members while remaining a non-member function
    Show answer

    D. It can access a class's private and protected members while remaining a non-member function

    A friend function is not a member of the class, but the class can grant it access to private and protected members.

  9. 9. Which statement about operator overloading is correct?

    • A. It can change the precedence of an operator
    • B. It cannot change an operator's precedence or associativity
    • C. It allows new operator symbols to be invented
    • D. It removes the original meaning of an operator for built-in types
    Show answer

    B. It cannot change an operator's precedence or associativity

    Operator overloading changes the behavior for user-defined types but does not change an operator's precedence, associativity, or number of operands.

  10. 10. Which operator is commonly overloaded to define prefix increment behavior for a class?

    • A. operator+()
    • B. operator--()
    • C. operator++()
    • D. operator*()
    Show answer

    C. operator++()

    The operator++ function overloads increment. Prefix and postfix forms use different parameter conventions when implemented as member functions.

  11. 11. How is postfix increment commonly distinguished from prefix increment in a member operator overload?

    • A. The postfix version takes an unused int parameter
    • B. The postfix version must return void
    • C. The prefix version takes two int parameters
    • D. The postfix version uses the keyword postfix
    Show answer

    A. The postfix version takes an unused int parameter

    A conventional postfix member overload uses a dummy int parameter to distinguish it from the prefix form, which takes no explicit parameter.

  12. 12. Which operator is commonly overloaded to provide indexed access to elements of a class object?

    • A. operator()
    • B. operator->
    • C. operator.
    • D. operator[]
    Show answer

    D. operator[]

    Overloading operator[] allows objects such as custom containers to support indexed access using square brackets.

  13. 13. Which operator is commonly overloaded to make an object callable like a function?

    • A. operator[]
    • B. operator()
    • C. operator::
    • D. operator.
    Show answer

    B. operator()

    Overloading operator() allows an object to be invoked using function-call syntax, which is useful for function objects.

  14. 14. Consider a class named Complex with an overloaded addition operator. What does the expression c1 + c2 represent when both operands are Complex objects?

    • A. It always adds the objects' memory addresses
    • B. It automatically merges the two class definitions
    • C. It invokes the applicable overloaded addition operator
    • D. It destroys both objects after addition
    Show answer

    C. It invokes the applicable overloaded addition operator

    The expression uses the overloaded operator+ function selected by the compiler to define addition for Complex objects.

  15. 15. Which statement about overloading the assignment operator (=) in C++ is correct?

    • A. A class can define a copy assignment operator to customize assignment between objects
    • B. The assignment operator can only be overloaded as a non-member function
    • C. The assignment operator must always return void
    • D. Every class must explicitly define an assignment operator to compile
    Show answer

    A. A class can define a copy assignment operator to customize assignment between objects

    A class can provide a copy assignment operator to control how one object is assigned to another. If suitable special member functions are not user-declared, the compiler may implicitly declare and define one.