Home Subjects C++ Programming OOP Concepts

C++ Programming
OOP Concepts

Object oriented C++ for GATE and placement

49 Q 1 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 49
Topics in C++ Programming
All OOP Concepts 100
Q.11 Medium OOP Concepts
What will be printed by the following code?
class Test { int x; public: Test() { x = 0; } ~Test() { cout
A Destructor
B DestructorDestructor
C No output
D Compilation Error
Correct Answer:  B. DestructorDestructor
EXPLANATION

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.

Test
Q.12 Medium OOP Concepts
Which of the following correctly describes function binding in C++?
A Static binding occurs at runtime for virtual functions
B Dynamic binding occurs at compile-time for non-virtual functions
C Virtual functions use dynamic binding, determined at runtime
D All member functions use dynamic binding by default
Correct Answer:  C. Virtual functions use dynamic binding, determined at runtime
EXPLANATION

Virtual functions use dynamic (runtime) binding where the function to be called is determined based on the actual object type at runtime. Non-virtual functions use static (compile-time) binding.

Test
Q.13 Medium OOP Concepts
What is the output of the following code?
class A { public: int x = 5; };
class B : public A { public: int x = 10; };
int main() { B obj; cout
A 5
B 10
C Compilation Error
D Undefined behavior
Correct Answer:  B. 10
EXPLANATION

The derived class B has its own member variable 'x' with value 10, which shadows the base class member. obj.x refers to B's x, which is 10.

Test
Q.14 Medium OOP Concepts
In the context of operator overloading, which operator cannot be overloaded in C++?
A ++
B ::
C +
D []
Correct Answer:  B. ::
EXPLANATION

The scope resolution operator (::), member access operator (.), pointer-to-member operator (.*), and ternary operator (?:) cannot be overloaded in C++.

Test
Q.15 Medium OOP Concepts
What is the purpose of the 'const' keyword when used with member functions?
A To make the function static
B To prevent modification of member variables within the function
C To make the function inline
D To prevent the function from being overridden
Correct Answer:  B. To prevent modification of member variables within the function
EXPLANATION

A const member function cannot modify any member variables of the object. It guarantees that the function will not alter the state of the object.

Test
Q.16 Medium OOP Concepts
Which of the following statements about abstract classes in C++ is true?
A Abstract classes can be instantiated
B Abstract classes must contain at least one pure virtual function
C Abstract classes cannot have constructors
D Abstract classes are defined using the 'abstract' keyword
Correct Answer:  B. Abstract classes must contain at least one pure virtual function
EXPLANATION

An abstract class in C++ must have at least one pure virtual function. It cannot be instantiated directly, but it can have constructors and other member functions.

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

This demonstrates runtime polymorphism. The virtual function is called based on the actual object type (Derived), not the pointer type (Base), so 'Derived' is printed.

Test
Q.18 Medium OOP Concepts
Which of the following correctly uses pure virtual functions?
A class A { virtual void func() = 0; }; A obj;
B class A { virtual void func() = 0; }; class B : public A { void func() {} };
C class A { void func() = 0; };
D class A { virtual void func() { } };
Correct Answer:  B. class A { virtual void func() = 0; }; class B : public A { void func() {} };
EXPLANATION

Abstract classes with pure virtual functions cannot be instantiated. Derived classes must override pure virtual functions. Option B correctly shows this pattern.

Test
Q.19 Medium OOP Concepts
What is the main difference between composition and inheritance?
A Composition is 'has-a' relationship, inheritance is 'is-a' relationship
B Inheritance is 'has-a' relationship, composition is 'is-a' relationship
C Both are the same
D Composition is faster than inheritance
Correct Answer:  A. Composition is 'has-a' relationship, inheritance is 'is-a' relationship
EXPLANATION

Composition represents a 'has-a' relationship where a class contains objects of other classes. Inheritance represents an 'is-a' relationship where a class extends another class.

Test
Q.20 Medium OOP Concepts
Which of the following is an example of compile-time polymorphism?
A Virtual functions
B Function overloading
C Inheritance
D Dynamic casting
Correct Answer:  B. Function overloading
EXPLANATION

Function overloading is resolved at compile time. Virtual functions and dynamic casting are examples of runtime polymorphism.

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