Download as pdf or txt
Download as pdf or txt
You are on page 1of 192

C++ MCQ Type Questions

Object Oriented Programming using C++


Questions and Answers – OOPs Basic Concepts

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
Explanation: 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.

2. Which of the following best defines a class?


a) Parent of an object
b) Instance of an object
c) Blueprint of an object
d) Scope of an object
View Answer
Answer: c
Explanation: A class is Blueprint of an object which describes/ shows all the
functions 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
properties of an object.

C++ Page 1
3. Who invented OOP?
a) Alan Kay
b) Andrea Ferro
c) Dennis Ritchie
d) Adele Goldberg
View Answer
Answer: a
Explanation: 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.

4. What is the additional feature in classes that was not in structures?


a) Data members
b) Member functions
c) Static data allowed
d) Public access specifier
View Answer
Answer: b
Explanation: Member functions are allowed inside a class but were not present in
structure concept. Data members, static data and public access specifiers were
present in structures too.

5. Which is not feature of OOP in general definitions?


a) Code reusability
b) Modularity
c) Duplicate/Redundant data
d) Efficient Code
View Answer
Answer: c
Explanation: 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 Object Oriented Programming C++ Newsletter

C++ Page 2
6. Pure OOP can be implemented without using class in a program. (True or False)
a) True
b) False
View Answer
Answer: b
Explanation: It’s false because for a program to be pure OO, everything must be
written inside classes. If this rule is violated, the program can’t be labelled as purely
OO.

7. Which Feature of OOP illustrated the code reusability?


a) Polymorphism
b) Abstraction
c) Encapsulation
d) Inheritance
View Answer
Answer: d
Explanation: Using inheritance we can reuse the code already written and also can
avoid creation of many new functions or variables, as that can be done one time and
be reused, using classes.

8. Which language does not support all 4 types of inheritance?


a) C++
b) Java
c) Kotlin
d) Small Talk
View Answer
Answer: b
Explanation: Java doesn’t support all 4 types of inheritance. It doesn’t support
multiple inheritance. But the multiple inheritance can be implemented using
interfaces in Java.

9. How many classes can be defined in a single program?


a) Only 1
b) Only 100
c) Only 999

C++ Page 3
d) As many as you want
View Answer
Answer: d
Explanation: 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.

10. When OOP concept did first came into picture?


a) 1970’s
b) 1980’s
c) 1993
d) 1995
View Answer
Answer: a
Explanation: 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.

11. Why Java is Partially OOP language?


a) It supports usual declaration of primitive data types
b) It doesn’t support all types of inheritance
c) It allows code to be written outside classes
d) It does not support pointers
View Answer
Answer: a
Explanation: As Java supports usual declaration of data variables, it is partial
implementation of OOP. Because according to rules of OOP, object constructors
must be used, even for declaration of variables.

12. Which concept of OOP is false for C++?


a) Code can be written without using classes
b) Code must contain at least one class
c) A class must have member functions
d) At least one object should be declared in code
View Answer

C++ Page 4
Answer: b
Explanation: In C++, it’s not necessary to use classes, and hence codes can be
written without using OOP concept. Classes may or may not contain member
functions, so it’s not a necessary condition in C++. And, an object can only be
declared in a code if its class is defined/included via header file.

13. Which header file is required in C++ to use OOP?


a) iostream.h
b) stdio.h
c) stdlib.h
d) OOP can be used without using any header file
View Answer
Answer: d
Explanation: We need not include any specific header file to use OOP concept in
C++, only specific functions used in code need their respective header files to be
included or classes should be defined if needed.

14. Which of the two features match each other?


a) Inheritance and Encapsulation
b) Encapsulation and Polymorphism
c) Encapsulation and Abstraction
d) Abstraction and Polymorphism
View Answer
Answer: c
Explanation: Encapsulation and Abstraction are similar features. Encapsulation is
actually binding all the properties in a single class or we can say hiding all the
features of object inside a class. And Abstraction is hiding unwanted data (for user)
and showing only the data required by the user of program.

15. Which feature allows open recursion, among the following?


a) Use of this pointer
b) Use of pointers
c) Use of pass by value
d) Use of parameterized constructor
View Answer

C++ Page 5
Answer: a
Explanation: 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
differentiate the variables of different scopes.

Object Oriented Programming using C++


Questions and Answers – Classes

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & 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
Explanation: 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.

2. Class is pass by _______


a) Value
b) Reference
c) Value or Reference, depending on program
d) Copy
View Answer
Answer: b
Explanation: 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 functions declared
within a class without any specifier, in C++?
a) Private

C++ Page 6
b) Protected
c) Public
d) Depends on compiler
View Answer
Answer: a
Explanation: The data members and member functions 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.

4. Which is most appropriate comment on following class definition?

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
Explanation: 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.

Subscribe Object Oriented Programming C++ Newsletter

5. Which is known as a generic class?


a) Abstract class
b) Final class
c) Template class
d) Efficient Code
View Answer

C++ Page 7
Answer: c
Explanation: 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.

6. Size of a class is _____________


a) Sum of the size of all the variables declared inside the class
b) Sum of the size of all the variables along with inherited variables in the class
c) Size of the largest size of variable
d) Classes doesn’t have any size
View Answer
Answer: d
Explanation: 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 functions without their implementation?


a) Default class
b) String class
c) Template class
d) Abstract class
View Answer
Answer: d
Explanation: Abstract classes can have member functions with no implementation,
where the inheriting subclasses must implement those functions.

8. Which of the following describes a friend class?


a) Friend class can access all the private members of the class, of which it is a
friend
b) Friend class can only access protected members of the class, of which it is a
friend
c) Friend class don’t have any implementation
d) Friend class can’t access any data member of another class but can use it’s
methods
View Answer

C++ Page 8
Answer: a
Explanation: 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 functions.

9. What is the scope of a class nested inside another class?


a) Protected scope
b) Private scope
c) Global scope
d) Depends on access specifier and inheritance used
View Answer
Answer: d
Explanation: 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.

10. Class with main() function can be inherited.


a) True
b) False
View Answer
Answer: a
Explanation: The class containing main function 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 function of a class?
a) All member functions must be defined
b) Member functions can be defined inside or outside the class body
c) Member functions need not be declared inside the class definition
d) Member functions can be made friend to another class using the friend keyword
View Answer
Answer: c
Explanation: Member functions must be declared inside class body, though the
definition can be given outside the class body. There is no way to declare the
member functions outside the class.

C++ Page 9
12. Which syntax for class definition is wrong?
a) class student{ };
b) student class{ };
c) class student{ public: student(int a){ } };
d) class student{ student(int a){} };
View Answer
Answer: b
Explanation: Keyword class should come first. Class name should come after
keyword class. Parameterized constructor definition depends on programmer so it
can be left empty also.

13. Which of the following pairs are similar?


a) Class and object
b) Class and structure
c) Structure and object
d) Structure and functions
View Answer
Answer: b
Explanation: Class and structure are similar to each other. Only major difference is
that a structure doesn’t have member functions whereas the class can have both
data members and member functions.

14. Which among the following is false for class features?


a) Classes may/may not have both data members and member functions
b) Class definition must be ended with a colon
c) Class can have only member functions with no data members
d) Class is similar to union and structures
View Answer
Answer: b
Explanation: Class definition must end with a semicolon, not colon. Class can have
only member functions in its body with no data members.

15. Instance of which type of class can’t be created?


a) Anonymous class
b) Nested class

C++ Page 10
c) Parent class
d) Abstract class
View Answer
Answer: d
Explanation: Instance of abstract class can’t be created as it will not have any
constructor of its own, hence while creating an instance of class, it can’t initialize the
object members. Actually the class inheriting the abstract class can have its instance
because it will have implementation of all members.

Object Oriented Programming using C++


Questions and Answers – Objects

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Objects”.

1. Which definition best describes an object?


a) Instance of a class
b) Instance of itself
c) Child of a class
d) Overview of a class
View Answer
Answer: a
Explanation: An object is instance of its class. It can be declared in the same way
that a variable is declared, only thing is you have to use class name as the data
type.

2. How many objects can be declared of a specific class in a single program?


a) 32768
b) 127
c) 1
d) As many as you want
View Answer

C++ Page 11
Answer: d
Explanation: You can create as many objects of a specific class as you want,
provided enough memory is available.

3. Which among the following is false?


a) Object must be created before using members of a class
b) Memory for an object is allocated only after its constructor is called
c) Objects can’t be passed by reference
d) Objects size depends on its class data members
View Answer
Answer: c
Explanation: 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.

4. Which of the following is incorrect?


a) class student{ }s;
b) class student{ }; student s;
c) class student{ }s[];
d) class student{ }; student s[5];
View Answer
Answer: c
Explanation: 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.

5. The object can’t be __________


a) Passed by reference
b) Passed by value
c) Passed by copy
d) Passed as function
View Answer
Answer: d
Explanation: Object can’t be passed as function as it is an instance of some class,
it’s not a function. Object can be passed by reference, value or copy. There is no
term defined as pass as function for objects.

Subscribe Object Oriented Programming C++ Newsletter

C++ Page 12
6. What is size of the object of following class (64 bit system)?

class student { int rollno; char name[20]; static int studentno; };

a) 20
b) 22
c) 24
d) 28
View Answer
Answer: c
Explanation: The size of any object of student class will be of size 4+20=24,
because static members are not really considered as property of a single object. So
static variables size will not be added.

7. Functions can’t return objects.


a) True
b) False
View Answer
Answer: b
Explanation: Functions can always return an object if the return type is same as that
of object being returned. Care has to be taken while writing the prototype of the
function.

8. How members of an object are accessed?


a) Using dot operator/period symbol
b) Using scope resolution operator
c) Using member names directly
d) Using pointer only
View Answer
Answer: a
Explanation: Using dot operator after 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 function, which of the following is true for an object
of that class?

C++ Page 13
a) Object is accessible outside the function
b) Object can be declared inside any other function
c) Object can be used to call other class members
d) Object can be used/accessed/declared locally in that function
View Answer
Answer: d
Explanation: For an object which belongs to a local class, it is mandatory to declare
and use the object within the function 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
Explanation: 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 initialize its members. Option d is correct because topper class is inheriting the
base abstract class student, and hence topper class object can be created easily.

11. Object declared in main() function _____________


a) Can be used by any other function
b) Can be used by main() function of any other program
c) Can’t be used by any other function
d) Can be accessed using scope resolution operator
View Answer
Answer: c
Explanation: The object declared in main() have local scope inside main() function
only. It can’t be used outside main() function. Scope resolution operator is used to
access globally declared variables/objects.

12. When an object is returned___________


a) A temporary object is created to return the value

C++ Page 14
b) The same object used in function is used to return the value
c) The Object can be returned without creation of temporary object
d) Object are returned implicitly, we can’t say how it happens inside program
View Answer
Answer: a
Explanation: A temporary object is created to return the value. It is created because
the object used in function is destroyed as soon as the function is returned. The
temporary variable returns the value and then gets destroyed.

13. Which among the following is correct?


a) class student{ }s1,s2; s1.student()=s2.student();
b) class student{ }s1; class topper{ }t1; s1=t1;
c) class student{ }s1,s2; s1=s2;
d) class student{ }s1; class topper{ }t1; s1.student()=s2.topper();
View Answer
Answer: c
Explanation: 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 option
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 initializing the class below?

class student{
int marks;
int cgpa;
public: student(int i, int j){
marks=I;
cgpa=j
}
};

a) student s[3]={ s(394, 9); s(394, 9); s(394,9); };


b) student s[2]={ s(394,9), s(222,5) };

C++ Page 15
c) student s[2]={ s1(392,9), s2(222,5) };
d) student s[2]={ s[392,9], s2[222,5] };
View Answer
Answer: b
Explanation: It is the way we can initialize the data members for an object array
using parameterized constructor. We can do this to pass our own intended values to
initialize 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
Explanation: The explanation 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.

Object Oriented Programming using C++


Questions and Answers – OOPs Features

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “OOP Features”.

1. Which feature of OOP indicates code reusability?


a) Encapsulation
b) Inheritance
c) Abstraction
d) Polymorphism
View Answer
Answer: b
Explanation: Inheritance indicates the code reusability. Encapsulation and

C++ Page 16
abstraction are meant to hide/group data into one element. Polymorphism is to
indicate different tasks performed by a single entity.

2. If a function can perform more than 1 type of tasks, where the function name
remains same, which feature of OOP is used here?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
View Answer
Answer: c
Explanation: 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 properties and functions of a real world entity is grouped or embedded


into a single element, what is it called in OOP language?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
View Answer
Answer: d
Explanation: It is Encapsulation, which groups different properties and functions of a
real world entity into single element. Abstraction, on other hand, is hiding of
functional or exact working of codes and showing only the things which are required
by the user.

4. Which of the following is not a feature of pure OOP?


a) Classes must be used
b) Inheritance
c) Data may/may not be declared using object
d) Functions Overloading
View Answer

C++ Page 17
Answer: c
Explanation: 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 violation of pure OOP concept.

5. Which among the following doesn’t come under OOP concept?


a) Platform independent
b) Data binding
c) Message passing
d) Data hiding
View Answer
Answer: a
Explanation: Platform independence is not feature of OOP. C++ supports OOP but
it’s not a platform independent language. Platform independence depends on
programming language.

Subscribe Object Oriented Programming C++ Newsletter

6. Which feature of OOP is indicated by the following code?

class student{ int marks; };


class topper:public student{ int age; topper(int age){ this.age=age; } };

a) Inheritance
b) Polymorphism
c) Inheritance and polymorphism
d) Encapsulation and Inheritance
View Answer
Answer: d
Explanation: Encapsulation is indicated by use of classes. Inheritance is shown by
inheriting 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.

7. Which feature may be violated if we don’t use classes in a program?


a) Inheritance can’t be implemented

C++ Page 18
b) Object must be used is violated
c) Encapsulation only is violated
d) Basically all the features of OOP gets violated
View Answer
Answer: d
Explanation: All the features are violated because Inheritance and Encapsulation
won’t be implemented. Polymorphism and Abstraction are still 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
Explanation: There are 7 basic features that define whether a programing language
is pure OOP or not. The 4 basic features are inheritance, polymorphism,
encapsulation and abstraction. 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
Explanation: The interaction 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.

C++ Page 19
10. ___________ underlines the feature of Polymorphism in a class.
a) Nested class
b) Enclosing class
c) Inline function
d) Virtual Function
View Answer
Answer: d
Explanation: Virtual Functions can be defined in any class using the keyword virtual.
All the classes which inherit the class containing the virtual function, define the
virtual function as required. Redefining the function on all the derived classes
according to class and use represents polymorphism.

11. Which feature in OOP is used to allocate additional function to a predefined


operator in any language?
a) Operator Overloading
b) Function Overloading
c) Operator Overriding
d) Function Overriding
View Answer
Answer: a
Explanation: The feature is operator overloading. There is not a feature named
operator overriding specifically. Function overloading and overriding doesn’t give
addition function to any operator.

12. Which among doesn’t illustrates polymorphism?


a) Function overloading
b) Function overriding
c) Operator overloading
d) Virtual function
View Answer
Answer: b
Explanation: Function overriding doesn’t illustrate polymorphism because the
functions are actually different and theirs scopes are different. Function and operator
overloading illustrate proper polymorphism. Virtual functions show polymorphism

C++ Page 20
because all the classes which inherit virtual function, define the same function in
different ways.

13. Exception handling is a feature of OOP.


a) True
b) False
View Answer
Answer: a
Explanation: Exception 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
Explanation: 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
partially OOP language.

15. Does OOP provide better security than POP?


a) Always true for any programming language
b) May not be true with respect to all programming languages
c) It depends on type of program
d) It’s vice-versa is true
View Answer
Answer: a
Explanation: 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.

C++ Page 21
Object Oriented Programming using C++
Questions and Answers – Polymorphism

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Polymorphism”.

1. Which among the following best describes polymorphism?


a) It is the ability for a message/data to be processed in more than one form
b) It is the ability for a message/data to be processed in only 1 form
c) It is the ability for many messages/data to be processed in one way
d) It is the ability for undefined message/data to be processed in at least one way
View Answer
Answer: a
Explanation: 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 entity
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
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++

C++ Page 22
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.

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
Explanation: The feature defined in question defines polymorphism features. Here
the different objects are capable of responding to the same message in different
ways, hence polymorphism.

5. Which class/set of classes can illustrate polymorphism in the following code?

Subscribe Object Oriented Programming C++ Newsletter

abstract class student


{
public : int marks;
calc_grade();
}
class topper:public student
{
public : calc_grade()
{
return 10;
}
};

C++ Page 23
class average:public student
{
public : calc_grade()
{
return 20;
}
};
class failed{ int marks; };

a) Only class student can show polymorphism


b) Only class student and topper together can show polymorphism
c) All class student, topper and average together can show polymorphism
d) Class failed should also inherit class student for this code to work for
polymorphism
View Answer
Answer: c
Explanation: Since Student class is abstract class and class topper and average are
inheriting student, class topper and average must define the function named
calc_grade(); in abstract class. Since both the definition are different in those
classes, calc_grade() will work in different way for same input from different objects.
Hence it shows polymorphism.

6. Which type of function among the following shows polymorphism?


a) Inline function
b) Virtual function
c) Undefined functions
d) Class member functions
View Answer
Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class
member functions can show polymorphism too but we should be sure that the same
function is being overloaded or is a function 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.

C++ Page 24
7. In case of using abstract class or function overloading, which function is supposed
to be called first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time,
because of low priority
View Answer
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread
scheduling in CPU, but it focuses on whether the function in local scope is present
or not, or if scope resolution is used in some way, or if the function matches the
argument signature. So all these things define which function has the highest priority
to be called in runtime. Local function could be one of the answer but we can’t say if
someone have used pointer to another function or same function name.

8. Which among the following can’t be used for polymorphism?


a) Static member functions
b) Member functions overloading
c) Predefined operator overloading
d) Constructor overloading
View Answer
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t
be considered for overloading/overriding. For polymorphism, function must be
property of object, not only of class.

9. What is output of the following program?

class student
{
public : int marks;
void disp()
{
cout<<”its base class”

C++ Page 25
};
class topper:public student
{
public :
void disp()
{
cout<<”Its derived class”;
}
}
void main() { student s; topper t;
s.disp();
t.disp();
}

a) Its base classIts derived class


b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class
View Answer
Answer: a
Explanation: You need to focus on how the output is going to be shown, no space
will be given after first message from base class. And then the message from
derived class will be printed. Function disp() in base class overrides the function of
base class being derived.

10. Which among the following can show polymorphism?


a) Overloading ||
b) Overloading +=
c) Overloading <<
d) Overloading &&
View Answer
Answer: c
Explanation: Only insertion operator can be overloaded among all the given options.
And the polymorphism can be illustrated here only if any of these is applicable of
being overloaded. Overloading is type of polymorphism.

C++ Page 26
11. Find the output of the following program.

class education
{
char name[10];
public : disp()
{
cout<<”Its education system”;
}
class school:public education
{
public: void dsip()
{
cout<<”Its school education system”;
}
};
void main()
{
school s;
s.disp();
}
}

a) Its school education system


b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system
View Answer
Answer: a
Explanation: Notice that the function name in derived class is different from the
function name in base class. Hence when we call the disp() function, base class
function is executed. No polymorphism is used here.

12. Polymorphism is possible in C language.


a) True

C++ Page 27
b) False
View Answer
Answer: a
Explanation: It is possible to implement polymorphism in C language, even though it
doesn’t support class. We can use structures and then declare pointers which in turn
points to some function. In this way we simulate the functions like member functions
but not exactly member function. Now we can overload these functions, hence
implementing polymorphism in C language.

13. Which problem may arise if we use abstract class functions 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 functions
d) Derived classes can’t redefine the function
View Answer
Answer: c
Explanation: The undefined functions must be defined is a problem, because one
may need to implement few undefined functions from abstract class, but he will have
to define each of the functions declared in abstract class. Being useless task, it is a
problem sometimes.

14. Which among the following is not true for polymorphism?


a) It is feature of OOP
b) Ease in readability of program
c) Helps in redefining the same functionality
d) Increases overhead of function definition always
View Answer
Answer: d
Explanation: It never increases function definition overhead, one way or another if
you don’t use polymorphism, you will use the definition in some other way, so it
actually helps to write efficient codes.

15. If 2 classes derive one base class and redefine a function of base class, also
overload some operators inside class body. Among these two things of function and
operator overloading, where is polymorphism used?

C++ Page 28
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can
be applied only once in a program
View Answer
Answer: d
Explanation: 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 times in a single program.

Object Oriented Programming using C++


Questions and Answers – Encapsulation

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Encapsulation”.

1. Which among the following best describes encapsulation?


a) It is a way of combining various data members into a single unit
b) It is a way of combining various member functions into a single unit
c) It is a way of combining various data members and member functions into a
single unit which can operate on any data
d) It is a way of combining various data members and member functions that
operate on those data members into a single unit
View Answer
Answer: d
Explanation: It is a way of combining both data members and member functions,
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.

