Q: 1 What is the difference between function overloading and function overriding in C++?
Overloading is a dynamic (run-time) binding and overriding is static (compile-time) binding.
Redefining a function in a friend class is called function overriding, while redefining a function in a derived class is called function overloading.
Overloading is a static (compile-time) binding and overriding is a dynamic (run-time) binding.
Redefining a function in a friend class is called function overloading, while redefining a function in a derived class is called function overriding.
[ 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: 2 Which of these supports compile-time polymorphism in C++?
Templates
Inheritance
Abstract Classes
Virtual Functions
[ 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: 3 A class containing at least a pure virtual function is called?
Pure class
Virtual class
Abstract class
Derived class
[ 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: 4 What is abstract class in C++?
Any class in C++ is an abstract class/Class from which any class is derived
Class specifically used as a base class with at least one virtual function
Class specifically used as a base with at least one pure virtual function
None of the above
[ 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: 5 What 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;
}
base::fun
derived::fun
derived::fun
derived::fun
derived::fun
base::fun
base::fun
base::fun
[ 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: 6 Two functions with same signature, one function available in base class and another function is available in derived class, this concept is known as?
Function overloading
Function hiding
Function overriding
Function overlapping
[ 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: 7 Given 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);
Calls double max (double, double)
Compilation error due to ambiguity
9.1
Calls int max(int,int)
[ 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: 8 Two 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
Function hiding
Function overriding
Function overloading
Both (b) and (c)
[ 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: 9 Consider 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;
}
This is A’s vfunc()
This is B’s vfunc()
This is C’s vfunc()
This is C’s vfunc()
This is A’s vfunc()
This is B’s vfunc()
This is A’s vfunc()
This is C’s vfunc()
This is B’s vfunc()
This is B’s vfunc()
This is A’s vfunc()
This is C’s vfunc()
[ 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: 10 How can one implement the compile-time polymorphism in the C++ programming language?
By using the template
By using the concepts of inheritance by using both the virtual functions and inheritance
More than one of the above
None of the above
[ 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: 11 Which of the following statements is correct about the C++ programming language?
In C++, both the static and dynamic types checking are allowed.
In C++, member function are allowed to be of the type const.
In C++, dynamic checking is allowed.
More than one of the above
[ 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: 12 Which 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
S1 and S2 only
S1, S2 and S3
S2 only
S3 and S2 only
[ 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: 13 Which of the following can be used to create an abstract class in the C++ programming language?
By using the pure virtual function in the class
By declaring a virtual function in the base class
By declaring the virtual keyword afterward the class declaration
None of the above
[ Option A ]
In C++, an abstract class is defined as a class that contains at least one pure virtual function. A pure virtual function is a virtual function declared by assigning it to zero (= 0) in its declaration, and it has no definition in the abstract class itself.
This forces derived classes to provide an implementation for this function, otherwise, they also become abstract. Abstract classes cannot be instantiated directly, they serve as blueprints for other subclasses.
class Person
{
public:
virtual void setName() = 0; // Pure Virtual Function.
};
Q: 14 Which of the following statements about virtual methods in C++ is TRUE?
Constructors in C++ can be declared as virtual to support polymorphism.
Redefining a virtual method in a derived class requires the same signature and return type as in the base class.
A function in a base class becomes virtual if it is declared virtual in any of its derived classes.
A virtual method must be redefined in every derived class, otherwise it leads to a compilation error.
[ Option B ]
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.