Home Subjects C++ Programming

C++ Programming

Object oriented C++ for GATE and placement

23 Q 1 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–23 of 23
Topics in C++ Programming
All OOP Concepts 100
Q.21 Hard OOP Concepts
What is the output of operator overloading in the following context?
class Complex {
public:
int real, imag;
Complex operator+(const Complex &c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
};
Complex c1, c2, c3;
c1 = {1, 2}; c2 = {3, 4};
c3 = c1 + c2;
A c3 has real=4, imag=6
B Compilation error
C Runtime error
D c3 has real=0, imag=0
Correct Answer:  A. c3 has real=4, imag=6
EXPLANATION

The + operator is overloaded to add two Complex numbers. c1 + c2 calls the overloaded operator+, resulting in real=1+3=4 and imag=2+4=6.

Test
Q.22 Hard OOP Concepts
Consider the following code:
class Base {
protected:
int x = 10;
};
class Derived : private Base {
public:
void display() { cout
A Prints 10
B Compilation error - x is not accessible
C Runtime error
D Prints garbage value
Correct Answer:  A. Prints 10
EXPLANATION

Even with private inheritance, protected members of Base are accessible within Derived class methods. The display() function can access x and will print 10.

Test
Q.23 Hard OOP Concepts
What is the output of the following code?
class A {
public:
A() { cout
A A B ~B ~A
B B A ~A ~B
C A B ~A ~B
D B ~B
Correct Answer:  A. A B ~B ~A
EXPLANATION

Constructors are called from base to derived (A then B), while destructors are called from derived to base (~B then ~A).

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips