What is the default access specifier for class members in C++?
Answer: B
In C++, the default access specifier for class members is 'private'. Only for structs, the default is 'public'.
Q.2Easy
Which keyword is used to create a derived class in C++?
Answer: C
In C++, the colon (:) operator is used to specify inheritance. Syntax: class Derived : public Base {}
Q.3Easy
What is the purpose of a virtual function in C++?
Answer: B
Virtual functions enable runtime polymorphism by allowing derived classes to override base class methods. The correct method is called based on the object type at runtime.
Q.4Medium
Which of the following correctly demonstrates constructor overloading?
Answer: A
Constructor overloading allows multiple constructors with different parameter lists. Option A is correct as it has constructors with no parameters and with an int parameter.
Q.5Medium
What will be the output of the following code?
class Base {
public:
virtual void display() { cout << "Base"; }
};
class Derived : public Base {
public:
void display() { cout << "Derived"; }
};
int main() {
Base *b = new Derived();
b->display();
}
Answer: B
Due to virtual function and dynamic polymorphism, the Derived class's display() method is called because the actual object is of type Derived, even though the pointer is of type Base.
Advertisement
Q.6Easy
Which access specifier allows access from derived classes but not from outside?
Answer: C
The 'protected' access specifier allows access from within the class and derived classes, but not from outside the class hierarchy.
Q.7Medium
What is the difference between method overloading and method overriding?
Answer: A
Method overloading occurs in the same class with different parameter lists (compile-time polymorphism). Method overriding occurs in derived classes with the same signature (runtime polymorphism).
Q.8Medium
Which of the following is true about abstract classes in C++?
Answer: B
C++ doesn't have a dedicated 'abstract' keyword. Abstract classes are created by defining at least one pure virtual function (virtual func() = 0;). You cannot instantiate abstract classes.
Q.9Easy
What is the purpose of 'this' pointer in C++?
Answer: B
The 'this' pointer is an implicit pointer that points to the current object. It's used to access member variables and functions of the current object.
Q.10Medium
Which type of inheritance can lead to the Diamond Problem in C++?
Answer: B
Multiple inheritance can lead to the Diamond Problem where a class inherits from two classes that share a common base. This is solved using virtual inheritance.
Q.11Medium
What is a copy constructor in C++?
Answer: A
A copy constructor takes a reference to another object of the same class and creates a copy of it. Syntax: ClassName(const ClassName &obj);
Q.12Medium
Which of the following demonstrates proper encapsulation?
Answer: B
Proper encapsulation hides data members (private) and provides controlled access through public getter and setter methods.
Q.13Hard
What is the output of the following code?
class A {
public:
A() { cout << "A "; }
~A() { cout << "~A "; }
};
class B : public A {
public:
B() { cout << "B "; }
~B() { cout << "~B "; }
};
int main() {
B obj;
return 0;
}
Answer: A
Constructors are called from base to derived (A then B), while destructors are called from derived to base (~B then ~A).
Q.14Easy
What happens when you try to access a private member of a class from outside?
Answer: B
Accessing private members from outside the class results in a compilation error. The compiler enforces access control at compile time.
Q.15Medium
Which of the following is a characteristic of a pure virtual function?
Answer: B
A pure virtual function (defined with = 0) must be overridden in derived classes. The base class containing it becomes abstract and cannot be instantiated.
Q.16Medium
What is the main advantage of using interfaces/abstract classes in C++?
Answer: B
Abstract classes define a contract (interface) that derived classes must implement. This ensures consistency and enables polymorphic behavior across different derived classes.
Q.17Hard
Consider the following code:
class Base {
protected:
int x = 10;
};
class Derived : private Base {
public:
void display() { cout << x; }
};
int main() {
Derived d;
d.display();
}
Answer: A
Even with private inheritance, protected members of Base are accessible within Derived class methods. The display() function can access x and will print 10.
Q.18Hard
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;
Answer: A
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.
Q.19Easy
In C++, which keyword is used to prevent a class from being inherited?
Answer: A
The 'final' keyword (introduced in C++11) prevents a class from being used as a base class for further inheritance.
Q.20Easy
Which of the following best describes polymorphism in C++?
Answer: B
Polymorphism allows objects to take on multiple forms through compile-time (overloading) and runtime (overriding) mechanisms, enabling flexible and extensible code design.