Which OOP feature allows the same function name to behave differently for different argument types?
Solution
Function overloading is a form of (compile-time) polymorphism.
20 technical questions of moderate difficulty, as asked in campus placement and company tests. Each has a short explanation.
Which OOP feature allows the same function name to behave differently for different argument types?
Function overloading is a form of (compile-time) polymorphism.
What is the output of this C++ program?
#include <iostream>
using namespace std;
class Base { public: Base() { cout << "Base "; } };
class Derived : public Base { public: Derived() { cout << "Derived "; } };
int main() { Derived d; return 0; }Constructors run from base to derived.
What is the output of this C++ program?
#include <iostream>
using namespace std;
class Base { public: ~Base() { cout << "~Base "; } };
class Derived : public Base { public: ~Derived() { cout << "~Derived "; } };
int main() { { Derived d; } return 0; }Destructors run in reverse order: derived first, then base.
What is the output of this C++ program?
#include <iostream>
using namespace std;
class Base { public: virtual void show() { cout << "Base"; } };
class Derived : public Base { public: void show() override { cout << "Derived"; } };
int main() { Base *b = new Derived(); b->show(); return 0; }show() is virtual, so the call through a base pointer uses the derived version (runtime polymorphism).
What is the output of this C++ program?
#include <iostream>
using namespace std;
class Base { public: void show() { cout << "Base"; } };
class Derived : public Base { public: void show() { cout << "Derived"; } };
int main() { Base *b = new Derived(); b->show(); return 0; }show() is not virtual, so the base-pointer call is resolved at compile time to Base::show().
A class that contains at least one pure virtual function is called:
A pure virtual function (= 0) makes the class abstract; it cannot be instantiated.
Which access specifier makes members accessible in the class and its derived classes, but not outside?
protected members are visible to derived classes but not to outside code.
What is the default access specifier for members of a class in C++?
Class members are private by default; struct members are public by default.
Which keyword lets a function access the private members of a class without being a member?
A friend function is granted access to private and protected members.
What is the output of this C++ program?
#include <iostream>
using namespace std;
int add(int a) { return a; }
int add(int a, int b) { return a + b; }
int main() { cout << add(5, 2); return 0; }The two-argument add() is chosen by overload resolution: 5 + 2 = 7.
What is the output of this C++ program?
#include <iostream>
using namespace std;
int main() { int x = 10; int &r = x; r += 5; cout << x; return 0; }A reference is another name for x, so changing r changes x.
Wrapping data and the functions that work on it into a single unit is called:
Encapsulation bundles data with methods and hides internal details.
Which type of inheritance can cause the 'diamond problem'?
When a class inherits from two classes with a common base, it gets two copies of the base; virtual inheritance solves this.
Which operator cannot be overloaded in C++?
The scope resolution operator (::), along with ., .* and ?:, cannot be overloaded.
What is a copy constructor used for?
A copy constructor initialises a new object from an existing one.
What is the output of this C++ program?
#include <iostream>
using namespace std;
class A { public: static int count; A() { count++; } };
int A::count = 0;
int main() { A a, b; cout << A::count; return 0; }A static member is shared by all objects; it is incremented once per object created.
Which of these is used for dynamic memory allocation in C++?
new allocates memory and calls the constructor; delete releases it.
What does the 'this' pointer point to?
this holds the address of the current object.
In C++, which container provides fast lookup by key with keys kept in sorted order?
std::map is usually a balanced binary search tree with sorted keys and O(log n) lookup.
What is the output of this C++ program?
#include <iostream>
#include <vector>
using namespace std;
int main() { vector<int> v = {4, 5, 6}; cout << v.size(); return 0; }size() returns the number of elements: 3.
Want guided preparation? Placement Papers Prep courses by Dr. Kiran Derle, with video lessons and full-length mocks.
See placement courses ↗