C++ Page 29
2. If data members are private, what can we do to access them from the class
object?
a) Create public member functions to access those data members
b) Create private member functions to access those data members
c) Create protected member functions to access those data members
d) Private data members can never be accessed from outside the class
View Answer
Answer: a
Explanation: We can define public member functions to access those private data
members and get their value for use or alteration. They can’t be accessed directly
but is possible to be access using member functions. This is done to ensure that the
private data doesn’t get modified accidentally.

3. While using encapsulation, which among the following is possible?


a) Code modification can be additional overhead
b) Data member’s data type can be changed without changing any other code
c) Data member’s type can’t be changed, or whole code have to be changed
d) Member functions can be used to change the data type of data members
View Answer
Answer: b
Explanation: Data member’s data type can be changed without changing any further
code. All the members using that data can continue in the same way without any
modification. Member functions can never change the data type of same class data
members.

4. Which feature can be implemented using encapsulation?


a) Inheritance
b) Abstraction
c) Polymorphism
d) Overloading
View Answer
Answer: b
Explanation: Data abstraction can be achieved by using encapsulation. We can hide
the operation and structure of actual program from the user and can show only
required information by the user.

C++ Page 30
5. Find which of the following uses encapsulation?
a) void main(){ int a; void fun( int a=10; cout<<a); fun(); }
b) class student{ int a; public: int b;};
c) class student{int a; public: void disp(){ cout<<a;} };
d) struct topper{ char name[10]; public : int marks; }
View Answer
Answer: c
Explanation: It is the class which uses both the data members and member
functions being declared inside a single unit. Only data members can be there in
structures also. And the encapsulation can only be illustrated if some
data/operations are associated within class.

Subscribe Object Oriented Programming C++ Newsletter

6. Encapsulation helps in writing ___________ classes in java.


a) Mutable
b) Abstract
c) Wrapper
d) Immutable
View Answer
Answer: d
Explanation: 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.

7. Which among the following should be encapsulated?


a) The data which is prone to change is near future
b) The data prone to change in long terms
c) The data which is intended to be changed
d) The data which belongs to some other class
View Answer
Answer: a
Explanation: 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 critical
working of program from outside world.

C++ Page 31
8. How can Encapsulation be achieved?
a) Using Access Specifiers
b) Using only private members
c) Using inheritance
d) Using Abstraction
View Answer
Answer: a
Explanation: Using access specifiers we can achieve encapsulation. Using this we
can in turn implement data abstraction. It’s not necessary that we only use private
access.

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.

10. Which among the following would destroy the encapsulation mechanism if it was
allowed in programming?
a) Using access declaration for private members of base class
b) Using access declaration for public members of base class
c) Using access declaration for local variable of main() function
d) Using access declaration for global variables
View Answer
Answer: a
Explanation: If using access declaration for private members of base class was
allowed in programming, it would have destroyed whole concept of encapsulation.
As if it was possible, any class which gets inherited privately, would have been able

C++ Page 32
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 encapsulation rules?
a) Using function pointers
b) Using char* string pointer to be passed to non-member function
c) Using object array
d) Using any kind of pointer/array address in passing to another function
View Answer
Answer: d
Explanation: 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 function or
similar variable. There are chances to modify its whole data easily. Hence Against
encapsulation.

12. Consider the following code and select the correct option.

class student
{
int marks;
public : int* fun()
{
return &marks;
}
};
main()
{
student s;
int *ptr=c.fun();
return 0;
}

a) This code is good to go


b) This code may result in undesirable conditions
c) This code will generate error

C++ Page 33
d) This code violates encapsulation
View Answer
Answer: d
Explanation: This code violates the encapsulation. 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.

13. Consider the code and select the wrong choice.

class hero
{
char name[10];
public : void disp()
{
cout<<name;
}
};

a) This maintains encapsulation


b) This code doesn’t maintain encapsulation
c) This code is vulnerable
d) This code gives error
View Answer
Answer: a
Explanation: This code maintains encapsulation. 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 function at maximum.

14. Encapsulation is the way to add functions in a user defined structure.


a) True
b) False
View Answer
Answer: b
Explanation: False, because we can’t call these structures if member functions are

C++ Page 34
involved, it must be called class. Also, it is not just about adding functions, it’s about
binding data and functions together.

15. Using encapsulation data security is ___________


a) Not ensured
b) Ensured to some extent
c) Purely ensured
d) Very low
View Answer
Answer: b
Explanation: The encapsulation can only ensure data security to some extent. If
pointer and addresses are misused, it may violate encapsulation. Use of global
variables also makes the program vulnerable, hence we can’t say that encapsulation
gives pure security.

Object Oriented Programming using C++


Questions and Answers – Abstraction
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Abstraction”.

1. Which among the following best defines abstraction?


a) Hiding the implementation
b) Showing the important data
c) Hiding the important data
d) Hiding the implementation and showing only the features
View Answer
Answer: d
Explanation: It includes hiding the implementation part and showing only the
required data and features to the user. It is done to hide the implementation
complexity and details from the user. And to provide a good interface in
programming.

C++ Page 35
2. Hiding the implementation complexity can ____________
a) Make the programming easy
b) Make the programming complex
c) Provide more number of features
d) Provide better features
View Answer
Answer: a
Explanation: It can make programming easy. The programming need not know how
the inbuilt functions are working but can use those complex functions directly in the
program. It doesn’t provide more number of features or better features.

3. Class is _________ abstraction.


a) Object
b) Logical
c) Real
d) Hypothetical
View Answer
Answer: b
Explanation: Class is logical abstraction because it provides a logical structure for all
of its objects. It gives an overview of the features of an object.

4. Object is ________ abstraction.


a) Object
b) Logical
c) Real
d) Hypothetical
View Answer
Answer: c
Explanation: Object is real abstraction because it actually contains those features of
class. It is the implementation of overview given by class. Hence the class is logical
abstraction and its object is real.

5. Abstraction gives higher degree of ________


a) Class usage
b) Program complexity

C++ Page 36
c) Idealized interface
d) Unstable interface
View Answer
Answer: c
Explanation: It is to idealize the interface. In this way the programmer can use the
programming features more efficiently and can code better. It can’t increase the
program complexity, as the feature itself is made to hide it.

Subscribe Object Oriented Programming C++ Newsletter

6. Abstraction can apply to ____________


a) Control and data
b) Only data
c) Only control
d) Classes
View Answer
Answer: a
Explanation: Abstraction applies to both. Control abstraction involves use of
subroutines and control flow abstraction. Data abstraction involves handling pieces
of data in meaningful ways.

7. Which among the following can be viewed as combination of abstraction of data


and code.
a) Class
b) Object
c) Inheritance
d) Interfaces
View Answer
Answer: b
Explanation: Object can be viewed as abstraction of data and code. It uses data
members and their functioning as data abstraction. Code abstraction as use of
object of inbuilt class.

8. Abstraction principle includes___________


a) Use abstraction at its minimum
b) Use abstraction to avoid longer codes

C++ Page 37
c) Use abstraction whenever possible to avoid duplication
d) Use abstraction whenever possible to achieve OOP
View Answer
Answer: c
Explanation: Abstraction principle includes use of abstraction to avoid duplication
(usually of code). It this way the program doesn’t contain any redundant functions
and make the program efficient.

9. Higher the level of abstraction, higher are the details.


a) True
b) False
View Answer
Answer: b
Explanation: Higher the level of abstraction, lower are the details. The best way to
understand this is to consider a whole system that is highest level of abstraction as it
hides everything inside. And next lower level would contain few of the computer
components and so on.

10. Encapsulation and abstraction differ as ____________


a) Binding and Hiding respectively
b) Hiding and Binding respectively
c) Can be used any way
d) Hiding and hiding respectively
View Answer
Answer: a
Explanation: Abstraction is hiding the complex code. For example, we directly use
cout object in C++ but we don’t know how is it actually implemented. Encapsulation
is data binding, as in, we try to combine a similar type of data and functions
together.

11. In terms of stream and files ____________


a) Abstraction is called a stream and device is called a file
b) Abstraction is called a file and device is called a stream
c) Abstraction can be called both file and stream

C++ Page 38
d) Abstraction can’t be defined in terms of files and stream
View Answer
Answer: a
Explanation: Abstraction is called stream to provide a level of complexity hiding, for
how the files operations are actually done. Actual devices are called file because in
one way or another, those can be considered as single entity and there is nothing
hidden.

12. If two classes combine some private data members and provides public member
functions to access and manipulate those data members. Where is abstraction
used?
a) Using private access specifier for data members
b) Using class concept with both data members and member functions
c) Using public member functions to access and manipulate the data members
d) Data is not sufficient to decide what is being used
View Answer
Answer: c
Explanation: It is the concept of hiding program complexity and actual working in
background. Hence use of public member functions illustrates abstraction here.

13. A phone is made up of many components like motherboard, camera, sensors


and etc. If the processor represents all the functioning of phone, display shows the
display only, and the phone is represented as a whole. Which among the following
have highest level of abstraction?
a) Motherboard
b) Display
c) Camera
d) Phone
View Answer
Answer: d
Explanation: Phone as a whole have the highest level of abstraction. This is
because the phone being a single unit represents the whole system. Whereas
motherboard, display and camera are its components.

C++ Page 39
14. Which among the following is not a level of abstraction?
a) Logical level
b) Physical level
c) View level
d) External level
View Answer
Answer: d
Explanation: Abstraction is generally divided into 3 different levels, namely, logical,
physical and view level. External level is not defined in terms of abstraction.

15. Using higher degree of abstraction __________


a) May get unsafe
b) May reduce readability
c) Can be safer
d) Can increase vulnerability
View Answer
Answer: c
Explanation: It will make the code safer. One may think it reduces the readability, but
the fact is, it actually helps us understand the code better. We don’t have to read the
complex code which is of no use in understanding the program.

Object Oriented Programming using C++


Questions and Answers – Constructors

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Constructors”.

1. Which among the following is called first, automatically, whenever an object is


created?
a) Class
b) Constructor

C++ Page 40
c) New
d) Trigger
View Answer
Answer: b
Explanation: Constructors are the member functions which are called automatically
whenever an object is created. It is a mandatory functions to be called for an object
to be created as this helps in initializing the object to a legal initial value for the
class.

2. Which among the following is not a necessary condition 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 definition body
d) It can contains arguments
View Answer
Answer: c
Explanation: 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 definition.

3. Which among the following is correct?


a) class student{ public: int student(){} };
b) class student{ public: void student (){} };
c) class student{ public: student{}{} };
d) class student{ public: student(){} };
View Answer
Answer: d
Explanation: The constructors must not have any return type. Also, the body may or
may not contain any body. Defining default constructor is optional, if you are not
using any other constructor.

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++ Page 41
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.

Subscribe Object Oriented Programming C++ Newsletter

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
Explanation: 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

C++ Page 42
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
Explanation: 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 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.

9. 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()

C++ Page 43
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.

10. Which among the following is true for copy constructor?


a) The argument object is passed by reference
b) It can be defined with zero arguments
c) Used when an object is passed by value to a function
d) Used when a function returns an object
View Answer
Answer: b
Explanation: It can’t be defined with zero number of arguments. This is because to
copy one object to another, the object must be mentioned so that compiler can take
values from that object.

11. If the object is passed by value to a copy constructor?


a) Only public members will be accessible to be copied
b) That will work normally
c) Compiler will give out of memory error
d) Data stored in data members won’t be accessible
View Answer
Answer: c
Explanation: 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 times, hence it must be passed by reference, so
that the constructor is not called.

12. Which object will be created first?

class student
{

C++ Page 44
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.

13. Which among the following helps to create a temporary instance?


a) Implicit call to a default constructor
b) Explicit call to a copy constructor
c) Implicit call to a parameterized constructor
d) Explicit call to a constructor
View Answer
Answer: d
Explanation: Explicit call to a constructor can let you create a temporary instance.
This is because the temporary instances doesn’t have any name. Those are deleted
from memory as soon as their reference is removed.

14. Which among the following is correct for the class defined below?

class student
{
int marks;
public: student(){}
student(int x)
{
marks=x;
}

C++ Page 45
};
main()
{
student s1(100);
student s2();
student s3=100;
return 0;
}

a) Object s3, syntax error


b) Only object s1 and s2 will be created
c) Program runs and all objects are created
d) Program will give compile time error
View Answer
Answer: c
Explanation: It is a special case of constructor with only 1 argument. While calling a
constructor with one argument, you are actually implicitly creating 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 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.

C++ Page 46
Object Oriented Programming using C++
Questions and Answers – Types of Constructors

This set of Object Oriented Programming using C++ online quiz focuses on “Types
of Constructors”.

1. How many types of constructors are available, in general, in any language?


a) 2
b) 3
c) 4
d) 5
View Answer
Answer: b
Explanation: 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.

2. Choose the correct option for the following code.

class student
{
int marks;
}
student s1;
student s2=2;

a) Object s1 should be passed with argument


b) Object s2 should not be declared
c) Object s2 will not be created, but program runs
d) Program gives compile time error
View Answer
Answer: d
Explanation: The object s2 can be assigned with one value only if a single argument

C++ Page 47
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.

3. Which constructor is called while assigning some object with another?


a) Default
b) Parameterized
c) Copy
d) Direct assignment is used
View Answer
Answer: c
Explanation: 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 Object Oriented Programming C++ Newsletter

4. It’s necessary to pass object by reference in copy constructor because


____________
a) Constructor is not called in pass by reference
b) Constructor is called in pass by reference only
c) It passes the address of new constructor to be created
d) It passes the address of new object to be created
View Answer
Answer: a
Explanation: 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 creating infinite number of objects and hence
memory shortage error will be shown.

5. Which specifier applies only to the constructors?


a) Public
b) Protected
c) Implicit

C++ Page 48
d) Explicit
View Answer
Answer: d
Explanation: 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).

6. Which among the following is true?


a) Default constructor can’t be defined by the programmer
b) Default parameters constructor isn’t equivalent to the default constructor
c) Default constructor can be called explicitly
d) Default constructor is and always called implicitly only
View Answer
Answer: c
Explanation: Default constructors can be called explicitly anytime. 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.

7. Which type of constructor can’t have a return type?


a) Default
b) Parameterized
c) Copy
d) Constructors don’t have a return type
View Answer
Answer: d
Explanation: Constructors don’t return any value. Those are special functions,
whose return type is not defined, not even void. This is so because the constructors
are meant to initialize the members of class and not to perform some task which can
return some value to newly created object.

8. Why do we use static constructors?


a) To initialize the static members of class
b) To initialize all the members with static value
c) To delete the static members when not required

C++ Page 49
d) To clear all the static members initialized values
View Answer
Answer: a
Explanation: Static constructors help in initializing the static members of the class.
This is provided because the static members are not considered to be property of
the object, rather they are considered as the property of class.

9. When and how many times a static constructor is called?


a) Created at time of object destruction
b) Called at first time when an object is created and only one time
c) Called at first time when an object is created and called with every new object
creation
d) Called whenever an object go out of scope
View Answer
Answer: b
Explanation: Those are called at very first call of object creation. That is called only
one time because the value of static members must be retained and continued from
the time it gets created.

10. Which among the following is true for static constructor?


a) Static constructors are called with every new object
b) Static constructors are used initialize data members to zero always
c) Static constructors can’t be parameterized constructors
d) Static constructors can be used to initialize the non-static members also
View Answer
Answer: c
Explanation: Static constructors can’t be parameterized constructors. Those are
used to initialize the value of static members only. And that must be a definite value.
Accepting arguments may make it possible that static members loses their value
with every new object being created.

11. Within a class, only one static constructor can be created.


a) True
b) False
View Answer

C++ Page 50
Answer: a
Explanation: Since the static 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 static constructor.

12. Default constructor initializes all data members as ___________


a) All numeric member with some garbage values and string to random string
b) All numeric member with some garbage values and string to null
c) All numeric member with zero and strings to random value
d) All numeric member with zero and strings to null
View Answer
Answer: d
Explanation: Default constructor, which even the programmer doesn’t define, always
initialize the values as zero if numeric and null if string. This is done so as to avoid
the accidental values to change the conditional statements being used and similar
conditions.

13. When is the static constructor called?


a) After the first instance is created
b) Before default constructor call of first instance
c) Before first instance is created
d) At time of creation of first instance
View Answer
Answer: c
Explanation: The static constructor is called before creation of the first instance of
that class. This is done so that even the first instance can use the static value of the
static members of the class and manipulate as required.

14. If constructors of a class are defined in private access, then __________


a) The class can’t be inherited
b) The class can be inherited
c) Instance can be created only in another class
d) Instance can be created anywhere in the program
View Answer

C++ Page 51
Answer: a
Explanation: 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 static 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()
{
cout<<”New student details can be added now”;
}
};
student s1;

a) Cout can’t be used inside the constructor


b) Constructor must contain only initializations
c) This program works fine
d) This program produces errors
View Answer
Answer: c
Explanation: This program will work fine. This is because it is not mandatory that a
constructor must contain only initialization only. If you want to perform a task on
each instance being created, that code can be written inside the constructor.

Object Oriented Programming using C++


Questions and Answers – Copy Constructor
This set of Basic Object Oriented Programming using C++ Questions and Answers
focuses on “Copy Constructor”.

C++ Page 52
1. Copy constructor is a constructor which ________________
a) Creates an object by copying values from any other object of same class
b) Creates an object by copying values from first object created for that class
c) Creates an object by copying values from another object of another class
d) Creates an object by initializing it with another previously created object of same
class
View Answer
Answer: d
Explanation: The object that has to be copied to new object must be previously
created. The new object gets initialized with the same values as that of the object
mentioned for being copied. The exact copy is made with values.

2. The copy constructor can be used to ____________


a) Initialize one object from another object of same type
b) Initialize one object from another object of different type
c) Initialize more than one object from another object of same type at a time
d) Initialize all the objects of a class to another object of another class
View Answer
Answer: a
Explanation: The copy constructor has the most basic function to initialize 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 function and only
they differ by class name. Can copy constructor be used to initialize 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
Explanation: The restriction 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

C++ Page 53
able to access all the members of another class. Hence we can’t use object of
another class for initialization.

4. The copy constructors can be used to ________


a) Copy an object so that it can be passed to a class
b) Copy an object so that it can be passed to a function
c) Copy an object so that it can be passed to another primitive type variable
d) Copy an object for type casting
View Answer
Answer: b
Explanation: When an object is passed to a function, actually its copy is made in the
function. To copy the values, copy constructor is used. Hence the object being
passed and object being used in function 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
Explanation: 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.

Subscribe Object Oriented Programming C++ Newsletter

6. If programmer doesn’t define any copy constructor then _____________


a) Compiler provides an implicit copy constructor
b) Compiler gives an error
c) The objects can’t be assigned with another objects
d) The program gives run time error if copying is used
View Answer
Answer: a
Explanation: The compiler provides an implicit copy constructor. It is not mandatory

C++ Page 54
to always create an explicit copy constructor. The values are copied using implicit
constructor only.

7. If a class implements some dynamic memory allocations 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
Explanation: In the case where dynamic memory allocation is used, the copy
constructor definition must be given. The implicit copy constructor is not capable of
manipulating the dynamic memory and pointers. Explicit definition allows to
manipulate the data as required.

8. What is the syntax of copy constructor?


a) classname (classname &obj){ /*constructor definition*/ }
b) classname (cont classname obj){ /*constructor definition*/ }
c) classname (cont classname &obj){ /*constructor definition*/ }
d) classname (cont &obj){ /*constructor definition*/ }
View Answer
Answer: c
Explanation: The syntax must contain the class name first, followed by the
classname as type and &object within parenthesis. Then comes the constructor
body. The definition 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 mentioned in parameter list
View Answer
Answer: a
Explanation: This is mandatory to pass the object by reference. Otherwise, the

C++ Page 55
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
Explanation: All the options 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.

11. Copy constructor will be called whenever the compiler __________


a) Generates implicit code
b) Generates member function calls
c) Generates temporary object
d) Generates object operations
View Answer
Answer: c
Explanation: Whenever the compiler creates a temporary object, copy constructor is
used to copy the values from existing 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
Explanation: While using explicit copy constructor, the pointers of copied object point

C++ Page 56
to the intended memory location. This is assured since the programmers themselves
manipulate the addresses.

13. Can a copy constructor be made private?


a) Yes, always
b) Yes, if no other constructor is defined
c) No, never
d) No, private members can’t be accessed
View Answer
Answer: a
Explanation: 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 allocation.

14. The arguments to a copy constructor _____________


a) Must be const
b) Must not be cosnt
c) Must be integer type
d) Must be static
View Answer
Answer: a
Explanation: The object should not be modified in the copy constructor. Because the
object itself is being copied. When the object is returned from a function, the object
must be a constant otherwise the compiler creates a temporary object which can die
anytime.

15. Copy constructors are overloaded constructors.


a) True
b) False
View Answer
Answer: a
Explanation: 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.

C++ Page 57
Object Oriented Programming using C++
Questions and Answers – Overloading
Constructors
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Overloading Constructors”.

1. Which among the following best describes constructor overloading?


