Home Subjects C++ Programming

C++ Programming

Object oriented C++ for GATE and placement

100 Q 1 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 100
Topics in C++ Programming
All OOP Concepts 100
Q.1 Medium OOP Concepts
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?
A The derived class function must use the same access specifier as the base class
B The derived class function can use any access specifier regardless of base class
C The derived class function must always be public
D The derived class function must be private for security
Correct Answer:  B. The derived class function can use any access specifier regardless of base class
EXPLANATION

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.

Test
Q.2 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.3 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.4 Medium OOP Concepts
Which of the following best describes the concept of method overloading?
A Multiple functions with the same name and same parameters
B Multiple functions with the same name but different parameters or return types
C A derived class defining a function with the same name as the base class
D Using virtual functions to achieve runtime polymorphism
Correct Answer:  B. Multiple functions with the same name but different parameters or return types
EXPLANATION

Method overloading allows multiple functions with the same name but different parameter lists (number, type, or order). Return type alone is insufficient for overloading.

Test
Q.5 Medium OOP Concepts
What is the primary use of the 'const' keyword when applied to member functions?
A To prevent the function from modifying object data members
B To prevent the function from being called
C To make the function static
D To optimize function execution speed
Correct Answer:  A. To prevent the function from modifying object data members
EXPLANATION

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.

Test
Q.6 Easy OOP Concepts
A pure virtual function is declared using which syntax in C++?
A virtual void func() = 0;
B abstract void func();
C pure virtual void func();
D virtual abstract void func() = 0;
Correct Answer:  A. virtual void func() = 0;
EXPLANATION

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.

Test
Q.7 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.8 Medium OOP Concepts
Which of the following statements about constructors is correct?
A Constructors can have a return type
B Constructors are automatically called when an object is created
C A class can have only one constructor
D Constructors are inherited automatically in all derived classes
Correct Answer:  B. Constructors are automatically called when an object is created
EXPLANATION

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.

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

With virtual functions, the actual object type (C) determines which function is called, not the pointer type. C::func() is invoked through multilevel inheritance.

Test
Q.10 Medium OOP Concepts
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?
A public
B protected
C private
D internal
Correct Answer:  B. protected
EXPLANATION

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.

Test
IGET
iget AI
Online · Ask anything about exams
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