Exercise: Questions on Operator Overloading


Questions on Operator Overloading : Question 1 :
How can we restrict dynamic allocation of objects of a class using new?

By overloading new operator
By making an empty private new operator.
By making an empty private new and new[] operators
By overloading new operator and new[] operators
Show Answer
Questions on Operator Overloading : Question 2 :
Which of the following operators cannot be overloaded

. (Member Access or Dot operator)
?: (Ternary or Conditional Operator )
:: (Scope Resolution Operator)
.* (Pointer-to-member Operator )
All of the above
Show Answer
Questions on Operator Overloading : Question 3 :
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 ( = ) 

Both 1 and 2
Only 1
Only 2
None of the two
Show Answer
Questions on Operator Overloading : Question 4 :
Which of the following operators should be preferred to overload as a global function rather than a member method?

Postfix ++
Comparison Operator
Insertion Operator <<
Prefix++
Show Answer
Questions on Operator Overloading : Question 5 :
How does C++ compiler differs between overloaded postfix and prefix operators?

C++ doesn't allow both operators to be overlaoded in a class
A postfix ++ has a dummy parameter
A prefix ++ has a dummy parameter
By making prefix ++ as a global function and postfix as a member function.
Show Answer
Questions on Operator Overloading : Question 6 :
Predict the output
#include<iostream>
using namespace std;
class A
{
    int i;
public:
    A(int ii = 0) : i(ii) {}
    void show() {  cout << i << endl;  }
};

class B
{
    int x;
public:
    B(int xx) : x(xx) {}
    operator A() const {  return A(x); }
};

void g(A a)
{
    a.show();
}

int main()
{
    B b(10);
    g(b);
    g(20);
    return 0;
}

Compiler Error
10
20
20
20
10
10
Show Answer
Questions on Operator Overloading : Question 7 :
Output of following program?
#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;
}

fun(int) called
fun(Test 2) called
Compiler Error: Ambiguous call to fun()
Show Answer
Questions on Operator Overloading : Question 8 :
Predict the output?
#include<stdlib.h>
#include<stdio.h>
#include<iostream>

using namespace std;

class Test {
    int x;
public:
    void* operator new(size_t size);
    void operator delete(void*);
    Test(int i) {
        x = i;
        cout << "Constructor called \n";
    }
    ~Test() { cout << "Destructor called \n"; }
};


void* Test::operator new(size_t size)
{
    void *storage = malloc(size);
    cout << "new called \n";
    return storage;
}

void Test::operator delete(void *p )
{
    cout<<"delete called \n";
    free(p);
}

int main()
{
    Test *m = new Test(5);
    delete m;
    return 0;
}

new called
Constructor called
delete called
Destructor called
new called
Constructor called
Destructor called
delete called
Constructor called
new called
Destructor called
delete called
Constructor called
new called
delete called
Destructor called
Show Answer
Questions on Operator Overloading : Question 9 :
#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;
}


x = 3, y = 2
Compiler Error
x = 2, y = 3
Show Answer
Questions on Operator Overloading : Question 10 :
Which of the following operator functions cannot be global, i.e., must be a member function.

new
delete
Converstion Operator
All of the above
Show Answer