Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which of the following operator is overloaded?(a) +(b) Operator(c) : : (d) =

Answer»

+ operator is overloaded

2.

Which of the following operator is by default overloaded by the compiler?(a) *(b) + (c) + = (d) = = Based on the following program answer the questions (8) to (10) #includeusing namespace std;class Point {private: int x, y;public:Point(int x1,int y1){x=x1;y=y1;}void operator+(Point & pt3);void show() {cout << “x = ” << x << “y = ” << y; }};void Point: :operator + (Point & pt3){x+=pt3.x;y += pt3.y;}int main(){Point pt1(3, 2),pt2(5, 4);pt1+pt2;pt1.show();return 0;}

Answer»

Answer is (b) +

3.

Which of the following statement invoke operator overloading?(a) pt1 + pt2;(b) Point pt1(3, 2),pt2(5, 4);(c) ptl.show();(d) return 0;

Answer»

(d) return 0;

4.

class sale ( int cost, discount ;public: sale(sale &); Write a non inline definition for constructor specified;

Answer»

class sale 

{

int cost, discount;

public:

sale (sale&);

};

// non inline constructor

sale: : sale(sale&s)

{

cost = s.cost;

discount = s.discount;

}

5.

class add{int x; public: add(int)}; Write an outline definition for the constructor.

Answer»

add (int temp)

{

x = temp;

}

6.

………… cannot have default arguments.(a) Operator overloading(b) Overloaded operators(c) Function overloading(d) prototype

Answer»

(b) Overloaded operators

7.

Answer the questions based on the following program #include #include using namespace std; class comp {public:char[10];void getstring(char str[10]){strcpy(s,str);}void operator = = (comp);}; void comp::operator = = (comp ob){if(strcmp(s,ob.s) = = 0)cout << “\nStrings are Equal”;elsecout<< “\nStrings are not Equal”;}int main(){comp ob, ob1;char stringl[10], string2[10];cout <<“Enter First String:”; cin>> string1; ob.getstring(string1);cout<<“\n Enter Second String:”; cin>> string2; ob1.getstring(string2);ob = = obi;return 0;}(i) Mention the objects which will have the scope till the end of the program.(ii) Name the object which gets destroyed in between the program.(iii) Name the operator which is over loaded and write the statement that invokes it.(iv) Write out the prototype of the overloaded member function.(v) What types of operands are used for the overloaded operator?(vi) Which constructor will get executed? Write the output of the program

Answer»

(i) Objects: ob and obi of main ()

(ii) object ob of void operator = = (comp ob);

(iii) Overloaded operator: = = Statement that invokes: ob = = ob1; (v) void operator = = (comp ob):

(v) Operands used are the objects of the class comp

(vi) The default constructor generated by the compiler will be executed.[comp();]

Output 1

Enter first string: hello

Enter second string: hello String are equal

Output 2

Enter first string: hello

Enter second string: fine String are not equal

8.

Discuss the benefit of constructor overloading?

Answer»

Function overloading can be applied for constructors, as constructors are special functions of classes. A class can have more than one constructor with different signature. Constructor overloading provides flexibility of creating multiple type of objects for a class.

1. Memory is allocated for the objects.

2. Initialisation for the objects.

9.

Write the coding for the following output using constructor overloading.Output:Constructor without parameters.Parameterized constructor… Copy Constructor… Enter data… 20 30 Object a: The numbers are..20 30 The sum of the numbers are.. 50 Object b : The numbers are..10 20 The sum of the numbers are.. 30Object c.. The numbers are..10 20 The sum of the numbers are.. 30

Answer»

#include

using namespace std;

class add

{

int num1, num2, sum; 

public:

add()

{

cout << “\n Constructor without parameters…”; 

num1 = 0;

num2 = 0;

sum = 0;

}

add (int s1, int s2 )

{

cout << “\n Parameterized constructor…”;

num1= s1;

num2=s2;

sum=0;

}

add (add &a)

{

cout << “\n Copy Constructor…”; ‘

num1 = a.num1;

num2 = a.num2;

sum = 0;

}

void getdata()

{

cout<<“\n Enter data …”; cin>>num 1 >> num2;

}

void addition()

{

sum=num 1 + num2;

}

void putdata()

{

cout << “\n The numbers are..”;

cout <cout << “\n The sum of the numbers are..” << sum; }

};

int main()

{

add a, b (10, 20), c(b);

a. getdata();

a. addition();

b. addition();

c. addition();

cout << “\n Object a : “;

a. putdata();

cout << “\n Object b : “;

b. putdata();

cout << “\n Object c..”;

c. putdata();

return 0; 

}

10.

Give the output of the following program.#include&lt;bit s/std C++h&gt;using namesspace std;class Greeks{Public:void func(int x){cout&lt;&lt;"value of";}void func(double x){cout&lt;&lt;"value of";}void func(int x, int){cout&lt;&lt;"value of";}};int main(){Greeks obj1;obj1.func(7);obj1.func(9.132);obj1.func(85,64);return 0;}

Answer»

Output: 

value of x is 7 

value of x is 9.132 

value of x and y is 85,64

11.

Explain overload resolution.

Answer»

When you call an overloaded function, the compiler determines the most appropriate definition . to use, by comparing the argument types you have used to callthe function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

12.

The process of selecting the most appropriate overloaded function or operator is called …………(a) overload resolution(b) prototype(c) polymorphism(d) operator overload

Answer»

(a) overload resolution

13.

The mechanism of giving special meaning to an operator is known as ………(a) operator overloading(b) parameter (c) function overloading(d) polymorphism

Answer»

(a) operator overloading

14.

What is the use of overloading a function?

Answer»

Function overloading is not only implementing polymorphism but also reduces the number of comparisons in a program and makes the program to execute faster. It also helps the programmer by reducing the number of function names to be remembered.

15.

Does the return type of a function help in overloading a function?

Answer»

No. The return type of overloaded functions are not considered for overloading same data type.

16.

What is function overloading?

Answer»

The ability of the function to process the message or data in more than one form is called as function overloading. In other words function overloading means two or more functions in the same scope share the same name but their parameters are different.

17.

List the operators that cannot be overloaded.

Answer»

Operator that are not overloaded are follows:

1. scope operator ::

2. sizeof

3. member selector.

4. member pointer selector *

5. ternary operator ?:

18.

What are the rules for function overloading?

Answer»

Rules for function overloading: 

1. The overloaded function must differ in the number of its arguments or data types. 

2. The return type of overloaded functions are not considered for overloading same data type.

3. The default arguments of overloaded functions are not considered as part of the parameter list in function overloading.

19.

How does a compiler decide as to which function should be invoked when there are many functions? Give an example.

Answer»

When you call an overloaded function (when there are many functions with same name), the compiler determines the most appropriate definition to use by comparing the argument types used to call the function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.

Example:

#include using namespace std; void print (int i)

{

cout<< “It is integer” <<i<<endl;

}

void print (string c)

{

cout<< “It is string”<< c<<endl;

}

int main ()

{

print (10);

print (“Good”);

return 0;

}

Output:

It is integer 10

It is string Good

20.

What is operator overloading? Give some example of operators which can be overloaded.

Answer»

The term operator overloading, refers to giving additional functionality to the normal C++ operators like It is also a type of polymorphism in which an operator is overloaded to give user defined meaning to it.

For example ‘+’ operator can be overloaded to perform addition on various data types, like for Integer, String (concatenation) etc.

21.

What are the rules for operator overloading?

Answer»

Following are some rules to be followed while implementing operator overloading.

1. Precedence and Associativity of an operator cannot be changed.

2. No new operators can be created, only existing operators can be overloaded.

3. Cannot redefine the meaning of an operator’s procedure. You cannot change how integers are added. Only additional functions can be to an operator.

4. Overloaded operators cannot have default arguments.

5. When binary operators are overloaded, the left hand object must be an object of the relevant class.

22.

Answer the question (i) to (v) after going through the following class.classBook{intBookCode ; char Bookname[20];float fees; public:Book() //Function 1{fees = 1000;BookCode = 1;strcpy (Bookname, “C++”);}void display(float C) //Function 2{cout &lt;&lt; BookCode &lt;&lt; “:”&lt;&lt; Bookname &lt;&lt; “:”&lt;&lt; fees &lt;&lt; endl;~ Book() //Function 3{cout &lt;&lt; “End of Book Object”&lt;&lt; endl;}Book (intSC, char S[ ], float F); //Function 4};1. In the above program, what are Function 1 and Function 4 combined together referred as?2. Which concept is illustrated by Function3? When is this function called/ invoked?3. What is the use of Function3?4. Write the statements in main to invoke functionl and function25. Write the definition for Function4.

Answer»

1. Constructor overloading (function 1 and function 4 are constructors with different signatures in the class book)

2. Function 3 is destructor of the class. Function 3 is executed when the object of the class book goes out of scope.

3. Function 3 is destructor of the class.

  • Destructor (function3) will free resources if any that the object may have acquired during its lifetime 
  • Destructor function removes the memory of an object which was allocated by the constructor at the time of creating an object. Thus frees the unused memory.
  • 4. book b;
  • b.display (4.5);

5. Book: :Book (int sc.char.s[], float, F) //

Function 4

{

Book Code = SC

strcpy (Book name, S);

fees = F;

}

23.

The return type of overloaded functions are not considered for overloading same …………(a) polymorphism(b) prototype(c) data type(d) overloading

Answer»

(c) data type

24.

Which of the following function(s) combination cannot be considered as overloaded function(s) in the given snippet?void print(char A,int B); // F1void printprint(int A, float B); // F2void Print(int P=10); // F3void printQ; // F4(a) F1, F2, F3, F4(b) F1, F2, F3(c) F1, F2, F4(d) F1, F3, F4

Answer»

(d) F1, F3, F4