Overloading in C++ MCQ Exercise 45 May 2025 | 2 min read 1. Can we overload the assignment operator (=) in C++?
Explanation: The correct answer is option (a). In C++, the assignment operator may be overloaded to customize the assignment behavior for a class object. 2. Can a non-member function be used to overload operators in C++?
Explanation: The correct answer is option (a). Operators can be overloaded using the non-member functions, mainly when they require to access private members of a class using friend functions. 3. Which of the following operators cannot be overloaded in C++?
Explanation: The correct answer is option (c). The .* operator cannot be overloaded in C++. 4. Which of the following statements about destructors in C++ is true?
Explanation: The correct answer is option (b). Destructors cannot be overloaded, and every class can have only one destructor in C++. 5. Can constructors be overloaded in C++ programming?
Explanation: The correct answer is option (a). In C++, constructors can be overloaded by defining two or multiple constructors with different types of parameter lists. 6. What will be the output of the following code?
Explanation: The correct answer is option (a). The << operator is correctly overloaded as a friend function. It prints the value of the Integer object. Next TopicOverloading-in-cpp-mcq-exercise-5 |
1. What is the output of the below following C++ Code? #include<iostream> using namespace std; class A { int x; public: A(int val) : x(val) {} friend void modify(A &a); }; void modify(A &a) { a.x += 5; } int main()...
3 min read
1. What is the best way to declare a friend function to overload the binary + operator in C ++? class A { int value; public: A(int v) : value(v) {} friend A operator+(const A&, const A&); };...
2 min read
C++ Multiple Choice Questions MCQ Based on Basics of C++ 1) #include<userdefined.h> Which of the following is the correct syntax to add the header file in the C++ program? #include<userdefined> #include "userdefined.h" <include> "userdefined.h" Both A and B Show Answer Workspace Answer: D Explanation: To include the herder files in the C++ program user can use any...
22 min read
1. Which of the following examples of template specialization is correct? template <> class ClassName<int> { ... }; template <typename T> class ClassName { ... }; template <int> class ClassName { ... }; template <typename T> class ClassName<T> { ... }; Show AnswerWorkspace Explanation: The correct answer is option "a". A particular...
3 min read
1. What is the output of the following code? #include <iostream> template <class T> T add(T a, T b) { return a + b; } int main() { std::cout << add<int>(2, 3); return 0; } 5 Compilation error 0 23 Show AnswerWorkspace Explanation: The correct answer...
3 min read
1. What is a Variadic template? A template that does not take any arguments A template that can take a variable number of arguments A template that can take only one argument None of the above Show AnswerWorkspace Explanation: The correct answer is option "b". A function or class template in C++...
3 min read
1. What will be the output of the following code? #include<iostream> using namespace std; class Complex { public: int real, imag; Complex(int r, int i): real(r), imag(i) {} Complex operator+(const Complex &c) { ...
3 min read
1. What is the motive for overloading the subscript operator[] in a class? To access class members using index notation. To perform arithmetic operations. To concatenate strings. To overload assignments. Show AnswerWorkspace Explanation: The correct answer is option (a). The subscript operator [] is typically overloaded to permit entry to elements of...
2 min read
1. Which of the following statements, which would be the scope of friend function in C++? Class scope Global scope Protected scope Local Show AnswerWorkspace Explanation: The correct answer is option (b). The friend functions in the c++ will have the global scope since they are not the members of the class. 2....
2 min read
1. What is the primary purpose of C++ templates? Enhanced security Improved performance Code reuse Simplified syntax Show AnswerWorkspace Explanation: The correct answer is option "c". In C++, templates are primarily used to enable code reuse by enabling functions and classes to work with any type of data. With the use of...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India