Complete C++ object oriented interview questions you could imagine

Complete C++ object oriented interview questions you could imagine
Object Orientation:
Implementation of Polymorphism in C++?

// Dynamic Allocation of objects and Polymorphism


using namespace std;

class Person {
int i;
virtual void foo() = 0;

class Student : public Person {
void foo() {
cout << "I am a Student\n" ; } }; class Professor : public Person { void foo() { cout << "I am a Professor\n" ; } }; int main(){ Person *person1 = new Student; Person *person2 = new Professor; person1->foo();

delete person1;
delete person2;
return 0;

Polymorphism includes both overloading and overriding.

Pure/ Virtual function (Run Time)
– Function Overloading (Compile Time)
– Operator Overloading (Compile Time)

Static polymorphism (Using templates) was left out.

You have a class whose internal representation is not exposed to the user and can change transparently.

The above scenario is an example of which one of the following?
Choice 1

Choice 2

Choice 3

Choice 4

Choice 5

why not abstraction

What is the difference between class members and instance members?
The members inside class either method or variables which are declared without static keyword known as instance member,and those are declared with static keyword known as class members.

Class members can be accessed without actually creating an object of their class.Identifieed with static keyword.

Instance members need to be accessed via an object.

. Explain OOP,classes,inheritance and polumorphism.
2. Difference between C,C++.

Whats the size of a derived object ? How would you calculate it ? Does it include size of base class size?

*A derived class size also includes the size of the immediate base class/classes.
+ size of all non-static members within the derived class.
+ Byte alignment may play a role true…
Can anyone give an explanation of “byte alignment”?

Other things that would add up to what harish said are
1. virtual functions-4 bytes of virtual table pointer will be added.
if base class A has a virtual function and a class variable of type char the sizeof(A’s obj) is sizeof(Vptr)+sizeof(char).
if there exists a derived class Class B:public Class A with another virtual function and class variable of int type. sizeof(B’s obj)=sizeof(A)+sizeof(int). It wil just change the Vptr to point to its virtual function table, it will not allocate another 4bytes for its virtual function rather use one derived from base class.
divyaC on April 18, 2010 |Edit | Edit

2. Mode of inheritance


In C++, sometimes we have to use virtual inheritance for some reasons. (One classic example is the implementation of final class in C++.) When we use virtual inheritance, there will be the overhead of 4 bytes for a virtual base class pointer in that class.

class ABase{
int iMem;

class BBase : public virtual ABase {
int iMem;

class CBase : public virtual ABase {
int iMem;

class ABCDerived : public BBase, public CBase {
int iMem;


* Size of ABase : 4
* Size of BBase : 12
* Size of CBase : 12
* Size of ABCDerived : 24


Size of ABCDerived will be 24 (not 28 = sizeof (BBase + CBase + int member)) because it will maintain only one Virtual Base pointer (Same way of maintaining virtual table pointer).
Explanation for mode of inheritance:

now for sizeof(ABCDerived)
size of its iMem(4)+
size of its base classes with only one version of Vptr ie(BBase: size of(B’s iMem)+sizeof(A)= 4+4) +CBase: size of(C’s iMem)+sizeof(A)= 4+4) + 4 (Vptr )
Not “immediate” base class/classes. It should be “all direct or indirect” base classes (parents, grand parents, great grand parents etc).
Helper is correct. It counts all the base classes
But, when you are talking about an immediate base class, you are indirectly including all the parent classes.
Where base has a virtual func foo() that is also implemented in derieved.
What are the implications ? (Note: its opposite of what one does for polymorphism..)
No! you cant do this.
I think you agree with the first one’s comment

pure virtual functions are there in the base class to make sure that the child classes override that(it gives a prototype that has to be implemented)…but here the object created is of base class(even though the the pointer is of child class)…which will give you a compile-time error if you call the pure virtual function…
With this assignment ,d is not complete and it over ridies the basic c++ type mechanism..compiler will throw an error “Invalid conversion from xx to yy”. Though it is possible through dynamic_cast operator.

Why is this not allowed in C++ ? Explain problems with this approach

Derived *d = new Base()
1) user can call some function which is not defined in the base class
2) by the help of casting it is allowed

It is illegal to point to an object of the base class with a pointer that is of the derived class type.

Base *b = new Derived(); // compiles properly

Design OO model for given movie name and zip code to find nearest theaters showing that movie.
Explain the different types of polymorphism in C++

What is encapsulation??
Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object’s operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data’s origin.
What is inheritance?
What is Polymorphism??
Base class object’s pointer can invoke methods in derived class objects.
Which one of the following is NOT polymorphic behavior?
I applied for a Financial Software Development entry level position and when filling my application they asked my strongest programming language among c,c++, and Java. As I had previous experience with Java I selected it as my strongest language. The following day they sent me an email with instructions to take an online skills assessment. Reading this forum, I practiced a few IQ/Intelligence test with good results so I felt confident to take the test. I took the test and to my surprise it was all Java programming questions (syncronisation, inheritance, interfaces, polymorphism, analysing pieces of code, etc). The questions where very specific and not about general OO programming concepts eg. Which one of the following is NOT polymorphic behavior? As I’ve been out of practice a bit in hands on programming I really struggled with the test. Basically I didn’t remember clearly the syntax and got confused with some sentences. My recommendation is that you should study throughly your programming language selection and be ready for tricky questions.

The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it’s sub-classes. Public members of a class can be accessed by anyone.

What is private inheritance? Why is it useful? Is it different from containment?
What is a nested class? Why can it be useful?
// Example 1: Nested class
class OuterClass
class NestedClass
// …
// …
When you instantiate as outer class, it won’t instantiate inside class.
What is a local class? Why can it be useful?
local class is a class defined within the scope of a function — any function, whether a member function or a free function. For example:
// Example 2: Local class
int f()
class LocalClass
// …
// …


About caustic
