What will be printed by the following code?
class Test { int x; public: Test() { x = 0; } ~Test() { cout << "Destructor"; } };
int main() { Test t1, t2; return 0; }
Answer: B
Two objects t1 and t2 are created. When the program exits, both objects are destroyed in the reverse order of creation (t2 then t1), calling the destructor twice.
Q.82Easy
Which access specifier should be used when a derived class needs to access a base class member but external classes should not?
Answer: C
The 'protected' access specifier allows derived classes to access the member while keeping it hidden from external classes. This provides the needed balance for inheritance hierarchies.
Q.83Medium
What is the primary advantage of using virtual functions in a base class?
Answer: B
Virtual functions enable runtime polymorphism, allowing the correct derived class method to be called through a base class pointer or reference, facilitating flexible and extensible code design.
Q.84Hard
Consider the following code: class A { public: virtual ~A() {} }; class B : public A {}; int main() { A *ptr = new B(); delete ptr; }. What does the virtual destructor ensure?
Answer: B
A virtual destructor ensures that when a derived class object is deleted through a base class pointer, the derived class destructor is called first, followed by the base class destructor, properly cleaning up all resources.
Q.85Medium
Which of the following is an example of operator overloading as a member function?
Answer: B
Option B shows operator overloading as a member function. The operator is defined as a member function of the class, which takes the second operand as a parameter.
Advertisement
Q.86Hard
What is the key difference between shallow copy and deep copy in the context of copy constructors?
Answer: B
Shallow copy performs a bitwise copy of members, which can be problematic for pointers (both objects point to same memory). Deep copy allocates new memory for dynamic members, ensuring independent objects.
Q.87Medium
In which scenario would you use an abstract base class instead of a concrete base class?
Answer: B
Abstract base classes are used to define interfaces and contracts that derived classes must follow. They cannot be instantiated but force derived classes to implement certain behaviors through pure virtual functions.
Q.88Medium
What will be the output of the following code?
class Base { public: void show() { cout << "Base"; } };
class Derived : public Base { public: void show() { cout << "Derived"; } };
int main() { Base *b = new Derived(); b->show(); }
Answer: A
Without virtual keyword, function overriding doesn't occur. The pointer type (Base*) determines which function is called, so Base::show() is invoked.
Q.89Easy
A class member declared as 'private' can be accessed by:
Answer: B
Private members are accessible only within the class itself and to friend functions/classes. They are not accessible to derived classes or external code.
Q.90Easy
Which type of inheritance allows multiple classes to inherit from a single base class?
Answer: B
Hierarchical inheritance occurs when one base class is inherited by multiple derived classes. Multiple inheritance is when one class inherits from multiple base classes.
Q.91Medium
Consider a scenario where a derived class needs to access a base class member that should not be accessible to external code. Which access specifier should be used in the base class?
Answer: B
The 'protected' access specifier allows access within the class and derived classes, but not to external code. This is ideal for members that derived classes need to use but external code should not access.
Q.92Medium
What is the output of the following code?
class A { public: virtual void func() { cout << "A"; } };
class B : public A { public: void func() { cout << "B"; } };
class C : public B { public: void func() { cout << "C"; } };
int main() { A *p = new C(); p->func(); }
Answer: C
With virtual functions, the actual object type (C) determines which function is called, not the pointer type. C::func() is invoked through multilevel inheritance.
Q.93Medium
Which of the following statements about constructors is correct?
Answer: B
Constructors have no return type, a class can have multiple constructors (constructor overloading), and constructors are not automatically inherited in C++ (until C++11 explicit inheritance). They are automatically invoked when objects are created.
Q.94Hard
What problem does the Diamond Problem primarily address in multiple inheritance?
Answer: A
The Diamond Problem occurs when a derived class inherits from two classes that both inherit from a common base class, creating multiple inheritance paths and ambiguity about which base class members to use. Virtual inheritance solves this.
Q.95Easy
A pure virtual function is declared using which syntax in C++?
Answer: A
In C++, a pure virtual function is declared by adding '= 0' after the function signature. C++ does not have 'abstract' or 'pure' keywords like Java.
Q.96Medium
What is the primary use of the 'const' keyword when applied to member functions?
Answer: A
A const member function cannot modify any non-static data members of the object. It guarantees that calling the function will not change the object's state.
Q.97Medium
Which of the following best describes the concept of method overloading?
Answer: B
Method overloading allows multiple functions with the same name but different parameter lists (number, type, or order). Return type alone is insufficient for overloading.
Q.98Hard
What will be the output?
class Test { public: static int count; };
int Test::count = 0;
int main() { Test t1, t2; Test::count = 5; cout << t1.count << " " << t2.count; }
Answer: B
Static members are shared by all instances of a class. When Test::count is set to 5, this value is shared across all objects t1 and t2, so both print 5.
Q.99Hard
An abstract class in C++ is one that contains at least one pure virtual function. Which statement about abstract classes is TRUE?
Answer: B
An abstract class with at least one pure virtual function cannot be instantiated directly. However, it serves as a blueprint for derived classes and can have constructors, destructors, and other functions.
Q.100Medium
In C++, when a derived class overrides a virtual function from a base class, which access specifier must be used to ensure proper polymorphic behavior?
Answer: B
In C++, a derived class can override a virtual function with a different access specifier than the base class. The access specifier controls who can call the function, but it doesn't affect the polymorphic behavior. However, it's a best practice to maintain the same access level.