Professional Documents
Culture Documents
C++ MCQ Question
C++ MCQ Question
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.
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.
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.
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.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Classes”.
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.
class Student
{
int a;
public : float a;
};
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.
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.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Objects”.
C++ Page 11
Answer: d
Explanation: You can create as many objects of a specific class as you want,
provided enough memory is available.
C++ Page 12
6. What is size of the object of following class (64 bit system)?
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.
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.
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.
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
}
};
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “OOP Features”.
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.
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.
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.
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.
C++ Page 20
because all the classes which inherit virtual function, define the same function in
different ways.
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.
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”.
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.
C++ Page 23
class average:public student
{
public : calc_grade()
{
return 20;
}
};
class failed{ int marks; };
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.
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();
}
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();
}
}
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Encapsulation”.
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.
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.
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;
}
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.
class hero
{
char name[10];
public : void disp()
{
cout<<name;
}
};
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.
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.
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.
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.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Constructors”.
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.
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.
C++ Page 42
constructor with zero parameters. But it will not create the object if signature doesn’t
match.
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.
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.
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.
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;
}
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”.
class student
{
int marks;
}
student s1;
student s2=2;
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.
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).
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.
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.
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;
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.
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.
C++ Page 54
to always create an explicit copy constructor. The values are copied using implicit
constructor only.
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.
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.
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”.
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.
5. Which constructor will be called from the object created in the code below?
class A
{
int i;
A()
{
i=0; cout<<i;
}
A(int x=0)
{
C++ Page 59
i=x; cout<<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.
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.
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.
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.
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.
advertisement
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.
class A{ };
class B{ };
class C: public A, public B{ };
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Access Specifiers”.
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.
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.
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;
}
};
C++ Page 71
3. Which among the following is true for the code given below?
class A
{
private : int marks; char name[20];
public :
A(int x=100)
{
marks=x;
}
};
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.
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.
class A
{
private : int sum(int x, int y)
{
return x+y;
}
public: A()
{
}
A(int x, int y)
{
cout<<sum(x,y);
}
};
class A()
{
C++ Page 74
int marks; char name[20];
public : A()
{
marks=100;
}
void disp()
{
cout<<”Marks= ”<'<marks;
cout<<”Student”;
}
};
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”.
C++ Page 76
class A
{
int marks;
protected : A()
{
marks=100;
}
public : A( int x)
{
marks=x;
}
};
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;
}
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<<”marks=”<<marks;
}
};
class B: protected A
{
};
B b;
b.disp();
C++ Page 79
This is because the class is being inherited in protected access, so all the members
will become protected in subclass B.
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.
class A
{
private: int marks;
C++ Page 80
A()
{
}
Public : disp()
{
cout<< marks;
}
};
class B: public A
{
}
B b;
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<<marks;
}
}
class B: protected A
{
char name[20];
}
A a; a.disp();
B b; b.disp();
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.
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.
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.
package pack1;
class A
{
public A()
{
System.out.print(“object created”);
}
}
package pack2;
import pack1.*;
class B
{
A a=new A();
}
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.
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.
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.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Member Functions”.
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.
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.
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.
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.
This set of Object Oriented Programming using C++ Interview Questions and
Answers focuses on “Passing and Returning Object with Functions”.
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.
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.
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.
Class A
{
int i;
public : A(int n)
{
i=n; cout<<”inside constructor ”;
}
~A()
{
cout<<”destroying ”<<i;
}
void seti(int n)
{
i=n;
C++ Page 97
}
int geti()
{
return I;
}
};
void t(A ob)
{
cout<<”something ”;
}
int main()
{
A a(1);
t(a);
cout<<”this is i in main ”;
cout<<a.geti();
}
C++ Page 98
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Object Array”.
C++ Page 99
4. If array of objects is declared as given below, which is the limitation on objects?
Class_name arrayName[size];
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Abstract 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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Template Class”.
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
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Base Class”.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
class A
{
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Single Level Inheritance”.
2. If class A and class B are derived from class C and class D, then
________________
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
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
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Multilevel Inheritance”.
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.
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
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Multiple Inheritance”.
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.
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.
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
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Hierarchical Inheritance”.
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.
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.
class A
{
int a;
};
class B:class A
{
int b;
};
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.
abstract class A
{
public Int a;
public void disp();
};
class B:public A
{
public: void dis()
{
court<<a;
}
};
class C:private A
{
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
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Hybrid Inheritance”.
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
This set of Object Oriented Programming using C++ Questions and Answers for
Entrance exams focuses on “Virtual Functions”.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on ” Abstract Function”.
This set of Object Oriented Programming using C++ Problems focuses on “Types of
Member Functions”.
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.
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
This set of Object Oriented Programming using C++ Questions and Answers for
Freshers focuses on “Overloading Member Functions”.
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
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Static Data Members”.
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.
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.
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.
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.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “Constructors Overloading”.
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.
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.
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
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.
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
}
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
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.
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.
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.
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “New Operator”.
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
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice
Questions & Answers (MCQs) focuses on “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.