Home Subjects C++ Programming OOP Concepts

C++ Programming
OOP Concepts

Object oriented C++ for GATE and placement

23 Q 1 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 23
Topics in C++ Programming
All OOP Concepts 100
Q.1 Hard OOP Concepts
An abstract class in C++ is one that contains at least one pure virtual function. Which statement about abstract classes is TRUE?
A Abstract classes can be instantiated if all pure virtual functions are defined
B Abstract classes cannot be instantiated directly but can be used as base classes
C Abstract classes must contain only pure virtual functions
D Abstract classes cannot have constructors
Correct Answer:  B. Abstract classes cannot be instantiated directly but can be used as base classes
EXPLANATION

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.

Test
Q.2 Hard OOP Concepts
What will be the output?
class Test { public: static int count; };
int Test::count = 0;
int main() { Test t1, t2; Test::count = 5; cout
A 0 0
B 5 5
C 5 0
D Compilation Error
Correct Answer:  B. 5 5
EXPLANATION

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.

Test
Q.3 Hard OOP Concepts
What problem does the Diamond Problem primarily address in multiple inheritance?
A A derived class inherits from two classes that share a common base class, causing ambiguity
B A base class cannot be inherited by more than two classes
C Virtual functions cannot be used in multiple inheritance
D Memory allocation becomes inefficient
Correct Answer:  A. A derived class inherits from two classes that share a common base class, causing ambiguity
EXPLANATION

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.

Test
Q.4 Hard OOP Concepts
What is the key difference between shallow copy and deep copy in the context of copy constructors?
A Shallow copy copies all members; deep copy copies only some members
B Shallow copy creates bitwise copies; deep copy allocates new memory for dynamically allocated members
C Deep copy is faster but uses more memory
D Shallow copy is used for virtual functions; deep copy is used for regular functions
Correct Answer:  B. Shallow copy creates bitwise copies; deep copy allocates new memory for dynamically allocated members
EXPLANATION

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.

Test
Q.5 Hard OOP Concepts
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?
A The constructor is also virtual
B The correct destructor sequence is called for derived class objects
C Memory is freed immediately after deletion
D The pointer cannot be null
Correct Answer:  B. The correct destructor sequence is called for derived class objects
EXPLANATION

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.

Test
Q.6 Hard OOP Concepts
What is the relationship between references and pointers in C++ OOP context?
A References are safer than pointers but less flexible
B Pointers provide more functionality than references
C References cannot be used for polymorphism, only pointers can
D Both can be used equally for polymorphism, but references provide better safety
Correct Answer:  D. Both can be used equally for polymorphism, but references provide better safety
EXPLANATION

Both references and pointers can be used for polymorphism in C++. References are generally safer (no null references, cannot be reassigned) while pointers are more flexible (can be null, reassigned).

Test
Q.7 Hard OOP Concepts
Consider a scenario where class C inherits from both class A and class B, and both A and B have a method foo(). Which statement correctly resolves the ambiguity?
A C::foo() will automatically call both A::foo() and B::foo()
B C must explicitly override foo() or use scope resolution operator to specify which foo() to call
C The compiler will automatically choose one based on declaration order
D C cannot inherit from both A and B in this case
Correct Answer:  B. C must explicitly override foo() or use scope resolution operator to specify which foo() to call
EXPLANATION

In multiple inheritance, if both base classes have the same method, the derived class must either override it or use the scope resolution operator (A::foo() or B::foo()) to specify which one to call.

Test
Q.8 Hard OOP Concepts
In a multiple inheritance scenario with diamond problem, how does C++ resolve ambiguity?
A Automatically chooses one base class
B Requires virtual inheritance
C Throws a compilation error
D Uses runtime resolution
Correct Answer:  B. Requires virtual inheritance
EXPLANATION

The diamond problem occurs when a derived class inherits from two classes that have a common base. Virtual inheritance ensures only one copy of the base class is created.

Test
Q.9 Hard OOP Concepts
What will be printed by this code?
class A { public: virtual void print() { cout print(); }
A AB
B AA
C BB
D BA
Correct Answer:  A. AB
EXPLANATION

With virtual functions and dynamic binding, arr[0]->print() calls A's print (outputs 'A'), and arr[1]->print() calls B's overridden print (outputs 'B'). Result: 'AB'.

Test
Q.10 Hard OOP Concepts
In C++, what is the result of dynamic_cast when it fails on a pointer?
A Throws an exception
B Returns nullptr
C Returns the original pointer
D Causes undefined behavior
Correct Answer:  B. Returns nullptr
EXPLANATION

When dynamic_cast fails on a pointer, it returns nullptr. For references, it throws a std::bad_cast exception.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips