1. What is Inheritance in C++?
A. Wrapping of data into a single class
B. Deriving new classes from existing classes
C. Overloading of classes
D. Classes with same names
Answer: B
Explanation:
: Inheritance is the concept of OOPs in which new classes are derived from existing classes in order to reuse the properties of classes defined earlier.
2. How many specifiers are used to derive a class?
A. 1
B. 2
C. 3
D. 4
Answer: C
Explanation:
: There are 3 specifiers used to derive a class. They are private, protected and public.
3. Which specifier makes all the data members and functions of base class inaccessible by the derived class?
A. private
B. protected
C. public
D. both private and protected
Answer: A
Explanation:
: Private access specifier is used to make all the data members and functions of the base class inaccessible.
4. If a class is derived privately from a base class then ______________________________
A. no members of the base class is inherited
B. all members are accessible by the derived class
C. all the members are inherited by the class but are hidden and cannot be accessible
D. no derivation of the class gives an error
Answer: C
Explanation:
: Whenever a class is derived, all the members of the base class is inherited by the derived class but are not accessible by the derived class.
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
A(){
cout<<"Constructor of class A\n";
}
};
class B: public A
{
int a = 15;
public:
B(){
cout<<"Constructor of class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
return 0;
}
A.
Constructor of class A
Constructor of class B
B. Constructor of class A
C. Constructor of class B
D.
Constructor of class B
Constructor of class A
Answer: A
Explanation:
: When a derived class is declared it calls both its constructor and the base class constructor. It first calls the base class constructor and then its own constructor.
6. What is a virtual function in C++?
A. Any member function of a class
B. All functions that are derived from the base class
C. All the members that are accessing base class data members
D. All the functions which are declared in the base class and is re-defined/overridden by the derived class
Answer: D
Explanation:
: Virtual function is a function that is declared inside the base class and is re-defined inside the derived class.
7. Which is the correct syntax of declaring a virtual function?
A. virtual int func();
B. virtual int func(){};
C. inline virtual func();
D. inline virtual func(){};
Answer: A
Explanation:
: To make a function virtual function we just need to add virtual keyword at the starting of the function declaration.
8. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
b.func();
return 0;
}
A. Hello this is class B
B. Hello this is class A
C. Error
D. Segmentation fault
Answer: A
Explanation:
: Normal execution of the program and object calls func() from class B.
9. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A
{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
A *a;
a->func();
return 0;
}
A. Hello this is class A
B. Hello this is class B
C. Error
D. Segmentation Fault
Answer: D
Explanation:
: As object ‘a’ is a pointer object and we know every pointer needs to be initialised memory before use. Hence segmentation fault. Use A *a = new A(); to initialise memory to the object.
10. What will be the output of the following C++ code?
#include<iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A
{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
A *a = new A();
B b;
a = &b;
a->func();
return 0;
}
A. Hello this is class A
B. Hello this is class B
C. Error
D. Segmentation Fault
Answer: B
Explanation:
: As pointer object a is pointing to the object b hence the definition of virtual function defined inside the class B will be class. This is one of the use of virtual function.
11. Which statement is incorrect about virtual function.
A. They are used to achieve runtime polymorphism
B. They are used to hide objects
C. Each virtual function declaration starts with the virtual keyword
D. All of the mentioned
Answer: B
Explanation:
: Virtual function are used to achieve runtime polymorphism by calling the right function during runtime. Their declaration starts with a virtual keyword.
12. The concept of deciding which function to invoke during runtime is called ______________________
A. late binding
B. dynamic linkage
C. static binding
D. both late binding and dynamic linkage
Answer: D
Explanation:
: The concept of deciding which function to invoke during runtime is called late binding or dynamic linkage. Late binding because function binding to the object is done during runtime. Dynamic linkage because this binding is done during runtime.
13. What is a pure virtual function?
A. A virtual function defined inside the base class
B. A virtual function that has no definition relative to the base class
C. A virtual function that is defined inside the derived class
D. Any function that is made virtual
Answer: B
Explanation:
: A virtual function that has no definition relative to the base class is called a pure virtual function.