a) Defining one constructor in each class of a program
b) Defining more than one constructor in single class
c) Defining more than one constructor in single class with different signature
d) Defining destructor with each constructor
View Answer
Answer: c
Explanation: 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 differentiated.

2. Can constructors be overloaded in derived class?


a) Yes, always
b) Yes, if derived class has no constructor
c) No, programmer can’t do it
d) No, never
View Answer
Answer: d
Explanation: 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 differentiate two functions

C++ Page 58
c) No, return type can’t differentiate two functions
d) No, constructors doesn’t have any return type
View Answer
Answer: d
Explanation: 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.

4. Which among the following is possible way to overload constructor?


a) Define default constructor, 1 parameter constructor and 2 parameter constructor
b) Define default constructor, zero argument constructor and 1 parameter
constructor
c) Define default constructor, and 2 other parameterized constructors with same
signature
d) Define 2 default constructors
View Answer
Answer: a
Explanation: 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?

Subscribe Object Oriented Programming C++ Newsletter

class A
{
int i;
A()
{
i=0; cout&lt;&lt;i;
}
A(int x=0)
{

C++ Page 59
i=x; cout&lt;&lt;I;
}
};
A obj1;

a) Default constructor
b) Parameterized constructor
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: When a default constructor is defined and another constructor with 1
default value argument is defined, creating object without parameter will create
ambiguity for the compiler. The compiler won’t be able to decide which constructor
should be called, hence compile time error.

6. Which among the following is false for a constructor?


a) Constructors doesn’t have a return value
b) Constructors are always user defined
c) Constructors are overloaded with different signature
d) Constructors may or may not have any arguments being accepted
View Answer
Answer: b
Explanation: 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.

7. When is the constructor called for an object?


a) As soon as overloading is required
b) As soon as class is derived
c) As soon as class is created
d) As soon as object is created
View Answer

C++ Page 60
Answer: d
Explanation: The constructor is called as soon as the object is created. The
overloading comes into picture as to identify which constructor have to be called
depending on arguments passed in the creation of object.

8. Which among the following function can be used to call default constructor
implicitly in java?
a) this()
b) that()
c) super()
d) sub()
View Answer
Answer: a
Explanation: The function 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.

9. Why do we use constructor overloading?


a) To use different types of constructors
b) Because it’s a feature provided
c) To initialize the object in different ways
d) To differentiate one constructor from another
View Answer
Answer: c
Explanation: The constructors are overloaded to initialize the objects of a class in
different ways. This allows us to initialize the object with either default values or
used given values. If data members are not initialized then program may give
unexpected results.

10. If programmer have defined parameterized constructor only, then


__________________
a) Default constructor will not be created by the compiler implicitly
b) Default constructor will be created by the compiler implicitly
c) Default constructor will not be created but called at runtime

C++ Page 61
d) Compile time error
View Answer
Answer: a
Explanation: 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.

11. Which among the following is not valid in java?


a) Constructor overloading
b) Recursive constructor call
c) Default value constructors
d) String argument constructor
View Answer
Answer: b
Explanation: 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 condition 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;

C++ Page 62
}
};
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
Explanation: The two argument constructor will be called as we are passing 2
arguments to the object while creation. The arguments will be passed together and
hence compiler resolves that two argument constructor have to be called.

Object Oriented Programming using C++


Questions and Answers – Destructors
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on ” Destructors”.

1. Which among the following describes a destructor?


a) A special function that is called to free the resources, acquired by the object
b) A special function that is called to delete the class
c) A special function that is called anytime to delete an object
d) A special function that is called to delete all the objects of a class
View Answer
Answer: a
Explanation: It is used to free the resources that the object might had used in its
lifespan. The destructors are called implicitly whenever an object’s life ends.

C++ Page 63
2. When a destructor is called?
a) After the end of object life
b) Anytime in between object’s lifespan
c) At end of whole program
d) Just before the end of object life
View Answer
Answer: d
Explanation: The destructor is called just before the object go out of scope or just
before its life ends. This is done to ensure that all the resources reserved for the
object are used and at last, are made free for others.

3. Which among the following is correct for abstract class destructors?


a) It doesn’t have destructors
b) It has destructors
c) It may or may not have destructors
d) It contains an implicit destructor
View Answer
Answer: a
Explanation: It doesn’t have destructors. Since an abstract class don’t have
constructors, and hence can’t have instances. Having this case, the abstract classes
don’t have destructors too, because that would be of no use here.

Note: Join free Sanfoundry classes at Telegram or Youtube.

advertisement

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

C++ Page 64
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.

Subscribe Object Oriented Programming C++ Newsletter

class A{ };
class B{ };
class C: public A, public B{ };

a) ~A(), ~B(), ~C()


b) ~B(), ~C(), ~A()
c) ~A(), ~C(), ~B()
d) ~C(), ~B(), ~A()
View Answer
Answer: d
Explanation: In multiple inheritance, the constructors are called in the sequence of
how they are written in inheritance sequence. And the destructors will be called in
the reverse order. This can be cross verified just by printing a message from each
destructor defined in classes.

6. When is the destructor of a global object called?


a) Just before end of program
b) Just after end of program
c) With the end of program
d) Anytime when object is not needed
View Answer
Answer: a
Explanation: This is because the lifespan of global object is from start of the
program, till the end of the program. And hence program end is the end of global
object too. Just before the end of program, the destructor will be called to free the
acquired resources by the objects.

7. How the constructors and destructors can be differentiated?


a) Destructor have a return type but constructor doesn’t

C++ Page 65
b) Destructors can’t be defined by the programmer, but constructors can be defined
c) Destructors are preceded with a tilde (~) symbol, and constructor doesn’t
d) Destructors are same as constructors in syntax
View Answer
Answer: c
Explanation: The destructors are preceded with the tilde (~) symbol. The name is
same as that of the class. These also doesn’t have any return type.

8. Destructors doesn’t accept parameters.


a) True
b) False
View Answer
Answer: a
Explanation: The destructors doesn’t accept the arguments. Those are just used to
free up the resources.

9. Destructors can be ________


a) Abstract type
b) Virtual
c) Void
d) Any type depending on situation
View Answer
Answer: b
Explanation: The destructors can be virtual. It is actually advised to keep the
destructors virtual always. This is done to suppress the problems that may arise if
inheritance is involved.

10. Global destructors execute in ___________ order after main function is


terminated.
a) Sequential
b) Random
c) Reverse
d) Depending on priority
View Answer

C++ Page 66
Answer: c
Explanation: The destructors are always called in reverse order no matter which
destructor it is. This is done to ensure that all the resources are able to get free. And
no resource is kept busy.

Object Oriented Programming using C++


Questions and Answers – Access Specifiers

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Access Specifiers”.

1. How many types of access specifiers are provided in OOP (C++)?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: 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.

2. Which among the following can be used together in a single class?


a) Only private
b) Private and Protected together
c) Private and Public together
d) All three together
View Answer
Answer: d
Explanation: All the classes can use any of the specifiers as needed. There is no
restriction on how many of them can be used together.

C++ Page 67
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.

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
Explanation: 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
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.

Subscribe Object Oriented Programming C++ Newsletter

C++ Page 68
6. Which among the following is false?
a) Private members can be accessed using friend functions
b) Member functions can be made private
c) Default members can’t be inherited
d) Public members are accessible from other classes also
View Answer
Answer: c
Explanation: The default members can be inherited. Provided that they are in same
package. It works in a little 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
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.

8. If class A has add() function 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 function of class A
b) The add() function of class A
c) Any member of class A
d) Private, protected and public members of class A
View Answer
Answer: d
Explanation: 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 function add(), but all the
members of class A will become private members of class B.

C++ Page 69
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
Explanation: 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
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.

Object Oriented Programming using C++


Questions and Answers – Private Access
Specifier

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Private Access Specifier”.

C++ Page 70
1. If a function has to be called only by using other member functions of the class,
what should be the access specifier used for that function?
a) Private
b) Protected
c) Public
d) Default
View Answer
Answer: a
Explanation: The function should be made private. In this way, the function will be
available to be called only from the class member functions. Hence the function will
be secure from the outside world.

2. Which among the following is correct for the code given below?

class student
{
private: student()
{
}
public : student( int x)
{
marks =x;
}
};

a) The object can never be created


b) The object can be created without parameters
c) Only the object with only 1 parameter can be created
d) Only the object with some parameters can be created
View Answer
Answer: c
Explanation: For creating object without parameters, the default constructor must be
defined in public access. But here, only parameterized constructor is public, hence
the objects being created with only one parameter will only be allowed.

C++ Page 71
3. Which among the following is true for the code given below?

Subscribe Object Oriented Programming C++ Newsletter

class A
{
private : int marks; char name[20];
public :
A(int x=100)
{
marks=x;
}
};

a) Objects can be created with one parameter or without parameter


b) Object can be created only with one parameter
c) Object can be created with more than one parameter
d) Objects can be create only without parameter
View Answer
Answer: a
Explanation: The constructor here has a default argument constructor. Hence we
can pass one parameter, but that is optional. If an object is created without
parameter, the default value will be used in the constructor definition.

4. Which among the following is correct to call a private member from outside the
class?
a) object.memberfunction( parameters );
b) object->memberfunction( parameters );
c) object->memberfunction( parameteres); or object.memberfunction( parameters );
d) Not possible
View Answer
Answer: d
Explanation: The private member function will not be accessible from outside the
class. Hence any syntax will not work to access the private members. If you have

C++ Page 72
the address of the member, may be you can access those members, but that is a
totally different case and concept.

5. If private members have to be accessed directly from outside the class but the
access specifier must not be changed, what should be done?
a) Specifier must be changed
b) Friend function should be used
c) Other public members should be used
d) It is not possible
View Answer
Answer: b
Explanation: For calling the function directly, we can’t use another function because
that will be indirect call. Using friend function, we can access the private members
directly.

6. Which access specifier is/are most secure during inheritance?


a) Private
b) Default
c) Protected
d) Private and default
View Answer
Answer: a
Explanation: The private members are most secure in inheritance. The default
members can still be in inherited in special cases, but the private members can’t be
accessed in any case.

7. Choose the correct option for the code given below.


class A{ static int c=0; public: A(){ c++; } };
a) Constructor will make c=1 for each object created
b) Constructor will make c=0 for each object created
c) Constructor will keep number of objects created
d) Constructor will just initialize c=0 then increment by 1
View Answer
Answer: c
Explanation: The constructor is using a static member to keep the count of the

C++ Page 73
number of objects created. This is done because the variable c is static and hence
the value will be common for all the objects created.

8. Which option is false for the following code?

class A
{
private : int sum(int x, int y)
{
return x+y;
}
public: A()
{
}
A(int x, int y)
{
cout&lt;&lt;sum(x,y);
}
};

a) Constructor can be created with zero argument


b) Constructor prints sum, if two parameters are passed with object creation
c) Constructor will give error if float values are passed
d) Constructor will take 0 as default value of parameters if not passed
View Answer
Answer: d
Explanation: Constructor is not having any default arguments hence no default value
will be given to any parameters. Only integer values must be passed to the
constructor if we need the sum as output, otherwise if float values are passed, type
mismatch will be shown as error.

9. Which member will never be used from the following class?

class A()
{

C++ Page 74
int marks; char name[20];
public : A()
{
marks=100;
}
void disp()
{
cout&lt;&lt;”Marks= ”&lt'&lt;marks;
cout&lt;&lt;”Student”;
}
};

a) name variable will never be used


b) marks variable will never be used
c) constructor will never be used
d) disp() function will never be used
View Answer
Answer: a
Explanation: Variable name will never be used. It is a private member. None other
than class members can access name, also, neither the constructor nor the disp()
function are accessing the variable name. Hence it will never be accessible.

10. Private member functions can be overloaded.


a) True
b) False
View Answer
Answer: a
Explanation: The private functions can also be overloaded. This can be done in
usual way by having the same name of the member function and having different
signature. Only thing is, they must be accessed from members of class only.

C++ Page 75
Object Oriented Programming using C++
Questions and Answers – Protected Access
Specifier

This set of Object Oriented Programming using C++ Question Bank focuses on
“Protected Access Specifier”.

1. Which among the following best describes the protected specifier?


a) Members are most secure and can’t be used outside class
b) Members are secure but can be used outside the class
c) Members are secure as private, but can be inherited
d) Members are secure like private, but can’t be inherited
View Answer
Answer: c
Explanation: The members which are made protected, are most secure if inheritance
is not used. But, this facility is provided to keep those members private and with that,
they can be inherited by other classes. This is done to make the code more flexible.

2. If a constructor is defined in protected access, then?


a) It’s instance can be created inside the subclasses
b) It’s instance can be created anywhere in the program
c) It’s instance can be created inside the subclasses and main() function
d) It’s instance can be created inside the parent class only
View Answer
Answer: a
Explanation: The instances will be allowed to be created only inside the sub classes.
This is because the protected members will be inherited and hence the constructor
too. This will allow the subclasses to call the constructor whenever an object is
created.

3. For the following code, choose the correct option.

C++ Page 76
class A
{
int marks;
protected : A()
{
marks=100;
}
public : A( int x)
{
marks=x;
}
};

a) The instances can be created only in subclasses


b) The instances can be created only in main() function
c) The instances can be created only in parent class
d) The instances can be created anywhere in the program
View Answer
Answer: d
Explanation: The instances can be created anywhere in the program. The only
restriction will be on which constructor will have to be called. The instances with zero
arguments will be allowed to be created only inside the subclasses, but the
instances with one argument can be created anywhere in the program.

Subscribe Object Oriented Programming C++ Newsletter

4. If the protected members are to be made accessible only to the nearest subclass
and no further subclasses, which access specifier should be used in inheritance?
a) The sub class should inherit the parent class privately
b) The sub class should inherit the parent class as protected
c) The sub class should inherit the parent class as public
d) The sub class can use any access modifier
View Answer
Answer: a
Explanation: The sub class should use private inheritance. This will allow only the

C++ Page 77
nearest sub classes to inherit the protected members and then those members will
become private. Hence further inheritance of those members will not be possible.

5. What will be the output of the following code (all header files and required things
are included)?

class A
{
int marks;
protected : A(int x)
{
marks=x;
}
public : A()
{
marks=100;
}
}
class B
{
A a;
A b=100;
};
main()
{
A a, b=100;
B c;
}

a) Program runs fine


b) Program gives runtime error
c) Program gives compile time error
d) Program gives logical error
View Answer

C++ Page 78
Answer: c
Explanation: The objects being created with assignment value are allowed, if the
constructor accepts only 1 argument. But main() function will not be able to create
the object here with assignment, as the constructor which accepts one argument is
in protected mode in the class.

6. Which among the following is true for the given code below?

class A
{
protected : int marks;
public :
A()
{
marks=100;
}
disp()
{
cout&lt;&lt;”marks=”&lt;&lt;marks;
}
};
class B: protected A
{
};
B b;
b.disp();

a) Object b can’t access disp() function


b) Object b can access disp() function inside its body
c) Object b can’t access members of class A
d) Program runs fine
View Answer
Answer: a
Explanation: The object of class B can’t access the members of A outside the class.

C++ Page 79
This is because the class is being inherited in protected access, so all the members
will become protected in subclass B.

7. Protected members differ from default members as _______


a) Protected members can be accessed outside package using inheritance, but
default can’t
b) Default members can be accessed outside package using inheritance, but
protected can’t
c) Protected members are allowed for inheritance but Default members are not
allowed
d) Both are same
View Answer
Answer: a
Explanation: The protected members are allowed in the same package but can also
be accessed in other packages using inheritance. But the default members can
never be accessible in other packages.

8. If all the members are defined in protected specifier then? (Constructors not
considered)
a) Instance of class can’t be created
b) Instance of class can be created anywhere
c) Instance of class can be created only in subclasses
d) Instance of class can be created only in main() function
View Answer
Answer: b
Explanation: The instances can be created anywhere in the program. This is
because the constructors are not considered among the members defined in
protected mode. Hence the default implicit constructor will be used whenever an
object is created.

9. Which among the following is correct for the code given?

class A
{
private: int marks;

C++ Page 80
A()
{
}
Public : disp()
{
cout&lt;&lt; marks;
}
};
class B: public A
{
}
B b;

a) Instance of B will not be created


b) Instance of B will be created
c) Program gives compile time error
d) Program gives runtime error
View Answer
Answer: a
Explanation: Instance of B will not be created. When you try to create an instance of
B, First the constructor of parent class will be called, but the parent class constructor
is private, hence it won’t be able to initialize and allocate memory for parent class
members. In turn, the object of B will not be created.

10. If protected inheritance is used then _____


a) Public members become public in subclass
b) Protected members become public in subclass
c) Protected members become protected in subclass
d) Protected and Public members become protected in subclass
View Answer
Answer: d
Explanation: The protected and public members of the parent class will become the
protected members in subclass. This is predefined rule of inheritance. The reason
behind is to maintain the level of security in subclass too.

C++ Page 81
Object Oriented Programming using C++
Questions and Answers – Public Access
Specifier

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Public Access Specifier”.

1. Which among the following is true for the code given below?

class A
{
int marks;
public : disp()
{
cout&lt;&lt;marks;
}
}
class B: protected A
{
char name[20];
}
A a; a.disp();
B b; b.disp();

a) Only object of class A can access disp() function


b) Only object of class B can access disp() function
c) Both instances can access disp() function
d) Accessing disp() outside class is not possible
View Answer
Answer: a
Explanation: The object of class A can access the disp() function. This is because
the disp() function is public in definition of class A. But it can’t be accessed from

C++ Page 82
instance of class B because the disp() function is protected in class B, since it was
inherited as protected.

2. If the members have to be accessed from anywhere in the program and other
packages also, which access specifier should be used?
a) Public
b) Private
c) Protected
d) Default
View Answer
Answer: a
Explanation: The access specifier must be public so as to access the members
outside the class and anywhere within the program without using inheritance. This is
a rule, predefined for the public members.

3. Which among the following have least security according to the access
permissions allowed?
a) Private
b) Default
c) Protected
d) Public
View Answer
Answer: d
Explanation: The public members are available to the whole program. This makes
the members most vulnerable to accidental changes, which may result in unwanted
modification and hence unstable programming.

Subscribe Object Oriented Programming C++ Newsletter

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

C++ Page 83
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.

5. We can reduce the visibility of inherited methods.


a) True
b) False
View Answer
Answer: b
Explanation: The statement given is false. This is because when we inherit the
members they can either be made more secure or be at same access. But the
visibility reduction is not possible, for example, if a member is protected in parent
class, then it can only be made protected or private in subclass and not public in any
case.

6. If members of a super class are public, then________


a) All those will be available in subclasses
b) None of those will be available in subclasses
c) Only data members will be available in subclass
d) Only member functions will be available in subclass
View Answer
Answer: a
Explanation: All the members will be available in subclasses. Though it is not
guaranteed whether the members will be available in subsequent subclasses from
the first subclass.

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

C++ Page 84
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.

8. What is the output of the following code?

package pack1;
class A
{
public A()
{
System.out.print(“object created”);
}
}
package pack2;
import pack1.*;
class B
{
A a=new A();
}

a) Output is: object created


b) Output is: object createdobject created
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: The program will give compile time error. Class A is defined with
default access specifier. This directly means that class A will be available within
package only. Even if the constructor is public, the object will not be created.

9. Which among the following for public specifier is false?


a) The static members can’t be public

C++ Page 85
b) The public members are available in other packages too
c) The subclasses can inherit the public members privately
d) There can be only one public class in java program
View Answer
Answer: a
Explanation: The static members are not property of any object of the class. Instead,
those are treated as property of class. This allows us to have public static members
too.

10. A class has its default constructor defined as public. Class B inherits class A
privately. The class ___________
a) B will not be able to have instances
b) Only A can have instances
c) Only B can have instances
d) Both classes can have instances
View Answer
Answer: d
Explanation: Class A can have instances as it has public default constructor. Class
will have its own constructors defined. Hence both classes can have instances.

Object Oriented Programming using C++


Questions and Answers – Data Members

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Data Members”.

1. What is the term used to indicate the variable and constants of a class?
a) Data members
b) Variables of class
c) Data characters
d) Constants
View Answer
C++ Page 86
Answer: a
Explanation: The variables inside a class are termed data members of the class. It is
not a mandatory rule but variables are used to refer to usual variables used in
functions or globally. The term is given because the values stored in those variables
represent some kind of data related to class.

2. Data members ________________ (C++)


a) Can be initialized with declaration in classes
b) Can be initialized only with help of constructors
c) Can be initialized either in declaration or by constructor
d) Can’t be initialized
View Answer
Answer: b
Explanation: The data members are not property of class, those are property of the
instances of the class. And the memory for the data members are not reserved until
a constructor is called. Hence we use constructors for their initialization after the
memory is reserved.

3. Which among the following is true for data members?


a) Private data members can be initialized with declaration in class
b) Static members are initialized in constructors
c) Protected data members can be initialized in class directly
d) Static data members are defined outside class, not in constructor
View Answer
Answer: d
Explanation: Static members are not property of instances of classes. Those are
shared by all the object of classes. Hence those are defined outside the constructor,
so as to make them common for all the objects.

