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.
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.