C++ MCQ (Part-3)
C++ MCQ (Part-3)
11. Which of the following class allows to declare only one object of it?
a) Abstract class
b) Virtual class
c) Singleton class
d) Friend class
View Answer
Answer: C
Explanation: Singleton class allows the programmer to declare only one object of it, If
one tries to declare more than one object the program results into error.
12. Which of the following is not a type of Constructor?
a) Friend constructor
b) Copy constructor
c) Default constructor
d) Parameterized constructor
View Answer
Answer: A
Explanation: Friend function is not a constructor whereas others are a type of
constructor used for object initialization.
13. Which of the following is correct?
View Answer
Answer: B
Explanation: C++ does not allow a derived class pointer to point a base class pointer
whereas Base class can point to a derived class object. Both base class and derived
class can have pointer objects.
14. Out of the following, which is not a member of the class?
a) Static function
b) Friend function
c) Constant function
d) Virtual function
View Answer
Answer: B
Explanation: Friend function is not a member of the class. They are given the same
access rights as the class member function have but they are not actual members of the
class.
15. What is the other name used for functions inside a class?
a) Member variables
b) Member functions
c) Class functions
d) Class variables
View Answer
Answer: B
Explanation: Functions of a class are also known as member functions of a class.
16. Which of the following cannot be a friend?
a) Function
b) Class
c) Object
d) Operator function
View Answer
Answer: C
Explanation: Objects of any class cannot be made a friend of any other or same class
whereas functions, classes and operator functions can be made a friend.
19. How many types of polymorphism are there in C++?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: B
Explanation: There are two types of polymorphism in C++ namely run-time and compile-
time polymorphisms.
20. How run-time polymorphisms are implemented in C++?
a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions
View Answer
Answer: D
Explanation: Run-time polymorphism is implemented using Inheritance and virtual in
which object decides which function to call.
21. How compile-time polymorphisms are implemented in C++?
a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions
View Answer
Answer: C
Explanation: Compile-time polymorphism is implemented using templates in which the
types(which can be checked during compile-time) are used decides which function to be
called.
22. Which of the following is an abstract data type?
a) int
b) float
c) class
d) string
View Answer
Answer: C
Explanation: Class is used as an abstract data type as it can be used to give
implementation independent view whereas no other data type can be used to provide
this.
25. Which of the following approach is used by C++?
a) Top-down
b) Bottom-up
c) Left-right
d) Right-left
View Answer
Answer: B
Explanation: C++ is an object-oriented language and OOL uses a bottom-up approach
to solve/view a problem.
26. Which operator is overloaded for a cout object?
a) >>
b) <<
c) <
d) >
View Answer
Answer: B
Explanation: cout in C++ uses << operator to print anything so << operator is
overloaded for a cout object.
27. Which of the following cannot be used with the virtual keyword?
a) Class
b) Member functions
c) Constructors
d) Destructors
View Answer
Answer: C
Explanation: Virtual keyword cannot be used with constructors as constructors are
defined to initialized an object of particular class hence no other class needs constructor
of other class.
28. Which concept is used to implement late binding?
a) Virtual functions
b) Operator functions
c) Constant functions
d) Static functions
View Answer
Answer: A
Explanation: Virtual functions are used to implement the concept of late binding i.e.
binding actual functions to their calls.
View Answer
Answer: D
Explanation: C++ allows both static and dynamic type checking i.e. types are checked by the compiler.
a) Function overloading
b) Operator overloading
c) Templates
View Answer
Answer: D
Explanation: All the options mentioned above uses static polymorphism mechanism. As the conflicts in
all these types of functions are resolved during compile-time.
a) Multiple
b) Multilevel
c) Distributive
d) Hierarchical
View Answer
Answer: C
Explanation: Distributive is not a type of inheritance whereas others are a type of inheritance having
their own meaning.
a) Multiple
b) Multilevel
c) Distributive
d) Hierarchical
View Answer
Answer: C
Explanation: Distributive is not a type of inheritance whereas others are a type of inheritance having
their own meaning.
View Answer
Answer: D
Explanation: Abstract class should have at least one pure virtual function. Therefore to declare an
abstract class one should declare a pure virtual function in a class.
View Answer
Answer: B
Explanation: An object is an instance of a class i.e. an object represents a class i.e. what class has(data
members) and what it can do(member functions).
a) +
b) |
c) <=
View Answer
Answer: D
#include <iostream>
int main() {
int a, b;
a = 10;
b = 4;
a = b;
b = 7;
return 0;
a) a:4 b:7
b) a:10 b:4
c) a:4 b:10
d) a:4 b:6
View Answer
Answer: A
Explanation: In this program, we are reassigning the values of a and b because of this we got the output
as a:4 b:7
int main() {
int a, b, c;
a = 2;
b = 7;
c = (a > b) ? a : b;
return 0;
a) 2
b) 7
c) 9
d) 14
View Answer
Answer: B
Explanation: We are using the ternary operator to evaluate this expression. It will return
first option, if first condition is true otherwise it will return second
7. What will be the output of the following C++ code?
#include <iostream>
int main() {
int a = 0;
int b = 10;
a = 2;
b = 7;
if (a && b) {
else
return 0;
a) true
b) false
c) error
d) 10
View Answer
Answer: B
Explanation: && is called as Logical AND operator, if there is no zero in the operand
means, it will be true otherwise false.
9. What is the name of | operator?
a) sizeof
b) or
c) and
d) modulus
View Answer
Answer: B
Explanation: | operator is used to find the ‘or’ of given values.
b) Encapsulation
c) Polymorphism
d) Modularity
View Answer
Answer: B
Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++
we call them classes) is called encapsulation.
View Answer
Answer: A
Explanation: In OOPs, Polymorphism is the concept of allowing a user to override functions either by
changing the types or number of parameters passed.
a) Encapsulation
b) Abstraction
c) Inheritance
d) Polymorphism
View Answer
Answer: C
Explanation: Inheritance allows you to reuse your already written code by inheriting the properties of
written code into other parts of the code, hence allowing you to reuse the already written code.
8. C++ is ______________
a) procedural programming language
View Answer
Answer: D
Explanation: C++ supports both procedural(step by step instruction) and object oriented
programming(using concept of classes and objects).
2. What do you call the languages that support classes but not polymorphism?
a) Class based language
b) Procedure Oriented language
c) Object-based language
d) If classes are supported, polymorphism will always be supported
View Answer
Answer: c
Explanation: The languages which support classes but doesn’t support polymorphism, are
known as object-based languages. Polymorphism is such an important feature, that is a
language doesn’t support this feature, it can’t be called as a OOP language.
3. Which among the following is the language which supports classes but not
polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
View Answer
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t support
the polymorphism feature. It is an object-based programming language. Note that it’s not an
OOP language.
1. Operator overloading
2. Function overloading
3. Function overriding
4. B Only
5. A & B
Answer: 5
1. override
2. virtual
3. void
4. none
Answer: 2
1. Increment operator
2. Constructor
3. Destructor
4. New and delete operator
Answer: 3
1. To create an interface
2. To make a class abstract
3. To force derived class to implement the pure virtual function
4. All the above
Answer: 4
All are correct. Recommended to read abstract class in c++ with example that
uses pure virtual function.
1. Constructor
2. Destructor
3. Both A & B
4. Only B
Answer: 3
C++ does not have keyword interface like C# or java. Pure virtual function is used
to create an interface in C++ programs.
1. Function overriding
2. Operator overloading
3. A & B
4. None
Answer: 1
1. Constructor
2. Class destructor
3. Both a & b
4. None
Answer: 2
1. New
2. Delete
3. ++
4. All can be overloaded
Answer: 4
(C) Encapsulation
(D) Overloading
View Answer
Ans: D
Overloading
Many shapes
Question: 3
(B) Inheritance
Operator Overloading
Question: 4
(B) Different
(C) Similar
(D) Complement
View Answer
Ans: A
Same
1. Wrapping data and its related functionality into a single entity is known as _____________
a) Abstraction
b) Encapsulation
c) Polymorphism
d) Modularity
View Answer
Answer: B
Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++
we call them classes) is called encapsulation.
Void operator – ()
Question: 3
Question: 4
(B) Overloaded
(C) Sizeof
(D) Friend
View Answer
Ans; B
Overloaded
9. Which among the following violates the principle of encapsulation almost always?
a) Local variables
b) Global variables
c) Public variables
d) Array variables
View Answer
Answer: b
Explanation: Global variables almost always violates the principles of encapsulation.
Encapsulation says the data should be accessed only by required set of elements. But
global variable is accessible everywhere, also it is most prone to changes. It doesn’t hide
the internal working of program.
1. Car obj;
2. Car *obj = new Car();
3. Only B
4. A & B both
Answer: 4
Both Car obj; and Car *obj = new Car() are valid way to create an object of the
class.
Q) In C++, Class object created statically(e.g. Car obj; and dynamically (Car
*obj = new Car() ; ) are stored in memory
1. Stack, heap
2. Heap, heap
3. Heap, stack
4. Stack, stack
Answer: 1
1. Function
2. Operator
3. Object
4. macro
Answer: 3
Q) Which is Abstract Data Type in C++
1. Class
2. Int
3. Float
4. array
Answer: 1
1. Singleton class
2. Abstract class
3. Friend class
4. All classes
Answer: 1
Q) When you create an object of a class A like A obj ; then which one
will be called automatically
1. Constructor
2. Destructor
3. Copy constructor
4. Assignment operator
Answer: 1
1. Derived class constructor is called first then the base class constructor
2. Base class constructor is called first then derived class constructor
3. base class constructor will not be called
4. none of the above
Answer: 2
Q) The class in C++ which act only as a base class and object of it
cannot be created is
1. parent class
2. super class
3. abstract class
4. none of the above
Answer: 3
1. protected
2. public
3. private
4. None
Answer: 3
1. :
2. ::
3. #
4. None
Answer: 2
1. An interface
2. An Abstract class
3. A singleton class
4. A&B
5. A, B & C
Answer: 4
1. Constructor
2. Destructor
3. Copy constructor
4. Assignment operator
5. All
Answer: 5
Explanation: Base class and derived class come under inheritance, one of the C++
oops principle. Also, it is known as parent child relationship.
Explanation:
1. Virtual constructor
2. Virtual destructor
3. Virtual function
4. Virtual Table
Answer: 1
There is no concept of virtual constructor in C++ programming. Read why virtual
constructor is not possible in C++.
class A{
public:
void f(){
cout<<"A::f()"<<endl;
}
};
class B:public A{
public:
void fb(){
cout<<"A::fb()"<<endl;
};
class C:public A{
public:
void fc(){
cout<<"A::fc()"<<endl;
};
class D: public B,public C{
public:
void fd(){
cout<<"A::fd()"<<endl;
};
int main(){
D obj;
obj.f();
return 0;
1. A::f()
2. A::f() A::f()
3. A::f() A::f()
4. Compiler error
Answer: 4
Explanation: The above class represent the virtual base class / diamond problem
in C++. The object D will try to access the function f() of base class via class B and
class C. as both B and C inherites A. This will lead to ambiguity call the function.
So, compiler will flash an error. i.e. ambiguous access of f().
If we make the class B and C virtual like below, then there would not be any ambiguity and
program will work fine.
class A { public: void f() {} };
class A {
public:
cout<<"function :f()"<<endl;
};
int main(){
A obj1, obj2,obj3;
return 0;
1. 0
2. 1
3. 3
4. 4
Answer: 2
Compiler will create only 1 virtual table for the class. Virtual table does not
depend upon number of objects created.
3. Which among the following can restrict class members to get inherited?
a) Private
b) Protected
c) Public
d) All three
View Answer
Answer: a
Explanation: Private members of a class can’t be inherited. These members can only be
accessible from members of its own class only. It is used to secure the data.
3. Which among the following can restrict class members to get inherited?
a) Private
b) Protected
c) Public
d) All three
View Answer
Answer: a
Explanation: Private members of a class can’t be inherited. These members can only be
accessible from members of its own class only. It is used to secure the data.
5. Which specifier allows a programmer to make the private members which can be
inherited?
a) Private
b) Default
c) Protected
d) Protected and default
View Answer
Answer: c
Explanation: Protected access is used to make the members private. But those members
can be inherited. This gives both security and code reuse capability to a program.
7. If a class has all the private members, which specifier will be used for its implicit
constructor?
a) Private
b) Public
c) Protected
d) Default
View Answer
Answer: b
Explanation: The implicit constructor will always be public. Otherwise the class wouldn’t be
able to have instances. In turn, no objects will be created and the class can only be used for
inheritance.
10. On which specifier’s data, does the size of a class’s object depend?
a) All the data members are added
b) Only private members are added
c) Only public members are added
d) Only default data members are added
View Answer
Answer: a
Explanation: All the data members are counted to calculate the size of an object of a class.
The data member access specifier doesn’t play any role here. Hence all the data size will
be added.
13. Which access specifier should be used so that all the parent class members can be
inherited and accessed from outside the class?
a) Private
b) Default or public
c) Protected or private
d) Public
View Answer
Answer: d
Explanation: All the members must be of public access. So that the members can be
inherited easily. Also, the members will be available from outside the class.
class A
{
int marks;
public : disp()
{
cout<<marks;
}
}
class B: protected A
{
char name[20];
}
A a; a.disp();
B b; b.disp();
4. Which among the following can be used for outermost class access specifier in java?
a) Private
b) Public
c) Default
d) Default or Public
View Answer
Answer: d
Explanation: Either default or public access specifier must be used for outermost classes.
Private can be used with inner classes. This is done so that all the members can access
and use the utmost class and that program execution can be done from anywhere. Inner
classes can be made private for security.
7. How many public class(s) (outermost) can be there in a java program?
a) 1
b) 2
c) 3
d) As required
View Answer
Answer: a
Explanation: There can be only one public class in a java program. The public class name
must match the name of file. And there can’t be more than one class with same name in a
single program in same scope. Hence it is not possible to have more than one public class
in java program.
package pack1;
class A
{
public A()
{
System.out.print(“object created”);
}
}
package pack2;
import pack1.*;
class B
{
A a=new A();
}
15. Which specifier allows to secure the public members of base class in inherited classes?
a) Private
b) Protected
c) Public
d) Private and Protected
View Answer
Answer: d
Explanation: Both the private and protected specifiers can make the public members of the
base class more secure. This is useful if we stop using the parent class members and use
the classes which inherited the parent class, so as to secure data better.
4. In which access should a constructor be defined, so that object of the class can be
created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explanation: Constructor function should be available to all the parts of program where the
object is to be created. Hence it is advised to define it in public access, so that any other
function is able to create objects.
4. In which access should a constructor be defined, so that object of the class can be
created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explanation: Constructor function should be available to all the parts of program where the
object is to be created. Hence it is advised to define it in public access, so that any other
function is able to create objects.
5. How many types of constructors are available for use in general (with respect to
parameters)?
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: a
Explanation: Two types of constructors are defined generally, namely, default constructor
and parameterized constructor. Default constructor is not necessary to be defined always.
8. If class C inherits class B. And B has inherited class A. Then while creating the object of
class C, what will be the sequence of constructors getting called?
a) Constructor of C then B, finally of A
b) Constructor of A then C, finally of B
c) Constructor of C then A, finally B
d) Constructor of A then B, finally C
View Answer
Answer: d
Explanation: While creating the object of class C, its constructor would be called by default.
But, if the class is inheriting some other class, firstly the parent class constructor will be
called so that all the data is initialized that is being inherited.
. In multiple inheritance, if class C inherits two classes A and B as follows, which class
constructor will be called first?
class A{ };
class B{ };
class C: public A, public B{ };
a) A()
b) B()
c) C()
d) Can’t be determined
View Answer
Answer: a
Explanation: Constructor of class A will be called first. This is because the constructors in
multiple inheritance are called in the sequence in which they are written to be inherited.
Here A is written first, hence it is called first.
class student
{
int marks;
};
student s1, s2, s3;
a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same time
View Answer
Answer: a
Explanation: The objects are created in the sequence of how they are written. This happens
because the constructors are called in the sequence of how the objects are mentioned. This
is done in sequence.
15. For constructor overloading, each constructor must differ in ___________ and
__________
a) Number of arguments and type of arguments
b) Number of arguments and return type
c) Return type and type of arguments
d) Return type and definition
View Answer
Answer: a
Explanation: Each constructor must differ in the number of arguments it accepts and the
type of arguments. This actually defines the constructor signature. This helps to remove the
ambiguity and define a unique constructor as required.
4. If in multiple inheritance, class C inherits class B, and Class B inherits class A. In which
sequence are their destructors called if an object of class C was declared?
a) ~C() then ~B() then ~A()
b) ~B() then ~C() then ~A()
c) ~A() then ~B() then ~C()
d) ~C() then ~A() then ~B()
View Answer
Answer: a
Explanation: The destructors are always called in the reverse order of how the constructors
were called. Here class A constructor would have been created first if Class C object is
declared. Hence class A destructor is called at last.
5. Choose the correct sequence of destructors being called for the following code.
class A{ };
class B{ };
class C: public A, public B{ };
class A{ };
class B{ };
class C: public B{ };
A a;
B b;
C c;
a) ~A()
b) ~B()
c) ~C()
d) ~B() and ~C()
View Answer
Answer: c
Explanation: The constructor that would have created at last, its destructor will be called first
when the code goes out of scope. This will help the program to manage the resources more
efficiently.
C++ Programming Questions and Answers – Operator
Overloading – 2
This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on
“Operator Overloading – 2”.
int i;
int j;
public:
complex(int a, int b)
{
i = a;
j = b;
}
complex operator+(complex c)
{
complex temp;
temp.i = this->i + c.i;
temp.j = this->j + c.j;
return temp;
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
}
};
Answer: c
Explanation: In the operator overloaded function we are trying to call default constructor of
the class complex but as we have overridden the constructor by our constructor therefore
the default constructor cannot be called hence the program gives error.
A
9. Choose the output of the following C++ code?
C++
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 class complex
5 {
6 int i;
7 int j;
8 public:
9 complex(int a, int b)
10 {
11 i = a;
12 j = b;
13 }
14 complex operator+(complex c)
15 {
16 complex temp;
17 temp.i = this->i + c.i;
18 temp.j = this->j + c.j;
19 return temp;
20 }
21 void show(){
22 cout<<"Please note it that Complex Number is: "<<i<<" + i"<<j<<endl;
23 }
24 };
25 int main(int argc, char const *argv[])
26 {
27 complex c1(1,2);
28 complex c2(3,4);
29 complex c3 = c1 + c2;
30 c3.show();
31 return 0;
32 }
A. Error
B. 4 + i6
C. Segmentation fault
D. 2 + i2
Answer - Click Here:
A
0. ________ operators cannot be overloaded?
A. The [ ] operator.
B. The -> operator.
C. The . operator.
D. The & operator
Answer - Click Here:
C
1.In flowchart rectangle symbol indicates:
a.Input/Output
b.Connector
c.Process
d.Decision
Answer - Click Here:
C
2.C++ is a(n):
a.Object-oriented programming language.
b.Event-driven programming language.
c.Structured programming language.
d.None of these.
Answer - Click Here:
A
3.The arithmetic operators are:
a.Ternary operators
b.Unary operators
c.Binary operators
d.None of these
Answer - Click Here:
C
4.THe escape sequence for carrige return is:
a./t
b./f
c./n
d./r
Answer - Click Here:
D
5.In switch structure, each case label may be an integer constant or:
a.Real constant
b.Character constant
c.String constant
d.None of these
Answer - Click Here:
B
6.WHat will be the value of ‘x’ after executing for(x=1;x<=15;x++);?
a.15
b.1
c.14
d.16
Answer - Click Here:
C
7.The pointers are used for implementing the concept of:
a.Polymorphism
b.Array
c.Structure
d.Inheritance
Answer - Click Here:
A
8.The function stlen(“ABC”);will return value:
a.4
b.ABC
c.3
d.None of these
Answer - Click Here:
C
9.The data item of a structured are called:
a.Fields
b.Elements
c.Members
d.ALl of these
Answer - Click Here:
D
10.In C++,the functions of a class are called:
a.Attributes
b.Methods
c.Member function
d.Both a and b
Answer - Click Here:
C
D
13.Polymorphism is achieved through:
a.destructor
b.constructor
c.virtual function
d.overloading operator
Answer - Click Here:
C
14.Which of the following strem class is used to perform both input and output
file operation:
a.ofstream
b.ifstream
c.fstream
d.iostream
Answer - Click Here:
C