4. What should be done for data member to be of user defined structure type?
a) The structure must have been defined before class.
b) The structure must have been defined after the class definition
c) The structure must be predefined
d) The structure type data members can’t be used
View Answer

C++ Page 87
Answer: a
Explanation: The structure must have been defined prior to its use. If the structure is
not defined, then the memory space will not be allocated for its members. This leads
to undefined use of new data types.

5. How many data members can a class contain?


a) 27
b) 255
c) 1024
d) As many as required
View Answer
Answer: d
Explanation: Any class can have as many data members as required. The only
restriction that may arise is when there is not enough memory space. This gives
flexibility to define a class with best properties possible.

Subscribe Object Oriented Programming C++ Newsletter

6. How to access data members of a class?


a) Dot operator
b) Arrow operator
c) Dot or arrow as required
d) Dot, arrow or direct call
View Answer
Answer: c
Explanation: 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.

7. To create a pointer to a private data member of a class, outside the class, which
among the following is correct?
a) Return the address of the private data member using a member function
b) Access the private member using a pointer outside class
c) Declare the member as pointer inside the class
d) Not possible to create pointer to a private member
View Answer

C++ Page 88
Answer: a
Explanation: We can call a public member function and return the address of any
private data member. Though the pointer being returned must be defined inside
class itself. And the returned address can be stored in a pointer.

8. Which among the following is true for use of setter() and getter() function?
a) Considered best for manipulating data values
b) Considered the only proper way to manipulate the values
c) Considered specially for private members manipulation
d) Considered a red flag, and not recommended for large scale use
View Answer
Answer: d
Explanation: This concept of getter and setter functions is not acceptable if used too
much. This is considered to be inappropriate in OOP perspective. Though it is
commonly used, it doesn’t work according to OOP concepts at some higher level of
understanding.

9. What is the output of following code?

int n=10; // global


class A()
{
private : int n;
public : int m;
A()
{
n=100; m=50;
}
void disp()
{
cout<<”n”<<m<<n;
};

a) 1050100
b) 1005010

C++ Page 89
c) n5010
d) n50100
View Answer
Answer: d
Explanation: In cout we have specified n as a string to be printed. And m is a
variable so its value gets printed. And global variable will not be used since local
variable have more preference.

10. The static member functions can only use ________


a) Static data members
b) Private data members
c) Protected data members
d) Constant data members
View Answer
Answer: a
Explanation: The static member functions can only access static data members.
This is because the static member function can’t work with the properties that
change object to object. It is mandatory that only the common properties of all the
objects be used. And only static data members are common to all as those are
property of class.

Object Oriented Programming using C++


Questions and Answers – Member Functions

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Member Functions”.

1. Which among the following best describes member functions?


a) Functions which are defined within the class
b) Functions belonging a class
c) Functions in public access of a class

C++ Page 90
d) Functions which are private to class
View Answer
Answer: b
Explanation: We can’t say that only functions that are defined inside class are
member functions. There can be some inherited functions. Though they doesn’t
belong to the class but are property of the objects once inheritance is used. So the
nearest definition is functions belonging to a class.

2. How many types of member functions are generally there in C++?


a) 2
b) 3
c) 4
d) 5
View Answer
Answer: d
Explanation: There are 5 types of member functions that are generally provided in
C++. Namely, simple, static, const, inline and friend member functions. Member
functions are specific to classes.

3. How can a static member function be called in the main function?


a) Using dot operator
b) Using arrow operator
c) Using dot or arrow operator
d) Using dot, arrow or using scope resolution operator with class name
View Answer
Answer: d
Explanation: The member functions can be called using only the dot operator or the
arrow operator. But the static members can be called using directly the class name
followed by the scope resolution operator and static member function name. This is
useful when you don’t have any object to call the member.

4. What are inline member functions?


a) Member functions which can be called without object
b) Member functions whose definition is expanded in place of its call
c) Member functions whose definition is faster than simple function

C++ Page 91
d) Member function which is defined in single line
View Answer
Answer: b
Explanation: The member functions whose definition is expanded at the call, and no
jump to function and return happened, are termed as inline functions. This is used to
make the program faster and more efficient.

5. What happens if non static members are used in static member function?
a) Compile time error
b) Runtime error
c) Executes fine
d) Executes if that member function is not used
View Answer
Answer: a
Explanation: There must be specific memory space allocated for the data members
before the static member functions uses them. But the space is not reserved if
object is not declared. Hence only if static members are not used, it leads to compile
time error.

Subscribe Object Oriented Programming C++ Newsletter

6. Static member functions _____________


a) Contains “this” pointer for data members
b) Contains “this” pointer if used for member functions
c) Doesn’t contain “this” pointer
d) Doesn’t contain “this” pointer if member functions are referred
View Answer
Answer: c
Explanation: The static member functions doesn’t contain “this” pointer. Static
member functions can’t be defined as const or volatile also. These are restrictions
on static member functions.

7. How to access members of the class inside a member function?


a) Using this pointer only
b) Using dot operator
c) Using arrow operator

C++ Page 92
d) Used directly or with this pointer
View Answer
Answer: d
Explanation: The members of a class can be used directly inside a member function.
We can use this pointer when there is a conflict between data members of class and
arguments/local function variable names.

8. For overloading “( )”, “[ ]” or “->” operators, a class __________


a) Must use static member functions
b) Must use non-static member functions
c) Must be non-static member and should not be friend of class
d) Must use static member function or a friend member function
View Answer
Answer: c
Explanation: For overloading those operators for a class, the class must use non-
static member function so that doesn’t remain common to all the objects, and each
object can use it independently. The friend functions is also restricted so as to keep
the security of data.

9. If a virtual member function is defined ___________


a) It should not contain any body and defined by subclasses
b) It must contain body and overridden by subclasses
c) It must contain body and be overloaded
d) It must not contain any body and should not be derived
View Answer
Answer: a
Explanation: The virtual functions are defined using virtual keyword. These are
made in order to make all the classes to define them as the class gets inherited.
Increases code understanding.

10. Member functions of a generic class are _____________


a) Not generic
b) Automatically generic
c) To be made generic explicitly

C++ Page 93
d) Given default type as double
View Answer
Answer: b
Explanation: When generic type is used in a class, the functions are automatically
generic. This is so because the functions would use the same type as defined to
make the class generic. The functions will get to know the type of data as soon as
the generic class is used. It’s inbuilt feature.

Object Oriented Programming using C++


Questions and Answers – Passing and Returning
Object with Functions

This set of Object Oriented Programming using C++ Interview Questions and
Answers focuses on “Passing and Returning Object with Functions”.

1. In how many ways can an object be passed to a function?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: The objects can be passed in three ways. Pass by value, pass by
reference and pass by address. These are the general ways to pass the objects to a
function.

2. If an object is passed by value _____________


a) A new copy of object is created implicitly
b) The object itself is used

C++ Page 94
c) Address of the object is passed
d) A new object is created with new random values
View Answer
Answer: a
Explanation: When an object is passed by value, a new object is created implicitly.
This new object uses the implicit values assignment, same as that of the object
being passed.

3. Pass by address passes the address of object _________ and pass by reference
passes the address of the object _________
a) Explicitly, explicitly
b) Implicitly, implicitly
c) Explicitly, Implicitly
d) Implicitly, explicitly
View Answer
Answer: c
Explanation: Pass by address uses the explicit address passing to the function
whereas pass by reference implicitly passes the address of the object.

4. If an object is passed by reference, the changes made in the function


___________
a) Are reflected to the main object of caller function too
b) Are reflected only in local scope of the called function
c) Are reflected to the copy of the object that is made during pass
d) Are reflected to caller function object and called function object also
View Answer
Answer: a
Explanation: When an object is passed by reference, its address is passed implicitly.
This will make changes to the main function whenever any modification is done.

5. Constructor function is not called when an object is passed to a function, will its
destructor be called when its copy is destroyed?
a) Yes, depending on code
b) Yes, must be called
c) No, since no constructor was called

C++ Page 95
d) No, since same object gets used
View Answer
Answer: b
Explanation: Even though the constructor is not called when the object is passed to
a function, the copy of the object is still created, where the values of the members
are same. When the object have to be destroyed, the destructor is called to free the
memory and resources that the object might have reserved.

Subscribe Object Oriented Programming C++ Newsletter

6. When an object is returned by a function, a _______________ is automatically


created to hold the return value.
a) Temporary object
b) Virtual object
c) New object
d) Data member
View Answer
Answer: a
Explanation: The temporary object is created. It holds the return value. The values
gets assigned as required, and the temporary object gets destroyed.

7. Is the destruction of temporary object safe (while returning object)?


a) Yes, the resources get free to use
b) Yes, other objects can use the memory space
c) No, unexpected side effects may occur
d) No, always gives rise to exceptions
View Answer
Answer: c
Explanation: The destruction of temporary variable may give rise to unexpected
logical errors. Consider the destructor which may free the dynamically allocated
memory. But this may abort the program if another is still trying to copy the values
from that dynamic memory.

8. How to overcome the problem arising due to destruction of temporary object?


a) Overloading insertion operator
b) Overriding functions can be used

C++ Page 96
c) Overloading parenthesis or returning object
d) Overloading assignment operator and defining copy constructor
View Answer
Answer: d
Explanation: The problem can be solved by overloading the assignment operator to
get the values that might be getting returned while the destructor free the dynamic
memory. Defining copy constructor can help us to do this in even simpler way.

9. How many objects can be returned at once?


a) Only 1
b) Only 2
c) Only 16
d) As many as required
View Answer
Answer: a
Explanation: Like any other value, only one object can be returned at ones. The only
possible way to return more than one object is to return address of an object array.
But that again comes under returning object pointer.

10. What will be the output of the following code?

Class A
{
int i;
public : A(int n)
{
i=n; cout&lt;&lt;”inside constructor ”;
}
~A()
{
cout&lt;&lt;”destroying ”&lt;&lt;i;
}
void seti(int n)
{
i=n;

C++ Page 97
}
int geti()
{
return I;
}
};
void t(A ob)
{
cout&lt;&lt;”something ”;
}
int main()
{
A a(1);
t(a);
cout&lt;&lt;”this is i in main ”;
cout&lt;&lt;a.geti();
}

a) inside constructor something destroying 2this is i in main destroying 1


b) inside constructor something this is i in main destroying 1
c) inside constructor something destroying 2this is i in main
d) something destroying 2this is i in main destroying 1
View Answer
Answer: a
Explanation: Although the object constructor is called only ones, the destructor will
be called twice, because of destroying the copy of the object that is temporarily
created. This is the concept of how the object should be passed and manipulated.

Object Oriented Programming using C++


Questions and Answers – Object Array

C++ Page 98
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Object Array”.

1. What is an array of objects?


a) An array of instances of class represented by single name
b) An array of instances of class represented by more than one name
c) An array of instances which have more than 2 instances
d) An array of instances which have different types
View Answer
Answer: a
Explanation: The array of objects an array of instances of a class. The array is
represented by a single name. The array name is itself a pointer. Array name
represents the first object.

2. Which among the following is a mandatory condition for array of objects?


a) All the objects should be of different class
b) All the objects should be of same program classes
c) All the objects should be of same class
d) All the objects should have different data
View Answer
Answer: c
Explanation: The objects of an array must be of same class. This is mandatory
because array is set of same type of elements. The objects of same class are
considered to be of same type.

3. What is the type of elements of array of objects?


a) Class
b) Void
c) String
d) Null
View Answer
Answer: a
Explanation: The class itself is the type of elements of array of objects. All the
objects possess the same properties. Like any other primitive data type, the objects
are of their respective class type.

C++ Page 99
4. If array of objects is declared as given below, which is the limitation on objects?

Class_name arrayName[size];

a) The objects will have same values


b) The objects will not be initialized individually
c) The objects can never be initialized
d) The objects will have same data
View Answer
Answer: b
Explanation: If the syntax given, is used to declare the array of objects, then the
objects can’t be initialized individually. All the objects will have to be initialized after
this declaration.

Subscribe Object Oriented Programming C++ Newsletter

5. Which is the condition that must be followed if the array of objects is declared
without initialization, only with size of array?
a) The class should have separate constructor for each object
b) The class must have no constructors
c) The class should not have any member function
d) The class must have a default or zero argument constructor
View Answer
Answer: d
Explanation: The class must have a default/zero argument constructor. Since the
declaration is done by only specifying the size of array, the class must have default
a construct to be called by default to reserve memory for each object. Also, we can’t
specify the arguments in this type of declaration hence the class should provide a
default initialization.

6. When are the array of objects without any initialization useful?


a) When object data is not required just after the declaration
b) When initialization of object data is to be made by the compiler
c) When object data doesn’t matter in the program
d) When the object should contain garbage data
View Answer

C++ Page 100


Answer: a
Explanation: Sometimes the object data is not mandatory to be used just after the
declaration or may be the program requires the data to be updated according to
what user inputs. Hence only declaration us also useful.

7. If constructor arguments are passed to objects of array then ____________ if the


constructors are overloaded.
a) It is mandatory to pass same number of arguments to all the objects
b) It is mandatory to pass same type of arguments to all the objects
c) It is not mandatory to call same constructor for all the objects
d) It is mandatory to call same constructor for all the constructors
View Answer
Answer: c
Explanation: It is not mandatory to call the same constructor for all the objects in an
array if initialized with the declaration. The objects can be passed with different set
of arguments in the same syntax, separated by commas.

8. How the objects of array can be denoted?


a) Indices
b) Name
c) Random numbers
d) Alphabets
View Answer
Answer: a
Explanation: Different objects in an array can be denoted with the indices of array.
The first object is denoted by 0. And the further indices denote the next objects in
sequence of array.

9. The objects in an object array _______________________


a) Can be created without use of constructor
b) Can be created without calling default constructor
c) Can’t be created with use of constructor
d) Can’t be created without calling default constructor
View Answer

C++ Page 101


Answer: b
Explanation: The objects need some constructor to get the memory spaced reserved
for those. If the default constructor is not used then we can use some arguments
constructor which will reserve the memory for the objects. The objects can be
passed with constructor arguments during declaration.

10. The Object array is created in _____________________


a) Heap memory
b) Stack memory
c) HDD
d) ROM
View Answer
Answer: a
Explanation: If the object arrays are declared dynamically, then the memory will be
reserved on heap. The memory for objects will be on stack only if some constructor
or some call and return tasks are happening. The program doesn’t run on HDD and
ROM is not used for the execution of programs.

Object Oriented Programming using C++


Questions and Answers – Abstract Class

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Abstract Class”.

1. Which among the following best describes abstract classes?


a) If a class has more than one virtual function, it’s abstract class
b) If a class have only one pure virtual function, it’s abstract class
c) If a class has at least one pure virtual function, it’s abstract class
d) If a class has all the pure virtual functions only, then it’s abstract class
View Answer
Answer: c
Explanation: The condition for a class to be called abstract class is that it must have
C++ Page 102
at least one pure virtual function. The keyword abstract must be used while defining
abstract class in java.

2. Can abstract class have main() function defined inside it?


a) Yes, depending on return type of main()
b) Yes, always
c) No, main must not be defined inside abstract class
d) No, because main() is not abstract function
View Answer
Answer: b
Explanation: This is a property of abstract class. It can define main() function inside
it. There is no restriction on its definition and implementation.

3. If there is an abstract method in a class then, ________________


a) Class must be abstract class
b) Class may or may not be abstract class
c) Class is generic
d) Class must be public
View Answer
Answer: a
Explanation: It is a rule that if a class have even one abstract method, it must be an
abstract class. If this rule was not made, the abstract methods would have got
skipped to get defined in some places which are undesirable with the idea of
abstract class.

4. If a class is extending/inheriting another abstract class having abstract method,


then _______________________
a) Either implementation of method or making class abstract is mandatory
b) Implementation of the method in derived class is mandatory
c) Making the derived class also abstract is mandatory
d) It’s not mandatory to implement the abstract method of parent class
View Answer
Answer: a
Explanation: Either of the two things must be done, either implementation or

C++ Page 103


declaration of class as abstract. This is done to ensure that the method intended to
be defined by other classes gets defined at every possible class.

5. Abstract class A has 4 virtual functions. Abstract class B defines only 2 of those
member functions as it extends class A. Class C extends class B and implements
the other two member functions of class A. Choose the correct option below.
a) Program won’t run as all the methods are not defined by B
b) Program won’t run as C is not inheriting A directly
c) Program won’t run as multiple inheritance is used
d) Program runs correctly
View Answer
Answer: d
Explanation: The program runs correctly. This is because even class B is abstract so
it’s not mandatory to define all the virtual functions. Class C is not abstract but all the
virtual functions have been implemented will that class.

Subscribe Object Oriented Programming C++ Newsletter

6. Abstract classes can ____________________ instances.


a) Never have
b) Always have
c) Have array of
d) Have pointer of
View Answer
Answer: a
Explanation: When an abstract class is defined, it won’t be having the
implementation of at least one function. This will restrict the class to have any
constructor. When the class doesn’t have constructor, there won’t be any instance of
that class.

7. We ___________________ to an abstract class.


a) Can create pointers
b) Can create references
c) Can create pointers or references
d) Can’t create any reference, pointer or instance
View Answer

C++ Page 104


Answer: c
Explanation: Even though there can’t be any instance of abstract class. We can
always create pointer or reference to abstract class. The member functions which
have some implementation inside abstract itself can be used with these references.

8. Which among the following is an important use of abstract classes?


a) Header files
b) Class Libraries
c) Class definitions
d) Class inheritance
View Answer
Answer: b
Explanation: The abstract classes can be used to create a generic, extensible class
library that can be used by other programmers. This helps us to get some already
implemented codes and functions that might have not been provided by the
programming language itself.

9. Use of pointers or reference to an abstract class gives rise to which among the
following feature?
a) Static Polymorphism
b) Runtime polymorphism
c) Compile time Polymorphism
d) Polymorphism within methods
View Answer
Answer: b
Explanation: The runtime polymorphism is supported by reference and pointer to an
abstract class. This relies upon base class pointer and reference to select the proper
virtual function.

10. The abstract classes in java can _________________


a) Implement constructors
b) Can’t implement constructor
c) Can implement only unimplemented methods
d) Can’t implement any type of constructor
View Answer

C++ Page 105


Answer: a
Explanation: The abstract classes in java can define a constructor. Even though
instance can’t be created. But in this way, only during constructor chaining,
constructor can be called. When instance of concrete implementation class is
created, it’s known as constructor chaining.

Object Oriented Programming using C++ Questions


and Answers – Template Class

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Template Class”.

1. A template class can have _____________


a) More than one generic data type
b) Only one generic data type
c) At most two data types
d) Only generic type of integers and not characters
View Answer
Answer: a
Explanation: The template class can support more than one data type. The only
thing is to add all the data types required in a list separated by comma within
template specification.

2. Which among the following is the proper syntax for the template class?
a) template <typename T1, typename T2>;
b) Template <typename T1, typename T2>;
c) template <typename T> T named(T x, T y){ }
d) Template <typename T1, typename T2> T1 named(T1 x, T2 y){ }
View Answer
Answer: c
Explanation: The syntax must start with keyword template, case sensitive. Then it

C++ Page 106


should include the typename and a variable to denote it. Then whenever that
variable is used, it replaces it with the data type needed.

3. Can default arguments be used with the template class?


a) Yes, in some special cases
b) Yes, always
c) No, it must satisfy some specific conditions first
d) No, it can’t be done
View Answer
Answer: b
Explanation: The template class can use default arguments. This is used to specify
the data type to be considered if it is not specified while passing to the generic class.
The default type will be used.

4. What is the syntax to use explicit class specialization?


a) template <int> class myClass<>{ }
b) template <int> class myClass<int>{ }
c) template <> class myClass<>{ }
d) template <> class myClass<int>{ }
View Answer
Answer: d
Explanation: The class specialization is creation of explicit specialization of a generic
class. We have to use template<> constructor for this to work. It works in the same
way as with explicit function specialization.

5. Which is the most significant feature that arises by using template classes?
a) Code readability
b) Ease in coding
c) Code reusability
d) Modularity in code
View Answer
Answer: c
Explanation: The code reusability is the feature that becomes more powerful with the
use of template classes. You can generate a single code that can be used in variety
of programming situations.

C++ Page 107


Subscribe Object Oriented Programming C++ Newsletter

6. A template class defines the form of a class _____________________ it will


operate.
a) With full specification of the data on which
b) With full specification of the functions on which
c) Without full specification of the data on which
d) Without full specification of the functions on which
View Answer
Answer: c
Explanation: The template classes can accept all types of data types. There is no
need to specify the data on which the class has to operate. Hence it gives us
flexibility to code without worrying about the type of data that might be used in the
code.

7. What are the two specializations of I/O template classes in C++?


a) 16-bit character and wide characters
b) 8-bit character and wide characters
c) 32-bit character and locale characters
d) 64-bit characters and locale characters
View Answer
Answer: b
Explanation: The I/O specialization is made with wide character and 8-bit characters.
Wide characters are used to store the characters that might take more than 1 byte of
space in memory or any size that is different from the one that the machine is using.

