OOP
OOP
Answer: c
Explana on: Alan Kay invented OOP, Andrea Ferro was a part of SmallTalk Development. Dennis
invented C++ and Adele Goldberg was in team to develop SmallTalk but Alan actually had got rewarded
for OOP.
Answer: d
Explana on: Duplicate/Redundant data is dependent on programmer and hence can’t be guaranteed by
OOP. Code reusability is done using inheritance. Modularity is supported by using different code files
and classes. Codes are more efficient because of features of OOP.
3. Which was the first purely object oriented programming language developed?
a) Kotlin
b) SmallTalk
c) Java
d) C++
View Answer
Answer: b
Explana on: SmallTalk was the first programming language developed which was purely object oriented.
It was developed by Alan Kay. OOP concept came into the picture in 1970’s.
Answer: c
Explana on: OOP first came into picture in 1970’s by Alan and his team. Later it was used by some
programming languages and got implemented successfully, SmallTalk was first language to use pure
OOP and followed all rules strictly.
Answer: d
Explana on: Inheritance indicates the code reusability. Encapsula on and abstrac on are meant to
hide/group data into one element. Polymorphism is to indicate different tasks performed by a single
en ty.
adver sement
Answer: a
Explana on: We need not include any specific header file to use OOP concept in C++, only specific
func ons used in code need their respec ve header files to be included or classes should be defined if
needed.
Answer: b
Explana on: As Java supports usual declara on of data variables, it is par al implementa on of OOP.
Because according to rules of OOP, object constructors must be used, even for declara on of variables.
Answer: b
Explana on: Firstly, keyword class should come, followed by the derived class name. Colon is must
followed by access in which base class has to be derived, followed by the base class name. And finally
the body of class. Semicolon a er the body is also must.
Answer: a
Explana on: Encapsula on is indicated by use of classes. Inheritance is shown by inheri ng the student
class into topper class. Polymorphism is not shown here because we have defined the constructor in the
topper class but that doesn’t mean that default constructor is overloaded.
11. The feature by which one object can interact with another object is _____________
a) Message reading
b) Message Passing
c) Data transfer
d) Data Binding
View Answer
Answer: b
Explana on: The interac on between two object is called the message passing feature. Data transfer is
not a feature of OOP. Also, message reading is not a feature of OOP.
12. Which among the following, for a pure OOP language, is true?
a) The language should follow at least 1 feature of OOP
b) The language must follow only 3 features of OOP
c) The language must follow all the rules of OOP
d) The language should follow 3 or more features of OOP
View Answer
Answer: c
Explana on: The language must follow all the rules of OOP to be called a purely OOP language. Even if a
single OOP feature is not followed, then it’s known to be a par ally OOP language.
13. How many types of access specifiers are provided in OOP (C++)?
a) 4
b) 3
c) 2
d) 1
View Answer
Answer: b
Explana on: Only 3 types of access specifiers are available. Namely, private, protected and public. All
these three can be used according to the need of security of members.
14. In mul level inheritance, which is the most significant feature of OOP used?
a) Code efficiency
b) Code readability
c) Flexibility
d) Code reusability
View Answer
Answer: d
Explana on: The classes using mul level inheritance will use the code in all the subsequent subclasses if
available. Hence the most significant feature among the op ons given is code reusability. This feature is
generally intended to use the data values and reuse the redundant func ons.
Answer: a
Explana on: It is a way of combining both data members and member func ons, which operate on
those data members, into a single unit. We call it a class in OOP generally. This feature have helped us
modify the structures used in C language to be upgraded into class in C++ and other languages.
Answer: b
Explana on: It never increases func on defini on overhead, one way or another if you don’t use
polymorphism, you will use the defini on in some other way, so it actually helps to write efficient codes.
17. Which constructor will be called from the object created in the below C++ code?
class A
int i;
A()
i=0; cout<<i;
A(int x=0)
i=x; cout<<I;
};
A obj1;
a) Parameterized constructor
b) Default constructor
c) Run me error
d) Compile me error
View Answer
Answer: d
Explana on: When a default constructor is defined and another constructor with 1 default value
argument is defined, crea ng object without parameter will create ambiguity for the compiler. The
compiler won’t be able to decide which constructor should be called, hence compile me error.
Answer: b
Explana on: Only inser on operator can be overloaded among all the given op ons. And the
polymorphism can be illustrated here only if any of these is applicable of being overloaded. Overloading
is type of polymorphism.
20. In which access should a constructor be defined, so that object of the class can be created in any
func on?
a) Any access specifier will work
b) Private
c) Public
d) Protected
View Answer
Answer: c
Explana on: Constructor func on 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 func on is able to create
objects.
21. Which among the following is correct for the class defined below?
class student
int marks;
public: student(){}
student(int x)
marks=x;
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
Answer: d
Explana on: It is a special case of constructor with only 1 argument. While calling a constructor with one
argument, you are actually implicitly crea ng a conversion from the argument type to the type of class.
Hence you can directly specify the value of that one argument with assignment operator.
Answer: c
Explana on: When an object is passed to a func on, actually its copy is made in the func on. To copy
the values, copy constructor is used. Hence the object being passed and object being used in func on
are different.
23. Which constructor will be called from the object obj2 in the following C++ program?
class A
int i;
A()
i=0;
A(int x)
{
i=x+1;
A(int y, int x)
i=x+y;
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int y, int x)
b) A(int y; int x)
c) A(int y)
d) A(int x)
View Answer
Answer: a
Explana on: The two argument constructor will be called as we are passing 2 arguments to the object
while crea on. The arguments will be passed together and hence compiler resolves that two argument
constructor have to be called.
Answer: b
Explana on: The constructors must contain only the class name. The class name is followed by the blank
parenthesis or we can have parameters if some values are to be passed.
26. Which access specifier is usually used for data members of a class?
a) Protected
b) Private
c) Public
d) Default
View Answer
Answer: b
Explana on: All the data members should be made private to ensure the highest security of data. In
special cases we can use public or protected access, but it is advised to keep the data members private
always.
Answer: d
Explana on: The data members can never be called directly. Dot operator is used to access the
members with help of object of class. Arrow is usually used if pointers are used.
Answer: a
Explana on: Using inheritance we can have the security of the class being inherited. The subclass can
access the members of parent class. And have more feature than a nested class being used.
29. Which keyword among the following can be used to declare an array of objects in java?
a) allocate
b) arr
c) new
d) create
View Answer
Answer: c
Explana on: The keyword new can be used to declare an array of objects in java. The syntax must be
specified with an object pointer which is assigned with a memory space containing the required number
of object space. Even ini aliza on can be done directly.
30. Which operator can be used to free the memory allocated for an object in C++?
a) Unallocate
b) Free()
c) Collect
d) delete
View Answer
Answer: d
Explana on: The delete operator in C++ can be used to free the memory and resources held by an
object. The func on can be called explicitly whenever required. In C++ memory management must be
done by the programmer. There is no automa c memory management in C++.
Answer: b
Explana on: The names are not property of an object. The iden ty can be in any form like address or
name of object but name can’t be termed as only iden ty of an object. The objects contain a ributes
that define what type of data an object can store.
32. Which type of members can’t be accessed in derived classes of a base class?
a) All can be accessed
b) Protected
c) Private
d) Public
View Answer
Answer: c
Explana on: The private members can be accessed only inside the base class. If the class is derived by
other classes. Those members will not be accessible. This concept of OOP is made to make the members
more secure.
Answer: a
Explana on: It can only be indicated by using the data and func ons that we use in derived class, being
provided by parent class. Copying code is nowhere similar to this concept, also using the code already
wri en is same as copying. Using already defined func ons is not inheritance as we are not adding any
of our own features.
Answer: d
Explana on: The run me inheritance is done when object of a class is created to call a method. At
run me the func on is searched if it is in class of object. If not, it will search in its parent classes and
hierarchy for that method.
Answer: b
Explana on: To overcome the ambiguity and conflict we can use keyword virtual. This will help us to
differen ate the func ons with same name that came to last derived class in diamond problem.
Answer: c
Explana on: The virtual keyword is used to declare virtual func ons. Anonymous keyword is used with
classes and have a different meaning. The virtual func ons are used to call the intended func on of the
derived class.
37. What happens if non sta c members are used in sta c member func on?
a) Executes fine
b) Compile me error
c) Executes if that member func on is not used
d) Run me error
View Answer
Answer: b
Explana on: There must be specific memory space allocated for the data members before the sta c
member func ons uses them. But the space is not reserved if object is not declared. Hence only if sta c
members are not used, it leads to compile me error.
Answer: a
Explana on: A non-member func on of a class which can access even the private data of a class is a
friend func on. It is an excep on on access to private members outside the class. It is some mes
considered as a member func ons since it has all the access that a member func on in general have.
Answer: d
Explana on: The memory for the objects or any other data is allocated in RAM ini ally. This is while we
run a program all the memory alloca on takes place in some RAM segments. Arrays in heap and local
members in stack etc.
Answer: c
Explana on: The member func on which is defined in base class and again in the derived class, is
overridden by the defini on given in the derived class. This is because the preference is given more to
the local members. When derived class object calls that func on, defini on from the derived class is
used.
Answer: a
Explana on: The polymorphism feature is exhibited by func on overriding. Polymorphism is the feature
which basically defines that same named func ons can have more than one func onali es.
Answer: d
Explana on: Even the private member func ons can be called outside the class. This is possible if
address of the func on is known. We can use the address to call the func on outside the class.
Answer: c
Explana on: The keyword used to declare sta c variables is sta c. This is must be used while declaring
the sta c variables. The compiler can make variables sta c if and only if they are men oned with sta c
keyword.
46. Which class/set of classes can illustrate polymorphism in the following C++ code?
calc_grade();
public : calc_grade()
return 10;
};
public : calc_grade()
return 20;
};
Answer: d
Explana on: Since Student class is abstract class and class topper and average are inheri ng student,
class topper and average must define the func on named calc_grade(); in abstract class. Since both the
defini on are different in those classes, calc_grade() will work in different way for same input from
different objects. Hence it shows polymorphism.
47. If data members are private, what can we do to access them from the class object?
a) Private data members can never be accessed from outside the class
b) Create public member func ons to access those data members
c) Create private member func ons to access those data members
d) Create protected member func ons to access those data members
View Answer
Answer: b
Explana on: We can define public member func ons to access those private data members and get
their value for use or altera on. They can’t be accessed directly but is possible to be access using
member func ons. This is done to ensure that the private data doesn’t get modified accidentally.
48. Which among the following is not a necessary condi on for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a defini on body
d) It can contains arguments
View Answer
Answer: c
Explana on: Constructors are predefined implicitly, even if the programmer doesn’t define any of them.
Even if the programmer declares a constructor, it’s not necessary that it must contain some defini on.
Answer: d
Explana on: This is mandatory to pass the object by reference. Otherwise, the object will try to create
another object to copy its values, in turn a constructor will be called, and this will keep on calling itself.
This will cause the compiler to give out of memory error.
50. If in mul ple 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) ~A() then ~B() then ~C()
b) ~C() then ~A() then ~B()
c) ~C() then ~B() then ~A()
d) ~B() then ~C() then ~A()
View Answer
Answer: c
Explana on: 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.
Answer: b
Explana on: Instance of abstract class can’t be created as it will not have any constructor of its own,
hence while crea ng an instance of class, it can’t ini alize the object members. Actually the class
inheri ng the abstract class can have its instance because it will have implementa on of all members.
Answer: a
Explana on: Virtual Func ons can be defined in any class using the keyword virtual. All the classes which
inherit the class containing the virtual func on, define the virtual func on as required. Redefining the
func on on all the derived classes according to class and use represents polymorphism.
53. Which feature in OOP is used to allocate addi onal func ons to a predefined operator in any
language?
a) Func on Overloading
b) Func on Overriding
c) Operator Overloading
d) Operator Overriding
View Answer
Answer: c
Explana on: The feature is operator overloading. There is not a feature named operator overriding
specifically. Func on overloading and overriding doesn’t give addi on func on to any operator.
This set of Object Oriented Programming using C++ online test focuses on “OOP Basic Concepts”.
1. Which was the first purely object oriented programming language developed?
a) Java
b) C++
c) SmallTalk
d) Kotlin
View Answer
Answer: c
Explana on: SmallTalk was the first programming language developed which was purely object oriented.
It was developed by Alan Kay. OOP concept came into the picture in 1970’s.
Answer: c
Explana on: A class is Blueprint of an object which describes/ shows all the func ons and data that are
provided by an object of a specific class. It can’t be called as parent or instance of an object. Class in
general describes all the proper es of an object.
Answer: a
Explana on: Alan Kay invented OOP, Andrea Ferro was a part of SmallTalk Development. Dennis
invented C++ and Adele Goldberg was in team to develop SmallTalk but Alan actually had got rewarded
for OOP.
adver sement
4. What is the addi onal feature in classes that was not in structures?
a) Data members
b) Member func ons
c) Sta c data allowed
d) Public access specifier
View Answer
Answer: b
Explana on: Member func ons are allowed inside a class but were not present in structure concept.
Data members, sta c data and public access specifiers were present in structures too.
Answer: c
Explana on: Duplicate/Redundant data is dependent on programmer and hence can’t be guaranteed by
OOP. Code reusability is done using inheritance. Modularity is supported by using different code files
and classes. Codes are more efficient because of features of OOP.
Subscribe Now: Object Oriented Programming C++ Newsle er | Important Subjects Newsle ers
6. Pure OOP can be implemented without using class in a program. (True or False)
a) True
b) False
View Answer
Answer: b
Explana on: It’s false because for a program to be pure OO, everything must be wri en inside classes. If
this rule is violated, the program can’t be labelled as purely OO.
Answer: d
Explana on: Using inheritance we can reuse the code already wri en and also can avoid crea on of
many new func ons or variables, as that can be done one me and be reused, using classes.
Answer: b
Explana on: Java doesn’t support all 4 types of inheritance. It doesn’t support mul ple inheritance. But
the mul ple inheritance can be implemented using interfaces in Java.
Answer: d
Explana on: Any number of classes can be defined inside a program, provided that their names are
different. In java, if public class is present then it must have the same name as that of file.
Answer: a
Explana on: OOP first came into picture in 1970’s by Alan and his team. Later it was used by some
programming languages and got implemented successfully, SmallTalk was first language to use pure
OOP and followed all rules strictly.
Answer: a
Explana on: As Java supports usual declara on of data variables, it is par al implementa on of OOP.
Because according to rules of OOP, object constructors must be used, even for declara on of variables.
Answer: d
Explana on: We need not include any specific header file to use OOP concept in C++, only specific
func ons used in code need their respec ve header files to be included or classes should be defined if
needed.
Answer: c
Explana on: Encapsula on and Abstrac on are similar features. Encapsula on is actually binding all the
proper es in a single class or we can say hiding all the features of object inside a class. And Abstrac on
is hiding unwanted data (for user) and showing only the data required by the user of program.
Answer: a
Explana on: Use of this pointer allows an object to call data and methods of itself whenever needed.
This helps us call the members of an object recursively, and differen ate the variables of different
scopes.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Classes”.
1. Which of the following is not type of class?
a) Abstract Class
b) Final Class
c) Start Class
d) String Class
View Answer
Answer: c
Explana on: Only 9 types of classes are provided in general, namely, abstract, final, mutable, wrapper,
anonymous, input-output, string, system, network. We may further divide the classes into parent class
and subclass if inheritance is used.
Answer: b
Explana on: Classes are pass by reference, and the structures are pass by copy. It doesn’t depend on the
program.
3. What is default access specifier for data members or member func ons declared within a class
without any specifier, in C++?
a) Private
b) Protected
c) Public
d) Depends on compiler
View Answer
Answer: a
Explana on: The data members and member func ons are Private by default in C++ classes, if none of
the access specifier is used. It is actually made to increase the privacy of data.
adver sement
class Student
int a;
public : float a;
};
a) Error : same variable name can’t be used twice
b) Error : Public must come first
c) Error : data types are different for same variable
d) It is correct
View Answer
Answer: a
Explana on: Same variable can’t be defined twice in same scope. Even if the data types are different,
variable name must be different. There is no rule like Public member should come first or last.
Answer: c
Explana on: Template classes are known to be generic classes because those can be used for any data
type value and the same class can be used for all the variables of different data types.
Answer: d
Explana on: Classes doesn’t have any size, actually the size of object of the class can be defined. That is
done only when an object is created and its constructor is called.
7. Which class can have member func ons without their implementa on?
a) Default class
b) String class
c) Template class
d) Abstract class
View Answer
Answer: d
Explana on: Abstract classes can have member func ons with no implementa on, where the inheri ng
subclasses must implement those func ons.
Answer: a
Explana on: A friend class can access all the private members of another class, of which it is a friend. It
is a special class provided to use when you need to reuse the data of a class but don’t want that class to
have those special func ons.
Answer: d
Explana on: It depends on the access specifier and the type of inheritance used with the class, because
if the class is inherited then the nested class can be used by subclass too, provided it’s not of private
type.
Answer: a
Explana on: The class containing main func on can be inherited and hence the program can be
executed using the derived class names also in java.
11. Which among the following is false, for a member func on of a class?
a) All member func ons must be defined
b) Member func ons can be defined inside or outside the class body
c) Member func ons need not be declared inside the class defini on
d) Member func ons can be made friend to another class using the friend keyword
View Answer
Answer: c
Explana on: Member func ons must be declared inside class body, though the defini on can be given
outside the class body. There is no way to declare the member func ons outside the class.
Answer: b
Explana on: Class and structure are similar to each other. Only major difference is that a structure
doesn’t have member func ons whereas the class can have both data members and member func ons.
Answer: b
Explana on: Class defini on must end with a semicolon, not colon. Class can have only member
func ons in its body with no data members.
Answer: d
Explana on: Instance of abstract class can’t be created as it will not have any constructor of its own,
hence while crea ng an instance of class, it can’t ini alize the object members. Actually the class
inheri ng the abstract class can have its instance because it will have implementa on of all members.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Objects”.
Answer: d
Explana on: You can create as many objects of a specific class as you want, provided enough memory is
available.
Answer: c
Explana on: Objects can be passed by reference. Objects can be passed by value also. If the object of a
class is not created, we can’t use members of that class.
adver sement
Answer: c
Explana on: The array must be specified with a size. You can’t declare object array, or any other linear
array without specifying its size. It’s a mandatory field.
Answer: d
Explana on: Object can’t be passed as func on as it is an instance of some class, it’s not a func on.
Object can be passed by reference, value or copy. There is no term defined as pass as func on for
objects.
Subscribe Now: Object Oriented Programming C++ Newsle er | Important Subjects Newsle ers
a) 20
b) 22
c) 24
d) 28
View Answer
Answer: c
Explana on: The size of any object of student class will be of size 4+20=24, because sta c members are
not really considered as property of a single object. So sta c variables size will not be added.
Answer: b
Explana on: Func ons can always return an object if the return type is same as that of object being
returned. Care has to be taken while wri ng the prototype of the func on.
Answer: a
Explana on: Using dot operator a er the name of object we can access its members. It is not necessary
to use the pointers. We can’t use the names directly because it may be used outside the class.
9. If a local class is defined in a func on, which of the following is true for an object of that class?
a) Object is accessible outside the func on
b) Object can be declared inside any other func on
c) Object can be used to call other class members
d) Object can be used/accessed/declared locally in that func on
View Answer
Answer: d
Explana on: For an object which belongs to a local class, it is mandatory to declare and use the object
within the func on because the class is accessible locally within the class only.
10. Which among the following is wrong?
a) class student{ }; student s;
b) abstract class student{ }; student s;
c) abstract class student{ }s[50000000];
d) abstract class student{ }; class toppers: public student{ }; topper t;
View Answer
Answer: b
Explana on: We can never create instance of an abstract class. Abstract classes doesn’t have
constructors and hence when an instance is created there is no facility to ini alize its members. Op on d
is correct because topper class is inheri ng the base abstract class student, and hence topper class
object can be created easily.
Answer: c
Explana on: The object declared in main() have local scope inside main() func on only. It can’t be used
outside main() func on. Scope resolu on operator is used to access globally declared variables/objects.
Answer: a
Explana on: A temporary object is created to return the value. It is created because the object used in
func on is destroyed as soon as the func on is returned. The temporary variable returns the value and
then gets destroyed.
Answer: c
Explana on: Only if the objects are of same class then their data can be copied from to another using
assignment operator. This actually comes under operator overloading. Class constructors can’t be
assigned any explicit value as in op on class student{ }s1; class topper{ }t1; s1=t1; and class student{ }s1;
class topper{ }t1; s1.student()=s2.topper();.
14. Which among following is correct for ini alizing the class below?
class student{
int marks;
int cgpa;
marks=I;
cgpa=j
};
Answer: b
Explana on: It is the way we can ini alize the data members for an object array using parameterized
constructor. We can do this to pass our own intended values to ini alize the object array data.
15. Object can’t be used with pointers because they belong to user defined class, and compiler can’t
decide the type of data may be used inside the class.
a) True
b) False
View Answer
Answer: b
Explana on: The explana on given is wrong because object can always be used with pointers like with
any other variables. Compiler doesn’t have to know the structure of the class to use a pointer because
the pointers only points to a memory address/stores that address.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “OOP Features”.
Answer: b
Explana on: Inheritance indicates the code reusability. Encapsula on and abstrac on are meant to
hide/group data into one element. Polymorphism is to indicate different tasks performed by a single
en ty.
2. If a func on can perform more than 1 type of tasks, where the func on name remains same, which
feature of OOP is used here?
a) Encapsula on
b) Inheritance
c) Polymorphism
d) Abstrac on
View Answer
Answer: c
Explana on: For the feature given above, the OOP feature used is Polymorphism. Example of
polymorphism in real life is a kid, who can be a student, a son, a brother depending on where he is.
3. If different proper es and func ons of a real world en ty is grouped or embedded into a single
element, what is it called in OOP language?
a) Inheritance
b) Polymorphism
c) Abstrac on
d) Encapsula on
View Answer
Answer: d
Explana on: It is Encapsula on, which groups different proper es and func ons of a real world en ty
into single element. Abstrac on, on other hand, is hiding of func onal or exact working of codes and
showing only the things which are required by the user.
adver sement
Answer: c
Explana on: Data must be declared using objects. Object usage is mandatory because it in turn calls its
constructors, which in turn must have a class defined. If object is not used, it is a viola on of pure OOP
concept.
5. Which among the following doesn’t come under OOP concept?
a) Pla orm independent
b) Data binding
c) Message passing
d) Data hiding
View Answer
Answer: a
Explana on: Pla orm independence is not feature of OOP. C++ supports OOP but it’s not a pla orm
independent language. Pla orm independence depends on programming language.
a) Inheritance
b) Polymorphism
c) Inheritance and polymorphism
d) Encapsula on and Inheritance
View Answer
Answer: d
Explana on: Encapsula on is indicated by use of classes. Inheritance is shown by inheri ng the student
class into topper class. Polymorphism is not shown here because we have defined the constructor in the
topper class but that doesn’t mean that default constructor is overloaded.
Answer: d
Explana on: All the features are violated because Inheritance and Encapsula on won’t be implemented.
Polymorphism and Abstrac on are s ll possible in some cases, but the main features like data binding,
object use and etc won’t be used hence the use of class is must for OOP concept.
8. How many basic features of OOP are required for a programming language to be purely OOP?
a) 7
b) 6
c) 5
d) 4
View Answer
Answer: a
Explana on: There are 7 basic features that define whether a programing language is pure OOP or not.
The 4 basic features are inheritance, polymorphism, encapsula on and abstrac on. Further, one is,
object use is must, secondly, message passing and lastly, Dynamic binding.
9. The feature by which one object can interact with another object is _____________
a) Data transfer
b) Data Binding
c) Message Passing
d) Message reading
View Answer
Answer: c
Explana on: The interac on between two object is called the message passing feature. Data transfer is
not a feature of OOP. Also, message reading is not a feature of OOP.
Answer: d
Explana on: Virtual Func ons can be defined in any class using the keyword virtual. All the classes which
inherit the class containing the virtual func on, define the virtual func on as required. Redefining the
func on on all the derived classes according to class and use represents polymorphism.
11. Which feature in OOP is used to allocate addi onal func on to a predefined operator in any
language?
a) Operator Overloading
b) Func on Overloading
c) Operator Overriding
d) Func on Overriding
View Answer
Answer: a
Explana on: The feature is operator overloading. There is not a feature named operator overriding
specifically. Func on overloading and overriding doesn’t give addi on func on to any operator.
Answer: a
Explana on: Excep on handling is a feature of OOP as it includes classes concept in most of the cases.
Also it may come handy while using inheritance.
14. Which among the following, for a pure OOP language, is true?
a) The language should follow 3 or more features of OOP
b) The language should follow at least 1 feature of OOP
c) The language must follow only 3 features of OOP
d) The language must follow all the rules of OOP
View Answer
Answer: d
Explana on: The language must follow all the rules of OOP to be called a purely OOP language. Even if a
single OOP feature is not followed, then it’s known to be a par ally OOP language.
Answer: a
Explana on: It is always true as we have the facility of private and protected access specifiers. Also, only
the public and global data are available globally or else the program should have proper permission to
access the private data.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Polymorphism”.
Answer: a
Explana on: It is actually the ability for a message / data to be processed in more than one form. The
word polymorphism indicates many-forms. So if a single en ty takes more than one form, it is known as
polymorphism.
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
Explana on: 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
Explana on: 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.
adver sement
4. If same message is passed to objects of several different classes and all of those can respond in a
different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
View Answer
Answer: c
Explana on: The feature defined in ques on defines polymorphism features. Here the different objects
are capable of responding to the same message in different ways, hence polymorphism.
Subscribe Now: Object Oriented Programming C++ Newsle er | Important Subjects Newsle ers
abstract class student
calc_grade();
public : calc_grade()
return 10;
};
public : calc_grade()
return 20;
};
Answer: c
Explana on: Since Student class is abstract class and class topper and average are inheri ng student,
class topper and average must define the func on named calc_grade(); in abstract class. Since both the
defini on are different in those classes, calc_grade() will work in different way for same input from
different objects. Hence it shows polymorphism.
Answer: b
Explana on: Only virtual func ons among these can show polymorphism. Class member func ons can
show polymorphism too but we should be sure that the same func on is being overloaded or is a
func on of abstract class or something like this, since we are not sure about all these, we can’t say
whether it can show polymorphism or not.
7. In case of using abstract class or func on overloading, which func on is supposed to be called first?
a) Local func on
b) Func on with highest priority in compiler
c) Global func on
d) Func on with lowest priority because it might have been halted since long me, because of low
priority
View Answer
Answer: b
Explana on: Func on with highest priority is called. Here, it’s not about the thread scheduling in CPU,
but it focuses on whether the func on in local scope is present or not, or if scope resolu on is used in
some way, or if the func on matches the argument signature. So all these things define which func on
has the highest priority to be called in run me. Local func on could be one of the answer but we can’t
say if someone have used pointer to another func on or same func on name.
Answer: a
Explana on: Sta c member func ons are not property of any object. Hence it can’t be considered for
overloading/overriding. For polymorphism, func on must be property of object, not only of class.
class student
void disp()
public :
void disp()
s.disp();
t.disp();
Answer: a
Explana on: You need to focus on how the output is going to be shown, no space will be given a er first
message from base class. And then the message from derived class will be printed. Func on disp() in
base class overrides the func on of base class being derived.
Answer: c
Explana on: Only inser on operator can be overloaded among all the given op ons. And the
polymorphism can be illustrated here only if any of these is applicable of being overloaded. Overloading
is type of polymorphism.
class educa on
{
char name[10];
public : disp()
};
void main()
school s;
s.disp();
Answer: a
Explana on: No ce that the func on name in derived class is different from the func on name in base
class. Hence when we call the disp() func on, base class func on is executed. No polymorphism is used
here.
13. Which problem may arise if we use abstract class func ons for polymorphism?
a) All classes are converted as abstract class
b) Derived class must be of abstract type
c) All the derived classes must implement the undefined func ons
d) Derived classes can’t redefine the func on
View Answer
Answer: c
Explana on: The undefined func ons must be defined is a problem, because one may need to
implement few undefined func ons from abstract class, but he will have to define each of the func ons
declared in abstract class. Being useless task, it is a problem some mes.
Answer: d
Explana on: It never increases func on defini on overhead, one way or another if you don’t use
polymorphism, you will use the defini on in some other way, so it actually helps to write efficient codes.
15. If 2 classes derive one base class and redefine a func on of base class, also overload some operators
inside class body. Among these two things of func on and operator overloading, where is polymorphism
used?
a) Func on overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either func on overloading or operator overloading because polymorphism can be applied only once
in a program
View Answer
Answer: c
Explana on: Both of them are using polymorphism. It is not necessary that polymorphism can be used
only once in a program, it can be used anywhere, any number of mes in a single program.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Encapsula on”.
1. Which among the following best describes encapsula on?
a) It is a way of combining various data members into a single unit
b) It is a way of combining various member func ons into a single unit
c) It is a way of combining various data members and member func ons into a single unit which can
operate on any data
d) It is a way of combining various data members and member func ons that operate on those data
members into a single unit
View Answer
Answer: d
Explana on: It is a way of combining both data members and member func ons, which operate on
those data members, into a single unit. We call it a class in OOP generally. This feature have helped us
modify the structures used in C language to be upgraded into class in C++ and other languages.
2. If data members are private, what can we do to access them from the class object?
a) Create public member func ons to access those data members
b) Create private member func ons to access those data members
c) Create protected member func ons to access those data members
d) Private data members can never be accessed from outside the class
View Answer
Answer: a
Explana on: We can define public member func ons to access those private data members and get
their value for use or altera on. They can’t be accessed directly but is possible to be access using
member func ons. This is done to ensure that the private data doesn’t get modified accidentally.
Answer: b
Explana on: Data member’s data type can be changed without changing any further code. All the
members using that data can con nue in the same way without any modifica on. Member func ons
can never change the data type of same class data members.
adver sement
Answer: c
Explana on: It is the class which uses both the data members and member func ons being declared
inside a single unit. Only data members can be there in structures also. And the encapsula on can only
be illustrated if some data/opera ons are associated within class.
Answer: d
Explana on: Immutable classes are used for caching purpose generally. And it can be created by making
the class as final and making all its members private.
Answer: a
Explana on: The data prone to change in near future is usually encapsulated so that it doesn’t get
changed accidentally. We encapsulate the data to hide the cri cal working of program from outside
world.
9. Which among the following violates the principle of encapsula on almost always?
a) Local variables
b) Global variables
c) Public variables
d) Array variables
View Answer
Answer: b
Explana on: Global variables almost always violates the principles of encapsula on. Encapsula on 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.
10. Which among the following would destroy the encapsula on mechanism if it was allowed in
programming?
a) Using access declara on for private members of base class
b) Using access declara on for public members of base class
c) Using access declara on for local variable of main() func on
d) Using access declara on for global variables
View Answer
Answer: a
Explana on: If using access declara on for private members of base class was allowed in programming,
it would have destroyed whole concept of encapsula on. As if it was possible, any class which gets
inherited privately, would have been able to inherit the private members of base class, and hence could
access each and every member of base class.
11. Which among the following can be a concept against encapsula on rules?
a) Using func on pointers
b) Using char* string pointer to be passed to non-member func on
c) Using object array
d) Using any kind of pointer/array address in passing to another func on
View Answer
Answer: d
Explana on: If we use any kind of array or pointer as data member which should not be changed, but in
some case its address is passed to some other func on or similar variable. There are chances to modify
its whole data easily. Hence Against encapsula on.
12. Consider the following code and select the correct op on.
class student
int marks;
public : int* fun()
return &marks;
};
main()
student s;
int *ptr=c.fun();
return 0;
Answer: d
Explana on: This code violates the encapsula on. By this code we can get the address of the private
member of the class, hence we can change the value of private member, which is against the rules.
class hero
char name[10];
cout<<name;
};
Answer: a
Explana on: This code maintains encapsula on. Here the private member is kept private. Outside code
can’t access the private members of class. Only objects of this class will be able to access the public
member func on at maximum.
14. Encapsula on is the way to add func ons in a user defined structure.
a) True
b) False
View Answer
Answer: b
Explana on: False, because we can’t call these structures if member func ons are involved, it must be
called class. Also, it is not just about adding func ons, it’s about binding data and func ons together.
Answer: b
Explana on: The encapsula on can only ensure data security to some extent. If pointer and addresses
are misused, it may violate encapsula on. Use of global variables also makes the program vulnerable,
hence we can’t say that encapsula on gives pure security.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Abstrac on”.
Answer: d
Explana on: It includes hiding the implementa on part and showing only the required data and features
to the user. It is done to hide the implementa on complexity and details from the user. And to provide a
good interface in programming.
Answer: a
Explana on: It can make programming easy. The programming need not know how the inbuilt func ons
are working but can use those complex func ons directly in the program. It doesn’t provide more
number of features or be er features.
Answer: b
Explana on: Class is logical abstrac on because it provides a logical structure for all of its objects. It
gives an overview of the features of an object.
adver sement
Answer: c
Explana on: Object is real abstrac on because it actually contains those features of class. It is the
implementa on of overview given by class. Hence the class is logical abstrac on and its object is real.
Answer: c
Explana on: It is to idealize the interface. In this way the programmer can use the programming
features more efficiently and can code be er. It can’t increase the program complexity, as the feature
itself is made to hide it.
Sanfoundry Cer fica on Contest of the Month is Live. 100+ Subjects. Par cipate Now!
Answer: a
Explana on: Abstrac on applies to both. Control abstrac on involves use of subrou nes and control
flow abstrac on. Data abstrac on involves handling pieces of data in meaningful ways.
7. Which among the following can be viewed as combina on of abstrac on of data and code.
a) Class
b) Object
c) Inheritance
d) Interfaces
View Answer
Answer: b
Explana on: Object can be viewed as abstrac on of data and code. It uses data members and their
func oning as data abstrac on. Code abstrac on as use of object of inbuilt class.
Answer: c
Explana on: Abstrac on principle includes use of abstrac on to avoid duplica on (usually of code). It
this way the program doesn’t contain any redundant func ons and make the program efficient.
Answer: b
Explana on: Higher the level of abstrac on, lower are the details. The best way to understand this is to
consider a whole system that is highest level of abstrac on as it hides everything inside. And next lower
level would contain few of the computer components and so on.
Answer: a
Explana on: Abstrac on is called stream to provide a level of complexity hiding, for how the files
opera ons are actually done. Actual devices are called file because in one way or another, those can be
considered as single en ty and there is nothing hidden.
12. If two classes combine some private data members and provides public member func ons to access
and manipulate those data members. Where is abstrac on used?
a) Using private access specifier for data members
b) Using class concept with both data members and member func ons
c) Using public member func ons to access and manipulate the data members
d) Data is not sufficient to decide what is being used
View Answer
Answer: c
Explana on: It is the concept of hiding program complexity and actual working in background. Hence
use of public member func ons illustrates abstrac on here.
13. A phone is made up of many components like motherboard, camera, sensors and etc. If the
processor represents all the func oning of phone, display shows the display only, and the phone is
represented as a whole. Which among the following have highest level of abstrac on?
a) Motherboard
b) Display
c) Camera
d) Phone
View Answer
Answer: d
Explana on: Phone as a whole have the highest level of abstrac on. This is because the phone being a
single unit represents the whole system. Whereas motherboard, display and camera are its components.
Answer: d
Explana on: Abstrac on is generally divided into 3 different levels, namely, logical, physical and view
level. External level is not defined in terms of abstrac on.
Answer: c
Explana on: It will make the code safer. One may think it reduces the readability, but the fact is, it
actually helps us understand the code be er. We don’t have to read the complex code which is of no
use in understanding the program.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Constructors”.
1. Which among the following is called first, automa cally, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger
View Answer
Answer: b
Explana on: Constructors are the member func ons which are called automa cally whenever an object
is created. It is a mandatory func ons to be called for an object to be created as this helps in ini alizing
the object to a legal ini al value for the class.
Answer: c
Explana on: Constructors are predefined implicitly, even if the programmer doesn’t define any of them.
Even if the programmer declares a constructor, it’s not necessary that it must contain some defini on.
Answer: d
Explana on: The constructors must not have any return type. Also, the body may or may not contain
any body. Defining default constructor is op onal, if you are not using any other constructor.
adver sement
4. In which access should a constructor be defined, so that object of the class can be created in any
func on?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer
Answer: a
Explana on: Constructor func on 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 func on 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
Explana on: Two types of constructors are defined generally, namely, default constructor and
parameterized constructor. Default constructor is not necessary to be defined always.
Sanfoundry Cer fica on Contest of the Month is Live. 100+ Subjects. Par cipate Now!
6. If a programmer defines a class and defines a default value parameterized constructor inside it.
He has not defined any default constructor. And then he try to create the object without passing
arguments, which among the following will be correct?
a) It will not create the object (as parameterized constructor is used)
b) It will create the object (as the default arguments are passed)
c) It will not create the object (as the default constructor is not defined)
d) It will create the object (as at least some constructor is defined)
View Answer
Answer: b
Explana on: It will create the object without any problem, because the default arguments use the
default value if no value is passed. Hence it is equal to default constructor with zero parameters. But it
will not create the object if signature doesn’t match.
7. Default constructor must be defined, if parameterized constructor is defined and the object is to be
created without arguments.
a) True
b) False
View Answer
Answer: a
Explana on: If the object is create without arguments and only parameterized constructors are used,
compiler will give an error as there is no default constructor defined. And some constructor must be
called so as to create an object in memory.
8. If class C inherits class B. And B has inherited class A. Then while crea ng the object of class C, what
will be the sequence of constructors ge ng 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
Explana on: While crea ng the object of class C, its constructor would be called by default. But, if the
class is inheri ng some other class, firstly the parent class constructor will be called so that all the data is
ini alized that is being inherited.
9. In mul ple inheritance, if class C inherits two classes A and B as follows, which class constructor will
be called first?
class A{ };
class B{ };
a) A()
b) B()
c) C()
d) Can’t be determined
View Answer
Answer: a
Explana on: Constructor of class A will be called first. This is because the constructors in mul ple
inheritance are called in the sequence in which they are wri en to be inherited. Here A is wri en first,
hence it is called first.
Answer: b
Explana on: It can’t be defined with zero number of arguments. This is because to copy one object to
another, the object must be men oned so that compiler can take values from that object.
Answer: c
Explana on: Compiler runs out of memory. This is because while passing the argument by value, a
constructor of the object will be called. That in turn called another object constructor for values, and
this goes on. This is like a constructor call to itself, and this goes on infinite mes, hence it must be
passed by reference, so that the constructor is not called.
class student
int marks;
};
a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same me
View Answer
Answer: a
Explana on: The objects are created in the sequence of how they are wri en. This happens because the
constructors are called in the sequence of how the objects are men oned. This is done in sequence.
Implicit calls: The compiler might implicitly call a default constructor (if available) when you declare an
object without arguments. However, these are not temporary instances as they are assigned to a
variable.
Explicit calls: You explicitly use the new keyword followed by the constructor name and arguments to
create a new object. This is the most common way to create temporary objects.
Copy constructors: These constructors are used to create a copy of an exis ng object. They are not
typically used for temporary instances.
Therefore, only an explicit call to a constructor allows you to create a temporary instance that isn’t
assigned to a variable and exists momentarily within the expression or statement.
14. Which among the following is correct for the class defined below?
class student
int marks;
public: student(){}
student(int x)
marks=x;
};
main()
student s1(100);
student s2();
student s3=100;
return 0;
Answer: c
Explana on: It is a special case of constructor with only 1 argument. While calling a constructor with one
argument, you are actually implicitly crea ng a conversion from the argument type to the type of class.
Hence you can directly specify the value of that one argument with assignment operator.
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 defini on
View Answer
Answer: a
Explana on: 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.
This set of Object Oriented Programming using C++ online quiz focuses on “Types of Constructors”.
Answer: b
Explana on: There are 3 types of constructors in general, namely, default constructors, parameterized
constructors and copy constructors. Default one is called whenever an object is created without
arguments.
class student
int marks;
student s1;
student s2=2;
Answer: d
Explana on: The object s2 can be assigned with one value only if a single argument constructor is
defined in class, but here, it can’t be done as no constructor is defined. Hence every object must be
declare or created without using arguments.
adver sement
Answer: c
Explana on: Copy constructor is used while an object is assigned with another. This is mandatory since
we can’t decide which member should be assigned to which member value. By using copy constructor,
we can assign the values in required form.
Subscribe Now: Object Oriented Programming C++ Newsle er | Important Subjects Newsle ers
Answer: a
Explana on: Object must be passed by reference to copy constructor because constructor is not called
in pass by reference. Otherwise, in pass by value, a temporary object will be created which in turn will
try to call its constructor that is already being used. This results in crea ng infinite number of objects
and hence memory shortage error will be shown.
Answer: d
Explana on: The keyword explicit can be used while defining the constructor only. This is used to
suppress the implicit call to the constructor. It ensures that the constructors are being called with the
default syntax only (i.e. only by using object and constructor name).
Answer: c
Explana on: Default constructors can be called explicitly any me. They are specifically used to allocate
memory space for the object in memory, in general. It is not necessary that these should always be
called implicitly.
Answer: d
Explana on: Constructors don’t return any value. Those are special func ons, whose return type is not
defined, not even void. This is so because the constructors are meant to ini alize the members of class
and not to perform some task which can return some value to newly created object.
Answer: a
Explana on: Sta c constructors help in ini alizing the sta c members of the class. This is provided
because the sta c members are not considered to be property of the object, rather they are considered
as the property of class.
Answer: c
Explana on: Sta c constructors can’t be parameterized constructors. Those are used to ini alize the
value of sta c members only. And that must be a definite value. Accep ng arguments may make it
possible that sta c members loses their value with every new object being created.
Answer: a
Explana on: Since the sta c constructors are can’t be parameterized, they can’t be overloaded. Having
this case, only one constructor will be possible to be created in a local scope, because the signature will
always be same and hence it will not be possible to overload sta c constructor.
Answer: d
Explana on: Default constructor, which even the programmer doesn’t define, always ini alize the values
as zero if numeric and null if string. This is done so as to avoid the accidental values to change the
condi onal statements being used and similar condi ons.
Answer: c
Explana on: The sta c constructor is called before crea on of the first instance of that class. This is
done so that even the first instance can use the sta c value of the sta c members of the class and
manipulate as required.
Answer: a
Explana on: If the constructors are defined in private access, then the class can’t be inherited by other
classes. This is useful when the class contains sta c members only. The instances can never be created.
15. Which among the following is correct, based on the given code below?
class student
int marks;
public : student()
};
student s1;
Answer: c
Explana on: This program will work fine. This is because it is not mandatory that a constructor must
contain only ini aliza on only. If you want to perform a task on each instance being created, that code
can be wri en inside the constructor.
This set of Basic Object Oriented Programming using C++ Ques ons and Answers focuses on “Copy
Constructor”.
Answer: d
Explana on: The object that has to be copied to new object must be previously created. The new object
gets ini alized with the same values as that of the object men oned for being copied. The exact copy is
made with values.
Answer: a
Explana on: The copy constructor has the most basic func on to ini alize the members of an object
with same values as that of some previously created object. The object must be of same class.
3. If two classes have exactly same data members and member func on and only they differ by class
name. Can copy constructor be used to ini alize one class object with another class object?
a) Yes, possible
b) Yes, because the members are same
c) No, not possible
d) No, but possible if constructor is also same
View Answer
Answer: c
Explana on: The restric on for copy constructor is that it must be used with the object of same class.
Even if the classes are exactly same the constructor won’t be able to access all the members of another
class. Hence we can’t use object of another class for ini aliza on.
adver sement
Answer: b
Explana on: When an object is passed to a func on, actually its copy is made in the func on. To copy
the values, copy constructor is used. Hence the object being passed and object being used in func on
are different.
5. Which returning an object, we can use ____________
a) Default constructor
b) Zero argument constructor
c) Parameterized constructor
d) Copy constructor
View Answer
Answer: d
Explana on: While returning an object we can use the copy constructor. When we assign the return
value to another object of same class then this copy constructor will be used. And all the members will
be assigned the same values as that of the object being returned.
Answer: a
Explana on: The compiler provides an implicit copy constructor. It is not mandatory to always create an
explicit copy constructor. The values are copied using implicit constructor only.
7. If a class implements some dynamic memory alloca ons and pointers then _____________
a) Copy constructor must be defined
b) Copy constructor must not be defined
c) Copy constructor can’t be defined
d) Copy constructor will not be used
View Answer
Answer: a
Explana on: In the case where dynamic memory alloca on is used, the copy constructor defini on must
be given. The implicit copy constructor is not capable of manipula ng the dynamic memory and
pointers. Explicit defini on allows to manipulate the data as required.
Answer: c
Explana on: The syntax must contain the class name first, followed by the classname as type and
&object within parenthesis. Then comes the constructor body. The defini on can be given as per
requirements.
9. Object being passed to a copy constructor ___________
a) Must be passed by reference
b) Must be passed by value
c) Must be passed with integer type
d) Must not be men oned in parameter list
View Answer
Answer: a
Explana on: This is mandatory to pass the object by reference. Otherwise, the object will try to create
another object to copy its values, in turn a constructor will be called, and this will keep on calling itself.
This will cause the compiler to give out of memory error.
10. Out of memory error is given when the object _____________ to the copy constructor.
a) Is passed with & symbol
b) Is passed by reference
c) Is passed as <classname &obj>
d) Is not passed by reference
View Answer
Answer: d
Explana on: All the op ons given, directly or indirectly indicate that the object is being passed by
reference. And if object is not passed by reference then the out of memory error is produced. Due to
infinite constructor call of itself.
Answer: c
Explana on: Whenever the compiler creates a temporary object, copy constructor is used to copy the
values from exis ng object to the temporary object.
12. The deep copy is possible only with the help of __________
a) Implicit copy constructor
b) User defined copy constructor
c) Parameterized constructor
d) Default constructor
View Answer
Answer: b
Explana on: While using explicit copy constructor, the pointers of copied object point to the intended
memory loca on. This is assured since the programmers themselves manipulate the addresses.
Answer: a
Explana on: The copy constructor can be defined as private. If we make it private then the objects of
the class can’t be copied. It can be used when a class used dynamic memory alloca on.
Answer: a
Explana on: The object should not be modified in the copy constructor. Because the object itself is
being copied. When the object is returned from a func on, the object must be a constant otherwise the
compiler creates a temporary object which can die any me.
Answer: a
Explana on: The copy constructors are always overloaded constructors. They have to be. All the classes
have a default constructor and other constructors are basically overloaded constructors.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Overloading Constructors”.
Answer: c
Explana on: If more than one constructors are defined in a class with same signature, then that results
in error. The signatures must be different. So that the constructors can be differen ated.
Answer: d
Explana on: The constructor must be having the same name as that of a class. Hence a constructor of
one class can’t even be defined in another class. Since the constructors can’t be defined in derived class,
it can’t be overloaded too, in derived class.
3. Does constructor overloading include different return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes different
b) Yes, because return types can differen ate two func ons
c) No, return type can’t differen ate two func ons
d) No, constructors doesn’t have any return type
View Answer
Answer: d
Explana on: The constructors doesn’t have any return type. When we can’t have return type of a
constructor, overloading based on the return type is not possible. Hence only parameters can be
different.
adver sement
Answer: a
Explana on: All the constructors defined in a class must have a different signature in order to be
overloaded. Here one default and other parameterized constructors are used, wherein one is of only
one parameter and other accepts two. Hence overloading is possible.
5. Which constructor will be called from the object created in the code below?
class A
int i;
A()
i=0; cout<<i;
}
A(int x=0)
i=x; cout<<I;
};
A obj1;
a) Default constructor
b) Parameterized constructor
c) Compile me error
d) Run me error
View Answer
Answer: c
Explana on: When a default constructor is defined and another constructor with 1 default value
argument is defined, crea ng object without parameter will create ambiguity for the compiler. The
compiler won’t be able to decide which constructor should be called, hence compile me error.
Answer: b
Explana on: The constructors are not always user defined. The construct will be provided implicitly from
the compiler if the used doesn’t defined any constructor. The implicit constructor makes all the string
values null and allocates memory space for each data member.
Answer: d
Explana on: The constructor is called as soon as the object is created. The overloading comes into
picture as to iden fy which constructor have to be called depending on arguments passed in the
crea on of object.
8. Which among the following func on can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()
View Answer
Answer: a
Explana on: The func on this() can be used to call the default constructor from inside any other
constructor. This helps to further reuse the code and not to write the redundant data in all the
constructors.
Answer: c
Explana on: The constructors are overloaded to ini alize the objects of a class in different ways. This
allows us to ini alize the object with either default values or used given values. If data members are not
ini alized then program may give unexpected results.
Answer: a
Explana on: When the programmer doesn’t specify any default constructor and only defines some
parameterized constructor. The compiler doesn’t provide any default constructor implicitly. This is
because it is assumed that the programmer will create the objects only with constructors.
Answer: b
Explana on: Java doesn’t provide the feature to recursively call the constructor. This is to eliminate the
out of memory error in some cases that arises unexpectedly. That is an predefined condi on for
constructors in java.
12. Which constructor will be called from the object obj2 in the following program?
class A
{
int i;
A()
i=0;
A(int x)
i=x+1;
A(int y, int x)
i=x+y;
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int x)
b) A(int y)
c) A(int y, int x)
d) A(int y; int x)
View Answer
Answer: c
Explana on: The two argument constructor will be called as we are passing 2 arguments to the object
while crea on. The arguments will be passed together and hence compiler resolves that two argument
constructor have to be called.
13. What are we only create an object but don’t call any constructor for it in java?
a) Implicit constructor will be called
b) Object is ini alized to some null values
c) Object is not created
d) Object is created but points to null
View Answer
Answer: d
Explana on: The object becomes a reference object which can be ini alized will another object. Then
this object will indirectly become another name of the object being assigned. If not assigned, it simply
points to null address.
Answer: a
Explana on: Kotlin language allows constructor overloading. This is a basic feature for the constructors.
The constructor overloading allows the object to be ini alized according to the user.
Answer: c
Explana on: The syntax for object crea ng in java with calling a constructor for is it is as in op on c. The
syntax must contain the classname followed by the object name. The keyword new must be used and
then the constructor call with or without the parameters as required.
This set of Object Oriented Programming using C++ Ques ons and Answers for Experienced people
focuses on “Execu on of Constructor or Destructor”.
Answer: b
Explana on: The constructors are special type of func ons which are called whenever an object is
created. This is to ini alize the data members of the class. The constructor allocates memory space for
all the data members.
Answer: a
Explana on: The Destructors are special func ons which are called just before an object is destroyed.
This func ons is responsible to free all the allocated resources to the object. Objects are destroyed
whenever those go out of scope.
Answer: d
Explana on: The constructors must contain only the class name. The class name is followed by the blank
parenthesis or we can have parameters if some values are to be passed.
adver sement
Answer: c
Explana on: The destructor must have same name as that of the corresponding class. The class name
should be preceded by the lde symbol (~).
Answer: a
Explana on: First the constructor of parent class are called. The order in which the parent class
constructors are called is same in the sequence of inheritance used.
Answer: d
Explana on: The destructors are called in the reverse order as that of the constructors being called. This
is done to ensure that all the resources are released in sequence. That is, the derived class destructors
will be called first.
Answer: b
Explana on: The destructors doesn’t have any arguments. The destructors have to be called implicitly
whenever an object goes out of scope. The user can’t pass argument to the implicit call.
Answer: c
Explana on: The destructors are usually called implicitly whenever an object goes out of scope. The
destructors can also be called explicitly if required. The call must be made, implicitly or explicitly.
Answer: a
Explana on: Destructor will be called only to free the resources allocated for an object. The resources
are allocated only the constructor for an object is called.
Answer: b
Explana on: Even if the class have 4 constructors, only one would be used. And only one destructor is
allowed.
Answer: c
Explana on: The destructors can never be overloaded. The destructors doesn’t have arguments. And to
get overloaded, they must have different signature. This is not possible if arguments are not allowed.
Answer: d
Explana on: The constructors doesn’t have any return type. The constructors are intended to allocate
the resources for the object. Memory spaces are to be finalized.
Answer: d
Explana on: The destructors are intended to free the memory space. And all the resources that were
allocated for the object. The return value is not supported since only memory has to be made free.
15. The destructor can be called before the constructor if required.
a) True
b) False
View Answer
Answer: b
Explana on: The destructors can be called only a er the constructor calls. It is not a mandatory rule but
the dele on can only take place if there is something created using the constructor.
This set of Object Oriented Programming (OOPs) using C++ Mul ple Choice Ques ons & Answers
(MCQs) focuses on “Access Specifiers”.
Answer: c
Explana on: Only 3 types of access specifiers are available. Namely, private, protected and public. All
these three can be used according to the need of security of members.
Answer: d
Explana on: All the classes can use any of the specifiers as needed. There is no restric on on how many
of them can be used together.
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
Explana on: 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.
adver sement
4. Which access specifier is used when no access specifier is used with a member of class (java)?
a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: b
Explana on: Default access is used if the programmer doesn’t specify the specifier. This acts in a similar
way as that of private. But since nothing is specified we call it to default access.
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
Explana on: 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.
Subscribe Now: Object Oriented Programming C++ Newsle er | Important Subjects Newsle ers
Answer: c
Explana on: The default members can be inherited. Provided that they are in same package. It works in
a li le different way from private access specifier.
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
Explana on: 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.
8. If class A has add() func on with protected access, and few other members in public. Then class B
inherits class A privately. Will the user will not be able to call _________ from the object of class B.
a) Any func on of class A
b) The add() func on of class A
c) Any member of class A
d) Private, protected and public members of class A
View Answer
Answer: d
Explana on: Class B object will not be able to call any of the private, protected and public members of
class A. It is not only about the func on add(), but all the members of class A will become private
members of class B.
9. Which access specifier should be used in a class where the instances can’t be created?
a) Private default constructor
b) All private constructors
c) Only default constructor to be public
d) Only default constructor to be protected
View Answer
Answer: b
Explana on: All the constructors must be made private. This will restrict the instance of class to be made
anywhere in the program. Since the constructors are private, no instance will be able to call them and
hence won’t be allocated with any memory space.
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
Explana on: 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.
11. If class B inherits class A privately. And class B has a friend func on. Will the friend func on be able
to access the private member of class A?
a) Yes, because friend func on can access all the members
b) Yes, because friend func on is of class B
c) No, because friend func on can only access private members of friend class
d) No, because friend func on can access private member of class A also
View Answer
Answer: c
Explana on: The friend func on of class B will not be able to access private members of class A. Since B
is inheri ng class A privately, the members will become private in class B. But private members of class
A won’t be inherited at all. Hence it won’t be accessible.
12. If an abstract class has all the private members, then _________
a) No class will be able to implement members of abstract class
b) Only single inheritance class can implement its members
c) Only other enclosing classes will be able to implement those members
d) No class will be able to access those members but can implement.
View Answer
Answer: a
Explana on: The classes which inherit the abstract class, won’t be able to implement the members of
abstract class. The private members will not be inherited. This will restrict the subclasses to implement
those members.
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
Explana on: 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.
14. Which access specifier is usually used for data members of a class?
a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: a
Explana on: All the data members should be made private to ensure the highest security of data. In
special cases we can use public or protected access, but it is advised to keep the data members private
always.
15. Which specifier should be used for member func ons of a class?
a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: d
Explana on: It is always advised that the member func ons should be kept public so that those
func ons can be used from out of the class. This is usually done to ensure that the features provided by
the class can be used at its maximum.