Home Subjects C++ Programming

C++ Programming

Object oriented C++ for GATE and placement

49 Q 1 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 31–40 of 49
Topics in C++ Programming
All OOP Concepts 100
Q.31 Medium OOP Concepts
Consider a scenario where Class B inherits from Class A, and Class C also inherits from Class A. Class D inherits from both B and C. Which problem does this create?
A Circular Inheritance
B Multiple Inheritance Problem
C Diamond Problem
D Ambiguous Inheritance
Correct Answer:  C. Diamond Problem
EXPLANATION

The Diamond Problem occurs when a class inherits from two classes that both inherit from a common base class, creating ambiguity in the inheritance hierarchy.

Test
Q.32 Medium OOP Concepts
What is the output of the following code?
class Base { public: void display() { cout
A Derived
B Base
C Compilation Error
D BaseDerived
Correct Answer:  B. Base
EXPLANATION

Since 'display()' is not virtual and obj is of type Base, it calls Base::display(). Object slicing occurs when a Derived object is assigned to Base reference.

Test
Q.33 Medium OOP Concepts
Which keyword is used to prevent a derived class from further overriding a virtual function in C++?
A sealed
B final
C lock
D restrict
Correct Answer:  B. final
EXPLANATION

The 'final' keyword (introduced in C++11) prevents further overriding of a virtual function in derived classes.

Test
Q.34 Medium OOP Concepts
What happens when you try to call a non-virtual function through a pointer to a base class pointing to a derived object?
A The derived class version is called
B The base class version is called
C Compilation error occurs
D Runtime error occurs
Correct Answer:  B. The base class version is called
EXPLANATION

For non-virtual functions, the function call is resolved based on the pointer type (base class), not the actual object type. Therefore, the base class version is called.

Test
Q.35 Medium OOP Concepts
What is the purpose of the 'const' keyword when applied to member functions?
A To make the function faster
B To prevent the function from modifying object members
C To make the function private
D To make the function virtual
Correct Answer:  B. To prevent the function from modifying object members
EXPLANATION

A const member function cannot modify the state of the object (its member variables). It's used to indicate that a function only reads data without changing it.

Test
Q.36 Medium OOP Concepts
What does encapsulation in C++ primarily achieve?
A Reducing code size
B Hiding internal implementation details and controlling access
C Improving program execution speed
D Enabling multiple inheritance
Correct Answer:  B. Hiding internal implementation details and controlling access
EXPLANATION

Encapsulation bundles data and functions together, hiding implementation details using access specifiers (private, protected, public) and controlling how external code interacts with the object.

Test
Q.37 Medium OOP Concepts
What is the main difference between method overloading and method overriding?
A Overloading is compile-time, overriding is runtime
B Overloading changes return type, overriding changes parameter list
C Overloading requires virtual keyword, overriding doesn't
D Overloading is used only for operators
Correct Answer:  A. Overloading is compile-time, overriding is runtime
EXPLANATION

Method overloading is resolved at compile-time (static polymorphism) with different function signatures, while method overriding is resolved at runtime (dynamic polymorphism) using virtual functions.

Test
Q.38 Medium OOP Concepts
What will be the output of the following code?
class A {
public:
A() { cout
A A B
B B A
C B
D A
Correct Answer:  A. A B
EXPLANATION

When a derived class object is created, the base class constructor is called first, then the derived class constructor. Output is 'A B'.

Test
Q.39 Medium OOP Concepts
Which of the following statements about abstract classes in C++ is correct?
A Abstract classes cannot have constructors
B Abstract classes must have at least one pure virtual function
C Objects of abstract classes can be created if all pure virtual functions are defined
D Abstract classes are declared using the 'abstract' keyword
Correct Answer:  B. Abstract classes must have at least one pure virtual function
EXPLANATION

An abstract class in C++ is defined by having at least one pure virtual function (declared with = 0). Objects cannot be instantiated from abstract classes.

Test
Q.40 Medium OOP Concepts
What is the output of the following code?
class Base {
public:
virtual void display() { cout
A Base
B Derived
C Compilation Error
D No output
Correct Answer:  B. Derived
EXPLANATION

Due to virtual function and runtime polymorphism, the Derived class's display() method is called through the Base class pointer, printing 'Derived'.

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