8. Can typeid() function be used with the object of generic classes?


a) Yes, only if default type is given
b) Yes, always
c) No, generic data can’t be determined
d) No, never possible
View Answer
Answer: b
Explanation: The typeid() function can be used with the objects of generic classes.
An instance of a template class will take the type of data that is being used with it.

C++ Page 108


Hence when typeid() function is used, the data type would have already been
defined and hence we can get desired result from typeid() function.

9. The _____________ class is a specialization of a more general template class.


a) String
b) Integer
c) Digit
d) Math
View Answer
Answer: a
Explanation: The string class is more specialized. Since the string must be able to
store any kind of data that is given to the string. Hence it needs maximum
specialization.

10. How is function overloading different from template class?


a) Overloading is multiple function doing same operation, Template is multiple
function doing different operations
b) Overloading is single function doing different operations, Template is multiple
function doing different operations
c) Overloading is multiple function doing similar operation, Template is multiple
function doing identical operations
d) Overloading is multiple function doing same operation, Template is same function
doing different operations
View Answer
Answer: c
Explanation: The function overloading is multiple functions with similar or different
functionality but generic class functions perform the same task on given different
types of data.

Object Oriented Programming using C++


Questions and Answers – Base Class

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Base Class”.

C++ Page 109


1. Which is most appropriate definition of a base class?
a) It is parent of any of its derived class
b) It is child of one of the parent class
c) It is most basic class of whole program
d) It is class with maximum number of members
View Answer
Answer: a
Explanation: A class which is parent of another class, or from which other classes
can be derived, is known as a base class. It is mandatory that a class must have at
least one derived class to be called as a base class.

2. A base class is also known as _____________ class.


a) Basic
b) Inherited
c) Super
d) Sub
View Answer
Answer: c
Explanation: A class which is being derived by other classes, is called as super
class. This concept is clearly used in java as we call the functions of a base class by
using the keyword super as required.

3. An abstract class is always a __________ class.


a) Base
b) Derived
c) Template
d) Nested
View Answer
Answer: a
Explanation: Every abstract class is a base class. It must be so, because the
functions which are not defined inside the abstract class, must be defined in the
derived classes. Hence it becomes a base class.

4. How many base classes can a single class inherit in java?


a) 1

C++ Page 110


b) 2
c) 3
d) As many as required
View Answer
Answer: a
Explanation: In java, multiple inheritance is not supported, which leads to the fact
that a class can have only 1 parent class if inheritance is used. Only if interfaces are
used then the class can implement more than one base class.

5. How to make a derived class a base class?


a) Change name of the class
b) Use keyword base
c) Make a class derive from it
d) Can’t be done
View Answer
Answer: c
Explanation: Making another class derive from it will make that class as base class.
It is not necessary that we have to write different code for it. If at least one class
derives that class, it becomes the base class for the new class.

Subscribe Object Oriented Programming C++ Newsletter

6. If a base class is being derived by two other classes, which inheritance will that be
called?
a) Single
b) Multiple
c) Multi-level
d) Hierarchical
View Answer
Answer: d
Explanation: When more than one classes are being derived from a single parent
class, the inheritance is known as hierarchical inheritance. This is usually useful
when the base class is higher abstraction of its derived classes.

7. Which among the following must be in a base class?


a) Data members

C++ Page 111


b) Member functions
c) Access specifiers
d) Nothing
View Answer
Answer: d
Explanation: Even a class that doesn’t have any members can be a base class. It is
not mandatory to have any member or attribute in base class.

8. Which type of members can’t be accessed in derived classes of a base class?


a) Protected
b) Private
c) Public
d) All can be accessed
View Answer
Answer: b
Explanation: 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.

9. If a class is enclosing more than one class, than it can be called as base class of
those classes.
a) True
b) False
View Answer
Answer: b
Explanation: When a class have more than one nested classes, it is known as
enclosing class. It can’t be called as parent or base class since there is no
inheritance involved.

10. Base class have ________________ of abstraction.


a) Higher degree
b) Lower degree
c) Intermediate
d) Minimum degree
View Answer

C++ Page 112


Answer: b
Explanation: A base class will have lesser information as compared to those of
derived classes. Since derived classes inherit the base class properties and then
add on their own features, they elaborate more hence have lower degree of
abstraction.

11. Always the base class constructors are called ___________ constructor of
derived class.
a) Before
b) After
c) Along
d) According to priority of
View Answer
Answer: a
Explanation: When the base class object is created, its constructor will be called for
sure. But if a derived class constructor is called, first base class constructor is called
and then derived class constructor is taken into consideration.

12. Can we call methods of base class using the constructor of the derived class?
a) Yes, always
b) Yes, but not always
c) No, never
d) No, but we can call in some cases
View Answer
Answer: a
Explanation: If the function is defined in the base class, it can always be called from
the constructor of its derived class. Since the constructors are not private, they can
be accessed in derived class even if those are protected.

13. If a base class is inherited from another class and then one class derives it,
which inheritance is shown?
a) Multiple
b) Single
c) Hierarchical

C++ Page 113


d) Multi-level
View Answer
Answer: d
Explanation: If a base class is inherited from another class, single inheritance is
shown. But when one more class inherits the derived class, this becomes a multi-
level inheritance.

14. How many base classes can a single derived class have in C++?
a) 1
b) 2
c) 3
d) As many as required
View Answer
Answer: d
Explanation: This is because C++ allows multiple inheritance. A derived class can
have more than one base class and hence can derive all of their features.

15. If a base class is added with a few new members, its subclass must also be
modified.
a) True
b) False
View Answer
Answer: b
Explanation: The base class can be added with new members without affecting the
subclasses. This is because the subclasses may get some more features inherited
but it won’t use them. But the base class will be able to use the new members as
would be required.

Object Oriented Programming using C++


Questions and Answers – Derived Class
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Derived Class”.

C++ Page 114


1. Which among the following is best definition of a derived class?
a) A child class
b) A class which inherits one or more classes
c) A class with keyword derived
d) A class with more than one constructor
View Answer
Answer: b
Explanation: Any class which inherits one or more classes is a derived class. The
only condition is it must inherit at least one class in order to be called as a derived
class.

2. Which among the following is inherited by a derived class from base class?
a) Data members only
b) Member functions only
c) All the members except private members
d) All the members of base class
View Answer
Answer: c
Explanation: The class inheriting another class, inherits all the data members and
member functions that are not private. This is done to ensure the security features
with maximum flexibility.

3. If there is a derived class in a program, how many classes must be in that


program?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: b
Explanation: If there is a derived class in a program, there must be at least 2 classes
in that program. One is a base class and another derived class. Hence at least 2
classes must be there.

C++ Page 115


4. Which members can never be accessed in derived class from the base class?
a) Private
b) Protected
c) Public
d) All except private
View Answer
Answer: d
Explanation: There is no restriction for a derived class to access the members of the
base class until and unless the members are private. Private member are declared
so that those members are not accessible outside the class.

5. How many types of inheritance are supported in C++ for deriving a class?
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There are three types of inheritance possible. Private inheritance,
protected inheritance, and public inheritance. The inheritance defines the access
specifier to be used with the inherited members in the derived class.

Subscribe Object Oriented Programming C++ Newsletter

6. How many derived class can a single base class have?


a) 1
b) 2
c) 3
d) As many are required
View Answer
Answer: d
Explanation: There is no restriction on how many classes can inherit a single base
class. Hence there can be as many derived classes as required in a program from a
single base class.

C++ Page 116


7. Which among the following is correct?
a) Friend function of derived class can access non-private members of base class
b) Friend function of base class can access derived class members
c) Friend function of derived class can access members of only derived class
d) Friend function can access private members of base class of a derived class
View Answer
Answer: a
Explanation: The friend function of a class can access the non-private members of
base class. The reason behind is that the members of base class gets derived into
the derived class and hence become members of derived class too. Hence a friend
function can access all of those.

8. If a class is being derived using more than two base classes, which inheritance
will be used?
a) Single
b) Multi-level
c) Hierarchical
d) Multiple
View Answer
Answer: d
Explanation: The statement given is the definition of multiple inheritance with respect
to the derived class. The concept can be illustrated with many other samples but the
main aspects are base class and derived class only.

9. Derived class is also known as ______________ class.


a) Subclass
b) Small class
c) Big class
d) Noticeable class
View Answer
Answer: a
Explanation: It is just another name given to the derived classes. This is used while
denoting all the derived classes subsequent to a superclass.

C++ Page 117


10. If class A is derived from another derived class B which is derived from class C,
which class will have maximum level of abstraction?
a) Class A
b) Class B
c) Class C
d) All have the same level of abstraction
View Answer
Answer: c
Explanation: The abstraction level of class C will be maximum. This is because the
parent class have higher level of abstraction. Hence the parent of all other class will
have maximum level of abstraction.

11. If base class is an abstract class then derived class ______________ the
undefined functions.
a) Must define
b) Must become another abstract class or define
c) Must become parent class for
d) Must implement 2 definitions of
View Answer
Answer: b
Explanation: The function must be defined in the program which are not defined in
the base class. Hence the class must be defined as abstract of implement the
function definition in it.

12. How many classes can be derived from a derived class?


a) Only 1
b) At most 1
c) At least 1
d) As many as required
View Answer
Answer: d
Explanation: When a class is to be derived from another derived class, the derived
class behaves as a normal base class hence there are no restriction on how many
class can be derived from a derived class. The derived class again behaves as a
normal superclass.

C++ Page 118


13. The members of a derived class can never be derived.
a) True
b) False
View Answer
Answer: b
Explanation: This is not true that the members of a derived class can’t be derived.
All the classes are considered to be a normal class when used for derivation. The
members can be derived with respect to their access specifiers.

14. Which feature is not related to the derived classes among the following?
a) Inheritance
b) Encapsulation
c) Run time memory management
d) Compile time function references
View Answer
Answer: c
Explanation: The memory management is the feature that is not necessary for
derived classes that will be a part of whole program. The functions references must
be resolved for their proper use if inheritance is used.

15. Deriving a class in such a way that that the base class members are not
available for further inheritance is known as ___________________
a) Public inheritance
b) Protected inheritance
c) Protected or private inheritance
d) Private inheritance
View Answer
Answer: d
Explanation: The private members of a class can never be derived to another class.
When a class derives another class using private inheritance, all the members
become private members of the derived class. Hence these member won’t be
available for further inheritance.

C++ Page 119


Object Oriented Programming using C++ Questions
and Answers – Inheritance
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Inheritance”.

1. Which among the following best describes the Inheritance?


a) Copying the code already written
b) Using the code already written once
c) Using already defined functions in programming language
d) Using the data and functions into derived segment
View Answer
Answer: d
Explanation: It can only be indicated by using the data and functions that we use in
derived class, being provided by parent class. Copying code is nowhere similar to
this concept, also using the code already written is same as copying. Using already
defined functions is not inheritance as we are not adding any of our own features.

2. How many basic types of inheritance are provided as OOP feature?


a) 4
b) 3
c) 2
d) 1
View Answer
Answer: a
Explanation: There are basically 4 types of inheritance provided in OOP, namely,
single level, multilevel, multiple and hierarchical inheritance. We can add one more
type as Hybrid inheritance but that is actually the combination any types of
inheritance from the 4 basic ones.

3. Which among the following best defines single level inheritance?


a) A class inheriting a derived class
b) A class inheriting a base class
c) A class inheriting a nested class

C++ Page 120


d) A class which gets inherited by 2 classes
View Answer
Answer: b
Explanation: A class inheriting a base class defines single level inheritance.
Inheriting an already derived class makes it multilevel inheritance. And if base class
is inherited by 2 other classes, it is multiple inheritance.

4. Which among the following is correct for multiple inheritance?


a) class student{public: int marks;}s; class stream{int total;}; class topper:public
student, public stream{ };
b) class student{int marks;}; class stream{ }; class topper: public student{ };
c) class student{int marks;}; class stream:public student{ };
d) class student{ }; class stream{ }; class topper{ };
View Answer
Answer: a
Explanation: Class topper is getting derived from 2 other classes and hence it is
multiple inheritance. Topper inherits class stream and class student publicly and
hence can use its features. If only few classes are defined, there we are not even
using inheritance (as in option class student{ }; class stream{ }; class topper{ };).

5. Which programming language doesn’t support multiple inheritance?


a) C++ and Java
b) C and C++
c) Java and SmallTalk
d) Java
View Answer
Answer: d
Explanation: Java doesn’t support multiple inheritance. But that feature can be
implemented by using the interfaces concept. Multiple inheritance is not supported
because of diamond problem and similar issues.

Subscribe Object Oriented Programming C++ Newsletter

6. Which among the following is correct for a hierarchical inheritance?


a) Two base classes can be used to be derived into one single class
b) Two or more classes can be derived into one class

C++ Page 121


c) One base class can be derived into other two derived classes or more
d) One base class can be derived into only 2 classes
View Answer
Answer: c
Explanation: One base class can be derived into the other two derived classes or
more. If only one class gets derived by only 2 other classes, it is also hierarchical
inheritance, but it is not a mandatory condition, because any number of derived
classes can be there.

7. Which is the correct syntax of inheritance?


a) class derived_classname : base_classname{ /*define class body*/ };
b) class base_classname : derived_classname{ /*define class body*/ };
c) class derived_classname : access base_classname{ /*define class body*/ };
d) class base_classname :access derived_classname{ /*define class body*/ };
View Answer
Answer: c
Explanation: 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 after the
body is also must.

8. Which type of inheritance leads to diamond problem?


a) Single level
b) Multi-level
c) Multiple
d) Hierarchical
View Answer
Answer: c
Explanation: When 2 or more classes inherit the same class using multiple
inheritance and then one more class inherits those two base classes, we get a
diamond like structure. Here, ambiguity arises when same function gets derived into
2 base classes and finally to 3rd level class because same name functions are being
inherited.

C++ Page 122


9. Which access type data gets derived as private member in derived class?
a) Private
b) Public
c) Protected
d) Protected and Private
View Answer
Answer: a
Explanation: It is a rule, that when a derived class inherits the base class in private
access mode, all the members of base class gets derived as private members of the
derived class.

10. If a base class is inherited in protected access mode then which among the
following is true?
a) Public and Protected members of base class becomes protected members of
derived class
b) Only protected members become protected members of derived class
c) Private, Protected and Public all members of base, become private of derived
class
d) Only private members of base, become private of derived class
View Answer
Answer: a
Explanation: As the programming language rules apply, all the public and protected
members of base class becomes protected members of derived class in protected
access mode. It can’t be changed because it would hinder the security of data and
may add vulnerability in the program.

11. Members which are not intended to be inherited are declared as


________________
a) Public members
b) Protected members
c) Private members
d) Private or Protected members
View Answer
Answer: c
Explanation: Private access specifier is the most secure access mode. It doesn’t

C++ Page 123


allow members to be inherited. Even Private inheritance can only inherit protected
and public members.

Object Oriented Programming using C++


Questions and Answers – Types of Inheritance
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Types of Inheritance”.

1. How many types of inheritance are possible in C++?


a) 2
b) 3
c) 4
d) 5
View Answer
Answer: d
Explanation: There are five types of inheritance that are possible in C++. Single
level, Multilevel, multiple, hierarchical and hybrid. Here we count hybrid also
because it sometimes can bring up a new form of inheritance, Like inheritance using
multiple and hierarchical, which sometimes results in diamond problem.

2. Which among the following is true?


a) Java supports all types of inheritance
b) Java supports multiple inheritance
c) Java doesn’t support multiple inheritance
d) Java doesn’t support inheritance
View Answer
Answer: c
Explanation: Java doesn’t support multiple inheritance. This is done to avoid the
diamond problem that sometimes arises with inherited functions. Though, multiple
inheritance can be implemented in java using interfaces.

3. Which type of inheritance is illustrated by the following code?

C++ Page 124


class student{ public: int marks; };
class topper: public student { public: char grade; };
class average{ public: int makrs_needed; };
class section: public average{ public: char name[10]; };
class overall: public average{ public: int students; };

a) Single level
b) Multilevel and single level
c) Hierarchical
d) Hierarchical and single level
View Answer
Answer: d
Explanation: It is hierarchical inheritance and single level inheritance. Since class
topper is inheriting class student, it is single level inheritance. And then average is
inherited by section and overall, so it is hierarchical inheritance. But both of them are
separate. Hence it is not hybrid inheritance.

Subscribe Object Oriented Programming C++ Newsletter

4. Which among the following best describes multiple inheritance?


a) Two classes being parent of any other classes
b) Three classes being parent of other classes
c) More than one class being parent of other child classes
d) More than one class being parent of single child
View Answer
Answer: d
Explanation: If a class inherits more than one class, it is known as multiple
inheritance. This should not be referred with only two or three classes being
inherited. But there must be one class which inherits more than one class to be
called as multiple inheritance.

5. How many types of inheritance can be used at a time in a single program?


a) Any two types
b) Any three types
c) Any 4 types

C++ Page 125


d) Any type, any number of times
View Answer
Answer: d
Explanation: Any type of inheritance can be used in any program. There is no rule to
use only few types of inheritance. Only thing that matters is how the classes are
inherited and used.

6. Which type of inheritance results in the diamond problem?


a) Single level
b) Hybrid
c) Hierarchical
d) Multilevel
View Answer
Answer: b
Explanation: In diamond problem, hierarchical inheritance is used first, where two
different classes inherit the same class and then in turn a 4th class inherits the two
classes which had inherited the first class. Using more than one type of inheritance
here, it is known as hybrid inheritance.

7. If 6 classes uses single level inheritance with pair classes (3 pairs), which
inheritance will this be called?
a) Single
b) Multiple
c) Hierarchical
d) Multilevel
View Answer
Answer: a
Explanation: Here all the pairs are using single inheritance. And no different pairs
are inheriting same classes. Hence it can’t be called hybrid or multilevel inheritance.
You can say the single inheritance is used 3 times in that program.

8. Which among the following is correct for the following code?

class A
{

C++ Page 126


public : class B
{
public : B(int i): data(i)
{
}
int data;
}
};
class C: public A
{
class D:public A::B{ };
};

a) Multi-level inheritance is used, with nested classes


b) Multiple inheritance is used, with nested classes
c) Single level inheritance is used, with enclosing classes
d) Single level inheritance is used, with both enclosing and nested classes
View Answer
Answer: d
Explanation: Class C is inheriting Class A. Class D is inheriting class B, both are
nested. Hence it is single inheritance. For multiple inheritance, class C or D should
have inherited both class A and class B.

9. Which among the following is false?


a) If one class inherits the inherited class in single level inheritance, it is multi-level
inheritance
b) Hybrid inheritance always contains multiple inheritance
c) Hierarchical inheritance involves inheriting same class into more than one classes
d) Hybrid inheritance can involve any types of inheritance together
View Answer
Answer: b
Explanation: It is not necessary to have multiple inheritance in hybrid type. It can
have any type together. This doesn’t have to be of specific type always.

C++ Page 127


10. If class A has two nested classes B and C. Class D has one nested class E, and
have inherited class A. If E inherits B and C, then ________________
a) It shows multiple inheritance
b) It shows hierarchical inheritance
c) It shows multiple inheritance
d) Multiple inheritance among nested classes, and single level for enclosing classes
View Answer
Answer: d
Explanation: This involves the same concept of inheritance, where the nested
classes also follow the inheritance rules. The Enclosing classes are having single
inheritance. Nested classes involves multiple.

Object Oriented Programming using C++


Questions and Answers – Single Level
Inheritance

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Single Level Inheritance”.

1. Which among the following defines single level inheritance?


a) One base class derives another class
b) One derived class inherits from one base class
c) One base class inherits from one derived class
d) One derived class derives from another derived class
View Answer
Answer: b
Explanation: If only one base class is used to derive only one subclass, it is known
as single level inheritance. The reason of this name is that we inherit the base class
to one more level and stop the inheritance any further.

2. If class A and class B are derived from class C and class D, then
________________

C++ Page 128


a) Those are 2 pairs of single inheritance
b) That is multilevel inheritance
c) Those is enclosing class
d) Those are all independent classes
View Answer
Answer: a
Explanation: Since class A is derived from class C and then class B is derived from
class D, there are two pairs of classes which shows single inheritance. Those two
pairs are independent of each other though.

3. If single inheritance is used, program will contain ________________


a) At least 2 classes
b) At most 2 classes
c) Exactly 2 classes
d) At most 4 classes
View Answer
Answer: a
Explanation: The program will contain at least 2 classes in the sense of base and
derived classes. At least one base class and one derived class must be there. Types
of inheritance remains the same though.

4. Single level inheritance supports _____________ inheritance.


a) Runtime
b) Compile time
c) Multiple inheritance
d) Language independency
View Answer
Answer: a
Explanation: The runtime inheritance is done when object of a class is created to call
a method. At runtime the function is searched if it is in class of object. If not, it will
search in its parent classes and hierarchy for that method.

5. Which method in the code below is single level inherited?

Subscribe Object Oriented Programming C++ Newsletter

C++ Page 129


