Operator Overloading MCQs

Operator Overloading MCQs

Our experts have gathered these Operator Overloading MCQs through research, and we hope that you will be able to see how much knowledge base you have for the subject of Operator Overloading by answering these 20 multiple-choice questions.
Get started now by scrolling down!

1:

Output of following program?

#include <iostream>

using namespace std;

class Test2

{

int y;

};


class Test

{

int x;

Test2 t2;

publi

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(Test 2) called


B.  

 fun(int) called


C.  

 Compiler Error: Ambiguous call to fun()


2: The fields in a class of a C++ program are by default

A.   None of these

B.   Protected

C.   Private

D.   Public

3: Polymorphism results in more memory because of:

A.   Function overloading,

B.   Virtual tables

C.   Operator overloading

4: Operator overloading works only on pass by value

A.   True

B.   False

5: An object of abstract class cannot be created

A.   True

B.   False

6: Which of the following operators cannot be overloaded

A.   .* (Pointer-to-member Operator )

B.   :: (Scope Resolution Operator)

C.   . (Member Access or Dot operator)

D.   ?: (Ternary or Conditional Operator )

E.   All of these

7: Derived class must implement pure virtual functions

A.   False

B.   True

8: Operator overloading is very much like function overloading

A.   False

B.   True

9: Which of the following operators should be preferred to overload as a global function rather than a member method?

A.   Comparison Operator

B.   Postfix ++

C.   Prefix++

D.   Insertion Operator <<

10: Exact virtual function is decided at run-time

A.   True

B.   False

11: Overload function in C++

A.   A group function with the same name

B.   All

C.   All have the same number and type of arguments

D.   Functions with same name and same number and type of arguments

12: Operator overloading may lead to copies

A.   True

B.   False

13: A constructor is called whenever

A.   A class is declared

B.   A object is declared

C.   A class is used

D.   An object is used

14: How can we restrict dynamic allocation of objects of a class using new?

A.   By making an empty private new operator.

B.   By making an empty private new and new[] operators

C.   By overloading new operator and new[] operators

D.   By overloading new operator

15: How does C++ compiler differs between overloaded postfix and prefix operators?

A.   C++ doesn't allow both operators to be overlaoded in a class

B.   A postfix ++ has a dummy parameter

C.   A prefix ++ has a dummy parameter

D.   By making prefix ++ as a global function and postfix as a member function.

16:

Which of the following operators are overloaded by default by the compiler?

1) Comparison Operator ( == )

2) Assignment Operator ( = )


A.  

 Only 1


B.  

 None of the two


C.  

 Both 1 and 2


D.  

 Only 2


17: Which of the following operator functions cannot be global, i.e., must be a member function.

A.   All of these

B.   New

C.   Conversion Operator

D.   Delete

18:

Predict the output?

#include<stdlib.h>

#include<stdio.h>

#include<iostream>


using namespace std;


class Test {

int x;

publi

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;

}

A.  

Constructor called

new called

delete called

Destructor called


B.  

Constructor called

new called

Destructor called

delete called


C.  

 new called

Constructor called

Destructor called

delete called


D.  

new called

Constructor called

delete called

Destructor called


19:

#include<iostream>

using namespace std;


class Point {

private:

int x, y;

publi

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.  

Compiler Error

B.  

x = 2, y = 3

C.  

x = 3, y = 2

20: Only a field name or an expression may be used for the ____ property in a calculated control.

A.   Control Source

B.   Control Wizard

C.   Row Source

D.   Control Margin