This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1A ____________ function is a member function in base class that you expect to redefine in derived classes.
Option B
A virtual function is a member function of a base class that is declared using the virtual keyword. It is intended to be redefined (overridden) in the derived class.
Virtual functions enable runtime polymorphism (dynamic binding), which means that the function to be executed is determined at runtime based on the type of the object.
| Function Type | Purpose |
|---|---|
| Virtual Function | Allows derived classes to override the base class function and supports runtime polymorphism. |
| Friend Function | Not a member function. It can access private and protected members of a class. |
| Static Function | Belongs to the class rather than to individual objects. |
| Inline Function | Requests the compiler to replace a function call with the functions code to reduce call overhead. |
Q: 2What is the difference between function overloading and function overriding in C++?
Option C
In C++, function overloading and function overriding are two different concepts used to achieve polymorphism.
Function overloading occurs when multiple functions have the same name but different parameter lists in the same scope. The compiler decides which function to call at compile time, so overloading is an example of static (compile-time) binding.
Function overriding occurs when a derived class provides its own implementation of a function that already exists in the base class with the same signature. The function call is resolved at run time, so overriding is an example of dynamic (run-time) binding.
Q: 3Which of these supports compile-time polymorphism in C++?
Option A
Compile-time polymorphism in C++ means the decision about which function to invoke is made during compilation, not at runtime. There are two main features in C++ that support compile-time polymorphism:
Templates enable generic programming, where functions or classes can work with different data types without being rewritten. The compiler generates code at compile-time for each data type used, which is compile-time polymorphism.
Q: 4Which of the following is used to access the overridden function of the base class from the derived class?
Option A
In C++, when a derived class defines a function with the same name and parameters as a function in the base class, the base class function is overridden (or hidden).
To access the overridden function of the base class from the derived class, the Scope Resolution Operator (::) is used.
#include <iostream>
using namespace std;
class Base
{
public:
void display()
{
cout << "Base Class display()"<<endl;
}
};
class Derived : public Base
{
public:
void display()
{
cout << "Derived Class display()"<<endl;
}
void show()
{
Base::display();
}
};
int main()
{
Derived obj;
obj.display();
obj.show();
return 0;
}
OUTPUT
Derived Class display()
Base Class display()Q: 5A class containing at least a pure virtual function is called?
Option C
In C++, a pure virtual function is a virtual function that has no implementation in the base class and is declared using =0.
virtual void display()=0; //Pure-Virtual Function.
A class that contains at least one pure virtual function is called an abstract class.
Q: 6What is abstract class in C++?
Option C
An abstract class in C++ is a class that is designed only to be a base class and cannot be instantiated on its own mean we cannot create object of abstract class.
The key feature of an abstract class is that it contains at least one pure virtual function. A do-nothing function is called pure virtual function which is declared using the syntax virtual returnType functionName() = 0;.
Derived classes must override these pure virtual functions to provide a concrete implementation.
Q: 7What will be the output of the following program?
#include<iostream>
using namespace std;
class base {
public:
virtual void fun() {
cout<<”base::fun”<<endl;}
};
class derived : public base {
public:
void fun() {
cout<<”derived::fun”<<endl;}
};
int main() {
derived t1;
base *t2=new derived();
base *t3=&t1;
t2->fun();
t3->fun();
return 0;
}
Option B
Here, fun() in the base class is virtual, which means if a base class pointer points to a derived class object, the derived class function will be called.
Q: 8Two functions with same signature, one function available in base class and another function is available in derived class, this concept is known as?
Option C
In object-oriented programming (C++), function overriding occurs when a derived class provides its own implementation of a function that is already defined in the base class, with the same signature.
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
cout<<"Base class show() Called."<<endl;
}
};
class Derived : public Base
{
public:
void show()
{
cout<<"Derived Class show() Called."<<endl;
}
};
int main()
{
Base obj1;
Derived obj2;
obj1.show();
obj2.show();
}
OUTPUT
Base class show() Called.
Derived Class show() Called.
Q: 9Given the overloaded functions:
int max (int a, int b);
double max (double a, double b);
What will be the output of the following code? (Assume no syntax errors)
double val = 6.8;
double result = max (2.3, val);
Option A
C++ resolves function overloading based on the exact match of argument types. In this case, max(2.3, 6.8) passes two double values, so the compiler selects the function double max(double a, double b). Since the argument types match exactly, there is no ambiguity or type conversion required.
Q: 10Two functions with the same name but different argument, one function is present in parent class and other present in child class, this is known as
Option A
In C++, when a derived class declares a function with the same name as a function in the base class, but with different arguments, the base class function becomes hidden. This situation is called function hiding, not overloading, or overriding.
Q: 11Which of the following statement is NOT correct regarding virtual function in C++?
I. It must be a member of some class.
II. It is accessed by using object points.
Option D
A virtual function in C++ is a non-static member function declared using the virtual keyword in a base class.
Virtual functions are used to achieve runtime polymorphism, where the function call is resolved during program execution rather than during compilation.
The statement (I) is correct. In C++, a virtual function must be a non-static member function of a class. You cannot declare global or static functions as virtual.
The statement (II) is correct. The statement most likely means “object pointers.” Virtual functions are generally accessed through base class pointers or references. This allows the program to decide at runtime which function should execute.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void display()
{
cout<<"Base Class";
}
};
class Derived : public Base
{
public:
void display()
{
cout<<"Derived Class";
}
};
int main()
{
Base *ptr;
Derived d;
ptr=&d;
ptr->display();
return 0;
}
Q: 12Consider the following code and determine the output:
#include<iostream>
using namespace std;
class A
{
public:
virtual void vfunc() {
cout<<”This is A’s vfunc()
”;
}
};
class B : public A
{
public:
void vfunc() {
cout<<”This is B’s vfunc()
”;
}
};
class C : public A
{
public:
void vfunc() {
cout<<”This is C’s vfunc()
”;
}
};
int main()
{
A *p, b;
B d1;
C d2;
p=&d1;
p->vfunc();
p=&b;
p->vfunc();
p=&d2;
p->vfunc();
return 0;
}
Option D
In class A, vfunc() is declared as virtual, which means that when this function is called through a base class pointer, the function of the actual object type will be executed. This concept is called Runtime Polymorphism.
Q: 13Identify the correct statements.
(i) Derived classes do not inherit or overload constructors or destructors from their base classes.
(ii) Destructors can be declared with the keyword virtual.
(iii) Constructors can be declared with the keyword virtual.
Option B
In C++, a constructor is a special member function that is automatically called when an object is created, whereas a destructor is automatically called when an object is destroyed.
Constructors and destructors have special rules regarding inheritance and virtual functions.
Constructors and destructors are not inherited by derived classes. Every derived class has its own constructor and destructor.
However, the base class constructor is automatically called when a derived class object is created, and the base class destructor is automatically called when the object is destroyed.
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base Constructor" << endl;
}
~Base()
{
cout << "Base Destructor" << endl;
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived Constructor" << endl;
}
~Derived()
{
cout << "Derived Destructor" << endl;
}
};
int main()
{
Derived obj;
return 0;
}
OUTPUT
Base Constructor
Derived Constructor
Derived Destructor
Base DestructorA destructor can be declared as virtual. Virtual destructors are used in inheritance to ensure that when a derived class object is deleted through a base class pointer, the destructors of both the derived class and the base class are called in the correct order.
#include <iostream>
using namespace std;
class Base
{
public:
virtual ~Base()
{
cout << "Base Destructor" << endl;
}
};
class Derived : public Base
{
public:
~Derived()
{
cout << "Derived Destructor" << endl;
}
};
int main()
{
Base *ptr = new Derived();
delete ptr;
return 0;
}
OUTPUT
Derived Destructor
Base DestructorA constructor cannot be declared as virtual because it is responsible for creating the object. Virtual functions require an object to exist before they can be invoked. Therefore, virtual constructors are not allowed in C++.
Therefore,
Q: 14How can one implement the compile-time polymorphism in the C++ programming language?
Option A
In C++, compile-time polymorphism is achieved when the function or class behavior is determined at compile time. This can be done using:
Function Overloading: Multiple functions with the same name but different parameters.
Operator Overloading: Redefining operators for user-defined types.
Templates: Allow writing generic functions or classes that work with any data type.
Q: 15Which of the following statements is correct about the C++ programming language?
Option D
C++ primarily uses static type checking (at compile time) for variables and functions, catching most errors early. It also supports dynamic type checking at runtime via RTTI (Run-Time Type Information) features like dynamic_cast and typeid, allowing safe polymorphic casts.
Additionally, C++ allows const member functions, which promise not to modify class data and can be called on const objects.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.