MCQs > IT & Programming > Programming Languages MCQs > Operator Overloading MCQs

Operator Overloading MCQ

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

Answer

Correct Answer: Control Source

Note: This Question is unanswered, help us to find answer for this one

#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;

}


Answer

Correct Answer:

x = 3, y = 2


Note: This Question is unanswered, help us to find answer for this one

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;

}

Answer

Correct Answer:

 new called

Constructor called

Destructor called

delete called



Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Conversion Operator

Note: This Question is unanswered, help us to find answer for this one

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

1) Comparison Operator ( == )

2) Assignment Operator ( = )


Answer

Correct Answer:

 Only 2



Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A postfix ++ has a dummy parameter

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: By making an empty private new and new[] operators

Note: This Question is unanswered, help us to find answer for this one

A constructor is called whenever

Answer

Correct Answer: A object is declared

Note: This Question is unanswered, help us to find answer for this one

Operator overloading may lead to copies

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

Overload function in C++

Answer

Correct Answer: A group function with the same name

Note: This Question is unanswered, help us to find answer for this one

Exact virtual function is decided at run-time

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Insertion Operator <<

Note: This Question is unanswered, help us to find answer for this one

Operator overloading is very much like function overloading

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

Derived class must implement pure virtual functions

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

Which of the following operators cannot be overloaded

Answer

Correct Answer: All of these

Note: This Question is unanswered, help us to find answer for this one

An object of abstract class cannot be created

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

Operator overloading works only on pass by value

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

Polymorphism results in more memory because of:

Answer

Correct Answer: Virtual tables

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Private

Note: This Question is unanswered, help us to find answer for this one

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;

}

Answer

Correct Answer:

 Compiler Error: Ambiguous call to fun()



Note: This Question is unanswered, help us to find answer for this one