class A
{
protected int a, b;
public: void show()
{
cout&lt;&lt;a&lt;&lt;b;
}
};
class B: public A
{
public: void disp()
{
cout&lt;&lt;a++&lt;&lt;b++;
}
};
class C: private A, public B
{
void avg()
{
cout&lt;&lt;(a+b)/2;
}
};

a) Class A
b) Class B
c) Class C
d) None
View Answer
Answer: b
Explanation: Class B is using single level inheritance. Class C is using multiple
inheritance. And class A is parent of other two classes.

6. If single level inheritance is used and an abstract class is created with some
undefined functions, can its derived class also skip some definitions?
a) Yes, always possible

C++ Page 130


b) Yes, possible if only one undefined function
c) No, at least 2 undefined functions must be there
d) No, the derived class must implement those methods
View Answer
Answer: d
Explanation: The derived class must implement those methods. This is because the
parent class is abstract and hence will have some undefined functions which has to
be defined in derived classes. Since we are using single level inheritance, if derived
class doesn’t implement those functions then one more class has to be there which
will become multi-level inheritance.

7. Which among the following is false for single level inheritance?


a) There can be more than 2 classes in program to implement single inheritance
b) There can be exactly 2 classes to implement single inheritance in a program
c) There can be more than 2 independent classes involved in single inheritance
d) The derived class must implement all the abstract method if single inheritance is
used
View Answer
Answer: c
Explanation: If more than 2 independent classes are involved to implement the
single level inheritance, it won’t be possible as there must be only one child and one
parent class and none other related class.

8. Which concept will result in derived class with more features (consider maximum
3 classes)?
a) Single inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Hierarchical inheritance
View Answer
Answer: b
Explanation: If single inheritance is used then only feature of a single class are
inherited, and if multilevel inheritance is used, the 2nd class might have use private
inheritance. Hence only multiple inheritance can result in derived class with more

C++ Page 131


features. This is not mandatory but in a case if we consider same number of
features in each class, it will result the same.

9. Single level inheritance is safer than _____________


a) Multiple inheritance
b) Interfaces
c) Implementations
d) Extensions
View Answer
Answer: a
Explanation: Interfaces also represent a way of inheritance but is a wide word to
decide which inheritance we are talking about in it, hence can’t be considered.
Implementation and extensions also doesn’t match that level of specific idea. And
multiple inheritance not so safe as it might result in some ambiguity.

10. Which language doesn’t support single level inheritance?


a) Java
b) C++
c) Kotlin
d) All languages support it
View Answer
Answer: d
Explanation: All the languages support single level inheritance. Since any class can
inherit other classes as required, if single level inheritance was not allowed it would
result in failing a lot of features of OOP.

Object Oriented Programming using C++


Questions and Answers – Multilevel Inheritance

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Multilevel Inheritance”.

C++ Page 132


1. Which among the following best defines multilevel inheritance?
a) A class derived from another derived class
b) Classes being derived from other derived classes
c) Continuing single level inheritance
d) Class which have more than one parent
View Answer
Answer: b
Explanation: Only if the class is being derived from other derived class, it can be
called as multilevel inheritance. If a class is derived from another class, it is single
level inheritance. There must be more than one level of inheritance.

2. If there are 5 classes, E is derived from D, D from C, C from B and B from A.


Which class constructor will be called first if the object of E or D is created?
a) A
b) B
c) C
d) A and B
View Answer
Answer: a
Explanation: A is parent of all other classes indirectly. Since A is parent of B and B is
parent of C and so on till E. Class A constructor will be called first always.

3. If there are 3 classes. Class C is derived from class B and B is derived from A,
Which class destructor will be called at last if object of C is destroyed.
a) A
b) B
c) C
d) All together
View Answer
Answer: a
Explanation: The destructors are called in the reverse order of the constructors
being called. Hence in multilevel inheritance, the constructors are created from
parent to child, which leads to destruction from child to parent. Hence class A
destructor will be called at last.

C++ Page 133


4. Which Class is having highest degree of abstraction in multilevel inheritance of 5
levels?
a) Class at 1st level
b) Class 2nd last level
c) Class at 5th level
d) All with same abstraction
View Answer
Answer: a
Explanation: The class with highest degree of abstraction will be the class at the 1st
level. You can look at a simple example like, a CAR is more abstract than SPORTS
CAR class. The level of abstraction decrease with each level as more details comes
out.

5. If all the classes use private inheritance in multilevel inheritance then


______________
a) It will not be called multilevel inheritance
b) Each class can access only non-private members of its parent
c) Each subsequent class can access all members of previous level parent classes
d) None of the members will be available to any other class
View Answer
Answer: b
Explanation: The classes will be able to access only the non-private members of its
parent class. The classes are using private inheritance, hence all the members of
the parent class become private in the derived class. In turn those won’t be allowed
for further inheritance or direct access outside the class.

Subscribe Object Oriented Programming C++ Newsletter

6. Multilevel inheritance allows _________________ in the program.


a) Only 7 levels of inheritance
b) At least 7 levels of inheritance
c) At most 16 levels of inheritance
d) As many levels of inheritance as required
View Answer

C++ Page 134


Answer: d
Explanation: The multilevel inheritance allows any number of levels of inheritance.
This is the maximum flexibility feature to make the members available to all the new
classes and to add their own functionalities. The code reusability is used too.

7. What is the minimum number of levels for a implementing multilevel inheritance?


a) 1
b) 2
c) 3
d) 4
View Answer
Answer: c
Explanation: There must be at least 3 levels of inheritance. Otherwise if less, it will
be single level inheritance or would have got no inheritance implemented. There
must be a derived class from which another class is derived.

8. In multilevel inheritance one class inherits _______________


a) Only one class
b) More than one class
c) At least one class
d) As many classes as required
View Answer
Answer: a
Explanation: The classes inherit only from one class. This continues as each class
inherits only one class. There should not be any class that inherits from two or more
classes or which have more than one subclass.

9. All the classes must have all the members declared private to implement
multilevel inheritance.
a) True
b) False
View Answer
Answer: b
Explanation: There is no mandatory rule to make the members private for multilevel

C++ Page 135


inheritance. Moreover, if all the classes have only the private members then there
won’t be any member to get inherited. Hence the working will be of no use.

10. Can abstract classes be used in multilevel inheritance?


a) Yes, always
b) Yes, only one abstract class
c) No, abstract class doesn’t have constructors
d) No, never
View Answer
Answer: a
Explanation: The abstract classes can always be used in multilevel inheritance. The
only condition that may arise is that all the undefined functions must be defined in
subclasses. There must not be any undefined function.

Object Oriented Programming using C++


Questions and Answers – Multiple Inheritance

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Multiple Inheritance”.

1. Multiple inheritance is ____________________


a) When a class is derived from another class
b) When a class is derived from two or more classes
c) When a class is derived from other two derived classes
d) When a class is derived from exactly one class
View Answer
Answer: b
Explanation: The multiple inheritance is used when a class is being derived using
two base classes or more. This way a single class can have features of more than
one classes inherited into a single unit. This lets us combine two class members into
a single class.

C++ Page 136


2. Which problem arises due to multiple inheritance, if hierarchical inheritance is
used previously for its base classes?
a) Diamond
b) Circle
c) Triangle
d) Loop
View Answer
Answer: a
Explanation: The diamond problem arises when multiple inheritance is used. This
problem arises because the same name member functions get derived into a single
class. Which in turn creates ambiguity in calling those methods.

3. How many classes should a program contain to implement the multiple


inheritance?
a) Only 1
b) At least 1
c) At least 3
d) Exactly 3
View Answer
Answer: c
Explanation: For the implementation of multiple inheritance, there must be at least 3
classes in a program. At least 2 base classes and one class to inherit those two
classes. If lesser, it becomes single level inheritance.

4. Which programming language restricts the use of multiple inheritance?


a) C++
b) PHP
c) SmallTalk
d) Java
View Answer
Answer: d
Explanation: Java doesn’t allow use of multiple inheritance with classes. But this can
be done by using the interfaces. This is more secure and unambiguous way to
implement multiple inheritance.

C++ Page 137


5. Is it possible to have all the abstract classes as base classes of a derived class
from those?
a) Yes, always
b) Yes, only if derived class implements all the methods
c) No, because abstract classes doesn’t have constructors
d) No, never
View Answer
Answer: b
Explanation: The condition for abstract class applies same here too. All the
undefined functions must be defined. Hence all the base classes can be abstract but
derived class must implement all those undefined functions.

Subscribe Object Oriented Programming C++ Newsletter

6. If class A inherits class B and class C as “class A: public class B, public class C {//
class body ;}; ”, which class constructor will be called first?
a) Class A
b) Class B
c) Class C
d) All together
View Answer
Answer: b
Explanation: The constructors of parent class will be called first. In that, the
constructor of the classes will be called in the same sequence as that mentioned in
class definition inheritance. Since class B is mentioned first for inheritance, its
constructor will be called first.

7. Why does diamond problem arise due to multiple inheritance?


a) Methods with same name creates ambiguity and conflict
b) Methods inherited from the superclass may conflict
c) Derived class gets overloaded with more than two class methods
d) Derived class can’t distinguish the owner class of any derived method
View Answer
Answer: a
Explanation: All the derived classes can distinguish the base class members, but if a

C++ Page 138


method is being inherited to the base classes from another class which again gets
inherited into same class (diamond shape), that may create conflict in using the
function from two available.

8. How many base classes can a derived class have which is implementing multiple
inheritance?
a) Only 2
b) At least 2
c) At most 2
d) As many as required
View Answer
Answer: d
Explanation: The classes can derive from as many classes as required since the
multiple inheritance feature is made to combine or group together the functions that
are from different classes. This make the derived class stronger in terms of its
flexibility.

9. How to overcome diamond problem?


a) Using alias name
b) Using seperate derived class
c) Using virtual keyword with same name function
d) Can’t be done
View Answer
Answer: c
Explanation: To overcome the ambiguity and conflict we can use keyword virtual.
This will help us to differentiate the functions with same name that came to last
derived class in diamond problem.

10. When multiple inheritance is used, which class object should be used in order to
access all the available members of parent and derived class?
a) Derived class object
b) Parent class objects
c) Use Abstract derived class
d) Derive a class from derived class
View Answer

C++ Page 139


Answer: a
Explanation: The derived class object can access all of its own members. It can also
access the available members of the parent classes, because the members are
derived into the derived class.

11. If all the members of all the base classes are private then _____________
a) There won’t be any use of multiple inheritance
b) It will make those members public
c) Derived class can still access them in multiple inheritance
d) Compile time error
View Answer
Answer: a
Explanation: The derived class will not be able to access any members of the base
classes. Since private member’s are not inheritable. It leads to no use of multiple
inheritance.

Object Oriented Programming using C++


Questions and Answers – Hierarchical
Inheritance

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Hierarchical Inheritance”.

1. Which among the following is best to define hierarchical inheritance?


a) More than one classes being derived from one class
b) More than 2 classes being derived from single base class
c) At most 2 classes being derived from single base class
d) At most 1 class derived from another class
View Answer
Answer: a
Explanation: When two or more classes get derived from a single base class, it is

C++ Page 140


known as hierarchical inheritance. This gives us freedom to use same code with
different scopes and flexibility into different classes.

2. Do members of base class gets divided among all of its child classes?
a) Yes, equally
b) Yes, depending on type of inheritance
c) No, it’s doesn’t get divided
d) No, it may or may not get divided
View Answer
Answer: c
Explanation: The class members doesn’t get divided among the child classes. All the
members get derived to each of the subclasses as whole. The only restriction is
from the access specifiers used.

3. Each class can inherit the base class ________________


a) Independently using any inheritance
b) Independently with private inheritance only
c) With same type of inheritance
d) With each class using different inheritance only
View Answer
Answer: a
Explanation: The classes can inherit the base class using any type of inheritance.
There is no mandatory condition to use same private,public or protected inheritance
only.

4. How many classes must be there to implement hierarchical inheritance?


a) Exactly 3
b) At least 3
c) At most 3
d) At least 1
View Answer
Answer: b
Explanation: At least 3 classes must be there. Two derived classes and one base
class. This lets us implement two classes that have common characteristics from
base class.

C++ Page 141


5. Base class _______________
a) Can be made abstract
b) Can’t be made abstract
c) Must be abstract
d) If made abstract, compile time error
View Answer
Answer: a
Explanation: The base class may or may not be declared abstract. It depends on the
need of program. If it is made abstract, it can contain undefined functions too. In
turn, those functions will have to be implemented by each of the derived classes.

Subscribe Object Oriented Programming C++ Newsletter

6. Which access specifiers should be used so that all the derived classes restrict
further inheritance of base class members?
a) Private
b) Public
c) Protected
d) Any inheritance type can be used
View Answer
Answer: a
Explanation: All the derived classes must use private inheritance. This will make the
members of base class private in derived classes. Hence none of the members of
base class will be available for further inheritance.

7. Which class uses hierarchical inheritance in following code?

class A
{
int a;
};
class B:class A
{
int b;
};

C++ Page 142


class C:class A,class B
{
int c;
};
class D:class A
{
int d;
};

a) Class A, B, C
b) Class B, C, D
c) Class A, C, D
d) Class D, A, B
View Answer
Answer: d
Explanation: Class A is base class and B and D are derived classes. If class C is
considered, it shows hybrid inheritance, involving single level and multiple
inheritance.

8. Which among the following is correct for following code?

abstract class A
{
public Int a;
public void disp();
};
class B:public A
{
public: void dis()
{
court&lt;&lt;a;
}
};
class C:private A
{

C++ Page 143


public void incr()
{
a++;
}
}
void main()
{
B b.disp();
}

a) Compile time error


b) Runtime error
c) Program runs and o/p is 0
d) Program runs and o/p is garbage value
View Answer
Answer: a
Explanation: The derived class D have not implemented the undefined function.
Here the main concept involves hierarchical inheritance with abstract base class.

9. How many classes can be derived from the base class using hierarchical
inheritance?
a) As many as required
b) Only 7
c) Only 3
d) Up to 127
View Answer
Answer: a
Explanation: The number of classes that can be derived from a base class doesn’t
have any restriction and hence will be able to derive as many classes as required.
This feature gives more flexibility and code reusability.

10. If one class have derived the base class privately then another class can’t derive
the base class publically.
a) True

C++ Page 144


b) False
View Answer
Answer: b
Explanation: The classes are independent and can access the base class and
inherit it in whichever way it is required. The classes can use the base base class
members privately or publically maintaining the security of data and methods.

Object Oriented Programming using C++


Questions and Answers – Hybrid Inheritance

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Hybrid Inheritance”.

1. Which among the following best defines the hybrid inheritance?


a) Combination of two or more inheritance types
b) Combination of same type of inheritance
c) Inheritance of more than 7 classes
d) Inheritance involving all the types of inheritance
View Answer
Answer: a
Explanation: When more than one type of inheritance are used together, it results in
new type of inheritance which is in general known as hybrid inheritance. This may of
may not have better capabilities.

2. How many types of inheritance should be used for hybrid?


a) Only 1
b) At least 2
c) At most two
d) Always more than 2
View Answer
Answer: b
Explanation: There must be combination of at least 2 types of inheritance. The
inheritance should be of different type.

C++ Page 145


3. If single inheritance is used with class A and B. A is base class. Then class C, D
and E where C is base class and D is derived from C, then E is derived from D.
Class C is made to inherit from class B. Which is the resultant type?
a) Single level
b) Multilevel
c) Hybrid
d) Multiple
View Answer
Answer: b
Explanation: The statement represents multilevel inheritance. It is not hybrid since
looking at complete idea, one can’t differentiate whether two type of inheritance are
used. Hence it is multilevel inheritance.

4. Diamond problem includes ____________________ hybrid inheritance.


a) Hierarchical and Multiple
b) Hierarchical and Hierarchical
c) Multiple and Multilevel
d) Single, Hierarchical and Multiple
View Answer
Answer:a
Explanation: The diamond problem arises when more than one classes are derived
from one class and then those classes are used to derive single clas. Resulting in
ambiguity of same functions from each class.

5. If __________________ inheritance is done continuously, it is similar to tree


structure.
a) Hierarchical
b) Multiple
c) Multilevel
d) Hierarchical and Multiple
View Answer
Answer: a
Explanation: Hierarchical inheritance is deriving more than one classes from a base
class, it it is done continuously and subsequently, it results forming a tree like
structure of classes being linked.

C++ Page 146


Subscribe Object Oriented Programming C++ Newsletter

6. Which amongst the following is true for hybrid inheritance?


a) Constructor calls are in reverse
b) Constructor calls are priority based
c) Constructor of only derived class is called
d) Constructor calls are usual
View Answer
Answer: d
Explanation: The constructors will be called in usual way. First the parent class
Constructor and then the derived class Constructors. This is done to initialise all the
members properly.

7. Which type of inheritance must be used so that the resultant is hybrid?


a) Multiple
b) Hierarchical
c) Multilevel
d) None
View Answer
Answer: d
Explanation: The use of any specific type is not necessary. Though the final
structure should not be the same, it should represent more than one type of
inheritance if class diagram is drawn.

8. The private member’s are made public to all the classes in inheritance.
a) True
b) False
View Answer
Answer: b
Explanation: The private member’s scope can’t be changed and those can never be
accessed in other classes. Only the class containing private member’s can access
its own members.

9. If hierarchical inheritance requires to inherit more than one class to single class,
which syntax is correct? (A, B, C are class names)
a) hierarchical class A: public B, public C

C++ Page 147


b) multiple class A: public B, public C
c) many class A: public B, public C
d) class A: public B, public C
View Answer
Answer: d
Explanation: The syntax is as same as declaration of other classes. There is no
specific keyword defined for using hybrid inheritance in programming. Only thing is
to specify the class name separated by commas.

10. What is the maximum number of classes allowed in hybrid inheritance?


a) 7
b) 127
c) 255
d) As many as required
View Answer
Answer: d
Explanation: The classes in any type of inheritance can inherit as many classes as
required. The only condition that may arise is memory management. The classes
can inherit most of the features from more than one class.

11. What is the minimum number of classes to be there in a program implementing


hybrid inheritance?
a) 2
b) 3
c) 4
d) No limit
View Answer
Answer: d
Explanation: The answer is no limit. There is no condition defined for limit of classes
that has to be used in hybrid. Though you must have at least 4 classes so that one
set of multiple or hierarchical inheritance is there and one more class to use single
level inheritance.

C++ Page 148


Object Oriented Programming using C++
Questions and Answers – Virtual Functions

This set of Object Oriented Programming using C++ Questions and Answers for
Entrance exams focuses on “Virtual Functions”.

1. Virtual function is ______ class function which expected to be redefined in


______ class, so that when reference is made to derived class object using pointer
then we can call virtual function to execute ________ class definition version.
a) Base, derived, derived
b) Derived, Derived, Derived
c) Base, derived, base
d) Base, base, derived
View Answer
Answer: a
Explanation: The functions which may give rise to ambiguity due to inheritance, can
be declared virtual. So that whenever derived class object is referred using pointer
or reference to the base class methods, we can still call the derived class methods
using virtual function. Hence this differentiates those methods from each other.

2. What does a virtual function ensure for an object, among the following?
a) Correct method is called, regardless of the class defining it
b) Correct method is called, regardless of the object being called
c) Correct method is called, regardless of the type of reference used for function call
d) Correct method is called, regardless of the type of function being called by objects
View Answer
Answer: c
Explanation: It is property of the virtual function and one of their main use. Its use
ensure that the correct method is called even though it is been called from different
pointer or references. This also decreases chance of mistakes in program.

3. Virtual functions are mainly used to achieve _____________


a) Compile time polymorphism
b) Interpreter polymorphism

C++ Page 149


c) Runtime polymorphism
d) Functions code polymorphism
View Answer
Answer: c
Explanation: It is used to achieve runtime polymorphism. The functions which are
inherited and overridden, so at runtime the correct function is executed. The correct
function call is made from the intended class.

4. Which keyword is used to declare virtual functions?


a) virtual
b) virt
c) anonymous
d) virtually
View Answer
Answer: a
Explanation: The virtual keyword is used to declare virtual functions. Anonymous
keyword is used with classes and have a different meaning. The virtual functions are
used to call the intended function of the derived class.

5. Where the virtual function should be defined?


a) Twice in base class
b) Derived class
c) Base class and derived class
d) Base class
View Answer
Answer: d
Explanation: The virtual function should be declared in base class. So that when the
derived class inherits from the base class, the functions can be differentiated from
the one in base class and another in derived class.

Subscribe Object Oriented Programming C++ Newsletter

6. The resolving of virtual functions is done at ______________


a) Compile time
b) Interpret time
c) Runtime

C++ Page 150


d) Writing source code
View Answer
Answer: c
Explanation: The resolving of virtual functions that are to be called is done at run
time. The base class and the derived classes may contain different definitions and
different variables, so all these things are resolved at run time and decided which
function is to be called.

7. In which access specifier should a virtual function be defined?


a) Private
b) Public
c) Protected
d) Default
View Answer
Answer: b
Explanation: The virtual functions must be defined in public section of a class. This
is to ensure that the virtual function is available everywhere in the program. Also to
avoid any error while resolving the method.

8. Virtual functions can never be made _______________


