C++ OOPS Interview Questions

    Question 1C++ Class Declaration

    Which is the correct way to declare a class in C++?

    Question 2C++ Guess the Output – Constructor

    What will this program print?

    #include <iostream> using namespace std; class Test { public: Test() { cout << "Constructor called"; } }; int main() { Test obj; return 0; }

    Question 3C++ Access Specifiers

    Which access specifier allows members to be accessed only inside the class?

    Question 4C++ Guess the Output – Encapsulation

    What will be the output of this program?

    #include <iostream> using namespace std; class Person { private: int age; public: void setAge(int a) { age = a; } int getAge() { return age; } }; int main() { Person p; p.setAge(21); cout << p.getAge(); return 0; }

    Question 5C++ Inheritance

    Which symbol is used to denote inheritance in C++?

    Question 6C++ Guess the Output – Function Overloading

    What will this program print?

    #include <iostream> using namespace std; class Math { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }; int main() { Math m; cout << m.add(2, 3) << " " << m.add(2.5, 3.5); return 0; }

    Question 7C++ Polymorphism – Virtual Function

    Why do we use virtual functions in C++?

    Question 8C++ Guess the Output – Virtual Function

    What will be the output of this program?

    #include <iostream> using namespace std; class Base { public: virtual void show() { cout << "Base"; } }; class Derived : public Base { public: void show() { cout << "Derived"; } }; int main() { Base *b; Derived d; b = &d; b->show(); return 0; }

    Question 9C++ this Pointer

    What does the this pointer in C++ represent?

    Question 10C++ Guess the Output – Destructor

    What will the program print?

    #include <iostream> using namespace std; class Sample { public: Sample() { cout << "Constructor "; } ~Sample() { cout << "Destructor"; } }; int main() { Sample s; return 0; }