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: 1What 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: 2Which 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: 3A 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: 4What 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: 5What 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: 6Two 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: 7Given 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: 8Two 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: 9Which 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: 10Consider 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: 11How 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: 12Which 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.
Q: 13
Math List—1 with List—2.
| List—1 | List—2 |
|---|---|
| (i) Overriding | A. Allows a function or operator to be given more than one definition. |
| (ii) Overloading | B. A function that is called to deallocate memory. |
| (iii) Destructor | C. The ability of change the definition of an inherited method. |
Option C
Overriding:
Overriding means redefining an inherited function in the derived class. It allows a derived class to change the behavior of a method inherited from the base class.
#include<iostream>
using namespace std;
class Shape
{
public:
void draw()
{
cout<<"Drawing Shape"<<endl;
}
};
class Circle : public Shape
{
public:
void draw()
{
cout<<"Drawing Circle"<<endl;
}
};
int main()
{
Circle c1;
c1.draw();
return 0;
}
OUTPUT
Drawing Circle
Overloading:
Overloading allows multiple functions or operators to have the same name with different parameters or definitions.
#include<iostream>
using namespace std;
class Demo
{
public:
void add(int a, int b)
{
cout<<"Sum : "<<a+b<<endl;
}
void add(int a, int b, int c)
{
cout<<"Sum : "<<a+b+c;
}
};
int main()
{
Demo d;
d.add(10,20);
d.add(10,20,30);
return 0;
}
OUTPUT
Sum : 30
Sum : 60
Destructor:
A destructor is a special member function that is automatically called when an object is destroyed. It is mainly used to release or deallocate memory.
#include<iostream>
using namespace std;
class Demo
{
public:
Demo()
{
cout<<"Constructor Called."<<endl;
}
~Demo()
{
cout<<"Destructor Called.";
}
};
int main()
{
Demo d;
return 0;
}
OUTPUT
Constructor Called.
Destructor Called.
Q: 14Which of the following is/are true statement(s) in C++?
S1 : Function can be overloaded
S2 : Operator can be overloaded
S3 : Constructor can be overloaded
Option B
In C++, Overloading allows multiple functions, operators, or constructors to have the same name but behave differently based on their parameters (either number, type or sequence of parameters).
S1: Function can be overloaded – True. We can have multiple functions with the same name but different number or types or sequence of parameters.
S2: Operator can be overloaded – True. Operator overloading allows defining custom behaviors for operators (like +, -, *, == etc.) for user-defined types (classes). This means you can change how operators work with your objects.
class Complex {
int real, imag;
public:
Complex operator+(Complex const &c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return(temp);
}
};
S3: Constructor can be overloaded – True. Constructors are special functions called when objects are created. A class can have multiple constructors with different parameter lists.
class MyClass {
public:
MyClass() {} // default constructor
MyClass(int a) {} // one argument constructor
MyClass(int a, int b) {} // two argument constructor
};
Q: 15In C++, the operator that cannot be overloaded is
Option D
In C++, operator overloading allows programmers to redefine how operators work for user-defined data types like structure and classes. However, not all operators can be overloaded. Operators that cannot be overloaded include:
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.