a) Static function
b) Parameterized function
c) Default argument function
d) Zero parameter function
View Answer
Answer: a
Explanation: The virtual function must not be static. Those functions are the property
of individual objects and not of a class as a whole. The functions should not be
made common for all the objects of that class.

9. Virtual functions can’t be made friend function of other classes.


a) True
b) False
View Answer

C++ Page 151


Answer: a
Explanation: The friend functions can access the private members also. This may
hinder the security of class members. This is why the functions should not be made
friend functions of other class.

10. Which is a must condition for virtual function to achieve runtime polymorphism?
a) Virtual function must be accessed with direct name
b) Virtual functions must be accessed using base class object
c) Virtual function must be accessed using pointer or reference
d) Virtual function must be accessed using derived class object only
View Answer
Answer: c
Explanation: The virtual functions must be called using pointer or reference. This is
mandatory so that the intended function gets executed while resolving the method at
runtime. The must not be any ambiguity between the method of parent class and
derived class.

Object Oriented Programming using C++


Questions and Answers – Abstract Function

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on ” Abstract Function”.

1. Which among the following best defines the abstract methods?


a) Functions declared and defined in base class
b) Functions only declared in base class
c) Function which may or may not be defined in base class
d) Function which must be declared in derived class
View Answer
Answer: b
Explanation: The abstract functions must only be declared in base class. Their
definitions are provided by the derived classes. It is a mandatory condition.

C++ Page 152


2. Which among the following is true?
a) The abstract functions must be only declared in derived classes
b) The abstract functions must not be defined in derived classes
c) The abstract functions must be defined in base and derived class
d) The abstract functions must be defined either in base or derived class
View Answer
Answer: a
Explanation: The abstract functions can’t be defined in base class. They are to be
defined in derived classes. It is a rule for abstract functions.

3. How are abstract functions different from the abstract functions?


a) Abstract must not be defined in base class whereas virtual function can be
defined
b) Either of those must be defined in base class
c) Different according to definition
d) Abstract functions are faster
View Answer
Answer: a
Explanation: The abstract functions are only declared in base class. Derived classes
have to implement those functions in order to inherit that base class. The functions
are always defined in derived classes only.

4. Which among the following is correct?


a) Abstract functions should not be defined in all the derived classes
b) Abstract functions should be defined only in one derived class
c) Abstract functions must be defined in base class
d) Abstract functions must be defined in all the derived classes
View Answer
Answer: d
Explanation: The abstract function are only declared in base classes and then has to
be defined in all the derived classes. This allows all the derived classes to define
own definition of any function whose declaration in base class might be common to
all the other derived classes.

C++ Page 153


5. It is ____________________ to define the abstract functions.
a) Mandatory for all the classes in program
b) Necessary for all the base classes
c) Necessary for all the derived classes
d) Not mandatory for all the derived classes
View Answer
Answer: c
Explanation: The derived classes must define the abstract function of base class in
their own body. This is a necessary condition. Because the abstract functions
doesn’t contain any definition in base class and hence becomes mandatory for the
derived class to define them. All the functions in a program must have some
definition.

Subscribe Object Oriented Programming C++ Newsletter

6. The abstract function definitions in derived classes is enforced at _________


a) Runtime
b) Compile time
c) Writing code time
d) Interpreting time
View Answer
Answer: b
Explanation: When the program is compiled, these definitions are checked if
properly defined. This compiler also ensure that the function is being defined by all
the derived classes. Hence we get a compile time error if not done.

7. What is this feature of enforcing definitions of abstract function at compile time


called?
a) Static polymorphism
b) Polymorphism
c) Dynamic polymorphism
d) Static or dynamic according to need
View Answer
Answer: c
Explanation: The feature is known as Dynamic polymorphism. Because the

C++ Page 154


definitions are resolved at runtime. Even though the definitions are checked at
compile time, they are resolved at runtime only.

8. What is the syntax for using abstract method?


a) <access-modifier>abstract<return-type>method_name (parameter)
b) abs<return-type>method name (parameter)
c) <access-modifier>abstract return-type method name (parameter)
d) <access-modifier>abstract <returning> method name (parameter)
View Answer
Answer: a
Explanation: The syntax must firstly contain the access modifier. Then the keyword
abstract is written to mention clearly to the compiler that it is an abstract method.
Then prototype of the function with return type, function name and parameters.

9. If a function declared as abstract in base class doesn’t have to be defined in


derived class then ______
a) Derived class must define the function anyhow
b) Derived class should be made abstract class
c) Derived class should not derive from that base class
d) Derived class should not use that function
View Answer
Answer: b
Explanation: If the function that is not to be defined in derived class but is declared
as abstract in base class then the derived class must be made an abstract class.
This will make the concept mandatory that the derived class must have one
subclass to define that method.

10. Static methods can’t be made abstract in java.


a) True
b) False
View Answer
Answer: a
Explanation: The abstract functions can’t be made static in a program. If those are
made static then the function will be a property of class rather than each object. In

C++ Page 155


turn ever object or derived class must use the common definition given in the base
class. But abstract functions can’t be defined in the base class. Hence not possible.

11. Which among the following is true?


a) Abstract methods can be static
b) Abstract methods can be defined in derived class
c) Abstract methods must not be static
d) Abstract methods can be made static in derived class
View Answer
Answer: c
Explanation: The abstract methods can never be made static. Even if it is in derived
class, it can’t be made static. If this happens, then all the subsequent sub classes
will have a common definition of abstract function which is not desirable.

Object Oriented Programming using C++


Questions and Answers – Types of Member
Functions

This set of Object Oriented Programming using C++ Problems focuses on “Types of
Member Functions”.

1. How many types of member functions are possible in general?


a) 2
b) 3
c) 4
d) 5
View Answer
Answer: d
Explanation: There are basically 5 types of member functions possible. The types
include simple, static, const, inline, and friend member functions. Any of these types
can be used in a program as per requirements.

C++ Page 156


2. Simple member functions are ______________________
a) Ones defined simply without any type
b) Ones defined with keyword simple
c) Ones that are implicitly provided
d) Ones which are defined in all the classes
View Answer
Answer: a
Explanation: When there is no type defined for any function and just a simple syntax
is used with the return type, function name and parameter list then those are known
as simple member functions. This is a general definition of simple members.

3. What are static member functions?


a) Functions which use only static data member but can’t be accessed directly
b) Functions which uses static and other data members
c) Functions which can be accessed outside the class with the data members
d) Functions using only static data and can be accessed directly in main() function
View Answer
Answer: d
Explanation: The static member functions can be accessed directly in the main
function. There is no restriction on direct use. We can call them with use of objects
also. But the restriction is that the static member functions can only use the static
data members of the class.

4. How can static member function can be accessed directly in main() function?
a) Dot operator
b) Colon
c) Scope resolution operator
d) Arrow operator
View Answer
Answer: c
Explanation: The static member functions can be accessed directly in the main()
function. The only restriction is that those must use only static data members of the
class. These functions are property of class rather than each object.

C++ Page 157


5. Correct syntax to access the static member functions from the main() function is
______________
a) classObject::functionName();
b) className::functionName();
c) className:classObject:functionName();
d) className.classObject:functionName();
View Answer
Answer: b
Explanation: The syntax in option b must be followed in order to call the static
functions directly from the main() function. That is a predefined syntax. Scope
resolution helps to spot the correct function in the correct class.

Subscribe Object Oriented Programming C++ Newsletter

6. What are const member functions?


a) Functions in which none of the data members can be changed in a program
b) Functions in which only static members can be changed
c) Functions which treat all the data members as constant and doesn’t allow
changes
d) Functions which can change only the static members
View Answer
Answer: c
Explanation: The const member functions are intended to keep the value of all the
data members of a class same and doesn’t allow any changes on them. The data
members are treated as constant data and any modification inside the const function
is restricted.

7. Which among the following best describes the inline member functions?
a) Functions defined inside the class only
b) Functions with keyword inline only
c) Functions defined outside the class
d) Functions defined inside the class or with the keyword inline
View Answer
Answer: d
Explanation: The functions which are defined with the keyword inline or are defined

C++ Page 158


inside the class are treated to be inline functions. Definitions inside the class are
implicitly made inline if none of the complex statements are used in the definition.

8. What are friend member functions (C++)?


a) Member function which can access all the members of a class
b) Member function which can modify any data of a class
c) Member function which doesn’t have access to private members
d) Non-member functions which have access to all the members (including private)
of a class
View Answer
Answer: d
Explanation: A non-member function of a class which can access even the private
data of a class is a friend function. It is an exception on access to private members
outside the class. It is sometimes considered as a member functions since it has all
the access that a member function in general have.

9. What is the syntax of a const member function?


a) void fun() const {}
b) void fun() constant {}
c) void const fun() {}
d) const void fun(){}
View Answer
Answer: a
Explanation: The general syntax to be followed in order to declare a const function in
a class is as in option a. The syntax may vary in different programming languages.

10. Which keyword is used to make a nonmember function as friend function of a


class?
a) friendly
b) new
c) friend
d) connect
View Answer
Answer: c
Explanation: The keyword friend is provided in programming languages to use it

C++ Page 159


whenever a functions is to be made friend of one class or other. The keyword
indicates that the function is capable of new functionalities like accessing private
members.

11. Member functions _____________________


a) Must be defined inside class body
b) Can be defined inside class body or outside
c) Must be defined outside the class body
d) Can be defined in another class
View Answer
Answer: c
Explanation: The functions definitions can be given inside or outside the body of
class. If defined inside, general syntax is used. If defined outside then the class
name followed by scope resolution operator and then function name must be given
for the definition.

Object Oriented Programming using C++


Questions and Answers – Overloading Member
Functions

This set of Object Oriented Programming using C++ Questions and Answers for
Freshers focuses on “Overloading Member Functions”.

1. What does memory allocation for objects mean?


a) Actual creation and memory allocation for object members
b) Creation of member functions
c) Creation of data members for a class
d) Actual creation and data declaration for object members
View Answer
Answer: a
Explanation: The memory allocated for the object members indicates actual creation
of the object members. This is known as memory allocation for object.

C++ Page 160


2. Where is the memory allocated for the objects?
a) HDD
b) Cache
c) RAM
d) ROM
View Answer
Answer: c
Explanation: The memory for the objects or any other data is allocated in RAM
initially. This is while we run a program all the memory allocation takes place in
some RAM segments. Arrays in heap and local members in stack etc.

3. When is the memory allocated for an object?


a) At declaration of object
b) At compile time
c) When object constructor is called
d) When object is initialized to another object
View Answer
Answer: c
Explanation: The object memory allocation takes place when the object constructor
is called. Declaration of an object doesn’t mean that memory is allocated for its
members. If object is initialized with another object, it may just get a reference to the
previously created object.

4. Using new is type safe as _______________________


a) It require to be specified with type of data
b) It doesn’t require to be specified with type of data
c) It requires the name of data
d) It allocated memory for the data
View Answer
Answer: b
Explanation: The new is type safe because we don’t have to specify the type of data
that have to be allocated with memory. We can directly use it with data name. Name
of the data doesn’t matter though for type of memory allocation though.

C++ Page 161


5. Which of the following function can be used for dynamic memory allocation of
objects?
a) malloc()
b) calloc()
c) create()
d) both malloc() and calloc()
View Answer
Answer: d
Explanation: The malloc() function can be used to allocate dynamic memory for
objects. Function calloc() can also be use. These functions differ in the way they
allocate memory for objects.

Subscribe Object Oriented Programming C++ Newsletter

6. How much memory will be allocated for an object of class given below?

class Test{
int mark1;
int mark2;
float avg;
char name[10];
};

a) 22 Bytes
b) 24 Bytes
c) 20 Bytes
d) 18 Bytes
View Answer
Answer: a
Explanation: The size of an object of the class given in question will be of size 22
bytes. This is because the size of an object is always equal to the sum of sizes of
the data members of the class, except static members.

7. Which keyword among the following can be used to declare an array of objects in
java?
a) new

C++ Page 162


b) create
c) allocate
d) arr
View Answer
Answer: a
Explanation: 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 initialization
can be done directly.

8. When is the memory allocated for an object gets free?


a) At termination of program
b) When object goes out of scope
c) When main function ends
d) When system restarts
View Answer
Answer: b
Explanation: Whenever an object goes out of scope, the deletion of allocation
memory takes place. Actually the data is not deleted, instead the memory space is
flagged to be free for further use. Hence whenever an object goes out of scope the
object members become useless and hence memory is set free.

9. Which among the following keyword can be used to free the allocated memory for
an object?
a) delete
b) free
c) either delete or free
d) only delete
View Answer
Answer: c
Explanation: The memory allocated for an object is usually automatically made free.
But if explicitly memory has to be made free then we can use either free or delete
keywords depending on programming languages.

C++ Page 163


10. Which function is called whenever an object goes out of scope?
a) Destructor function
b) Constructor function
c) Delete function
d) Free function
View Answer
Answer: a
Explanation: The destructor function of the class is called whenever an object goes
out of scope. This is because the destructor set all the resources, acquired by the
object, free. This is an implicit work of compiler.

Object Oriented Programming using C++


Questions and Answers – Static Data Members

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Static Data Members”.

1. Which among the following best defines static variables members?


a) Data which is allocated for each object separately
b) Data which is common to all the objects of a class
c) Data which is common to all the classes
d) Data which is common to a specific method
View Answer
Answer: b
Explanation: The static data members are made common to all the object of a class.
They doesn’t change from object to object. Those are property of class rather than
of any individual object.

2. Which keyword should be used to declare static variables?


a) static
b) stat
c) common

C++ Page 164


d) const
View Answer
Answer: a
Explanation: The keyword used to declare static variables is static. This is must be
used while declaring the static variables. The compiler can make variables static if
and only if they are mentioned with static keyword.

3. Any changes made to static data member from one member function
_____________
a) Is reflected to only the corresponding object
b) Is reflected to all the variables in a program
c) Is reflected to all the objects of that class
d) Is constant to that function only
View Answer
Answer: c
Explanation: The changes made from any function to static data member will be a
common change for all the other objects also. If the change is made with respect to
one object and change is printed from another object, the result will be same.

4. Which is the correct syntax for declaring static data member?


a) static mamberName dataType;
b) dataType static memberName;
c) memberName static dataType;
d) static dataType memberName;
View Answer
Answer: d
Explanation: The syntax must firstly be mentioned with the keyword static. Then the
data type of the member followed by the member name should be given. This is
general form of declaring static data members.

5. The static data member ______________________


a) Must be defined inside the class
b) Must be defined outside the class
c) Must be defined in main function

C++ Page 165


d) Must be defined using constructor
View Answer
Answer: b
Explanation: The static data members must be defined outside the class. Since
these are common to all the objects and should be created only once, they must not
be defined in the constructor.

Subscribe Object Oriented Programming C++ Newsletter

6. The syntax for defining the static data members is __________


a) dataType className :: memberName = value;
b) dataType className : memberName = value;
c) dataType className . memberName = value;
d) dataType className -> memberName =value;
View Answer
Answer: a
Explanation: The syntax doesn’t contain the static keyword. Since it is already been
declared as static inside the class. The data type and the corresponding class name
must be there to allocate the variable to a class. The value is assigned using scope
resolution operator for the member name.

7. If static data members have to be used inside a class, those member functions
_______________
a) Must not be static member functions
b) Must not be member functions
c) Must be static member functions
d) Must not be member function of corresponding class
View Answer
Answer: c
Explanation: Only the static member functions can access the static data members.
The definition of static members is made common and hence the member function
should be capable of manipulating the static data members.

8. The static data member __________________________


a) Can be accessed directly
b) Can be accessed with any public class name

C++ Page 166


c) Can be accessed with dot operator
d) Can be accessed using class name if not using static member function
View Answer
Answer: d
Explanation: The static data members can be accessed using the class name also.
If the member functions is not used or is not to be used then we can call the static
data members directly by using its corresponding class name.

9. Which among the following is the correct syntax to access static data member
without using member function?
a) className -> staticDataMember;
b) className :: staticDataMember;
c) className : staticDataMember;
d) className . staticDataMember;
View Answer
Answer: b
Explanation: For accessing the static data members without using the static member
functions, the class name can be used. The class name followed by scope
resolution, indicating that static data members is member of this class, and then the
data member name.

10. Which data members among the following are static by default?
a) extern
b) integer
c) const
d) void
View Answer
Answer: c
Explanation: The const data members of any class are made static by default. This
is an implicit meaning given by the compiler to the member. Since const values
won’t change from object to object, hence are made static instead.

C++ Page 167


Object Oriented Programming using C++
Questions and Answers – Static Member
Functions

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Static Member Functions”.

1. Which among the following is correct definition for static member functions?
a) Functions created to allocate constant values to each object
b) Functions made to maintain single copy of member functions for all objects
c) Functions created to define the static members
d) Functions made to manipulate static programs
View Answer
Answer: b
Explanation: The functions which are made common, with respect to definition and
data usage, to all the objects. These functions are able to access the static data
members of a class.

2. The static member functions __________________


a) Have access to all the members of a class
b) Have access to only constant members of a class
c) Have access to only the static members of a class
d) Have direct access to all other class members also
View Answer
Answer: c
Explanation: The static member functions are common for all the objects. These
functions can use only the static members of a class in which those are defined.
This is because other members change with respect to each object created.

3. The static member functions ____________________


a) Can be called using class name
b) Can be called using program name
c) Can be called directly

C++ Page 168


d) Can’t be called outside the function
View Answer
Answer: a
Explanation: The static members can be accessed using class name also. This is
because the static members remain common to all the objects. Hence objects are
not required.

4. Which is correct syntax to access the static member functions with class name?
a) className . functionName;
b) className -> functionName;
c) className : functionName;
d) className :: functionName;
View Answer
Answer: d
Explanation: The scope resolution operator must be used to access the static
member functions with class name. This indicates that the function belongs to the
corresponding class.

5. Which among the following is not applicable for the static member functions?
a) Variable pointers
b) void pointers
c) this pointer
d) Function pointers
View Answer
Answer: c
Explanation: Since the static members are not property of objects, they doesn’t have
this pointer. Every time the same member is referred from all the objects, hence use
of this pointer is of no use.

Subscribe Object Oriented Programming C++ Newsletter

6. Which among the following is true?


a) Static member functions can’t be virtual
b) Static member functions can be virtual
c) Static member functions can be declared virtual if it is pure virtual class

C++ Page 169


d) Static member functions can be used as virtual in Java
View Answer
Answer: a
Explanation: The static member functions can’t be virtual. This is a restriction on
static member functions, since the definition should not change or should not be
overridden by any other function of derived class. The static members must remain
same for all the objects.

7. The static members are ______________________


a) Created with each new object
b) Created twice in a program
c) Created as many times a class is used
d) Created and initialized only once
View Answer
Answer: d
Explanation: The static members are created only once. Then those members are
reused whenever called or invoked. Memory is allocated only once.

8. Which among the following is true?


a) Static member functions can be overloaded
b) Static member functions can’t be overloaded
c) Static member functions can be overloaded using derived classes
d) Static member functions are implicitly overloaded
View Answer
Answer: b
Explanation: The static member functions can’t be overloaded because the definition
must be the same for all the instances of a class. If an overloaded function have
many definitions, none of them can be made static.

9. The static member functions _______________


a) Can’t be declared const
b) Can’t be declared volatile
c) Can’t be declared const or volatile
d) Can’t be declared const, volatile or const volatile
View Answer

C++ Page 170


Answer: d
Explanation: The static member functions can’t be made const, since any object or
class itself should be capable of making changes to the function. And the function
must retain all changes common to all the objects.

10. Which keyword should be used to declare the static member functions?
a) static
b) stat
c) const
d) common
View Answer
Answer: a
Explanation: The member functions which are to be made static, must be preceded
with the keyword static. This indicates the compiler to make the functions common
to all the objects. And a new copy is not created with each of the new object.

11. The keyword static is used _______________


a) With declaration inside class and with definition outside the class
b) With declaration inside class and not with definition outside the class
c) With declaration and definition wherever done
d) With each call to the member function
View Answer
Answer: b
Explanation: The keyword is used only inside the class while declaring the static
member. Outside the class, only definition with proper syntax is given. There is no
need of specifying the keyword static again.

Object Oriented Programming using C++


Questions and Answers – Constructors
Overloading

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Constructors Overloading”.

C++ Page 171


1. Which among the following is true for constructors overloading?
a) Constructors can’t be overloaded
b) Constructors can be overloaded using different signatures
c) Constructors can be overloaded with same signatures
d) Constructors can be overloaded with different return types
View Answer
Answer: b
Explanation: The constructors can be overloaded only if the definitions have different
signatures. Constructors doesn’t have any return type so can’t be overloaded using
return type. If constructors have same signature then it will produce a compile time
error.

2. If a constructors should be capable of creating objects without argument and with


arguments, which is a good alternative for this purpose?
a) Use zero argument constructor
b) Use constructor with one parameter
c) Use constructor with all default arguments
d) Use default constructor
View Answer
Answer: c
Explanation: The constructor should use all the default arguments. This will allow the
constructor to be called even if no arguments are passed. And if arguments are
passed, those will be accepted instead of the default values.

