Quiz about Operator Overloading

Operator Overloading

Last Updated :
Discuss
Comments

Question 1

How can we restrict dynamic allocation of objects of a class using new?
  • A
    By overloading new operator
  • B
    By making an empty private new operator.
  • C
    By making an empty private new and new[] operators
  • D
    By overloading new operator and new[] operators

Question 2

Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written?
1) Comparison Operator ( == )
2) Assignment Operator ( = ) 
  • A
    Both 1 and 2
  • B
    Only 1
  • C
    Only 2
  • D
    None of the two

Question 3

Which of the following operators should be preferred to overload as a global function rather than a member method?
  • A
    Postfix ++
  • B
    Comparison Operator
  • C
    Insertion Operator <<
  • D
    Prefix++

Question 4

Which of the following is an example of compile-time polymorphism?

  • A

    Function Overloading

  • B

    Virtual function

  • C

    Function Overriding

  • D

    Dynamic Casting

Question 5

CPP
#include<iostream>
using namespace std;

class Point {
private:
  int x, y;
public:
  Point() : x(0), y(0) { }
  Point& operator()(int dx, int dy);
  void show() {cout << "x = " << x << ", y = " << y; }
};

Point& Point::operator()(int dx, int dy)
{
    x = dx;
    y = dy;
    return *this;
}

int main()
{
  Point pt;
  pt(3, 2);
  pt.show();
  return 0;
}
  • A
    x = 3, y = 2
  • B
    Compiler Error
  • C
    x = 2, y = 3

Question 6

Output of following program? C
#include <iostream>
using namespace std;
class Test2
{
    int y;
};

class Test
{
    int x;
    Test2 t2;
public:
    operator Test2 ()  { return t2; }
    operator int () { return x; }
};

void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }

int main()
{
    Test t;
    fun(t);
    return 0;
}
  • A
    fun(int) called
  • B
    fun(Test 2) called
  • C
    Compiler Error: Ambiguous call to fun()

Question 7

Which of the following operators cannot be overloaded?

  • A
    . (Member Access or Dot operator)
  • B
    ?: (Ternary or Conditional Operator )
  • C
    :: (Scope Resolution Operator)
  • D
    .* (Pointer-to-member Operator )
  • E
    All of the above

Question 8

Which of the following statements are true regarding C++?

(a) Overloading gives the capability to an existing operator to operate on other data types.

(b) Inheritance in object oriented programming provides support to reusability.

(c) When object of a derived class is defined, first the constructor of derived class is executed then constructor of a base class is executed.

(d) Overloading is a type of polymorphism.

Choose the correct option from those given below:

  • A

    (a) and (b) only     

  • B

    (a), (b) and (c) only

  • C

    (a), (b) and (d) only     

  • D

    (b), (c) and (d) only

Question 9

What is Operator overloading?

Tags:

There are 9 questions to complete.

Take a part in the ongoing discussion