3. The Constructors with all the default arguments are similar as default
constructors.
a) True
b) False
View Answer
Answer: a
Explanation: The constructors with all the default arguments are similar as the
default constructors. Those can be used instead of the default constructors. So
defining the default constructor is not mandatory.

C++ Page 172


4. Which among the following is true?
a) The constructors overloading can be done by using different names
b) The constructors overloading can be done by using different return types
c) The constructors can be overloaded by using only one argument
d) The constructors must have the same name as that of class
View Answer
Answer: d
Explanation: The constructors must have the same name as that of the class name.
This is mandatory because only the constructor functions doesn’t have any return
type. Also, for overloading all the functions must have the same name.

5. Which among the following can be used in place of default constructor?


a) constructorName(int x, int y=0)
b) constructorName(int x=0, int y=0)
c) constructorName(int x=0, int y)
d) constructorName(int x, int y)
View Answer
Answer: b
Explanation: For a parameterized constructor to be used as a default constructor, it
must have all the default arguments. This makes the constructor to have optional
arguments which are not mandatory to be passed.

Subscribe Object Oriented Programming C++ Newsletter

6. Can a class have more than one function with all the default arguments?
a) Yes, always
b) Yes, if argument list is different
c) No, because constructors overloading doesn’t depend on argument list
d) No, never
View Answer
Answer: d
Explanation: A single class can never have more than once constructor with all the
default arguments. This is because it will make all those constructors as a default
constructor. And when an object is created with zero arguments then it will create
ambiguity.

C++ Page 173


7. Which is the correct syntax for using default arguments with the constructor?
a) default constructorName(default int x=0)
b) constructorName(default int x=0)
c) constructorName(int x=0)
d) constructorName()
View Answer
Answer: c
Explanation: The constructors using the default arguments must initialize the
arguments in the argument list. This is to make the constructor use the default value
when no arguments are passed. If no arguments are listed then it is a default
constructor.

8. How many parameters must be passed if only the following prototype is given to a
constructor?
Prototype: className(int x, int y, int z=0);
a) 1
b) 2
c) 3
d) Compile time error
View Answer
Answer: b
Explanation: In the prototype given, only 2 arguments are mandatory. Since the third
argument is default argument, so it is not mandatory to pass the argument.

9. If the constructors are overloaded by using the default arguments, which problem
may arise?
a) The constructors might have all the same arguments except the default
arguments
b) The constructors might have same return type
c) The constructors might have same number of arguments
d) The constructors can’t be overloaded with respect to default arguments
View Answer
Answer: a
Explanation: The constructors having same arguments except the default arguments
can give rise to errors. If only the mandatory arguments are passed, it will create

C++ Page 174


ambiguity in calling the correct constructor. Hence the mandatory arguments must
be different.

10. Which among the following is true?


a) More than one constructors with all default arguments is allowed
b) More than one constructors with all default arguments can be defined outside the
class
c) More than one constructors can be used with same argument list
d) More than one constructors with all default arguments can’t exist in same class
View Answer
Answer: d
Explanation: The constructors must have different argument list. Along that, if all the
arguments are default arguments, there can’t be more than once constructor like
that in the same class as that will create ambiguity while constructors are being
called.

Object Oriented Programming using C++


Questions and Answers – Upcasting

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Upcasting”.

1. What is upcasting?
a) Casting subtype to supertype
b) Casting super type to subtype
c) Casting subtype to super type and vice versa
d) Casting anytype to any other type
View Answer
Answer: a
Explanation: The upcasting concept includes only the casting of subtypes to the
super types. This casting is generally done implicitly. Smaller size types can fit into
larger size types implicitly.

C++ Page 175


2. Which among the following is true for upcasting in inheritance?
a) Downward to the inheritance tree
b) Upward to the inheritance tree
c) Either upward or downward
d) Doesn’t apply on inheritance
View Answer
Answer: b
Explanation: The upcasting concept in inheritance is always applied upward the
inheritance tree. The derived class objects can be type casted to any of its parent
class type. Since is a relationship applies in general inheritance.

3. Which among the following is safe?


a) Upcasting
b) Downcasting
c) Both upcasting and downcasting
d) If upcasting is safe then downcasting is not, and vice versa
View Answer
Answer: a
Explanation: The upcasting is always safe since the derived type or the smaller type
is converted into the base type or the larger size. This results in allocating a smaller
size data into bigger type data. No data is lost in casting, hence safe.

4. Which among the following is the best situation to use upcasting?


a) For general code dealing with only subtype
b) For general code dealing with only supertype
c) For general code dealing with both the supertype and subtype
d) For writing a rigid code with respect to subtype
View Answer
Answer: b
Explanation: When a general code has to be written where we use only the
supertype object or the data of bigger size, then upcasting would be the best option.
Since the whole code will require only the supertype name references.

5. Which property is shown most when upcasting is used?


a) Code reusability

C++ Page 176


b) Code efficiency
c) Complex code simple syntax
d) Encapsulation
View Answer
Answer: c
Explanation: The code written using upcasting mostly shows complex code in
simpler syntax features. This is because the upcasting concept can be applied as
polymorphism and to group the similar type of objects.

Subscribe Object Oriented Programming C++ Newsletter

6. Upcasting and downcasting objects are the same as casting primitive types.
a) True
b) False
View Answer
Answer: b
Explanation: It is a bit confusing concept since both casting concepts are different.
Primitive casting depends on the type and size of data being typecast. Whereas in
objects casting, the classes and inheritance order plays a big role.

7. Which casting among the following is allowed for the code given below?

class A
{
public :int a;
}
class B:public A
{
int b;
}
main()
{
B b=new A(); //casting 1
A a=new B(); //casting 2
}

C++ Page 177


a) Casting 1
b) Casting 2
c) casting 1 and casting 2
d) casting 1 nor casting 2
View Answer
Answer: b
Explanation: The casting 2 is correct. The objects casting must be done from
derived class object to a parent class object. That is, the object of the superclass
can be made an object of subclass only. Vice versa is not possible.

8. If multiple inheritance is implemented, which upcasting will be correct?


a) Upcast to first base class listed in inheritance
b) Upcast to send base class listed in inheritance
c) Upcast to any base class
d) Upcast is not possible
View Answer
Answer: c
Explanation: The upcasting of derived class object is possible to any base class.
This is because the base class object can represent any of its derived classes using
upcasting.

9. If class C inherits class B and class B inherits class A ________________


a) Class C object can be upcasted to object of class B only
b) Class C object can be upcasted to object of class A only
c) Class C object can be upcasted to object of either class A or B
d) Class C object can’t be upcasted
View Answer
Answer: c
Explanation: Both class A and B are parent classes of class C. Class C object hence
can be upcasted to any of those class objects. It is not compulsory to upcast to
nearest parent.

10. Upcasting is _____________________ without an explicit type cast.


a) Always allowed for public inheritance
b) Always allowed for protected inheritance

C++ Page 178


c) Always allowed for private inheritance
d) Not allowed
View Answer
Answer: a
Explanation: The public inheritance shows the most flexible is-a relationship. Hence
explicit type casting is not required. Implicit type casting is done by the compiler.

11. Which concept is needed because of implicit type casting use?


a) Static binding
b) Dynamic binding
c) Compile time binding
d) Source code binding
View Answer
Answer: b
Explanation: Since the implicit type casting allows casting of a base class pointer to
refer to its derived class object or even base class object. We need dynamic type
casting so that the references can be resolved during execution of program.

12. When are the pointer types known for upcasting the objects?
a) Compile time
b) Runtime
c) Source code build time
d) Doesn’t apply to pointer types
View Answer
Answer: a
Explanation: The pointer or reference types are known at compile time for the
upcasting of an object. This is because the addresses must be known for casting the
derived class to base class object.

13. When are the object type known for upcasting the objects?
a) Compile time
b) Runtime
c) Source code build time
d) Doesn’t apply to objects directly
View Answer

C++ Page 179


Answer: b
Explanation: The upcasting with objects directly requires runtime resolving. The
objects are fixed and address are allocated at compile time. But the execution of a
program requires runtime knowledge of object types, for implicit type cast.

14. If two classes are defined “Parent” and “Child” then which is the correct type
upcast syntax in C++?
a) Parent *p=child;
b) Parent *p=*child;
c) Parent *p=&child;
d) Parent *p=Child();
View Answer
Answer: c
Explanation: The syntax must contain the base class name first. So that the parent
class object pointer can be declared. Then the object is assigned with the derived
class object with & symbol. & symbol is added to get the address of the derived
class object.

15. Which among the following is true?


a) Upcasting is possible only for single level inheritance
b) Upcasting is possible only for multilevel inheritance
c) Upcasting is possible only for multiple inheritance
d) Upcasting is possible for any type of inheritance
View Answer
Answer: d
Explanation: The type of inheritance doesn’t matter with the upcasting concept.
Upcasting applies to all types of inheritance. Any derived class object can be
upcasted to any of its base class object.

Object Oriented Programming using C++


Questions and Answers – Downcasting

C++ Page 180


This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Downcasting”.

1. What is downcasting?
a) Casting subtype to supertype
b) Casting supertype to subtype
c) Casting subtype to supertype and vice versa
d) Casting anytype to any other type
View Answer
Answer: b
Explanation: The downcasting concept includes only the casting of supertypes to the
sub types. This casting is generally done explicitly. Larger size types are made to fit
into small size types explicitly.

2. Which among the following is a mandatory condition for downcasting?


a) It must not be done explicitly
b) It must be done implicitly
c) It must be done explicitly
d) It can’t be done explicitly
View Answer
Answer: c
Explanation: The downcasting of any object must be done explicitly. This is because
the compilers don’t support the implicit conversion of a supertype to subtype.

3. Downcasting is _______________________
a) Always safe
b) Never safe
c) Safe sometimes
d) Safe, depending on code
View Answer
Answer: b
Explanation: The downcasting concept is made for exception cases. When there is a
need to represent an entity in the form which is not suitable for it. Representing a
base type in derived type is not right but can be done for special cases.

C++ Page 181


4. Downcasting ____________________
a) Can result in unexpected results
b) Can’t result in unexpected result
c) Can result only in out of memory error
d) Can’t result in any error
View Answer
Answer: a
Explanation: The result of downcasting can be unexpected. This is because
downcasting is done on the objects into the objects which doesn’t contain any
information of data in lateral object.

5. What should be used for safe downcast?


a) Static cast
b) Dynamic cast
c) Manual cast
d) Implicit cast
View Answer
Answer: b
Explanation: The dynamic cast can be done using the operator dynamic_cast. This
converts one type to another type in a safe way.

Subscribe Object Oriented Programming C++ Newsletter

6. What does dynamic_cast return after successful type casting?


a) Address of object which is converted
b) Address of object that is used for conversion
c) Address of object that is mentioned in the syntax
d) Doesn’t return any address
View Answer
Answer: a
Explanation: The address of the object which is converted is returned by the
dynamic_cast operator. This is done to safely convert the subtype to supertype. This
ensures the proper assignment and conversion from one type to another.

7. If dynamic_cast fails, which value is returned?


a) void

C++ Page 182


b) null
c) void pointer
d) null pointer
View Answer
Answer: d
Explanation: The null pointer is returned by the dynamic_cast, if it fails. The
conversion sometimes fails because of too complex type conversion. The
conversion may also fail due to memory or some related issues.

8. Which is the proper syntax of dynamic_cast?


a) dynamic_cast(object)
b) dynamic_cast new (object)
c) dynamic_cast(object)
d) dynamic_cast(object)
View Answer
Answer: c
Explanation: The dynamic_cast is the name of the operator, which is followed by the
new type in which the object have to be converted. Then the object name is given.
This object name is then used after the type conversion.

9. Which is the exception handler for the exceptions of downcasting?


a) CastException
b) ClassCastingExeption
c) ClassCasting
d) ClassCastException
View Answer
Answer: d
Explanation: The exception handler for the exceptions produced during the
downcasting exception. This handler can be called during runtime to handle any
exception thrown.

10. How to prevent the ClassCastExceptions?


a) By using instanceof
b) By using is-a check
c) By using arrow operator with check function

C++ Page 183


d) By checking type of conversion
View Answer
11. Java supports direct downcasting.
a) True
b) False
View Answer
Answer: b
Explanation: The downcasting is not possible in java directly. This has to be done
explicitly. The downcasting is not safe but can be checked for safe casting using
instanceof function.

12. Which way the downcasting is possible with respect to inheritance?


a) Upward the inheritance order
b) Downward the inheritance order
c) Either upward or downward the inheritance order
d) Order of inheritance doesn’t matter
View Answer
Answer: b
Explanation: The downcasting is always downward the inheritance order. Since the
base class object have to be casted into derived class type. This is a basic definition
of downcasting.

13. What happens when downcasting is done but not explicitly defined in syntax?
a) Compile time error
b) Runtime error
c) Code write time error
d) Conversion error
View Answer
Answer: a
Explanation: The implicit downcasting is not possible. If tried, the compiler produces
an error. Since the compiler doesn’t allow coasting to a type that is not compatible.

14. When is the downcasting used?


a) To separate inherited class from base class
b) To write a more complex code

C++ Page 184


c) To compare two objects
d) To disable one class in inheritance
View Answer
Answer: c
Explanation: The downcasting can be used whenever there is a need to compare
one object to another. Equals() function can be used to compare whether the objects
were of same age. We can use getClass() function too.

15. Why is downcasting possible in any language?


a) Because inheritance follows has-a relationship
b) Because inheritance follows is-a relationship
c) Because inheritance doesn’t follow any relationship
d) Because inheritance is not involved in casting
View Answer
Answer: b
Explanation: The downcasting is possible because the classes in inheritance follow
is-a relationship. Hence the derived class is a base class. Which in turn make the
downcasting possible.

Object Oriented Programming using C++


Questions and Answers – New Operator

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “New Operator”.

1. What is the new operator?


a) Allocates memory for an object or array
b) Allocates memory for an object or array and returns a particular pointer
c) Used as return type when an object is created
d) Used to declare any new thing in a program
View Answer
Answer: b
Explanation: The new keyword is used to allocate memory of an object or array. The

C++ Page 185


new object or array can be of any type. Then it returns a suitable non zero pointer to
the object.

2. Microsoft C++ Components extensions support new keyword to _____________


a) Modify a vtable
b) Replace a vtable slot entry
c) Add new vtable slot entries
d) Rearrange vtable slot entries
View Answer
Answer: c
Explanation: The new keyword is used for adding new vtable slot entries. This is an
additional feature in Microsoft C++. It can use predefined class object for this work.

3. What happens when new fails?


a) Returns zero always
b) Throws an exception always
c) Either throws an exception or returns zero
d) Terminates the program
View Answer
Answer: c
Explanation: While creating new objects, the new operator may fail because of
memory errors or due to permissions. At that moment the new operator returns zero
or it may throw an exception. The exception can be handled as usual.

4. If new throws an error, which function can be called to write a custom exception
handler?
a) _set_handler
b) _new_handler
c) _handler_setter
d) _set_new_handler
View Answer
Answer: d
Explanation: If the default exception handler has to be replaced by a user defined
handler, we can call _set_new_handler run-time library function with the function

C++ Page 186


name as an argument. This lets the programmer to give a custom definition for
handling new operator failure.

5. In C++, if new operator is used, when is the constructor called?


a) Before the allocation of memory
b) After the allocation of memory
c) Constructor is called to allocate memory
d) Depends on code
View Answer
Answer: b
Explanation: The constructor function is called after the allocation of memory. In C++
the feature works in a bit different way. The memory for all the data members is
allocated first and then the constructor function is called to finalize the memory
allocated.

Subscribe Object Oriented Programming C++ Newsletter

6. Which among the following is correct syntax to declare a 2D array using new
operator?
a) char (*pchar)[10] = new char[][10];
b) char (pchar) = new char[][10];
c) char (*char) = new char[10][];
d) char (*char)[][10]= new char;
View Answer
Answer: a
Explanation: The new operator usage to declare a 2D array requires a pointer and
size of array to be declared. Data type and then the pointer with size of array. The
left index can be left blank or any variable can be assigned to it.

7. For declaring data by using new operator ____________________


a) Type name can’t contain const
b) Type name can’t contain volatile
c) Type name can’t contain class declarations
d) Type name can’t contain const, volatile, class declaration or enumerations
View Answer

C++ Page 187


Answer: d
Explanation: The declaration of any data where we use new operator, any of the
mentioned types are not allowed. This is because the new operator allocated
memory based on the type of data which can be allocated dynamically.

8. The new operator _____________


a) Can allocate reference types too
b) Doesn’t allocate reference types
c) Can allocate reference to objects
d) Doesn’t allocate any data
View Answer
Answer: b
Explanation: The new operator doesn’t allocate reference types. This is because the
reference types are not objects. The new operator is used to allocate memory to the
direct objects.

9. Which among the following is true?


a) New operator can’t allocate functions but pointer to functions can be allocated
b) New operator can allocate functions as well as pointer to functions
c) New operator can allocate any type of functions
d) New operator is not applicable with functions allocation
View Answer
Answer: a
Explanation: The new operator can’t allocate functions but can allocate pointer to the
functions. This is a security feature as well as to reduce the ambiguity in code. The
new keyword is not given functionality to directly allocate any function.

10. Which among the following is added in grammar of new operator?


a) Finalize
b) Arg
c) Initializer
d) Allocator
View Answer
Answer: c
Explanation: The new operator grammar is added with an initializer field. This can be

C++ Page 188


used to initialize an object with a user defined constructor. Hence can allocate
memory as intended by the programmer.

Object Oriented Programming using C++


Questions and Answers – Delete Operator

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Delete Operator”.

1. What is a delete operator?


a) Deallocates a block of memory
b) Deallocates whole program memory
c) Deallocates only primitive data memory
d) Deallocates all the data reserved for a class
View Answer
Answer: a
Explanation: The delete operator is the reverse process of a new operator. It
deallocates all the memory allocated for an object. The object can be of any type.
The delete operator completely destroys an object so that the resources can be
used for other purposes.

2. If an object is allocated using new operator ____________


a) It should be deleted using delete operator
b) It can’t be deleted using delete operator
c) It may or may not be deleted using delete operator
d) The delete operator is not applicable
View Answer
Answer: a
Explanation: The new operator allocates an object in memory and hence the
memory allocation is bit different from usual allocation of an object. The delete
operator can be used to delete the memory allocated for an object.

C++ Page 189


3. Does delete return any value?
a) Yes, positive value
b) Yes, negative value
c) Yes, zero value
d) No
View Answer
Answer: d
Explanation: The delete operator doesn’t return any value. Its function is to delete
the memory allocated for an object. This is done in reverse way as that new operator
works.

4. Which type of value has resulted from the delete operator?


a) void
b) void pointer
c) null pointer
d) null
View Answer
Answer: a
Explanation: The result of the delete operator is void. The values returned is of no
use to the program or any other system function hence the return type is not defined
for the delete operator.

5. If delete is used to delete an object which was not allocated using new
_______________
a) Then out of memory error arises
b) Then unreachable code error arises
c) Then unpredictable errors may arise
d) Then undefined variable error arises
View Answer
Answer: c
Explanation: When the delete operator is used with the objects that were not
allocated using new operator then unpredictable errors may arise. This is because
the delete can’t perform the required actions on the type of memory allocated for the
object.

C++ Page 190


Subscribe Object Oriented Programming C++ Newsletter

6. Delete operator _________________


a) Can be used on pointers with null value
b) Can be used on pointers with void value
c) Can be used on pointer with value 0
d) Can be used on pointer with any value
View Answer
Answer: c
Explanation: The delete operator can be used on pointers with the value 0. This
actually means that when new operator fails and return value 0 then deleting the
result of failed new remains harmless. Hence the deletion is possible.

7. When delete operator is used ___________________ (If object has a destructor)


a) Object destructor is called after deallocation
b) Object destructor is called before deallocation
c) Object destructor is not used
d) Object destructor can be called anytime during destruction
View Answer
Answer: b
Explanation: The destructor is called before the memory is deallocated for any
object. The destructor call initiates the destruction process and the deallocation of
memory takes place.

8. If delete is applied to an object whose l-value is modifiable, then


_______________ after the object is deleted.
a) Its value is defined as null
b) Its value is defined as void
c) Its value is defined as 0
d) Its value is undefined
View Answer
Answer: d
Explanation: After performing delete operation on an object whole l-value is
modifiable, its values becomes undefined. This is done so as to denote that the
memory space is available to be used for other purposes.

C++ Page 191


9. How many variants of delete operator are available?
a) Only 1
b) Only 2
c) Only 3
d) Only 4
View Answer
Answer: b
Explanation: There are two variants of delete operator. One is for object deletion.
Other is for deletion of object array.

10. Which is the correct syntax to delete a single object?


a) delete *objectName;
b) objectName delete;
c) delete objectName;
d) objectName *delete;
View Answer
Answer: c
Explanation: The object to be deleted is mentioned after the keyword delete. This
deletes the object from memory and free up the memory that was acquired by the
object.

C++ Page 192

You might also like