0% found this document useful (0 votes)
19 views145 pages

Sanfoundary OOPs

The document discusses basic concepts of Object-Oriented Programming (OOP), including the definition of classes, features such as inheritance and encapsulation, and the history of OOP. It also covers the characteristics of various programming languages in relation to OOP principles, such as Java and C++. Additionally, the document includes questions and answers related to OOP concepts for educational purposes.

Uploaded by

saleemaafsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views145 pages

Sanfoundary OOPs

The document discusses basic concepts of Object-Oriented Programming (OOP), including the definition of classes, features such as inheritance and encapsulation, and the history of OOP. It also covers the characteristics of various programming languages in relation to OOP principles, such as Java and C++. Additionally, the document includes questions and answers related to OOP concepts for educational purposes.

Uploaded by

saleemaafsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 145

“OOP Basic Concepts”. 5. Which is not feature of OOP in general definitions?

1. Which was the first purely object oriented a) Code reusability


programming language developed? b) Modularity
a) Java c) Duplicate/Redundant data
b) C++ d) Efficient Code
c) SmallTalk
d) Kotlin Answer: c
Explanation: Duplicate/Redundant data is dependent
Answer: c on programmer and hence can’t be guaranteed by
Explanation: SmallTalk was the first programming OOP. Code reusability is done using inheritance.
language developed which was purely object oriented. Modularity is supported by using different code files
It was developed by Alan Kay. OOP concept came into and classes. Codes are more efficient because of
picture in 1970’s. features of OOP.
2. Which of the following best defines a class? 6. Pure OOP can be implemented without using class
a) Parent of an object in a program. (True or False)
b) Instance of an object a) True
c) Blueprint of an object b) False
d) Scope of an object
Answer: b
Answer: b Explanation: It’s false because for a program to be pure
Explanation: A class is Blueprint of an object which OO, everything must be written inside classes. If this
describes/ shows all the functions and data that are rule is violated, the program can’t be labelled as purely
provided by an object of a specific class. It can’t be OO.
called as parent or instance of an object. Class in 7. Which Feature of OOP illustrated the code
general describes all the properties of an object. reusability?
3. Who invented OOP? a) Polymorphism
a) Alan Kay b) Abstraction
b) Andrea Ferro c) Encapsulation
c) Dennis Ritchie d) Inheritance
d) Adele Goldberg
Answer: d
Answer: a Explanation: Using inheritance we can reuse the code
Explanation: Alan Kay invented OOP, Andrea Ferro already written and also can avoid creation of many
was a part of SmallTalk Development. Dennis invented new functions or variables, as that can be done one
C++ and Adele Goldberg was in team to develop time and be reused, using classes.
SmallTalk but Alan actually had got rewarded for 8. Which language does not support all 4 types of
OOP. inheritance?
4. What is the additional feature in classes that was not a) C++
in structures? b) Java
a) Data members c) Kotlin
b) Member functions d) Small Talk
c) Static data allowed
d) Public access specifier Answer: b
Explanation: Java doesn’t support all 4 types of
Answer: b inheritance. It doesn’t support multiple inheritance. But
Explanation: Member functions are allowed inside a the multiple inheritance can be implemented using
class but were not present in structure concept. Data interfaces in Java.
members, static data and public access specifiers were 9. How many classes can be defined in a single
present in structures too. program?
a) Only 1
1
b) Only 100 d) OOP can be used without using any header file
c) Only 999
d) As many as you want Answer: d
Explanation: We need not include any specific header
Answer: d file to use OOP concept in C++, only specific functions
Explanation: Any number of classes can be defined used in code need their respective header files to be
inside a program, provided that their names are included or classes should be defined if needed.
different. In java, if public class is present then it must 14. Which of the two features match each other?
have the same name as that of file. a) Inheritance and Encapsulation
10. When OOP concept did first came into picture? b) Encapsulation and Polymorphism
a) 1970’s c) Encapsulation and Abstraction
b) 1980’s d) Abstraction and Polymorphism
c) 1993
d) 1995 Answer: c
Explanation: Encapsulation and Abstraction are similar
Answer: a features. Encapsulation is actually binding all the
Explanation: OOP first came into picture in 1970’s by properties in a single class or we can say hiding all the
Alan and his team. Later it was used by some features of object inside a class. And Abstraction is
programming languages and got implemented hiding unwanted data (for user) and showing only the
successfully, SmallTalk was first language to use pure data required by the user of program.
OOP and followed all rules strictly. 15. Which feature allows open recursion, among the
11. Why Java is Partially OOP language? following?
a) It supports usual declaration of primitive data types a) Use of this pointer
b) It doesn’t support all types of inheritance b) Use of pointers
c) It allows code to be written outside classes c) Use of pass by value
d) It does not support pointers d) Use of parameterized constructor

Answer: a Answer: a
Explanation: As Java supports usual declaration of data Explanation: Use of this pointer allows an object to call
variables, it is partial implementation of OOP. Because data and methods of itself whenever needed. This helps
according to rules of OOP, object constructors must be us call the members of an object recursively, and
used, even for declaration of variables. differentiate the variables of different scopes.
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 Classes -
d) At least one object should be declared in code
“Classes”.
Answer: b 1. Which of the following is not type of class?
Explanation: In C++, it’s not necessary to use classes, a) Abstract Class
and hence codes can be written without using OOP b) Final Class
concept. Classes may or may not contain member c) Start Class
functions, so it’s not a necessary condition in C++. d) String Class
And, an object can only be declared in a code if its
class is defined/included via header file. Answer: c
13. Which header file is required in C++ to use OOP? Explanation: Only 9 types of classes are provided in
a) iostream.h general, namely, abstract, final, mutable, wrapper,
b) stdio.h anonymous, input-output, string, system, network. We
c) stdlib.h may further divide the classes into parent class and
subclass if inheritance is used.
2
2. Class is pass by _______ value and the same class can be used for all the
a) Value variables of different data types.
b) Reference 6. Size of a class is :
c) Value or Reference, depending on program a) Sum of size of all the variables declared inside the
d) Copy class
b) Sum of size of all the variables along with inherited
Answer: b variables in the class
Explanation: Classes are pass by reference, and the c) Size of largest size of variable
structures are pass by copy. It doesn’t depend on d) Classes doesn’t have any size
program.
3. What is default access specifier for data members or Answer: d
member functions declared within a class without any Explanation: Classes doesn’t have any size, actually
specifier, in C++ ? the size of object of the class can be defined. That is
a) Private done only when an object is created and its constructor
b) Protected is called.
c) Public 7. Which class can have member functions without
d) Depends on compiler their implementation?
a) Default class
Answer: a b) String class
Explanation: The data members and member functions c) Template class
are Private by default in C++ classes, if none of the d) Abstract class
access specifier is used. It is actually made to increase
the privacy of data. Answer: d
4. Which is most appropriate comment on following Explanation: Abstract classes can have member
class definition : functions with no implementation, w . the inheriting
class Student subclasses must implement those functions.
{ 8. Which of the following describes a friend class?
int a; a) Friend class can access all the private members of
public : float a; the class, of which it is a friend
}; b) Friend class can only access protected members of
a) Error : same variable name can’t be used twice the class, of which it is a friend
b) Error : Public must come first c) Friend class don’t have any implementation
c) Error : data types are different for same variable d) Friend class can’t access any data member of
d) It is correct another class but can use it’s methods

Answer: a Answer: a
Explanation: Same variable can’t be defined twice in Explanation: A friend class can access all the private
same scope. Even if the data types are different, members of another class, of which it is friend. It is a
variable name must be different. T . is no rule like special class provided to use when you need to reuse
Public member should come first or last. the data of a class but don’t want that class to have
5. Which is known as generic class? those special functions.
a) Abstract class 9. What is scope of a class nested inside another class?
b) Final class a) Protected scope
c) Template class b) Private scope
d) Efficient Code c) Global scope
d) Depends on access specifier and inheritance used
Answer: c
Explanation: Template classes are known to be generic Answer: d
classes because those can be used for any data type Explanation: It depends on the access specifier and the
type of inheritance used with the class, because if the
3
class is inherited then the nested class can be used by a) Classes may/may not have both data members and
subclass too, provided it’s not of private type. member functions
10. Class with main() function can be inherited b) Class definition must be ended with a colon
(True/False) c) Class can have only member functions with no data
a) True members
b) False d) Class is similar to union and structures

Answer: a Answer: b
Explanation: The class containing main function can be Explanation: Class definition must end with a
inherited and hence the program can be executed using semicolon, not colon. Class can have only member
the derived class names also in java. functions in its body with no data members.
11. Which among the following is false, for member 15. Instance of which type of class can’t be created?
function of a class? a) Anonymous class
a) All member functions must be defined b) Nested class
b) Member functions can be defined inside or outside c) Parent class
the class body d) Abstract class
c) Member functions need not be declared inside the
class definition Answer: d
d) Member functions can be made friend to another Explanation: Instance of abstract class can’t be created
class using friend keyword as it will not have any constructor of its own, hence
while creating an instance of class, it can’t initialize the
Answer: c object members. Actually the class inheriting the
Explanation: Member functions must be declared abstract class can have its instance, because it will have
inside class body, thought the definition can be given implementation of all members.
outside the class body. T . is no way to declare the .
member functions inside the class. .
12. Which syntax for class definition is wrong?
a) class student{ }; Objects . -
b) student class{ };
c) class student{ public: student(int a){ } }; “Objects”.
d) class student{ student(int a){} }; 1. Which definition best describes an object?
a) Instance of a class
Answer: b b) Instance of itself
Explanation: Keyword class should come first. Class c) Child of a class
name should come after keyword class. Parameterized d) Overview of a class
constructor definition depends on programmer so it can
be left empty also. Answer: a
13. Which of the following pairs are similar? Explanation: An object is instance of its class. It can be
a) Class and object declared in the same way that a variable is declared,
b) Class and structure only thing is you have to use class name as the data
c) Structure and object type.
d) Structure and functions 2. How many objects can be declared of a specific
class in a single program?
Answer: b a) 32768
Explanation: Class and structure are similar to each b) 127
other. Only major difference is that a structure doesn’t c) 1
have member functions w .as the class can have both d) As many as you want
data members and member functions.
14. Which among the following is false for class Answer: d
features? Explanation: You can create as many objects of a
4
specific class as you want, provided enough memory is 7. Functions can’t return objects. (True/False)
available. a) True
3. Which among the following is false? b) False
a) Object must be created before using members of a
class Answer: b
b) Memory for an object is allocated only after its Explanation: Functions can always return an object if
constructor is called the return type is same as that of object being returned.
c) Objects can’t be passed by reference Care has to be taken while writing the prototype of
d) Objects size depends on its class data members function.
8. How members of an object are accessed?
Answer: c a) Using dot operator/period symbol
Explanation: Objects can be passed by reference. b) Using scope resolution operator
Objects can be passed by value also. If object of a class c) Using member names directly
is not created, we can’t use members of that class. d) Using pointer only
4. Which of the following is incorrect?
a) class student{ }s; Answer: a
b) class student{ }; student s; Explanation: Using dot operator after the name of
c) class student{ }s[]; object we can access its members. It is not necessary to
d) class student{ }; student s[5]; use the pointers. We can’t use the names directly
because it may be used outside the class.
Answer: c 9. If a local class is defined in a function, which of the
Explanation: The array must be specified with a size. following is true for an object of that class?
You can’t declare object array, or any other linear a) Object is accessible outside the function
array without specifying its size. It’s a mandatory field. b) Object can be declared inside any other function
5. The object can’t be: c) Object can be used to call other class members
a) Passed by reference d) Object can be used/accessed/declared locally in that
b) Passed by value function.
c) Passed by copy
d) Passed as function Answer: d
Explanation: For an object which belongs to a local
Answer: d class, it is mandatory to declare and use the object
Explanation: Object can’t be passed as function as it is within the function because the class is accessible
an instance of some class, it’s not a function. Object locally within the class only.
can be passed by reference, value or copy. T . is no 10. Which among the following is wrong?
term defined as pass as function for objects. a) class student{ }; student s;
6. What is size of the object of following class (64 bit b) abstract class student{ }; student s;
system)? c) abstract class student{ }s[50000000];
class student { int rollno; char name[20]; static int d) abstract class student{ }; class toppers: public
studentno; }; student{ }; topper t;
a) 20
b) 22 Answer: b
c) 24 Explanation: We can never create instance of an
d) 28 abstract class. Abstract classes doesn’t have
constructors and hence when an instance is created t .
Answer: c is no facility to initialize its members. Option d is
Explanation: The size of any object of student class correct because topper class is inheriting the base
will be of size 4+20=24, because static members are abstract class student, and hence topper class object
not really considered as property of a single object. So can be created easily.
static variables size will not be added. 11. Object declared in main() function:
a) Can be used by any other function
5
b) Can be used by main() function of any other c) student s[2]={ s1(392,9), s2(222,5) };
program d) student s[2]={ s[392,9], s2[222,5] };
c) Can’t be used by any other function
d) Can be accessed using scope resolution operator Answer: b
Explanation: It is the way we can initialize the data
Answer: c members for an object array using parameterized
Explanation: The object declared in main() have local constructor. We can do this to pass our own intended
scope inside main() function only. It can’t be used values to initialize the object array data.
outside main() function. Scope resolution operator is 15. Object can’t be used with pointers because they
used to access globally declared variables/objects. belong to user defined class, and compiler can’t decide
12. When an object is returned___________ the type of data may be used inside the class.
a) A temporary object is created to return the value (True/False)
b) The same object used in function is used to return a) True
the value b) False
c) The Object can be returned without creation of
temporary object Answer: b
d) Object are returned implicitly, we can’t say how it Explanation: The explanation given is wrong because
happens inside program object can always be used with pointers like with any
other variables. Compiler doesn’t have to know the
Answer: a structure of the class to use a pointer because the
Explanation: A temporary object is created to return pointers only points to a memory address/stores that
the value. It is created because object used in function address.
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? OOP Features -
a) class student{ }s1,s2; s1.student()=s2.student();
b) class student{ }s1; class topper{ }t1; s1=t1; “OOP Features”.
c) class student{ }s1,s2; s1=s2; 1. Which feature of OOP indicates code reusability?
d) class student{ }s1; class topper{ }t1; a) Encapsulation
s1.student()=s2.topper(); b) Inheritance
c) Abstraction
Answer: c d) Polymorphism
Explanation: Only if the objects are of same class then
their data can be copied from to another using Answer: b
assignment operator. This actually comes under Explanation: Inheritance indicates the code reusability.
operator overloading. Class constructors can’t be Encapsulation and abstraction are meant to hide/group
assigned any explicit value as in option b and d. data into one element. Polymorphism is to indicate
14. Which among following is correct for initializing different tasks performed by a single entity.
the class below? 2. If a function can perform more than 1 type of tasks,
class student{ w . the function name remains same, which feature of
int marks; OOP is used .?
int cgpa; a) Encapsulation
public: student(int i, int j){ b) Inheritance
marks=I; c) Polymorphism
cgpa=j d) Abstraction
}
}; Answer: c
a) student s[3]={ s(394, 9); s(394, 9); s(394,9); }; Explanation: For the feature given above, the OOP
b) student s[2]={ s(394,9), s(222,5) }; feature used is Polymorphism. Example of
6
polymorphism in real life is a kid, who can be a Answer: d
student, a son, a brother depending on w . he is. Explanation: Encapsulation is indicated by use of
3. If different properties and functions of a real world classes. Inheritance is shown by inheriting the student
entity is grouped or embedded into a single element, class into topper class. Polymorphism is not shown .
what is it called in OOP language? because we have defined the constructor in topper class
a) Inheritance but that doesn’t mean that default constructor is
b) Polymorphism overloaded.
c) Abstraction 7. Which feature may be violated if we don’t use
d) Encapsulation classes in a program?
a) Inheritance can’t be implemented
Answer: d b) Object must be used is violated
Explanation: It is Encapsulation, which groups c) Encapsulation only is violated
different properties and functions of a real world entity d) Basically all the features of OOP gets violated
into single element. Abstraction, on other hand, is
hiding of functional or exact working of codes and Answer: d
showing only the things which are required by the user. Explanation: All the features are violated because
4. Which of the following is not feature of pure OOP? Inheritance and Encapsulation won’t be implemented.
a) Classes must be used Polymorphism and Abstraction is still possible in some
b) Inheritance cases, but the main features like data binding, object
c) Data may/may not be declared using object use and etc won’t be used hence use of class is must
d) Functions Overloading for OOP concept.
8. How many basic features of OOP are required for a
Answer: c programming language to be purely OOP?
Explanation: Data must be declared using objects. a) 7
Object usage is mandatory because it in turn calls its b) 6
constructors, which in turn must have a class defined. c) 5
If object is not used, it is violation of pure OOP d) 4
concept.
5. Which among the following doesn’t come under Answer: a
OOP concept? Explanation: T . are 7 basic features that define
a) Platform independent whether a programing language is pure OOP or not.
b) Data binding The 4 basic features are inheritance, polymorphism,
c) Message passing encapsulation and abstraction. Further, one is, object
d) Data hiding use is must, secondly, message passing and lastly,
Dynamic binding.
Answer: a 9. The feature by which one object can interact with
Explanation: Platform independence is not feature of another object is:
OOP. C++ supports OOP but it’s not a platform a) Data transfer
independent language. Platform independence depends b) Data Binding
on programming language. c) Message Passing
6. Which feature of OOP is indicated by the following d) Message reading
code?
class student{ int marks; }; Answer: c
class topper:public student{ int age; topper(int age){ Explanation: The interaction between two object is
this.age=age; } }; called message passing feature. Data transfer is not
a) Inheritance feature of OOP. Also, message reading is not feature of
b) Polymorphism OOP.
c) Inheritance and polymorphism 10. ___________ underlines the feature of
d) Encapsulation and Inheritance Polymorphism in a class.
a) Nested class
7
b) Enclosing class OOP
c) Inline function c) The language must follow only 3 features of OOP
d) Virtual Function d) The language must follow all the rules of OOP

Answer: d Answer: d
Explanation: Virtual Functions can be defined in any Explanation: The language must follow all the rules of
class using the keyword virtual. All the classes which OOP to be called a purely OOP language. Even if a
inherit the class containing the virtual function, define single OOP feature is not followed, then it’s known to
the virtual function as required. Redefining the be a partially OOP language.
function on all the derived classes according to class 15. OOP provides better security than POP:
and use represents polymorphism. a) Always true for any programming language
11. Which feature in OOP is used to allocate additional b) May not be true with respect to all programming
function to a predefined operator in any language? languages
a) Operator Overloading c) It depends on type of program
b) Function Overloading d) It’s vice-versa is true
c) Operator Overriding
d) Function Overriding Answer: a
Explanation: It is always true as we have the facility of
Answer: a private and protected access specifiers. Also, only the
Explanation: The feature is operator overloading. T . is public and global data is available globally or else
not feature named operator overriding specifically. program should have proper permission to access the
Function overloading and overriding doesn’t give private data.
addition function to any operator. .
12. Which among doesn’t illustrates polymorphism? .
a) Function overloading
b) Function overriding Polymorphism -
c) Operator overloading
d) Virtual function “Polymorphism”.
1. Which among the following best describes
Answer: b polymorphism?
Explanation: Function overriding doesn’t illustrate a) It is the ability for a message/data to be processed in
polymorphism because the functions are actually more than one form
different and theirs scopes are different. Function and b) It is the ability for a message/data to be processed in
operator overloading illustrate proper polymorphism. only 1 form
Virtual functions show polymorphism because all the c) It is the ability for many messages/data to be
classes which inherit virtual function, define the same processed in one way
function in different ways. d) It is the ability for undefined message/data to be
13. Exception handling is feature of OOP. (True/False) processed in at least one way
a) True
b) False Answer: a
Explanation: It is actually the ability for a message /
Answer: a data to be processed in more than one form. The word
Explanation: Exception handling is feature of OOP as polymorphism indicates many-forms. So if a single
it includes classes concept in most of the cases. Also it entity takes more than one form, it is known as
may come handy while using inheritance. polymorphism.
14. Which among the following, for a pure OOP 2. What do you call the languages that support classes
language, is true? but not polymorphism?
a) The language should follow 3 or more features of a) Class based language
OOP b) Procedure Oriented language
b) The language should follow at least 1 feature of c) Object-based language
8
d) If classes are supported, polymorphism will always {
be supported public : calc_grade()
{
Answer: c return 20;
Explanation: The languages which support classes but }
doesn’t support polymorphism, are known as object- };
based languages. Polymorphism is such an important class failed{ int marks; };
feature, that is a language doesn’t support this feature, a) Only class student can show polymorphism
it can’t be called as a OOP language. b) Only class student and topper together can show
3. Which among the following is the language which polymorphism
supports classes but not polymorphism? c) All class student, topper and average together can
a) SmallTalk show polymorphism
b) Java d) Class failed should also inherit class student for this
c) C++ code to work for polymorphism
d) Ada
Answer: c
Answer: d Explanation: Since Student class is abstract class and
Explanation: Ada is the language which supports the class topper and average are inheriting student, class
concept of classes but doesn’t support the topper and average must define the function named
polymorphism feature. It is an object-based calc_grade(); in abstract class. Since both the definition
programming language. Note that it’s not an OOP are different in those classes, calc_grade() will work in
language. different way for same input from different objects.
4. If same message is passed to objects of several Hence it shows polymorphism.
different classes and all of those can respond in a 6. Which type of function among the following shows
different way, what is this feature called? polymorphism?
a) Inheritance a) Inline function
b) Overloading b) Virtual function
c) Polymorphism c) Undefined functions
d) Overriding d) Class member functions

Answer: c Answer: b
Explanation: The feature defined in question defines Explanation: Only virtual functions among these can
polymorphism feature. . the different objects are show polymorphism. Class member functions can
capable of responding to the same message in different show polymorphism too but we should be sure that the
ways, hence polymorphism. same function is being overloaded or is a function of
5. Which class/set of classes can illustrate abstract class or something like this, since we are not
polymorphism in the following code: sure about all these, we can’t say whether it can show
abstract class student polymorphism or not.
{ 7. In case of using abstract class or function
public : int marks; overloading, which function is supposed to be called
calc_grade(); first?
} A) Local function
class topper:public student B) Function with highest priority in compiler
{ C) Global function
public : calc_grade() D) Function with lowest priority because it might have
{ been halted since long time, because of low priority
return 10;
} Answer: b
}; Explanation: Function with highest priority is called. .,
class average:public student it’s not about the thread scheduling in CPU, but it
9
focuses on whether the function in local scope is derived class will be printed. Function disp() in base
present or not, or if scope resolution is used in some class overrides the function of base class being derived.
way, or if the function matches the argument signature. 10. Which among the following can show
So all these things define which function has the polymorphism?
highest priority to be called in runtime. Local function a) Overloading ||
could be one of the answer but we can’t say if someone b) Overloading +=
have used pointer to another function or same function c) Overloading <<
name. d) Overloading &&
8. Which among the following can’t be used for
polymorphism? Answer: c
a) Static member functions Explanation: Only insertion operator can be overloaded
b) Member functions overloading among all the given options. And the polymorphism
c) Predefined operator overloading can be illustrated . only if any of these is applicable of
d) Constructor overloading being overloaded. Overloading is type of
polymorphism.
Answer: a 11. Find the output of the program:
Explanation: Static member functions are not property class education
of any object. Hence it can’t be considered for {
overloading/overriding. For polymorphism, function char name[10];
must be property of object, not only of class. public : disp()
9. What is output of the following program? {
class student cout<<”Its education system”;
{ }
public : int marks; class school:public education
void disp() {
{ public: void dsip()
cout<<”its base class” {
}; cout<<”Its school
class topper:public student education system”;
{ }
public : };
void disp() void main()
{ {
cout<<”Its derived school s;
class”; s.disp();
} }
} }
void main() { student s; topper t; a) Its school education system
s.disp(); b) Its education system
t.disp(); c) Its school education systemIts education system
} d) Its education systemIts school education system
a) Its base classIts derived class
b) Its base class Its derived class Answer: a
c) Its derived classIts base class Explanation: Notice that the function name in derived
d) Its derived class Its base class class is different from the function name in base class.
Hence when we call the disp() function, base class
Answer: a function is executed. No polymorphism is used ..
Explanation: You need to focus on how the output is 12. Polymorphism is possible in C language.
going to be shown, no space will be given after first a) True
message from base class. And then the message from
10
b) False Answer: d
Explanation: Both of them are using polymorphism. It
Answer: a is not a necessary that polymorphism can be used only
Explanation: It is possible to implement polymorphism once in a program, it can be used anyw ., any number
in C language, even though it doesn’t support class. of times in a single program.
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 Encapsulation -
functions, hence implementing polymorphism in C
language. “Encapsulation”.
13. Which problem may arise if we use abstract class 1. Which among the following best describes
functions for polymorphism? encapsulation?
a) All classes are converted as abstract class a) It is a way of combining various data members into
b) Derived class must be of abstract type a single unit
c) All the derived classes must implement the b) It is a way of combining various member functions
undefined functions into a single unit
d) Derived classes can’t redefine the function c) It is a way of combining various data members and
member functions into a single unit which can operate
Answer: c on any data
Explanation: The undefined functions must be defined d) It is a way of combining various data members and
is a problem, because one may need to implement few member functions that operate on those data members
undefined functions from abstract class, but he will into a single unit
have to define each of the functions declared in
abstract class. Being useless task, it is a problem Answer: d
sometimes. Explanation: It is a way of combining both data
14. Which among the following is not true for members and member functions, which operate on
polymorphism? those data members, into a single unit. We call it a
a) It is feature of OOP class in OOP generally. This feature have helped us
b) Ease in readability of program modify the structures used in C language to be
c) Helps in redefining the same functionality upgraded into class in C++ and other languages.
d) Increases overhead of function definition always 2. If data members are private, what can we do to
access them from the class object?
Answer: d a) Create public member functions to access those data
Explanation: It never increases function definition members
overhead, one way or another if you don’t use b) Create private member functions to access those
polymorphism, you will use the definition in some data members
other way, so it actually helps to write efficient codes. c) Create protected member functions to access those
15. If 2 classes derive one base class and redefine a data members
function of base class, also overload some operators d) Private data members can never be accessed from
inside class body. Among these two things of function outside the class
and operator overloading, w . is polymorphism used?
a) Function overloading only Answer: a
b) Operator overloading only Explanation: We can define public member functions
c) Both of these are using polymorphism to access those private data members and get their
d) Either function overloading or operator overloading value for use or alteration. They can’t be accessed
because polymorphism can be applied only once in a directly but is possible to be access using member
program functions. This is done to ensure that the private data
doesn’t get modified accidentally.

11
3. While using encapsulation, which among the purpose generally. And it can be created by making the
following is possible? class as final and making all its members private.
a) Code modification can be additional overhead 7. Which among the following should be encapsulated?
b) Data member’s data type can be changed without a) The data which is prone to change is near future
changing any other code b) The data prone to change in long terms
c) Data member’s type can’t be changed, or whole c) The data which is intended to be changed
code have to be changed d) The data which belongs to some other class
d) Member functions can be used to change the data
type of data members Answer: a
Explanation: The data prone to change in near future is
Answer: b usually encapsulated so that it doesn’t get changed
Explanation: Data member’s data type can be changed accidentally. We encapsulate the data to hide the
without changing any further code. All the members critical working of program from outside world.
using that data can continue in the same way without 8. How can Encapsulation be achieved?
any modification. Member functions can never change a) Using Access Specifiers
the data type of same class data members. b) Using only private members
4. Which feature can be implemented using c) Using inheritance
encapsulation? d) Using Abstraction
a) Inheritance
b) Abstraction Answer: a
c) Polymorphism Explanation: Using access specifiers we can achieve
d) Overloading encapsulation. Using this we can in turn implement
data abstraction. It’s not necessary that we only use
Answer: b private access.
Explanation: Data abstraction can be achieved by using 9. Which among the following violates the principle of
encapsulation. We can hide the operation and structure encapsulation almost always?
of actual program from the user and can show only a) Local variables
required information by the user. b) Global variables
5. Find which of the following uses encapsulation? c) Public variables
a) void main(){ int a; void fun( int a=10; cout<<a); d) Array variables
fun(); }
b) class student{ int a; public: int b;}; Answer: b
c) class student{int a; public: void disp(){ cout<<a;} }; Explanation: Global variables almost always violates
d) struct topper{ char name[10]; public : int marks; } the principles of encapsulation. Encapsulation says the
data should be accessed only by required set of
Answer: c elements. But global variable is accessible everyw .,
Explanation: It is the class which uses both the data also it is most prone to changes. It doesn’t hide the
members and member functions being declared inside internal working of program.
a single unit. Only data members can be t . in structures 10. Which among the following would destroy the
also. And the encapsulation can only be illustrated if encapsulation mechanism if it was allowed in
some data/operations are associated within class. programming?
6. Encapsulation helps in writing ___________ classes a) Using access declaration for private members of
in java base class
a) Mutable b) Using access declaration for public members of base
b) Abstract class
c) Wrapper c) Using access declaration for local variable of main()
d) Immutable function
d) Using access declaration for global variables
Answer: d
Explanation: Immutable classes are used for caching
12
Answer: a class hero
Explanation: If using access declaration for private {
members of base class was allowed in programming, it char name[10];
would have destroyed whole concept of encapsulation. public : void disp()
As if it was possible, any class which gets inherited {
privately, would have been able to inherit the private cout<<name;
members of base class, and hence could access each }
and every member of base class. };
11. Which among the following can be a concept a) This maintains encapsulation
against encapsulation rules? b) This code doesn’t maintain encapsulation
a) Using function pointers c) This code is vulnerable
b) Using char* string pointer to be passed to non- d) This code gives error
member function
c) Using object array Answer: a
d) Using any kind of pointer/array address in passing to Explanation: This code maintains encapsulation. . the
another function private member is kept private. Outside code can’t
access the private members of class. Only objects of
Answer: d this class will be able to access the public member
Explanation: If we use any kind of array or pointer as function at maximum.
data member which should not be changed, but in 14. Encapsulation is the way to add functions in a user
some case its address is passed to some other function defined structure.
or similar variable. T . are chances to modify its whole a) True
data easily. Hence Against encapsulation. b) False
12. Consider the following code and select the correct
option: Answer: b
class student Explanation: False, because we can’t call these
{ structures if member functions are involved, it must be
int marks; called class. Also, it is not just about adding functions,
public : int* fun() it’s about binding data and functions together.
{ 15. Using encapsulation data security is ___________
return &marks; a) Not ensured
} b) Ensured to some extent
}; c) Purely ensured
main() d) Very low
{
student s; Answer: b
int *ptr=c.fun(); Explanation: The encapsulation can only ensure the
return 0; data security to some extent. If pointer and addresses
} are misused, it may violate encapsulation. Use of
a) This code is good to go global variables also makes the program vulnerable,
b) This code may result in undesirable conditions hence we can’t say that encapsulation gives pure
c) This code will generate error security.
d) This code violates encapsulation .
.
Answer: d
Explanation: This code violates the encapsulation. By Abstraction -
this code we can get the address of the private member
of the class, hence we can change the value of private “Abstraction”.
member, which is against the rules. 1. Which among the following best defines
13. Consider the code and select the wrong choice: abstraction?
13
a) Hiding the implementation d) Unstable interface
b) Showing the important data
c) Hiding the important data Answer: c
d) Hiding the implementation and showing only the Explanation: It is to idealize the interface. In this way
features the programmer can use the programming features
more efficiently and can code better. It can’t increase
Answer: d the program complexity, as the feature itself is made to
Explanation: It includes hiding the implementation part hide it.
and showing only the required data and features to the 6. Abstraction can apply to:
user. It is done to hide the implementation complexity a) Control and data
and details from the user. And to provide a good b) Only data
interface in programming. c) Only control
2. Hiding the implementation complexity can: d) Classes
a) Make the programming easy
b) Make the programming complex Answer: a
c) Provide more number of features Explanation: Abstraction applies to both. Control
d) Provide better features abstraction involves use of subroutines and control
flow abstraction. Data abstraction involves handling
Answer: a pieces of data in meaningful ways.
Explanation: It can make the programming easy. The 7. Which among the following can be viewed as
programming need not know how the inbuilt functions combination of abstraction of data and code.
are working but can use those complex functions a) Class
directly in the program. It doesn’t provide more b) Object
number of features or better features. c) Inheritance
3. Class is _________ abstraction d) Interfaces
a) Object
b) Logical Answer: b
c) Real Explanation: Object can be viewed as abstraction of
d) Hypothetical data and code. It uses data members and their
functioning as data abstraction. Code abstraction as use
Answer: b of object of inbuilt class.
Explanation: Class is logical abstraction because it 8. Abstraction principle includes___________
provides a logical structure for all of its objects. It a) Use abstraction at its minimum
gives an overview of the features of an object. b) Use abstraction to avoid longer codes
4. Object is ________ abstraction c) Use abstraction whenever possible to avoid
a) Object duplication
b) Logical d) Use abstraction whenever possible to achieve OOP
c) Real
d) Hypothetical Answer: c
Explanation: Abstraction principle includes use of
Answer: c abstraction to avoid duplication (usually of code). It
Explanation: Object is real abstraction because it this way the program doesn’t contain any redundant
actually contains those features of class. It is the functions and make the program efficient.
implementation of overview given by class. Hence the 9. Higher the level of abstraction, higher are the
class is logical abstraction and its object is real. details.
5. Abstraction gives higher degree of ________ a) True
a) Class usage b) False
b) Program complexity
c) Idealized interface Answer: b
Explanation: Higher the level of abstraction, lower are
14
the details. The best way to understand this is to represents all the functioning of phone, display shows
consider a whole system that is highest level of the display only, and the phone is represented as a
abstraction as it hides everything inside. And next whole. Which among the following have highest level
lower level would contain few of the computer of abstraction?
components and so on. a) Motherboard
10. Encapsulation and abstraction differ as: b) Display
a) Binding and Hiding respectively c) Camera
b) Hiding and Binding respectively d) Phone
c) Can be used any way
d) Hiding and hiding respectively Answer: d
Explanation: Phone as a whole have the highest level
Answer: a of abstraction. This is because the phone being a single
Explanation: Abstraction is hiding the complex code. unit represents the whole system. W .as motherboard,
For example we directly use cout object in C++ but we display and camera are its components.
don’t know how is it actually implemented. 14. Which among the following is not a level of
Encapsulation is data binding, as in, we try to combine abstraction:
the similar type of data and functions together. a) Logical level
11. In terms of stream and files________ b) Physical level
a) Abstraction is called a stream and device is called a c) View level
file d) External level
b) Abstraction is called a file and device is called a
stream Answer: d
c) Abstraction can be called both file and stream Explanation: Abstraction is generally divided into 3
d) Abstraction can’t be defined in terms of files and different levels, namely, logical, physical and view
stream level. External level is not defined in terms of
abstraction.
Answer: a 15. Using higher degree of abstraction __________
Explanation: Abstraction is called stream to provide a a) May get unsafe
level of complexity hiding, for how the files operations b) May reduce readability
are actually done. Actual devices are called file c) Can be safer
because in one way or another, those can be considered d) Can increase vulnerability
as single entity and t . is nothing hidden.
12. If two classes combine some private data members Answer: c
and provides public member functions to access and Explanation: It will make the code safer. One may
manipulate those data members. W . is abstraction think it reduces the readability, but the fact is, it
used? actually helps us understand the code better. We don’t
a) Using private access specifier for data members have to read the complex code which is of no use in
b) Using class concept with both data members and understanding the program.
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 Constructors -

Answer: c “Constructors”.
Explanation: It is the concept of hiding program 1. Which among the following is called first,
complexity and actual working in background. Hence automatically, whenever an object is created?
use of public member functions illustrates a) Class
abstraction .. b) Constructor
13. A phone is made up of many components like c) New
motherboard, camera, sensors and etc. If the processor
15
d) Trigger d) 5

Answer: b Answer: a
Explanation: Constructors are the member functions Explanation: Two types of constructors are defined
which are called automatically whenever an object is generally, namely, default constructor and
created. It is a mandatory functions to be called for an parameterized constructor. Default constructor is not
object to be created as this helps in initializing the necessary to be defined always.
object to a legal initial value for the class. 6. If a programmer defines a class and defines a default
2. Which among the following is not a necessary value parameterized constructor inside it.
condition for constructors? He has not defined any default constructor. And then
a) Its name must be same as that of class he try to create the object without passing arguments,
b) It must not have any return type which among the following will be correct?
c) It must contain a definition body a) It will not create the object (as parameterized
d) It can contains arguments constructor is used)
b) It will create the object (as the default arguments are
Answer: c passed )
Explanation: Constructors are predefined implicitly, c) It will not create the object ( as the default
even if the programmer doesn’t define any of them. constructor is not defined )
Even if the programmer declares a constructor, it’s not d) It will create the object ( as at least some constructor
necessary that it must contain some definition. is defined )
3. Which among the following is correct?
a) class student{ public: int student(){} }; Answer: b
b) class student{ public: void student (){} }; Explanation: It will create the object without any
c) class student{ public: student{}{} }; problem, because the default arguments use the default
d) class student{ public: student(){} }; value if no value is passed. Hence it is equal to default
constructor with zero parameters. But it will not create
Answer: d the object if signature doesn’t match.
Explanation: The constructors must not have any return 7. Default constructor must be defined, if
type. Also, the body may or may not contain any body. parameterized constructor is defined and the object is
Defining default constructor is optional, if you are not to be created without arguments.
using any other constructor. a) True
4. In which access should a constructor be defined, so b) False
that object of the class can be created in any function?
a) Public Answer: a
b) Protected Explanation: If the object is create without arguments
c) Private and only parameterized constructors are used, compiler
d) Any access specifier will work will give an error as t . is no default constructor
defined. And some constructor must be called so as to
Answer: a create an object in memory.
Explanation: Constructor function should be available 8. If class C inherits class B. And B has inherited class
to all the parts of program w . the object is to be A. Then while creating the object of class C, what will
created. Hence it is advised to define it in public be the sequence of constructors getting called?
access, so that any other function is able to create a) Constructor of C then B, finally of A
objects. b) Constructor of A then C, finally of B
5. How many types of constructors are available for c) Constructor of C then A, finally B
use in general (with respect to parameters)? d) Constructor of A then B, finally C
a) 2
b) 3 Answer: d
c) 4 Explanation: While creating the object of class C, its
constructor would be called by default. But, if the class
16
is inheriting some other class, firstly the parent class };
constructor will be called so that all the data is student s1, s2, s3;
initialized that is being inherited. a) s1 then s2 then s3
9. In multiple inheritance, if class C inherits two b) s3 then s2 then s1
classes A and B as follows, which class constructor c) s2 then s3 then s1
will be called first: d) All are created at same time
class A{ };
class B{ }; Answer: a
class C: public A, public B{ }; Explanation: The objects are created in the sequence of
a) A() how they are written. This happens because the
b) B() constructors are called in the sequence of how the
c) C() objects are mentioned. This is done in sequence.
d) Can’t be determined 13. Which among the following helps to create a
temporary instance?
Answer: a a) Implicit call to a default constructor
Explanation: Constructor of class A will be called first. b) Explicit call to a copy constructor
This is because the constructors in multiple inheritance c) Implicit call to a parameterized constructor
are called in the sequence in which they are written to d) Explicit call to a constructor
be inherited. . A is written first, hence it is called first.
10. Which among the following is true for copy Answer: d
constructor? Explanation: Explicit call to a constructor can let you
a) The argument object is passed by reference create temporary instance. This is because the
b) It can be defined with zero arguments temporary instances doesn’t have any name. Those are
c) Used when an object is passed by value to a function deleted from memory as soon as their reference is
d) Used when a function returns an object removed.
14. Which among the following is correct for the class
Answer: b defined below?
Explanation: It can’t be defined with zero number of class student
arguments. This is because to copy one object to {
another, the object must be mentioned so that compiler int marks;
can take values from that object. public: student(){}
11. If the object is passed by value to a copy student(int x)
constructor: {
a) Only public members will be accessible to be copied marks=x;
b) That will work normally }
c) Compiler will give out of memory error };
d) Data stored in data members won’t be accessible main()
{
Answer: c student s1(100);
Explanation: Compiler runs out of memory. This is student s2();
because while passing the argument by value, a student s3=100;
constructor of the object will be called. That in turn return 0;
called another object constructor for values, and this a) Object s3, syntax error
goes on. This is like a constructor call to itself, and this b) Only object s1 and s2 will be created
goes on infinite times, hence it must be passed by c) Program runs and all objects are created
reference, so that the constructor is not called. d) Program will give compile time error
12. Which object will be created first?
class student Answer: c
{ Explanation: It is a special case of constructor with
int marks; only 1 argument. While calling a constructor with one
17
argument, you are actually implicitly creating a Answer: d
conversion from the argument type to the type of class. Explanation: The object s2 can be assigned with one
Hence you can directly specify the value of that one value only if a single argument constructor is defined
argument with assignment operator. in class, but ., it can’t be done as no constructor is
15. For constructor overloading, each constructor must defined. Hence every object must be declare or created
differ in ___________ and __________ without using arguments.
a) Number of arguments and type of arguments 3. Which constructor is called while assigning some
b) Number of arguments and return type object with another?
c) Return type and type of arguments a) Default
d) Return type and definition b) Parameterized
c) Copy
Answer: a d) Direct assignment is used
Explanation: Each constructor must differ in the
number of arguments it accepts and the type of Answer: c
arguments. This actually defines the constructor Explanation: Copy constructor is used while an object
signature. This helps to remove the ambiguity and is assigned with another. This is mandatory since we
define a unique constructor as required. can’t decide which member should be assigned to
. which member value. By using copy constructor, we
. can assign the values in required form.
4. It’s necessary to pass object by reference in copy
Object Oriented Programming Quiz Online - constructor because:
a) Constructor is not called in pass by reference
This set of Object Oriented Programming online quiz b) Constructor is called in pass by reference only
focuses on “Types of Constructors”. c) It passes the address of new constructor to be
1. How many types of constructors are available, in created
general, in any language? d) It passes the address of new object to be created
a) 2
b) 3 Answer: a
c) 4 Explanation: Object must be passed by reference to
d) 5 copy constructor because constructor is not called in
pass by reference. Otherwise, in pass by value, a
Answer: b temporary object will be created which in turn will try
Explanation: T . are 3 types of constructors in general, to call its constructor that is already being used. This
namely, default constructors, parameterized results in creating infinite number of objects and hence
constructors and copy constructors. Default one is memory shortage error will be shown.
called whenever an object is created without 5. Which specifier applies only to the constructors?
arguments. a) Public
2. Choose the correct option for the following code. b) Protected
class student c) Implicit
{ d) Explicit
int marks;
} Answer: d
student s1; Explanation: The keyword explicit can be used while
student s2=2; defining the constructor only. This is used to suppress
a) Object s1 should be passed with argument. the implicit call to the constructor. It ensures that the
b) Object s2 should not be declared constructors are being called with the default syntax
c) Object s2 will not be created, but program runs only (i.e. only by using object and constructor name).
d) Program gives compile time error 6. Which among the following is true?
a) Default constructor can’t be defined by the
programmer
18
b) Default parameters constructor isn’t equivalent to of static members must be retained and continued from
default constructor the time it gets created.
c) Default constructor can be called explicitly 10. Which among the following is true for static
d) Default constructor is and always called implicitly constructor?
only a) Static constructors are called with every new object
b) Static constructors are used initialize data members
Answer: c to zero always
Explanation: Default constructors can be called c) Static constructors can’t be parameterized
explicitly anytime. They are specifically used to constructors
allocate memory space for the object in memory, in d) Static constructors can be used to initialize the non-
general. It is not necessary that these should always be static members also
called implicitly.
7. Which type of constructor can’t have a return type? Answer: c
a) Default Explanation: Static constructors can’t be parameterized
b) Parameterized constructors. Those are used to initialize the value of
c) Copy static members only. And that must be a definite value.
d) Constructors don’t have a return type Accepting arguments may make it possible that static
members loses their value with every new object being
Answer: d created.
Explanation: Constructors don’t return any value. 11. Within a class, only one static constructor can be
Those are special functions, whose return type is not created.
defined, not even void. This is so because the a) True
constructors are meant to initialize the members of b) False
class and not to perform some task which can return
some value to newly created object. Answer: a
8. Why do we use static constructors? Explanation: Since the static constructors are can’t be
a) To initialize the static members of class parameterized, they can’t be overloaded. Having this
b) To initialize all the members with static value case, only one constructor will be possible to be
c) To delete the static members when not required created in a local scope, because the signature will
d) To clear all the static members initialized values always be same and hence it will not be possible to
overload static constructor.
Answer: a 12. Default constructor initializes all data members as:
Explanation: Static constructors help in initializing the a) All numeric member with some garbage values and
static members of the class. This is provided because string to random string
the static members are not considered to be property of b) All numeric member with some garbage values and
the object, rather they are considered as the property of string to null
class. c) All numeric member with zero and strings to
9. When and how many times a static constructor is random value
called? d) All numeric member with zero and strings to null
a) Created at time of object destruction
b) Called at first time when an object is created and Answer: d
only one time Explanation: Default constructor, which even the
c) Called at first time when an object is created and programmer doesn’t define, always initialize the values
called with every new object creation as zero if numeric and null if string. This is done so as
d) Called whenever an object go out of scope to avoid the accidental values to change the conditional
statements being used and similar conditions.
Answer: b 13. When is the static constructor called?
Explanation: Those are called at very first call of object a) After the first instance is created
creation. That is called only one time because the value b) Before default constructor call of first instance
c) Before first instance is created
19
d) At time of creation of first instance This set of Basic Object Oriented Programming .
focuses on “Copy Constructor”.
Answer: c 1. Copy constructor is a constructor which
Explanation: The static constructor is called before ________________
creation of first instance of that class. This is done so a) Creates an object by copying values from any other
that even the first instance can use the static value of object of same class
the static members of the class and manipulate as b) Creates an object by copying values from first object
required. created for that class
14. If constructors of a class are defined in private c) Creates an object by copying values from another
access, then: object of another class
a) The class can’t be inherited d) Creates an object by initializing it with another
b) The class can be inherited previously created object of same class
c) Instance can be created only in another class
d) Instance can be created anyw . in the program Answer: d
Explanation: The object that has to be copied to new
Answer: a object must be previously created. The new object gets
Explanation: If the constructors are defined in private initialized with the same values as that of the object
access, then the class can’t be inherited by other mentioned for being copied. The exact copy is made
classes. This is useful when the class contains static with values.
members only. The instances can never be created. 2. The copy constructor can be used to:
15. Which among the following is correct, based on the a) Initialize one object from another object of same
given code below: type
class student b) Initialize one object from another object of different
{ type
int marks; c) Initialize more than one object from another object
public : student() of same type at a time
{ d) Initialize all the objects of a class to another object
cout<<”New student details can of another class
be added now”;
} Answer: a
}; Explanation: The copy constructor has the most basic
student s1; function to initialize the members of an object with
a) Cout can’t be used inside the constructor same values as that of some previously created object.
b) Constructor must contain only initializations The object must be of same class.
c) This program works fine 3. If two classes have exactly same data members and
d) This program produces errors member function and only they differ by class name.
Can copy constructor be used to initialize one class
Answer: c object with another class object?
Explanation: This program will work fine. This is a) Yes, possible
because it is not mandatory that a constructor must b) Yes, because the members are same
contain only initialization only. If you want to perform c) No, not possible
a task on each instance being created, that code can be d) No, but possible if constructor is also same
written inside the constructor.
. Answer: c
To practice all areas of Object Oriented Programming Explanation: The restriction for copy constructor is that
for online Quizzes, . it must be used with the object of same class. Even if
the classes are exactly same the constructor won’t be
Basic able to access all the members of another class. Hence
we can’t use object of another class for initialization.

20
4. The copy constructors can be used to ________ Explicit definition allows to manipulate the data as
a) Copy an object so that it can be passed to a class required.
b) Copy an object so that it can be passed to a function 8. What is the syntax of copy constructor?
c) Copy an object so that it can be passed to another a) classname (classname &obj){ /*constructor
primitive type variable definition*/ }
d) Copy an object for type casting b) classname (cont classname obj){ /*constructor
definition*/ }
Answer: b c) classname (cont classname &obj){ /*constructor
Explanation: When an object is passed to a function, definition*/ }
actually its copy is made in the function. To copy the d) classname (cont &obj){ /*constructor definition*/ }
values, copy constructor is used. Hence the object
being passed and object being used in function are Answer: c
different. Explanation: The syntax must contain the class name
5. Which returning an object, we can use first, followed by the classname as type and &object
____________ within parenthesis. Then comes the constructor body.
a) Default constructor The definition can be given as per requirements.
b) Zero argument constructor 9. Object being passed to a copy constructor
c) Parameterized constructor ___________
d) Copy constructor a) Must be passed by reference
b) Must be passed by value
Answer: d c) Must be passed with integer type
Explanation: While returning an object we can use the d) Must not be mentioned in parameter list
copy constructor. When we assign the return value to
another object of same class then this copy constructor Answer: a
will be used. And all the members will be assigned the Explanation: This is mandatory to pass the object by
same values as that of the object being returned. reference. Otherwise the object will try to create
6. If programmer doesn’t define any copy constructor another object to copy its values, in turn a constructor
then _____________ will be called, and this will keep on calling itself. This
a) Compiler provides an implicit copy constructor will cause the compiler to give out of memory error.
b) Compiler gives an error 10. Out of memory error is given when the object
c) The objects can’t be assigned with another objects _____________ to the copy constructor.
d) The program gives run time error if copying is used a) Is passed with & symbol
b) Is passed by reference
Answer: a c) Is passed as
Explanation: The compiler provides an implicit copy d) Is not passed by reference
constructor. It is not mandatory to always create an
explicit copy constructor. The values are copied using Answer: d
implicit constructor only. Explanation: All the options given, directly or
7. If a class implements some dynamic memory indirectly indicate that the object is being passed by
allocations and pointers then _____________ reference. And if object is not passed by reference then
a) Copy constructor must be defined the out of memory error is produced. Due to infinite
b) Copy constructor must not be defined constructor call of itself.
c) Copy constructor can’t be defined 11. Copy constructor will be called whenever the
d) Copy constructor will not be used compiler __________
a) Generates implicit code
Answer: a b) Generates member function calls
Explanation: In the case w . dynamic memory c) Generates temporary object
allocation is used, the copy constructor definition must d) Generates object operations
be given. The implicit copy constructor is not capable
of manipulating the dynamic memory and pointers.
21
Answer: c To practice basic . on all areas of Object Oriented
Explanation: Whenever the compiler creates a Programming, .
temporary object, copy constructor is used to copy the
values from existing object to the temporary object. Overloading Constructors -
12. The deep copy is possible only with the help of
__________ “Overloading Constructors”.
a) Implicit copy constructor 1. Which among the following best describes
b) User defined copy constructor constructor overloading?
c) Parameterized constructor a) Defining one constructor in each class of a program
d) Default constructor b) Defining more than one constructor in single class
c) Defining more than one constructor in single class
Answer: b with different signature
Explanation: While using explicit copy constructor, the d) Defining destructor with each constructor
pointers of copied object point to the intended memory
location. This is assured since the programmers Answer: c
themselves manipulate the addresses. Explanation: If more than one constructors are defined
13. Can a copy constructor be made private? in a class with same signature, then that results in error.
a) Yes, always The signatures must be different. So that the
b) Yes, if no other constructor is defined constructors can be differentiated.
c) No, never 2. Can constructors be overloaded in derived class?
d) No, private members can’t be accessed a) Yes, always
b) Yes, if derived class has no constructor
Answer: a c) No, programmer can’t do it
Explanation: The copy constructor can be defined d) No, never
private. If we make it private then the objects of the
class can’t be copied. It can be used when a class used Answer: d
dynamic memory allocation. Explanation: The constructor must be having the same
14. The arguments to a copy constructor name as that of a class. Hence a constructor of one
_____________ class can’t even be defined in another class. Since the
a) Must be const constructors can’t be defined in derived class, it can’t
b) Must not be cosnt be overloaded too, in derived class.
c) Must be integer type 3. Does constructor overloading include different
d) Must be static return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes
Answer: a different
Explanation: The object should not be modified in the b) Yes, because return types can differentiate two
copy constructor. Because the object itself is being functions
copied. When the object is returned from a function, c) No, return type can’t differentiate two functions
the object must be a constant otherwise the compiler d) No, constructors doesn’t have any return type
creates a temporary object which can die anytime.
15. Copy constructors are overloaded constructors. Answer: d
a) True Explanation: The constructors doesn’t have any return
b) False type. When we can’t have return type of a constructor,
overloading based on the return type is not possible.
Answer: a Hence only parameters can be different.
Explanation: The copy constructors are always 4. Which among the following is possible way to
overloaded constructors. They has to be. All the classes overload constructor?
have a default constructor and other constructors are a) Define default constructor, 1 parameter constructor
basically overloaded constructors. and 2 parameter constructor
. b) Define default constructor, zero argument
22
constructor and 1 parameter constructor constructor. The implicit constructor makes all the
c) Define default constructor, and 2 other string values null and allocates memory space for each
parameterized constructors with same signature data member.
d) Define 2 default constructors 7. When is the constructor called for an object?
a) As soon as overloading is required
Answer: a b) As soon as class is derived
Explanation: All the constructors defined in a class c) As soon as class is created
must have different signature in order to be overloaded. d) As soon as object is created
. one default and other parameterized constructors are
used, w .in one is of only one parameter and other Answer: d
accepts two. Hence overloading is possible. Explanation: The constructor is called as soon as the
5. Which constructor will be called from the object object is created. The overloading comes into picture
created in the code below? as to identify which constructor have to be called
class A depending on arguments passed in creation of object.
{ 8. Which among the following function can be used to
int i; call default constructor implicitly in java?
A() a) this()
{ b) that()
i=0; cout&lt;&lt;i; c) super()
} d) sub()
A(int x=0)
{ Answer: a
i=x; cout&lt;&lt;I; 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
A obj1; redundant data in all the constructors.
a) Default constructor 9. Why do we use constructor overloading?
b) Parameterized constructor a) To use different types of constructors
c) Compile time error b) Because it’s a feature provided
d) Run time error c) To initialize the object in different ways
d) To differentiate one constructor from another
Answer: c
Explanation: When a default constructor is defined and Answer: c
another constructor with 1 default value argument is Explanation: The constructors are overloaded to
defined, creating object without parameter will create initialize the objects of a class in different ways. This
ambiguity for the compiler. The compiler won’t be allows us to initialize the object with either default
able to decide which constructor should be called, values or used given values. If data members are not
hence compile time error. initialized then program may give unexpected results.
6. Which among the following is false for a 10. If programmer have defined parameterized
constructor? constructor only, then __________________
a) Constructors doesn’t have a return value a) Default constructor will not be created by the
b) Constructors are always user defined compiler implicitly
c) Constructors are overloaded with different signature b) Default constructor will be created by the compiler
d) Constructors may or may not have any arguments implicitly
being accepted c) Default constructor will not be created but called at
runtime
Answer: b d) Compile time error
Explanation: The constructors are not always user
defined. The construct will be provided implicitly from Answer: a
the compiler if the used doesn’t defined any Explanation: When the programmer doesn’t specify
23
any default constructor and only defines some 13. What is we only create an object but don’t call any
parameterized constructor. The compiler doesn’t constructor for it in java?
provide any default constructor implicitly. This is a) Implicit constructor will be called
because it is assumed that the programmer will create b) Object is initialized to some null values
the objects only with constructors. c) Object is not created
11. Which among the following is not valid in java? d) Object is created but points to null
a) Constructor overloading
b) Recursive constructor call Answer: d
c) Default value constructors Explanation: The object becomes a reference object
d) String argument constructor which can be initialized will another object. Then this
object will indirectly become another name of the
Answer: b object being assigned. If not assigned, it simply points
Explanation: Java doesn’t provide the feature to to null address.
recursively call the constructor. This is to eliminate the 14. Which among the following is false?
out of memory error in some cases that arises a) Constructor can’t be overloaded in Kotlin
unexpectedly. That is an predefined condition for b) Constructors can’t be called recursively in java
constructors in java. c) Constructors can be overloaded in C++
12. Which constructor will be called from the object d) Constructors overloading depends on different
obj2 in the following program? signatures
class A
{ Answer: a
int i; Explanation: Kotlin language allows constructor
A() overloading. This is a basic feature for the
{ constructors. The constructor overloading allows the
i=0; object to be initialized according to the user.
} 15. Which is correct syntax?
A(int x) a) classname objectname= new() integer;
{ b) classname objectname= new classname;
i=x+1; c) classname objectname= new classname();
} d) classname objectname= new() classname();
A(int y, int x)
{ Answer: c
i=x+y; Explanation: The syntax for object creating in java
} with calling a constructor for is it is as in option c. The
}; syntax must contain the classname followed by the
A obj1(10); object name. The keyword new must be used and then
A obj2(10,20); the constructor call with or without the parameters as
A obj3; required.
a) A(int x) .
b) A(int y) .
c) A(int y, int x)
d) A(int y; int x) Object Oriented Programming . for Experienced -

Answer: c This set of Object Oriented Programming . for


Explanation: The two argument constructor will be Experienced people focuses on “Execution of
called as we are passing 2 arguments to the object Constructor or Destructor”.
while creation. The arguments will be passed together 1. Which among the following best describes the
and hence compiler resolves that two argument constructors?
constructor have to be called. a) A function which is called whenever an object is
referenced
24
b) A function which is called whenever an object is Answer: c
created to initialize the members Explanation: The destructor must have same name as
c) A function which is called whenever an object is that of the corresponding class. The class name should
assigned to copy the values be preceded by the tilde symbol (~).
d) A function which is called whenever an object is to 5. Which among the following is true?
be given values for members a) First the constructor of parent classes are called in
sequence of inheritance
Answer: b b) First the constructor of child classes are called in the
Explanation: The constructors are special type of sequence of inheritance
functions which are called whenever an object is c) First constructor called is of the object being created
created. This is to initialize the data members of the d) Constructors are called randomly
class. The constructor allocates memory space for all
the data members. Answer: a
2. Which among the following best describes Explanation: First the constructor of parent class are
destructor? called. The order in which the parent class constructors
a) A function which is called just before the objects are are called is same in the sequence of inheritance used.
destroyed 6. What is the sequence of destructors call?
b) A function which is called after each reference to a) Same order as that of the constructors call
the object b) Random order
c) A function which is called after termination of the c) According to the priority
program d) Revere of the order of constructor call
d) A function which is called before calling any
member function Answer: d
Explanation: The destructors are called in the reverse
Answer: a order as that of the constructors being called. This is
Explanation: The Destructors are special functions done to ensure that all the resources are released in
which are called just before an object is destroyed. sequence. That is, the derived class destructors will be
This functions is responsible to free all the allocated called first.
resources to the object. Objects are destroyed 7. The destructors _____________________
whenever those go out of scope. a) Can have maximum one argument
3. Which among the following represents correct b) Can’t have any argument
constructor? c) Can have more than one argument
a) ()classname d) Can’t have more than 3 arguments
b) ~classname()
c) –classname() Answer: b
d) classname() Explanation: The destructors doesn’t have any
arguments. The destructors have to be called implicitly
Answer: d whenever an object goes out of scope. The user can’t
Explanation: The constructors must contain only the pass argument to the implicit call.
class name. The class name is followed by the blank 8. Destructor calls ________________ (C++)
parenthesis or we can have parameters if some values a) Are only implicit
are to be passed. b) Are only explicit
4. Which among the following is correct syntax for the c) Can be implicit or explicit
destructors? d) Are made at end of program only
a) classname()
b) ()classname Answer: c
c) ~classname() Explanation: The destructors are usually called
d) -classname() implicitly whenever an object goes out of scope. The
destructors can also be called explicitly if required. The
call must be made, implicitly or explicitly.
25
9. Number of destructors called are resources for the object. Memory spaces are to be
a) Always equal to number of constructors called finalized.
b) Always less than the number of constructors called 14. The destructors ____________
c) Always greater than the number of constructors a) Have a return type
called b) May have a return type
d) Always less than or equal to number of constructors c) Of derived classes have return type
d) Doesn’t have a return type
Answer: a
Explanation: Destructor will be called only to free the Answer: d
resources allocated for an object. The resources are Explanation: The destructors are intended to free the
allocated only the constructor for an object is called. memory space. And all the resources that were
10. For explicit call _________________ allocated for the object. The return value is not
a) The destructor must be private supported since only memory have to be made free.
b) The destructor must be public 15. The destructor can be called before the constructor
c) The destructor must be protected if required. (True/False)
d) The destructor must be defined outside the class a) True
b) False
Answer: b
Explanation: The destructor must be public for explicit Answer: b
calls. If it is made private or protected then it won’t be Explanation: The destructors can be called only after
accessible outside the class. T . is no restriction of the constructor calls. It is not a mandatory rule but the
definition the destructor outside the class. deletion can only take place if t . is something created
11. If a class have 4 constructors then it must have 4 using the constructor.
destructors also. .
a) True To practice all areas of Object Oriented Programming
b) False for Experienced people, .

Answer: b Destructors -
Explanation: Even if the class have 4 constructors, only
one would be used. And only one destructor is allowed. ” Destructors”.
12. Which among the following is true for destructors? 1. Which among the following describes a destructor?
a) Destructors can be overloaded a) A special function that is called to free the resources,
b) Destructors can be define more than one time acquired by the object
c) Destructors can’t be overloaded b) A special function that is called to delete the class
d) Destructors are overloaded in derived classes c) A special function that is called anytime to delete an
object
Answer: c d) A special function that is called to delete all the
Explanation: The destructors can never be overloaded. objects of a class
The destructors doesn’t have arguments. And to get
overloaded, they must have different signature. This is Answer: a
not possible if arguments are not allowed. Explanation: It is used to free the resources that the
13. The constructor _____________ object might had used in its lifespan. The destructors
a) Have a return type are called implicitly whenever an object’s life ends.
b) May have a return type 2. When a destructor is called?
c) Of derived classes have return type a) After the end of object life
d) Doesn’t have a return type b) Anytime in between object’s lifespan
c) At end of whole program
Answer: d d) Just before the end of object life
Explanation: The constructors doesn’t have any return
type. The constructors are intended to allocate the
26
Answer: d 6. When is the destructor of a global object called?
Explanation: The destructor is called just before the a) Just before end of program
object go out of scope or just before its life ends. This b) Just after end of program
is done to ensure that all the resources reserved for the c) With the end of program
object are used and at last, are made free for others. d) Anytime when object is not needed
3. Which among the following is correct for abstract
class destructors? Answer: a
a) It doesn’t have destructors Explanation: This is because the lifespan of global
b) It has destructors object is from start of the program, till the end of the
c) It may or may not have destructors program. And hence program end is the end of global
d) It contains an implicit destructor object too. Just before the end of program, the
destructor will be called to free the acquired resources
Answer: a by the objects.
Explanation: It doesn’t have destructors. Since an 7. How the constructors and destructors can be
abstract class don’t have constructors, and hence can’t differentiated?
have instances. Having this case, the abstract classes a) Destructor have a return type but constructor doesn’t
don’t have destructors too, because that would be of no b) Destructors can’t be defined by the programmer, but
use .. constructors can be defined
4. If in multiple inheritance, class C inherits class B, c) Destructors are preceded with a tilde (~) symbol,
and Class B inherits class A. In which sequence are and constructor doesn’t
their destructors called, if an object of class C was d) Destructors are same as constructors in syntax
declared?
a) ~C() then ~B() then ~A() Answer: c
b) ~B() then ~C() then ~A() Explanation: The destructors are preceded with the
c) ~A() then ~B() then ~C() tilde (~) symbol. The name is same as that of the class.
d) ~C() then ~A() then ~B() These also doesn’t have any return type.
8. Destructors doesn’t accept parameters.
Answer: a a) True
Explanation: The destructors are always called in the b) False
reverse order of how the constructors were called. .
class A constructor would have been created first if Answer: a
Class C object is declared. Hence class A destructor is Explanation: The destructors doesn’t accept the
called at last. arguments. Those are just used to free up the resources.
5. Choose the correct sequence of destructors being 9. Destructors can be ________
called for the following code: a) Abstract type
class A{ }; b) Virtual
class B{ }; c) Void
class C: public A, public B{ }; d) Any type depending on situation
a) ~A(), ~B(), ~C()
b) ~B(), ~C(), ~A() Answer: b
c) ~A(), ~C(), ~B() Explanation: The destructors can be virtual. It is
d) ~C(), ~B(), ~A() actually advised to keep the destructors virtual always.
This is done to suppress the problems that may arise if
Answer: d inheritance is involved.
Explanation: In multiple inheritance, the constructors 10. Global destructors execute in ___________ order
are called in the sequence of how they are written in after main function is terminated
inheritance sequence. And the destructors will be a) Sequential
called in the reverse order. This can be cross verified b) Random
just by printing a message from each destructor defined c) Reverse
in classes.
27
d) Depending on priority Answer: c
Explanation: The constructor that would have created
Answer: c at last, its destructor will be called first when the code
Explanation: The destructors are always called in goes out of scope. This will help the program to
reverse order no matter which destructor it is. This is manage the resources more efficiently.
done to ensure that all the resources are able to get 14. When an object is passed to a function, its copy is
free. And no resource is kept busy. made in the function and then:
11. When is it advised to have user defined destructor? a) The destructor of the copy is called when function is
a) When class contains some pointer to memory returned
allocated in class b) The destructor is never called in this case
b) When a class contains static variables c) The destructor is called but it is always implicit
c) When a class contains static functions d) The destructor must be user defined
d) When a class is inheriting another class only
Answer: a
Answer: a Explanation: When an object is passed to a function, its
Explanation: This is always advised to have user copy is made in the function. This copy acts as a real
defined destructor when pointers are involved in class. object till the function is live. When the function is
This is usually done to ensure that the memory, that returned, the copy’s destructor is called to free the
was allocated dynamically, gets free after use and resources held by it.
doesn’t cause memory leak. 15. What happens when an object is passed by
12. Which among the following is correct for reference?
destructors concept? a) Destructor is not called
a) Destructors can be overloaded b) Destructor is called at end of function
b) Destructors can have only one parameter at c) Destructor is called when function is out of scope
maximum d) Destructor is called when called explicitly
c) Destructors are always called after object goes out of
scope Answer: a
d) T . can be only one destructor in a class Explanation: The destructor is never called in this
situation. The concept is that when an object is passed
Answer: d by reference to the function, the constructor is not
Explanation: This is so because the destructors can’t be called, but only the main object will be used. Hence no
overloaded. And the destructor must have the same destructor will be called at end of function.
name as that of class with a tilde symbol preceding the .
name of destructor. Hence t . can be only one .
destructor in a class. Since more than one function with
same name and signature can’t be present in same Access Specifiers -
scope.
13. Which class destructor will be called first, when “Access Specifiers”.
following code go out of scope? 1. How many types of access specifiers are provided in
class A{ }; OOP (C++)?
class B{ }; a) 1
class C: public B{ }; b) 2
A a; c) 3
B b; d) 4
C c;
a) ~A() Answer: c
b) ~B() Explanation: Only 3 types of access specifiers are
c) ~C() available. Namely, private, protected and public. All
d) ~B() and ~C() these three can be used according to the need of
security of members.
28
2. Which among the following can be used together in b) Member functions can be made private
a single class? c) Default members can’t be inherited
a) Only private d) Public members are accessible from other classes
b) Private and Protected together also
c) Private and Public together
d) All three together Answer: c
Explanation: The default members can be inherited.
Answer: d Provided that they are in same package. It works in a
Explanation: All the classes can use any of the little different way from private access specifier.
specifiers as needed. T . is no restriction on how many 7. If a class has all the private members, which
of them can be used together. specifier will be used for its implicit constructor?
3. Which among the following can restrict class a) Private
members to get inherited? b) Public
a) Private c) Protected
b) Protected d) Default
c) Public
d) All three Answer: b
Explanation: The implicit constructor will always be
Answer: a public. Otherwise the class wouldn’t be able to have
Explanation: Private members of a class can’t be instances. In turn, no objects will be created and the
inherited. These members can only be accessible from class can only be used for inheritance.
members of its own class only. It is used to secure the 8. If class A has add() function with protected access,
data. and few other members in public . Then class B
4. Which access specifier is used when no access inherits class A privately. Will the user will not be able
specifier is used with a member of class (java)? to call _________ from object of class B.
a) Private a) Any function of class A
b) Default b) The add() function of class A
c) Protected c) Any member of class A
d) Public d) Private, protected and public members of class A

Answer: b Answer: d
Explanation: Default access is used if the programmer Explanation: Class B object will not be able to call any
doesn’t specify the specifier. This acts in similar way of the private, protected and public members of class
as that of private. But since nothing is specified we call A. It is not only about the function add(), but all the
it default access. members of class A will become private members of
5. Which specifier allows a programmer to make the class B.
private members which can be inherited? 9. Which access specifier should be used in a class w .
a) Private the instances can’t be created?
b) Default a) Private default constructor
c) Protected b) All private constructors
d) Protected and default c) Only default constructor to be public
d) Only default constructor to be protected
Answer: c
Explanation: Protected access is used to make the Answer: b
members private. But those members can be inherited. Explanation: All the constructors must be made
This gives both security and code reuse capability to a private. This will restrict the instance of class to be
program. made anyw . in the program. Since the constructors are
6. Which among the following is false? private, no instance will be able to call them and hence
a) Private members can be accessed using friend won’t be allocated with any memory space.
functions
29
10. On which specifier’s data, does the size of a class’s b) Default or public
object depend? c) Protected or private
a) All the data members are added d) Public
b) Only private members are added
c) Only public members are added Answer: d
d) Only default data members are added Explanation: All the members must be of public
access. So that the members can be inherited easily.
Answer: a Also, the members will be available from outside the
Explanation: All the data members are counted to class.
calculate the size of an object of a class. The data 14. Which access specifier is usually used for data
member access specifier doesn’t play any role .. Hence members of a class?
all the data size will be added. a) Private
11. If class B inherits class A privately. And class B b) Default
has a friend function. Will the friend function be able c) Protected
to access the private member of class A? d) Public
a) Yes, because friend function can access all the
members Answer: a
b) Yes, because friend function is of class B Explanation: All the data members should be made
c) No, because friend function can only access private private to ensure the highest security of data. In special
members of friend class cases we can use public or protected access, but it is
d) No, because friend function can access private advised to keep the data members private always.
member of class A also 15. Which specifier should be used for member
functions of a class?
Answer: c a) Private
Explanation: The friend function of class B will not be b) Default
able to access private members of class A. Since B is c) Protected
inheriting class A privately, the members will become d) Public
private in class B. But private members of class A
won’t be inherited at all. Hence it won’t be accessible. Answer: d
12. If an abstract class has all the private members, Explanation: It is always advised that the member
then _________ functions should be kept public so that those functions
a) No class will be able to implement members of can be used from out of the class. This is usually done
abstract class to ensure that the features provided by the class can be
b) Only single inheritance class can implement its used at its maximum.
members .
c) Only other enclosing classes will be able to .
implement those members
d) No class will be able to access those members but Private Access Specifier -
can implement.
“Private Access Specifier”.
Answer: a 1. If a function has to be called only by using other
Explanation: The classes which inherit the abstract member functions of the class, what should be the
class, won’t be able to implement the members of access specifier used for that function?
abstract class. The private members will not be a) Private
inherited. This will restrict the subclasses to implement b) Protected
those members. c) Public
13. Which access specifier should be used so that all d) Default
the parent class members can be inherited and accessed
from outside the class? Answer: a
a) Private Explanation: The function should be made private. In
30
this way, the function will be available to be called 4. Which among the following is correct to call a
only from the class member functions. Hence the private member from outside the class?
function will be secure from outside world. a) object.memberfunction( parameters );
2. Which among the following is correct for the code b) object->memberfunction( parameters );
given below? c) object->memberfunction( parameteres); or
class student object.memberfunction( parameters );
{ d) Not possible
private: student()
{ Answer: d
} Explanation: The private member function will not be
public : student( int x) accessible from outside the class. Hence any syntax
{ will not work to access the private members. If you
marks =x; have the address of the member, may be you can
} access those members, but that is totally different case
}; and concept.
a) The object can never be created 5. If private members has to be accessed directly from
b) The object can be created without parameters outside the class but the access specifier must not be
c) Only the object with only 1 parameter can be created changed, what should be done?
d) Only the object with some parameters can be created a) Specifier must be changed
b) Friend function should be used
Answer: c c) Other public members should be used
Explanation: For creating object without parameters, d) It is not possible
the default constructor must be defined in public
access. But ., only parameterized constructor is public, Answer: b
hence the objects being created with only one Explanation: For calling the function directly, we can’t
parameter will only be allowed. use another function because that will be indirect call.
3. Which among the following is true for the code Using friend function, we can access the private
given below? members directly.
class A 6. Which access specifier is/are most secure during
{ inheritance?
private : int marks; char name[20]; a) Private
public : b) Default
A(int x=100) c) Protected
{ d) Private and default
marks=x;
} Answer: a
}; Explanation: The private members are most secure in
a) Objects can be created with one parameter or inheritance. The default members can still be in
without parameter inherited in special cases, but the private members
b) Object can be created only with one parameter can’t be accessed in any case.
c) Object can be created with more than one parameter 7. Choose the correct option for the code given below
d) Objects can be create only without parameter class A{ static int c=0; public: A(){ c++; } };
a) Constructor will make c=1 for each object created
Answer: a b) Constructor will make c=0 for each object created
Explanation: The constructor . has a default argument c) Constructor will keep number of objects created
constructor. Hence we can pass one parameter, but that d) Constructor will just initialize c=0 then increment
is optional. If an object is created without parameter, by 1
the default value will be used in the constructor
definition. Answer: c
Explanation: The constructor is using a static member
31
to keep the count of the number of objects created. c) constructor will never be used
This is done because the variable c is static and hence d) disp() function will never be used
the value will be common for all the objects created.
8. Which option is false for the following code? Answer: a
class A Explanation: Variable name will never be used. It is a
{ private member. None other than class members can
private : int sum(int x, int y) access name, also, neither the constructor nor the disp()
{ function are accessing the variable name. Hence it will
return x+y; never be accessible.
} 10. Private member functions can be overloaded.
public: A() a) True
{ b) False
}
A(int x, int y) Answer: a
{ Explanation: The private functions can also be
cout&lt;&lt;sum(x,y); 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
a) Constructor can be created with zero argument accessed from members of class only.
b) Constructor prints sum, if two parameters are passed 11. Which among the following is true?
with object creation a) Private member functions can’t be overloaded
c) Constructor will give error if float values are passed b) Private member functions can be overridden
d) Constructor will take 0 as default value of c) Private member functions can’t be overloaded with a
parameters if not passed public member
d) Private member function can’t be overridden
Answer: d
Explanation: Constructor is not having any default Answer: d
arguments hence no default value will be given to any Explanation: The private member functions can be
parameters. Only integer values must be passed to the overloaded but they can’t be overridden. This is
constructor if we need the sum as output, otherwise if because, overriding means a function with same name
float values are passed, type mismatch will be shown in derived class, gets more priority when called from
as error. object of derived class. ., the member function is
9. Which member will never be used from the private so t . is no way that it can be overridden.
following class? 12. Which data member in following code will be used
class A() whenever an object is created?
{ Class A
int marks; char name[20]; {
public : A() int x; int y; int z;
{ public : A()
marks=100; {
} y=100; x=100*y;
void disp() }
{ };
cout&lt;&lt;”Marks= a) x will be used
”&lt'&lt;marks; b) y will be used
cout&lt;&lt;”Student”; c) z will be used
} d) All will be used
};
a) name variable will never be used Answer: c
b) marks variable will never be used Explanation: Whenever an object will be created, the
32
constructor will be called. Inside constructor we are specifier
using the data members x and y. Hence these two will c) Private specifier must be used first
always be used with each object creation. d) Private specifier can be used anyw . in class
13. Which member can be considered most secure in
the code below? Answer: d
class A() Explanation: The private specifier can be used anyw .
{ in the class as required. It is not a rule to mention the
int a; private members first and then others. It is just
private : int b; followed to write first for better readability.
protected : int c; .
public : int d; .
};
a) a Object Oriented Programming Question Bank -
b) b
c) c This set of Object Oriented Programming Question
d) d Bank focuses on “Protected Access Specifier”.
1. Which among the following best describes the
Answer: b protected specifier?
Explanation: The default variables can be inherited in a) Members are most secure and can’t be used outside
some special cases but the public members can never class
be inherited. Hence the most secure data member in the b) Members are secure but can be used outside the
class is b. class
14. Which among the following is correct for the code c) Members are secure as private, but can be inherited
given below? d) Members are secure like private, but can’t be
class A inherited
{
private : A() Answer: c
{ Explanation: The members which are made protected,
} are most secure if inheritance is not used. But, this
public : A(int x) 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 a; a) It’s instance can be created inside the subclasses
A b(100); b) It’s instance can be created anyw . in the program
a) Program will give compile time error c) It’s instance can be created inside the subclasses and
b) Program will run fine main() function
c) Program will give runtime error d) It’s instance can be created inside the parent class
d) Program will give logical error only

Answer: a Answer: a
Explanation: The program will be giving a compile Explanation: The instances will be allowed to be
time error as the default constructor is private in class. created only inside the sub classes. This is because the
And, the logical errors are usually runtime so we can’t protected members will be inherited and hence the
say that the program will give logical error. The constructor too. This will allow the subclasses to call
program will not run. the constructor whenever an object is created.
15. Which among the following is correct? 3.For the following code, choose the correct option:
a) Private specifier must be used before public class A
specifier {
b) Private specifier must be used before protected int marks;
33
protected : A() }
{ }
marks=100; class B
} {
public : A( int x) A a;
{ A b=100;
marks=x; };
} main()
}; {
a) The instances can be created only in subclasses A a, b=100;
b) The instances can be created only in main() function B c;
c) The instances can be created only in parent class }
d) The instances can be created anyw . in the program a) Program runs fine
b) Program gives runtime error
Answer: d c) Program gives compile time error
Explanation: The instances can be created anyw . in the d) Program gives logical error
program. The only restriction will be on which
constructor will have to be called. The instances with Answer: c
zero arguments will be allowed to be created only Explanation: The objects being created with
inside the subclasses, but the instances with one assignment value are allowed, if the constructor
argument can be created anyw . in the program. accepts only 1 argument. But main() function will not
4. If the protected members are to be made accessible be able to create the object . with assignment, as the
only to the nearest subclass and no further subclasses, constructor which accepts one argument is in protected
which access specifier should be used in inheritance? mode in the class.
a) The sub class should inherit the parent class 6. Which among the following is true for the given
privately code below.
b) The sub class should inherit the parent class as class A
protected {
c) The sub class should inherit the parent class as protected : int marks;
public public :
d) The sub class can use any access modifier A()
{
Answer: a marks=100;
Explanation: The sub class should use private }
inheritance. This will allow only the nearest sub classes disp()
to inherit the protected members and then those {
members will become private. Hence further
inheritance of those members will not be possible. cout&lt;&lt;”marks=”&lt;&lt;marks;
5.What will be the output of following code (all header }
files and required things are included)? };
class A class B: protected A
{ {
int marks; };
protected : A(int x) B b;
{ b.disp();
marks=x; a) Object b can’t access disp() function
} b) Object b can access disp() function inside its body
public : A() c) Object b can’t access members of class A
{ d) Program runs fine
marks=100;
34
Answer: a B b;
Explanation: The object of class B can’t access the a) Instance of B will not be created
members of A outside the class. This is because the b) Instance of B will be created
class is being inherited in protected access, so all the c) Program gives compile time error
members will become protected in subclass B. d) Program gives runtime error
7. Protected members differ from default members
as_______ Answer: a
a) Protected members can be accessed outside package Explanation: Instance of B will not be created. When
using inheritance, but default can’t you try to create instance of B, First the constructor of
b) Default members can be accessed outside package parent class will be called, but the parent class
using inheritance, but protected can’t constructor is private, hence it won’t be able to
c) Protected members are allowed for inheritance but initialize and allocate memory for parent class
Default members are not allowed members. In turn, the object of B will not be created.
d) Both are same 10. If protected inheritance is used then _____
a) Public members become public in subclass
Answer: a b) Protected members become public in subclass
Explanation: The protected members are allowed in the c) Protected members become protected in subclass
same package but can also be accessed in other d) Protected and Public members become protected in
packages using inheritance. But the default members subclass
can never be accessible in other packages.
8. If all the members are defined in protected specifier Answer: d
then ( Constructors not considered ): Explanation: The protected and public members of the
a) Instance of class can’t be created parent class will become the protected members in
b) Instance of class can be created anyw . subclass. This is predefined rule of inheritance. The
c) Instance of class can be created only in subclasses reason behind is to maintain the level of security in
d) Instance of class can be created only in main() subclass too.
function 11. If protected members are to be accessed from
outside the class then__________
Answer: b a) Members must be inherited publicly in subclass
Explanation: The instances can be created anyw . in the b) Members must accessed using class pointers
program. This is because the constructors are not c) Members must be accessed as usual
considered among the members defined in protected d) Members must be made public
mode. Hence the default implicit constructor will be
used whenever an object is created. Answer: d
9. Which among the following is correct for the code Explanation: The members must be made public,
given. otherwise it is not possible. In every case, the protected
class A members will act as private members if it’s about
{ access specifier. It will only be inherited, that too will
private: int marks; lead to make those members protected again, in
A() subclasses.
{ 12. Which among the following can use protected
} access specifier?
Public : disp() a) Members which may be used in other packages
{ b) Members which have to be secure and should be
cout&lt;&lt; marks; used by other packages/subclass
} c) Members which have to be accessed from anyw . in
}; the program
class B: public A d) Members which have to be as secure as private but
{ can be used by main() function
}
35
Answer: b argument is default valued, it is not mandatory to pass
Explanation: The members which have to be secure anything to the new object.
and might get used in other packages or subclasses can .
use protected access. This also allows the members to To practice Object Oriented Programming Question
be safe from accidental modification. Bank, .
13. Protected access is same as default access that is
given implicitly in java if no specifier is mentioned. Public Access Specifier -
a) True
b) False “Public Access Specifier”.
1. Which among the following is true for the code
Answer: b given below:
Explanation: The statement given is true. The clear class A
difference is protected members are available in other {
packages also, but the default members are available int marks;
within the package only. public : disp()
14. If a class have default constructor defined in {
private access, and one parameter constructor in cout&lt;&lt;marks;
protected mode, how will it be possible to create }
instance of object? }
a) Define a constructor in public access with different class B: protected A
signature {
b) Directly create the object in the subclass char name[20];
c) Directly create the object in main() function }
d) Not possible A a; a.disp();
B b; b.disp();
Answer: a a) Only object of class A can access disp() function
Explanation: If a new constructor is defined in public b) Only object of class B can access disp() function
access. That will be available to whole program. Only c) Both instances can access disp() function
restriction will be of the way to use it. d) Accessing disp() outside class is not possible
15. What will be the output of the program given
below: Answer: a
class A Explanation: The object of class A can access the
{ disp() function. This is because the disp() function is
Public : A(int a=0) public in definition of class A. But it can’t be accessed
{ from instance of class B because the disp() function is
cout&lt;&lt;”new A”; protected in class B, since it was inherited as protected.
} 2. If the members have to be accessed from anyw . in
}; program and other packages also, which access
A a; specifier should be used?
A b; a) Public
A c; b) Private
a) new A new A new A c) Protected
b) newAnewAnewA d) Default
c) new Anew Anew A
d) new A new Anew A Answer: a
Explanation: The access specifier must be public so as
Answer: c to access the members outside the class and anyw .
Explanation: The constructor has a default argument. within the program without using inheritance. This is a
Whenever the object is created, the constructor will be rule, predefined for the public members.
called and print the message in its definition. Since the
36
3. Which among the following have least security members will be available in subsequent subclasses
according to the access permissions allowed? from the first subclass.
a) Private 7. How many public class(s) (outermost) can be t . in a
b) Default java program?
c) Protected a) 1
d) Public b) 2
c) 3
Answer: d d) As required
Explanation: The public members are available to
whole program. This makes the members most Answer: a
vulnerable to accidental changes, which may result in Explanation: T . can be only one public class in a java
unwanted modification and hence unstable program. The public class name must match the name
programming. of file. And t . can’t be more than one class with same
4. Which among the following can be used for name in a single program in same scope. Hence it is
outermost class access specifier in java? not possible to have more than one public class in java
a) Private program.
b) Public 8. What is output of following code?
c) Default package pack1;
d) Default or Public class A
{
Answer: d public A()
Explanation: Either default or public access specifier {
must be used for outermost classes. Private can be used System.out.print(“object
with inner classes. This is done so that all the members created”);
can access and use the utmost class and that program }
execution can be done from anyw .. Inner classes can }
be made private for security. package pack2;
5. We can reduce the visibility of inherited methods. import pack1.*;
a) True class B
b) False {
A a=new A();
Answer: b }
Explanation: The statement given is false. This is a) Output is: object created
because when we inherit the members they can either b) Output is: object createdobject created
be made more secure or be at same access. But the c) Compile time error
visibility reduction is not possible, for example, if a d) Run time error
member is protected in parent class, then it can only be
made protected or private in subclass and not public in Answer: c
any case. Explanation: The program will give compile time
6. If members of a super class are public, error. Class A is defined with default access specifier.
then________ This directly means that class A will be available
a) All those will be available in subclasses within package only. Even if the constructor is public,
b) None of those will be available in subclasses the object will not be created.
c) Only data members will be available in subclass 9. Which among the following for public specifier is
d) Only member functions will be available in subclass false?
a) The static members can’t be public
Answer: a b) The public members are available in other packages
Explanation: All the members will be available in too
subclasses. Though it is not guaranteed whether the c) The subclasses can inherit the public members
privately
37
d) T . can be only one public class in java program b) None of its instance creation will be allowed
c) None of its member function can be called outside
Answer: a the class
Explanation: The static members are not property of d) None of its data members will be able to get initial
any object of the class. Instead, those are treated as value
property of class. This allows us to have public static
members too. Answer: c
10. A class has its default constructor defined as Explanation: According to rule of private, protected
public. Class B inherits class A privately. The class: and default access specifiers, none of the members
a) B will not be able to have instances under these specifiers will be able to get invoked
b) Only A can have instances outside the class. We are not sure about the members
c) Only B can have instances of class specifically so other options doesn’t give a
d) Both classes can have instances fixed answer.
14. In multi-level inheritance(all public) , the public
Answer: d members of parent/superclass will ________
Explanation: Class A can have instances as it has a) Will continue to get inherited subsequently
public default constructor. Class will have its own b) Will not be inherited after one subclass inheritance
constructors defined. Hence both classes can have c) Will not be available to be called outside class
instances. d) Will not be able to allocated with any memory space
11. Which specifier can be used to inherit protected
members as protected in subclass but public as public Answer: a
in subclass? Explanation: The public inheritance makes the public
a) Private members of base class, public in derived classes. This
b) Default can be used when the same feature have to be
c) Public redefined with each new class inheriting the base class.
d) Protected 15. Which specifier allows to secure the public
members of base class in inherited classes?
Answer: c a) Private
Explanation: The specifier that can make protected b) Protected
member’s protected in subclass and public member’s c) Public
public in subclass, is public. This is done to maintain d) Private and Protected
the security level of protected members of parent class.
12. Which among the following is true for public Answer: d
class? Explanation: Both the private and protected specifiers
a) T . can be more than one public class in a single can make the public members of base class more
program secure. This is useful if we stop using the parent class
b) Public class members can be used without using members and use the classes which inherited the parent
instance of class class, so as to secure data better.
c) Public class is available only within the package .
d) Public classes can be accessed from any other class .
using instance
Data Members -
Answer: d
Explanation: The public class is a usual class. T . is no “Data Members”.
special rule but the members of the class can be 1. What is the term used to indicate the variable and
accessed from other classes using instance of the class. constants of a class?
This is usually useful to define main() function. a) Data members
13. If a class doesn’t have public members, b) Variables of class
then________ c) Data characters
a) None of its members will be able to get inherited
38
d) Constants memory space will not be allocated for its members.
This leads to undefined use of new data types.
Answer: a 5. How many data members can a class contain?
Explanation: The variables inside a class are termed a) 27
data members of the class. It is not a mandatory rule b) 255
but variables are used to refer usual variables used in c) 1024
functions or globally. The term is given because the d) As many as required
values stored in those variables represent some kind of
data related to class. Answer: d
2. Data members ________________ (C++) Explanation: Any class can have as many data
a) Can be initialized with declaration in classes members as required. The only restriction that may
b) Can be initialized only with help of constructors arise is when t . is not enough memory space. This
c) Can be initialized either in declaration or by gives flexibility to define a class with best properties
constructor possible.
d) Can’t be initialized 6. How to access data members of a class?
a) Dot operator
Answer: b b) Arrow operator
Explanation: The data members are not property of c) Dot or arrow as required
class, those are property of the instances of the class. d) Dot, arrow or direct call
And the memory for the data members are not reserved
until a constructor is called. Hence we use constructors Answer: c
for their initialization after the memory is reserved. Explanation: The data members can never be called
3. Which among the following is true for data directly. Dot operator is used to access the members
members? with help of object of class. Arrow is usually used if
a) Private data members can be initialized with pointers are used.
declaration in class 7. To create a pointer to a private data member of a
b) Static members are initialized in constructors class, outside the class, which among the following is
c) Protected data members can be initialized in class correct?
directly a) Return the address of the private data member using
d) Static data members are defined outside class, not in a member function
constructor b) Access the private member using a pointer outside
class
Answer: d c) Declare the member as pointer inside the class
Explanation: Static members are not property of d) Not possible to create pointer to a private member
instances of classes. Those are shared by all the object
of classes. Hence those are defined outside the Answer: a
constructor, so as to make them common for all the Explanation: We can call a public member function
objects. and return the address of any private data member.
4. What should be done for data member to be of user Though the pointer being returned must be defined
defined structure type? inside class itself. And the returned address can be
a) The structure must have been defined before class. stored in a pointer.
b) The structure must have been defined after the class 8. Which among the following is true for use of setter()
definition and getter() function?
c) The structure must be predefined a) Considered best for manipulating data values
d) The structure type data members can’t be used b) Considered the only proper way to manipulate the
values
Answer: a c) Considered specially for private members
Explanation: The structure must have been defined manipulation
prior to its use. If the structure is not defined, then the d) Considered a red flag, and not recommended for

39
large scale use b) False

Answer: d Answer: b
Explanation: This concept of getter and setter functions Explanation: The data members in a class can never
is not acceptable if used too much. This is considered refer to own class type. This is not possible because the
to be inappropriate in OOP perspective. Though it is data members should have some memory allocated for
commonly used, it doesn’t work according to OOP its object before the self-reference is used, but class
concepts at some higher level of understanding. must call constructor for that. Hence not possible.
9. What is the output of following code? 12. What is the keyword used to make data members
int n=10; // global have same value?
class A() a) static
{ b) const
private : int n; c) double
public : int m; d) abstract
A()
{ Answer: b
n=100; m=50; Explanation: The keyword const can be used anyw . to
} make the variable have same value all the time. This
void disp() restriction is made to use the same value whenever
{ required. Also, this can restrict accidental changes.
cout<<”n”<<m<<n; 13. Which data members can be inherited but are
}; private to a class?
a) 1050100 a) Private
b) 1005010 b) Protected
c) n5010 c) Protected and Static
d) n50100 d) Privately inherited

Answer: d Answer: b
Explanation: In cout we have specified n as a string to Explanation: Static members inheritance also depends
be printed. And m is a variable so its value gets on the type of specifier they have. Only the protected
printed. And global variable will not be used since members can be inherited but remain private to class.
local variable have more preference. If static members are defined in private access, they
10. The static member functions can only use won’t be allowed for inheritance.
________ 14. The arguments passed to member functions by
a) Static data members reference are considered as data members of class.
b) Private data members a) True
c) Protected data members b) False
d) Constant data members
Answer: b
Answer: a Explanation: This is a wrong statement. As only the
Explanation: The static member functions can only data defined inside class is considered as its member.
access static data members. This is because the static But even if a variable is passed by reference it would
member function can’t work with the properties that be the same variable that is outside the class. Hence it
change object to object. It is mandatory that only the can’t be considered class member.
common properties of all the objects be used. And only 15. Which among the following is not allowed for data
static data members are common to all as those are member declaration?
property of class. a) int a;
11. A class can have self-referential data members. b) static int a;
a) True c) abstract a;

40
d) Boolean a; using only the dot operator or the arrow operator. But
the static members can be called using directly the
Answer: c class name followed by the scope resolution operator
Explanation: The abstract keyword in declaration of and static member function name. This is useful when
data members is not allowed. This is because the you don’t have any object to call the member.
abstract keyword features can’t be used with the data 4. What are inline member functions?
members of the class. We can have all other syntax a) Member functions which can be called without
given, but not abstract. object
. b) Member functions whose definition is expanded in
. place of its call
c) Member functions whose definition is faster than
Member Functions - simple function
d) Member function which is defined in single line
“Member Functions”.
1. Which among the following best describes member Answer: b
functions? Explanation: The member functions whose definition
a) Functions which are defined within the class is expanded at the call, and no jump to function and
b) Functions belonging a class return happened, are termed as inline functions. This is
c) Functions in public access of a class used to make the program faster and more efficient.
d) Functions which are private to class 5. What happens if non static members are used in
static member function?
Answer: b a) Compile time error
Explanation: We can’t say that only functions which b) Runtime error
are defined inside class are member functions. T . can c) Executes fine
be some inherited functions. Though they doesn’t d) Executes if that member function is not used
belong to the class but are property of the objects once
inheritance is used. So the nearest definition is Answer: a
functions belonging a class. Explanation: T . must be specific memory space
2. How many types of member functions are generally allocated for the data members before the static
t . in C++? member functions uses them. But the space is not
a) 2 reserved if object is not declared. Hence only if static
b) 3 members are not used, it leads to compile time error.
c) 4 6. Static member functions _____________
d) 5 a) Contains “this” pointer for data members
b) Contains “this” pointer if used for member functions
Answer: d c) Doesn’t contain “this” pointer
Explanation: T . are 5 types of member functions that d) Doesn’t contain “this” pointer if member functions
are generally provided in C++. Namely, simple, static, are referred
const, inline and friend member functions. Member
functions are specific to classes. Answer: c
3. How can a static member function be called in main Explanation: The static member functions doesn’t
function? contain “this” pointer. Static member functions can’t
a) Using dot operator be defined as const or volatile also. These are
b) Using arrow operator restrictions on static member functions.
c) Using dot or arrow operator 7. How to access members of the class inside a
d) Using dot, arrow or using scope resolution operator member function?
with class name a) Using this pointer only
b) Using dot operator
Answer: d c) Using arrow operator
Explanation: The member functions can be called
41
d) Used directly or with this pointer the type of data as soon as the generic class is used. It’s
inbuilt feature.
Answer: d 11. Member function of a class can ____________
Explanation: The members of a class can be used a) Access all the members of the class
directly inside a member function. We can use this b) Access only Public members of the class
pointer when t . is a conflict between data members of c) Access only the private members of the class
class and arguments/local function variable names. d) Access subclass members
8. For overloading “( )”, “[ ]” or “->” operators, a class
__________ Answer: a
a) Must use static member functions Explanation: The member functions has access to all
b) Must use non-static member functions the members of the class. Whenever data members of a
c) Must be non-static member and should not be friend class, which might be private, have to be modified, we
of class make use of these member functions. This is more
d) Must use static member function or a friend member secure way to manipulate data.
function 12. Which among the following is proper syntax for
class given below?
Answer: c class A
Explanation: For overloading those operators for a {
class, the class must use non-static member function so int a,b;
that doesn’t remain common to all the objects, and public : void disp();
each object can use it independently. The friend }
functions is also restricted so as to keep the security of a) void disp::A(){ }
data. b) void A::disp(){ }
9. If a virtual member function is defined, c) void A:disp() { cout<<a<<b ; }
___________ d) void disp:A(){ cout<<a<<b; }
a) It should not contain any body and defined by
subclasses Answer: b
b) It must contain body and overridden by subclasses Explanation: The syntax in option b is correct. We use
c) It must contain body and be overloaded scope resolution to represent the member function of a
d) It must not contain any body and should not be class and to write its definition. It is not necessary for a
derived function to have anything in its definition.
13. A member function can _______________ of the
Answer: a same class
Explanation: The virtual functions are defined using a) Call other member functions
virtual keyword. These are made in order to make all b) Call only private member functions
the classes to define them as the class gets inherited. c) Call only static member functions
Increases code understanding. d) Call only const member functions
10. Member functions of a generic class are
_____________ Answer: a
a) Not generic Explanation: We can call one function inside another
b) Automatically generic function to access some data of class. A public member
c) To be made generic explicitly function can be used to call a private member function
d) Given default type as double which directly manipulates the private data of class.
14. Which member function doesn’t require any return
Answer: b type?
Explanation: When generic type is used in a class, the a) Static
functions are automatically generic. This is so because b) Constructor
the functions would use the same type as defined to c) Const
make the class generic. The functions will get to know d) Constructor and destructor

42
Answer: d 3. Can local class members access/use the general local
Explanation: All the member functions work same as variables (except static, abstract etc.) of the function in
normal functions with syntax. But the constructor and which it is defined?
destructor are also considered as member functions of a) Yes, it can access with arrow operator
a class, and they never have any data type. b) No, it can’t access with dot operator
15. Which among the following is not possible for c) Yes, it can access using dot operator
member function? d) No, it can’t access In anyway
a) Access protected members of parent class
b) Definition without return type Answer: d
c) Access public members of subclass Explanation: The local variables of the functions are
d) Access static members of class not available to the member functions of the class. This
is done to reduce the ambiguity in variables and their
Answer: c access rules.
Explanation: A member function of a class can only 4. Which type of data can a local class access from the
have the access to the members of its own class and function in which it is defined?
parent classes if inheritance used. Otherwise a member a) Static and extern
function can never access the members of a subclass. b) Abstract and static
Accessing static members of a class is possible by c) Void and extern
normal and static member functions. d) Const and static
.
. Answer: a
Explanation: The local classes have this feature to
Local Class - access the static and extern variables of the function in
which those are defined. This feature is available since
“Local Class”. these type of data are common to the program and is
1. What are local classes? created only one time. Run time creation and
a) Classes declared inside a package destruction of these variables is not done. The only
b) Classes declared inside a function restriction that may apply is those members must be
c) Classes declared inside a class constants.
d) Classes declared inside structure 5. Local classes can access the type names and
enumerators defined by the enclosing function.
Answer: b a) True
Explanation: The classes declared inside a package are b) False
available to all the functions and classes, hence can’t
be called local. This is somewhat similar concept that Answer: a
we use to denote variables of a function. The classes Explanation: This is a little tricky part with local
declared inside functions will be local to them. classes. Though the local class can’t access the general
2. All member functions of a local class must be variables of the function but can access the types that
___________ are defined inside the function. This is because the
a) Defined outside class body whole definition of that type would be existing inside
b) Defined outside the function definition the class.
c) Defined inside the class body 6. Can static variables be declared inside a local class?
d) Defined at starting of program a) Yes, with public access specifier
b) Yes, anyw . as required
Answer: c c) No, not possible in private access specifier
Explanation: T . is a restriction on w . the member d) No, not possible anyway
functions of the local class should be define. Those
must be defined inside the class body only. This is to Answer: d
reduce the ambiguity and complexity of program. Explanation: No, the static variables can’t be declared
inside a local class. This is because each time the
43
function is called, all the variables get created again classes that can be defined inside a function. But all
and are destroyed as soon as the function is returned. those classes must follow the rules and restrictions.
This would have been possible id the static variable 11. All the data members of local class must be
was of function. ___________
7. All the member functions of local classes are a) Defined with declaration
__________ by default. b) Defined in constructor
a) Static c) Declared and defined in constructor
b) Inline d) Declared using a member function
c) Abstract
d) Virtual Answer: b
Explanation: The data members follow same rules as
Answer: c of simple classes. Hence the data members must be
Explanation: All the members are defined inside the declared first. Then their definition must be given
class body. And when the member functions are using the constructors.
defined inside the class body, they are made inline by 12. Can two different functions have local class with
default. If the definition is too complex, those are made same name?
normal functions. a) Yes, since local
8. The enclosing function has no special access to the b) No, names must be different
members of local class. (True/False) c) No, scope doesn’t work .
a) True d) No, ambiguity arises
b) False
Answer: a
Answer: a Explanation: The local classes can have same name if
Explanation: This is a rule that the enclosing function they belong to different functions. The classes would
doesn’t have any special access to the members of be local to those specific functions and hence can have
local class. This is done to maintain the security of same name. This is same as that of local variables
class members. And to ad . to the rules of OOP. concept.
9. Which language can use inheritance with local 13. What is the scope of local class?
classes? a) Within the class only
a) Kotlin b) Within the function
b) Java c) Within the program
c) SmallTalk d) One time creation and live till end of program
d) SAP ABAP
Answer: b
Answer: d Explanation: The scope of a local class is limited only
Explanation: Other language might support inheritance within the function definition. The function can use the
with local classes but those doesn’t provide all the class as usual as local variables. The class gets
proper features of inheritance. Language SAP ABAP destroyed as soon as the function is returned.
provides a way to implement inheritance with local 14. Can a function, other than the enclosing function of
classes efficiently. local class, access the class members?
10. How many local classes can be defined inside a a) Yes, using object
single function? b) Yes, using direct call
a) Only 1 c) Yes, using pointer
b) Only 3 d) No, can’t access
c) Only 5
d) As many as required Answer: d
Explanation: The local classes are local to the specific
Answer: d enclosing function. Other functions can’t access the
Explanation: The local classes can be defined as class. Even if the pointers are used, the class must be
required. T . is no restriction on the number of local
44
alive when the pointer is used. But this will not happen c) 4
if the enclosing function is returned. d) 5
15. Which among the following is main advantage of
using local classes? Answer: a
a) Make program more efficient Explanation: The nested classes are divided into two
b) Makes program execution faster main categories. Namely, Static and non-static. The
c) Helps to add extra functionality to a function categories define how the classes can be used inside
d) Helps to add more members to a function another class.
4. Non-static nested classes have access to
Answer: c _____________ from enclosing class.
Explanation: The closest answer is to add more a) Private members
functionalities to a function or to make some specific b) Protected members
functions to be generic. Adding more members to a c) Public members
function can be done directly but to add some special d) All the members
functionality that are encapsulated, can be done using
local classes. Answer: d
. Explanation: The non-static nested class can access all
. the members of the enclosing class. All the data
members and member functions can be accessed from
Nested Class - the nested class. Even if the members are private, they
can be accessed.
“Nested Class”. 5. Static nested classes doesn’t have access to
1. Which among the following best describes a nested _________________ from enclosing class.
class? a) Private members
a) Class inside a class b) Protected members
b) Class inside a function c) Public members
c) Class inside a package d) Any other members
d) Class inside a structure
Answer: d
Answer: a Explanation: The static nested class doesn’t have
Explanation: If a class is defined inside another class, access to any other members of the enclosing class.
the inner class is termed as nested class. The inner This is a rule that is made to ensure that only the data
class is local to the enclosing class. Scope matters a lot which can be common to all the object is being
.. accessed by the static nested class.
2. Which feature of OOP reduces the use of nested 6. The nested class can be declared
classes? ___________________
a) Encapsulation a) Public
b) Inheritance b) Private
c) Binding c) Protected
d) Abstraction d) Public, Protected, Private or Package private

Answer: b Answer: d
Explanation: Using inheritance we can have the Explanation: The nested class can be declared with any
security of the class being inherited. The subclass can specifier, unlike the outer classes which can only be
access the members of parent class. And have more declared public or package private. This is a flexibility
feature than a nested class being used. given for the nested class being a member of enclosing
3. How many categories are nested classes divided class.
into? 7. Use of nested class ____________ encapsulation.
a) 2 a) Increases
b) 3 b) Decreases
45
c) Doesn’t affect InnerClass();
d) Slightly decreases d) OuterClass.InnerClass = outerObject.new
InnerClass();
Answer: a
Explanation: The use of nested class increases Answer: a
encapsulation as the inner class is getting even more Explanation: An instance of inner class can exist only
grouped into the enclosing class. Firstly the class within instance of outer class. To instantiate the inner
encapsulate the data, having nested classes can class, one must instantiate the outer class first. This can
increase the encapsulation even further. be done by the correct syntax above.
8. Which among the following is correct 12. What will be the output of the following code?
advantage/disadvantage of nested classes? public class Test
a) Makes the code more complex {
b) Makes the code unreadable public int a=0;
c) Makes the code efficient and readable class innerClass
d) Makes the code multithreaded {
public int a=1;
Answer: c void innermethod(int x)
Explanation: The use of nested classes make the code {
more streamed towards a single concept. This allows to
group the most similar and related classes together and System.out.println(“value of x = ” + x);
makes it even more efficient and readable.
9. How to access static nested classes? System.out.println(“value of this.x = ” +
a) OuterClass.StaticNestedClass this.x);
b) OuterClass->StaticNestedClass
c) OuterClass(StaticNestedClass) System.out.println(“value of Test.this.x = ”
d) OuterClass[StaticNestedClass]. + Test.T=this.x);
}
Answer: a }
Explanation: Like any other member of the class, the }
static nested class uses the dot operator to be accessed. public static void main( String args[] )
The reason behind is, the static classes can’t work with {
instances, hence we use enclosing class name to access Test t=new Test();
static nested class. Test.innerClass im=t.new innerClass();
10. A nested class can have its own static members. im.innermethod(55);
a) True }
b) False a) value of x = 55
value of this.x = 0
Answer: b value of Test.this.x = 1
Explanation: The nested classes are associated with the b) value of x = 1
object of the enclosing class. Hence have direct access value of this.x = 0
to the members of that object. Hence the inner class value of Test.this.x = 55
can’t have any static members of its own. Otherwise c) value of x = 55
the rule of static members would be violated using value of this.x = 1
enclosing class instance. value of Test.this.x = 0
11. How to create object of the inner class? d) value of x = 0
a) OuterClass.InnerClass innerObject = value of this.x = 55
outerObject.new InnerClass(); value of Test.this.x = 1
b) OuterClass.InnerClass innerObject = new
InnerClass(); Answer: c
c) InnerClass innerObject = outerObject.new Explanation: The variable x denotes the parameter of
46
the function. And this.x is the variable of the inner 1. In how many ways can an object be passed to a
class. Test.this.x is the variable of the outer class. function?
Hence we get this output. a) 1
13. Instance of inner class can exist only b) 2
_______________ enclosing class. c) 3
a) Within d) 4
b) Outside
c) Private to Answer: c
d) Public to Explanation: The objects can be passed in three ways.
Pass by value, pass by reference and pass by address.
Answer: a These are the general ways to pass the objects to a
Explanation: The class defined inside another class is function.
local to the enclosing class. This means that the 2. If an object is passed by value,
instance of inner class will not be valid outside the ____________________
enclosing class. T . is no restriction for instance to be a) A new copy of object is created implicitly
private or public always. b) The object itself is used
14. If a declaration of a member in inner class has the c) Address of the object is passed
same name as that in the outer class, then d) A new object is created with new random values
________________ enclosing scope.
a) Outer declaration shadows inner declaration in Answer: a
b) Inner declaration shadows outer declaration in Explanation: When an object is passed by value, a new
c) Declaration gives compile time error object is created implicitly. This new object uses the
d) Declaration gives runtime error implicit values assignment, same as that of the object
being passed.
Answer: b 3. Pass by address passes the address of object
Explanation: The inner class will have more preference _________ and pass by reference passes the address of
for its local members than those of the enclosing the object _________
members. Hence it will shadow the enclosing class a) Explicitly, explicitly
members. This process is known as shadowing. b) Implicitly, implicitly
15. A static nested class is _____________ class in c) Explicitly, Implicitly
behavior that is nested in another _________ class. d) Implicitly, explicitly
a) Top level, top level
b) Top level, low level Answer: c
c) Low level, top level Explanation: Pass by address uses the explicit address
d) Low level, low level passing to the function w .as pass by reference
implicitly passes the address of the object.
Answer: a 4. If an object is passed by reference, the changes made
Explanation: Top level class encloses the other classes in the function ___________
or have same preference as that of other top level a) Are reflected to the main object of caller function
classes. Having a class inside the top level class is too
indirectly having a top level class which higher degree b) Are reflected only in local scope of the called
of encapsulation. function
. c) Are reflected to the copy of the object that is made
. during pass
d) Are reflected to caller function object and called
Object Oriented Programming Interview . - function object also

This set of Object Oriented Programming Interview . Answer: a


focuses on “Passing and Returning Object with Explanation: When an object is passed by reference, its
Functions”.
47
address is passed implicitly. This will make changes to constructor
the main function whenever any modification is done.
5. Constructor function is not called when an object is Answer: d
passed to a function, will its destructor be called when Explanation: The problem can be solved by
its copy is destroyed? overloading the assignment operator to get the values
a) Yes, depending on code that might be getting returned while the destructor free
b) Yes, must be called the dynamic memory. Defining copy constructor can
c) No, since no constructor was called help us to do this in even simpler way.
d) No, since same object gets used 9. How many objects can be returned at once?
a) Only 1
Answer: b b) Only 2
Explanation: Even though the constructor is not called c) Only 16
when the object is passed to a function, the copy of the d) As many as required
object is still created, w . the values of the members are
same. When the object have to be destroyed, the Answer: a
destructor is called to free the memory and resources Explanation: Like any other value, only one object can
that the object might have reserved. be returned at ones. The only possible way to return
6. When an object is returned by a function, a more than one object is to return address of an object
_______________ is automatically created to hold the array. But that again comes under returning object
return value. pointer.
a) Temporary object 10. What will be the output of the following code?
b) Virtual object Class A
c) New object {
d) Data member int i;
public : A(int n)
Answer: a {
Explanation: The temporary object is created. It holds i=n; cout&lt;&lt;”inside
the return value. The values gets assigned as required, constructor ”;
and the temporary object gets destroyed. }
7. Is the destruction of temporary object safe (while ~A()
returning object)? {
a) Yes, the resources get free to use cout&lt;&lt;”destroying
b) Yes, other objects can use the memory space ”&lt;&lt;i;
c) No, unexpected side effects may occur }
d) No, always gives rise to exceptions void seti(int n)
{
Answer: c i=n;
Explanation: The destruction of temporary variable }
may give rise to unexpected logical errors. Consider int geti()
the destructor which may free the dynamically {
allocated memory. But this may abort the program if return I;
another is still trying to copy the values from that }
dynamic memory. };
8. How to overcome the problem arising due to void t(A ob)
destruction of temporary object? {
a) Overloading insertion operator cout&lt;&lt;”something ”;
b) Overriding functions can be used }
c) Overloading parenthesis or returning object int main()
d) Overloading assignment operator and defining copy {
A a(1);
48
t(a); d) No, temporary object is created
cout&lt;&lt;”this is i in main ”;
cout&lt;&lt;a.geti(); Answer: c
} Explanation: A copy of all the values is created. If the
a) inside constructor something destroying 2this is i in constructor is called, t . will be a compile time error or
main destroying 1 memory shortage. This happens because each time a
b) inside constructor something this is i in main constructor is called, it try to call itself again and that
destroying 1 goes infinite times.
c) inside constructor something destroying 2this is i in 14. Is it possible that an object of is passed to a
main function, and the function also have an object of same
d) something destroying 2this is i in main destroying 1 name?
a) No, Duplicate declaration is not allowed
Answer: a b) No, 2 objects will be created
Explanation: Although the object constructor is called c) Yes, Scopes are different
only ones, the destructor will be called twice, because d) Yes, life span is different
of destroying the copy of the object that is temporarily
created. This is the concept of how the object should be Answer: a
passed and manipulated. Explanation: T . can’t be more than one variable or
11. It is necessary to return the object if it was passed object with the same name in same scope. The scope is
by reference to a function. same, since the object is passed, it becomes local to
a) Yes, since the object must be same in caller function function and hence function can’t have one more
b) Yes, since the caller function needs to reflect the object of same name.
changes 15. Passing an object using copy constructor and pass
c) No, the changes are made automatically by value are same.
d) No, the changes are made explicitly a) True
b) False
Answer: c
Explanation: Having the address being passed to the Answer: b
function, the changes are automatically made to the Explanation: The copy constructor is used to copy the
main function. In all the cases if the address is being values from one object to other. Pass by values is not
used, the same memory location will be updated with same as copy constructor method. Actually the pass by
new values. value method uses a copy constructor to copy the
12. How many objects can be passed to a function values in a local object.
simultaneously? .
a) Only 1 To practice all areas of Object Oriented Programming
b) Only an array for Interviews, .
c) Only 1 or an array
d) As many as required Object Reference . -

Answer: d “Object Reference”.


Explanation: T . is no limit to how many objects can be 1. What is reference to an object?
passed. This works in same way as that any other a) It is address of an object
variable gets passed. Array and object can be passed at b) It is address of w . the variables and methods of
same time also. object are stored
13. If an object is passed by address, will be c) It is pointer having address of an object
constructor be called? d) It is address of only variables and not the methods of
a) Yes, to allocate the memory an object
b) Yes, to initialize the members
c) No, values are copied Answer: b
Explanation: Reference indicates the address w . the
49
object’s variables and methods are stored. It is not }
actual address of the object. This is done to directly use a) Point 1: 100, 100
the variables and methods whenever required. Point 2: 200,200
2. Whenever an object is assigned to a variable or b) Point 1: 200, 200
passed to a method, ________________ Point 2: 100,100
a) Actually the objects aren’t used c) Point 1: 100, 100
b) Actually only the objects are used Point 2: 100,100
c) Actually a pointer to an object is used d) Point 1: 200, 200
d) Actually copy of object is used Point 2: 200,200

Answer: a Answer: d
Explanation: Whenever an object is assigned to a Explanation: The expected output would be like p2
variable or passed to a method, we aren’t actually with values 100, 100. But this is not the case. The
using objects t .. Actually the reference to the objects is tricky part is assignment used ( p2=p1; ). . a reference
used. The reference makes a lot of difference as the is created from object p1 to p2, and not any new object
main object may or may not get affected depending on that would copy p1’s values. Hence when we change
the code. the values of p1 object members. T . changes are
3. Does use of object reference in assignment or reflected to the object p2 also.
passing means copy of the object is being used? 5. Is t . any explicit use of pointers in java that would
a) No, because the copy would create a new temporary be applicable to objects?
variable a) Yes, we use reference for this purpose
b) No, because the copy would not help to make b) Yes, we use java arrays for this purpose
changes to main object c) No, implicit pointing is possible
c) Yes, because the reference directly means using d) No, direct class names should be used
address
d) Yes, because the reference directly means the Answer: c
constructors are involved Explanation: The question clearly asks if t . is any
explicit use of pointers related to objects. Pointers are
Answer: c not applicable in java first of all. Secondly, the
Explanation: We can’t say that the reference involves pointing in java is achieved implicitly using the
constructors in passing the objects to some method. references and object arrays. Now first two options are
The reference is used to denote the addresses and wrong because they use implicit pointing, and not any
hence to point to the main object itself. T . is no copy explicit pointing.
involved. 6. Can a super class object give reference to a subclass
4. What will be the output of the following code? method?
import java.awt.Point; a) No, it is not possible
class Testing b) Maybe, it is possible
{ c) No, it’s not possible
public static void main(String[] args) d) No, It’s not possible in few cases only
{
Point p1,p2; Answer: c
p1=new Point(100,100); Explanation: The object of a super class can never refer
p2=p1; to methods of a subclass. W .as vice versa is possible.
p1.x=200; If t . is an overridden function in subclass, then the
p1.y=200; object of super class will execute the method of itself
System.out.println(“Point 1: ” + and not from the subclass.
p1.x + ”, “ + p1.y); 7. What will be the output of the following code?
System.out.println(“Point 2: ” + import java.awt.Point;
p2.x + ”, “ + p2.y); class Testing
} {
50
public static void main(String[] args) denoted with this. Or specifically the members of class
{ only.
Point t1,t2,t3; 10. How to refer to method of nested class?
t1=new Point(100,100); a) enclosingClassObject.innerClassObject.method();
t2=t1; b) innerClassObject.method();
t3=t1; c) method();
t1.x=200; d) depends on w . the method is being called
t1.y=200;
t2.x=300; Answer: d
t3.y=500; Explanation: This depends on w . the method is being
System.out.println(“Point 1: ” + called. If the method is called inside the enclosing class
p1.x + ”, “ + p1.y); itself. Then we can’t use object of enclosing class. If
} the method is being called within the inner class itself,
} then its object will also be of no use.
a) Point 1: 200, 200 11. How many objects can be referenced from same
b) Point 1: 100,100 variables?
c) Point 1: 300, 300 a) One at a time
d) Point 1: 300, 500 b) Many at a time
c) Many using array name
Answer: d d) 7 at max at same time
Explanation: When references are used, all the
variables point to the same object. Whenever any of Answer: a
the variable changes any values, it will be reflected to Explanation: T . should not be any confusion in how
all the variables pointing to the same object. many references can be made from a single variable. A
8. If a reference variable is declared final then, single variable can only point to one object at a time.
_________________ Even if it’s an array, the name of array is used and is
a) It can never be reassigned to refer to a different considered one object name only (representing first
object. array element).
b) It can be assigned to refer to any object anytime 12. Java handles memory dynamically and references
c) It can never be assigned with any object are deleted as soon as they are out of scope.
d) It can be assigned with 2 or more objects a) True
simultaneously b) False

Answer: a Answer: a
Explanation: Since the variable is declared final. It will Explanation: In Java, it is inbuilt feature that it handles
have a constant value throughout the program. It can all the memory dynamically. It is not necessary to free
refer to only one object at a time. And if it was made to or destroy all the references made from a function
refer to none of the object, it would have got no use. which is going out of scope. You can call destroy or
9. Which of the members are referred by this pointer free methods explicitly but t . is no mandatory rule.
usually (Java)? 13. Which among the following is true?
a) Members of class w . this is used a) Object referencing refers to methods address
b) Member of the parent class w . this is used b) Object referencing refers to variable of object
c) Members that are passed as argument to the object c) Object referencing points to same address, if
d) Pointers are not applicable in java assigned by variables
d) Object referencing is used to point methods
Answer: a
Explanation: We use this pointer to differentiate the Answer: c
members of the class w . this is used to the other Explanation: The object referencing will point to the
inherited or passed variables. The local variables are same address if variables are assigned. All the
variables might have a different name but they will
51
point to the same memory location. This is most basic members. This is known as memory allocation for
concept of references. object.
14. Invoking a method on a particular object is 2. W . is the memory allocated for the objects?
____________ sending a message to that object. a) HDD
a) Different from b) Cache
b) Same as c) RAM
c) Somewhat similar d) ROM
d) Part of
Answer: c
Answer: b Explanation: The memory for the objects or any other
Explanation: The methods invoked on a particular data is allocated in RAM initially. This is while we run
object is same as sending a message with same values a program all the memory allocation takes place in
to that object. Message would contain values in a some RAM segments. Arrays in heap and local
particular format which can be used by the object. And members in stack etc.
calling a method would be just another way to do the 3. When is the memory allocated for an object?
same task. a) At declaration of object
15. Can reference to an object be returned from a b) At compile time
method? c) When object constructor is called
a) Yes, always possible d) When object is initialized to another object
b) Yes, but not always
c) No, never possible Answer: c
d) No, Not possible because referred element would be Explanation: The object memory allocation takes place
destroyed when the object constructor is called. Declaration of an
object doesn’t mean that memory is allocated for its
Answer: b members. If object is initialized with another object, it
Explanation: This is possible but not always, since the may just get a reference to the previously created
reference being returned may get destroyed with the object.
return of method. This is an undesirable condition, 4. Using new is type safe as
hence it is not always possible to return references. But _______________________
it is always possible if the referred element is not local a) It require to be specified with type of data
to the method. 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

Object Oriented Programming Test - Answer: b


Explanation: The new is type safe because we don’t
This set of Object Oriented Programming test focuses have to specify the type of data that have to be
on “Memory Allocation of Object”. allocated with memory. We can directly use it with
1. What does memory allocation for objects mean? data name. Name of the data doesn’t matter though for
a) Actual creation and memory allocation for object type of memory allocation though.
members 5. Which of the following function can be used for
b) Creation of member functions dynamic memory allocation of objects?
c) Creation of data members for a class a) malloc()
d) Actual creation and data declaration for object b) calloc()
members c) create()
d) malloc() and calloc()
Answer: a
Explanation: The memory allocated for the object Answer: d
members indicates actual creation of the object Explanation: The malloc() function can be used to
allocate dynamic memory for objects. Function calloc()
52
can also be use. These functions differ in the way they a) delete
allocate memory for objects. b) free
6. How much memory will be allocated for an object of c) either delete or free
class given below? d) only delete
class Test
{ Answer: c
int mark1; Explanation: The memory allocated for an object is
int mark2; usually automatically made free. But if explicitly
float avg; memory has to be made free then we can use either
char name[10]; free or delete keywords depending on programming
}; languages.
a) 22 Bytes 10. Which function is called whenever an object goes
b) 24 Bytes out of scope?
c) 20 Bytes a) Destructor function
d) 18 Bytes b) Constructor function
c) Delete function
Answer: a d) Free function
Explanation: The size of an object of the class given in
question will be of size 22 bytes. This is because the Answer: a
size of an object is always equal to the sum of sizes of Explanation: The destructor function of the class is
the data members of the class, except static members. called whenever an object goes out of scope. This is
7. Which keyword among the following can be used to because the destructor set all the resources, acquired by
declare an array of objects in java? the object, free. This is an implicit work of compiler.
a) new 11. Which operator can be used to check the size of an
b) create object?
c) allocate a) sizeof(objectName)
d) arr b) size(objectName)
c) sizeofobject(objectName)
Answer: a d) sizedobject(objectName)
Explanation: The keyword new can be used to declare
an array of objects in java. The syntax must be Answer: a
specified with an object pointer which is assigned with Explanation: The sizeof operator is used to get the size
a memory space containing the required number of of an already created object. This operator must
object space. Even initialization can be done directly. constail keyword sizeof(objectName). The output will
8. When is the memory allocated for an object gets give the number of bytes acquired by a single object of
free? some class.
a) At termination of program 12. The memory allocated for an object
b) When object goes out of scope ____________________
c) When main function ends a) Can be only dynamic
d) When system restarts b) Can be only static
c) Can be static or dynamic
Answer: b d) Can’t be done using dynamic functions
Explanation: Whenever an object goes out of scope,
the deletion of allocation memory takes place. Actually Answer: c
the data is not deleted, instead the memory space is Explanation: The memory allocation for an object can
flagged to be free for further use. Hence whenever an be static or dynamic. The static memory allocation is
object goes out of scope the object members become when an object is declared directly without using any
useless and hence memory is set free. function usually. And dynamic allocation is when we
9. Which among the following keyword can be used to use some dynamic allocation function to allocate
free the allocated memory for an object? memory for data member of an object.
53
13. If an object is declared in a user defined function name
__________________ b) An array of instances of class represented by more
a) Its memory is allocated in stack than one name
b) Its memory is allocated in heap c) An array of instances which have more than 2
c) Its memory is allocated in HDD instances
d) Its memory is allocated in cache d) An array of instances which have different types

Answer: a Answer: a
Explanation: The memory for any data or object that Explanation: The array of objects an array of instances
are used in a user defined function are always allocated of a class. The array is represented by a single name.
in the stack. This is to ensure that the object is The array name is itself a pointer. Array name
destroyed as soon as the function is returned. Also this represents the first object.
ensures that the correct memory allocation and 2. Which among the following is a mandatory
destruction is performed. condition for array of objects?
14. In java, ____________________ takes care of a) All the objects should be of different class
managing memory for objects dynamically. b) All the objects should be of same program classes
a) Free collector c) All the objects should be of same class
b) Dust collector d) All the objects should have different data
c) Memory manager
d) Garbage collector Answer: c
Explanation: The objects of an array must be of same
Answer: d class. This is mandatory because array is set of same
Explanation: The garbage collector in java takes care type of elements. The objects of same class are
of the memory allocations and their deletions considered to be of same type.
dynamically. When an object is no more required then 3. What is the type of the elements of array of objects?
the garbage collector deletes the object and free up all a) Class
the resources that were held by that object. b) Void
15. Which operator can be used to free the memory c) String
allocated for an object in C++? d) Null
a) Free()
b) delete Answer: a
c) Unallocate Explanation: The class itself is the type of elements of
d) Collect array of objects. All the objects possess the same
properties. Like any other primitive data type, the
Answer: b objects are of their respective class type.
Explanation: The delete operator in C++ can be used to 4. If array of objects is declared as given below, which
free the memory and resources held by an object. The is the limitation on objects?
function can be called explicitly whenever required. In Class_name arrayName[size];
C++ memory management must be done by the a) The objects will have same values
programmer. T . is no automatic memory management b) The objects will not be initialized individually
in C++. c) The objects can never be initialized
. d) The objects will have same data
To practice all areas of Object Oriented Programming
for tests, . Answer: b
Explanation: If the syntax given, is used to declare
Object Array . - array of objects, then the objects can’t be initialized
individually. All the objects will have to be initialized
“Object Array”. after this declaration.
1. What is array of objects? 5. Which is the condition that must be followed if the
a) An array of instances of class represented by single array of objects is declared without initialization, only
54
with size of array? 8. How the objects of array can be denoted?
a) The class should have separate constructor for each a) Indices
object b) Name
b) The class must have no constructors c) Random numbers
c) The class should not have any member function d) Alphabets
d) The class must have a default or zero argument
constructor Answer: a
Explanation: Different objects in an array can be
Answer: d denoted with the indices of array. The first object is
Explanation: The class must have a default/zero denoted by 0. And the further indices denote the next
argument constructor. Since the declaration is done by objects in sequence of array.
only specifying the size of array, the class must have 9. The objects in an object array
default a construct to be called by default to reserve _______________________
memory for each object. Also, we can’t specify the a) Can be created without use of constructor
arguments in this type of declaration hence the class b) Can be created without calling default constructor
should provide a default initialization. c) Can’t be created with use of constructor
6. When are the array of objects without any d) Can’t be created without calling default constructor
initialization useful?
a) When object data is not required just after the Answer: b
declaration Explanation: The objects need some constructor to get
b) When initialization of object data is to be made by the memory spaced reserved for those. If the default
the compiler constructor is not used then we can use some
c) When object data doesn’t matter in the program arguments constructor which will reserve the memory
d) When the object should contain garbage data for the objects. The objects can be passed with
constructor arguments during declaration.
Answer: a 10. The Object array is created in
Explanation: Sometimes the object data is not _____________________
mandatory to be used just after the declaration or may a) Heap memory
be the program requires the data to be updated b) Stack memory
according to what user inputs. Hence only declaration c) HDD
us also useful. d) ROM
7. If constructor arguments are passed to objects of
array then ____________ if the constructors are Answer: a
overloaded. Explanation: If the object arrays are declared
a) It is mandatory to pass same number of arguments to dynamically, then the memory will be reserved on
all the objects heap. The memory for objects will be on stack only if
b) It is mandatory to pass same type of arguments to all some constructor or some call and return tasks are
the objects happening. The program doesn’t run on HDD and
c) It is not mandatory to call same constructor for all ROM is not used for execution of programs.
the objects 11. If an array of objects is of size 10 and a data value
d) It is mandatory to call same constructor for all the have to be retrieved from 5th object then
constructors ________________ syntax should be used.
a) Array_Name[4].data_variable_name;
Answer: c b) Data_Type Array_Name[4].data_variable_name;
Explanation: It is not mandatory to call the same c) Array_Name[4].data_variable_name.value;
constructor for all the objects in an array if initialized d) Array_Name[4].data_variable_name(value);
with the declaration. The objects can be passed with
different set of arguments in the same syntax, separated Answer: a
by commas. Explanation: The array name with the index of fifth
element is called, i.e. index 4. Then the dot operator is
55
used to access the data member of that object. This This set of Object Oriented Programming MCQs
Allows us to access the data members of all the objects focuses on “Object Use”.
in an object array. 1. Which among the following is main use of object?
12. Can we have two dimensional object array? a) To create instance of a function
a) Yes, always b) To create instance of a program
b) Yes, only if primitive type array c) To create instance of class
c) No, since two indices are impossible d) To create instance of structures
d) No, never
Answer: c
Answer: a Explanation: The objects are used to create instance of
Explanation: A two dimensional array can always be a class. Objects can represent a class in an independent
created. T . is no rule that only primitive type objects form. The basic blueprint, that contains the information
can have more than one dimension. The object array of the type of data that can be stored in an object, is
can also be made 2 dimensional. given by the class.
13. From which index does the array of objects start? 2. Which among the following is not a property of an
a) 0 object?
b) 1 a) Identity
c) 2 b) Properties
d) 3 c) Attributes
d) Names
Answer: a
Explanation: The index must start from 0. The index Answer: d
ends at size – 1 index. This is because the index is Explanation: The names are not property of an object.
always till n-1 w . n is the total number of beads. The identity can be in any form like address or name of
14. Two dimensional array can’t be initialized with the object but name can’t be termed as only identity of an
declaration. object. The objects contain attributes that define what
a) True type of data an object can store.
b) False 3. What is function object?
a) An object with a single function
Answer: b b) An object with only functions
Explanation: The two dimensional arrays can also be c) An object with more than one function
initialized using curly brackets. For each set, values in d) An object with no functions
curly bracket. And then another bracket is added at
first and end. This ensure that all the code belongs to Answer: a
the user. Explanation: A function object is the object with single
15. Is an array of characters always a string? function. In C++ a function object can be like
a) Yes, always operator() function. This acts more like a function
b) Yes, if each character is terminated by null rather than an object.
c) No, since each character is terminated by null 4. Immutable object are used
d) No, never ______________________
a) To set up as a fixed state
Answer: d b) To set up variable object
Explanation: The character arrays are not same as c) To set up an object of abstract class
string. The string once created then remains the same. d) To set up an object of derived class
The character array values may change.
. Answer: a
. Explanation: An immutable object can be created for
an object which have to be fixed with values. The
Object Oriented Programming MCQs - object data will not be changed throughout the

56
program. This can be useful to eliminate the directly with the >> and << operators respectively. The
unintentional changes in the data of object. objects are of iostream class. Class iostream is an
5. Which object can be used to contain other objects? inbuilt class.
a) First class object 9. Objects type ____________________
b) Derived class object a) Can be changed in runtime
c) Container object b) Can’t be changed in runtime
d) Enclosure object c) Can be changed in compile time
d) May or may not get changed
Answer: c
Explanation: A container object can be used to contain Answer: b
other objects. Container object is an ADT. Its object Explanation: The object types are always fixed. Once
are collection of other objects. Some specific rules the object is created of a specific type then it can’t be
apply to these type of objects. changed. Neither at runtime nor at compile time.
6. A factory object is used ______________________ 10. An object can be used to represent
a) To create new classes _________________
b) To create new function a) A real world entity
c) To create new data members b) A real function
d) To create new objects c) Some real data only
d) Some function only
Answer:d
Explanation: The factory object is an object that can be Answer: a
used to create other objects. If it is seen formally, it Explanation: The objects are actually meant to
behaves like a method that will return object on its use. represent an entity. The classes are real world object’s
The object returned is assumed to be a new object. blueprint. The classes then are used to create an entity
7. What are singleton objects? representation.
a) The only two objects of a class throughout the 11. Objects can be used _____________________
program a) To access any member of a class
b) The only object of a class throughout the program b) To access only public members of a class
c) The objects that are alive throughout the program c) To access only protected members of a class
d) The objects that are created and then deleted without d) To access only private members of a class
use
Answer: b
Answer: b Explanation: The objects are created for a specific
Explanation: If a class has only one object created and class. Then the objects can be used to access the public
that is the only object of the class. Then the object is members of a class. The members can be the data
known as singleton object. But only if that object is the members or the member functions of the class.
only object of the class and no other object is created 12. Which among the following is not a use of object?
for that class. a) Defining a member function
8. Object cout and cin _________________ b) Accessing data members
a) Can be used directly with << and >> symbols c) Creating instance of a class
respectively d) Using class members
b) Can be used directly with >> and << symbols
respectively Answer: a
c) Must be used as a function which accepts 2 Explanation: The objects can’t be used to define any
arguments member function. Member functions must be defined
d) Must be used as a function which accepts 3 by the class only. Objects can only access the members
arguments and use them.
13. Which object can be used to access the standard
Answer: a input?
Explanation: The cin and cout objects can be used a) System.inner
57
b) cin of the type of data that can be stored in an object, is
c) System.stdin given by the class.
d) console.input 2. Which among the following is not a property of an
object?
Answer: b a) Identity
Explanation: Object cin can be used to take input from b) Properties
the standard input. It is used in C++. In java we can use c) Attributes
System.in for the standard input stream. The syntax d) Names
changes from language to language.
14. A single object can be used __________________ Answer: d
a) As only two class types at a time Explanation: The names are not property of an object.
b) As only three class types at a time The identity can be in any form like address or name of
c) As only one class type at a time object but name can’t be termed as only identity of an
d) As of as many class types as required object. The objects contain attributes that define what
type of data an object can store.
Answer: c 3. What is function object?
Explanation: The object can be of only one type. The a) An object with a single function
type of an object can’t be changed. Object type is b) An object with only functions
mandatory to be of one class type to ensure the type c) An object with more than one function
and number of data members it have. d) An object with no functions
15. If same object name is given to different objects of
different class then _____________ Answer: a
a) Its compile time error Explanation: A function object is the object with single
b) Its runtime error function. In C++ a function object can be like
c) It’s not an error operator() function. This acts more like a function
d) Program suns smooth rather than an object.
4. Immutable object are used
Answer: a ______________________
Explanation: It is a compile time error as the compiler a) To set up as a fixed state
doesn’t allow same name objects to be declared more b) To set up variable object
than once. Compiler produces multiple declaration c) To set up an object of abstract class
error. Every object must have a different name. d) To set up an object of derived class
.
To practice MCQs on all areas of Object Oriented Answer: a
Programming, . Explanation: An immutable object can be created for
an object which have to be fixed with values. The
Object Oriented Programming MCQs - object data will not be changed throughout the
program. This can be useful to eliminate the
This set of Object Oriented Programming MCQs unintentional changes in the data of object.
focuses on “Object Use”. 5. Which object can be used to contain other objects?
1. Which among the following is main use of object? a) First class object
a) To create instance of a function b) Derived class object
b) To create instance of a program c) Container object
c) To create instance of class d) Enclosure object
d) To create instance of structures
Answer: c
Answer: c Explanation: A container object can be used to contain
Explanation: The objects are used to create instance of other objects. Container object is an ADT. Its object
a class. Objects can represent a class in an independent are collection of other objects. Some specific rules
form. The basic blueprint, that contains the information apply to these type of objects.
58
6. A factory object is used ______________________ 10. An object can be used to represent
a) To create new classes _________________
b) To create new function a) A real world entity
c) To create new data members b) A real function
d) To create new objects c) Some real data only
d) Some function only
Answer:d
Explanation: The factory object is an object that can be Answer: a
used to create other objects. If it is seen formally, it Explanation: The objects are actually meant to
behaves like a method that will return object on its use. represent an entity. The classes are real world object’s
The object returned is assumed to be a new object. blueprint. The classes then are used to create an entity
7. What are singleton objects? representation.
a) The only two objects of a class throughout the 11. Objects can be used _____________________
program a) To access any member of a class
b) The only object of a class throughout the program b) To access only public members of a class
c) The objects that are alive throughout the program c) To access only protected members of a class
d) The objects that are created and then deleted without d) To access only private members of a class
use
Answer: b
Answer: b Explanation: The objects are created for a specific
Explanation: If a class has only one object created and class. Then the objects can be used to access the public
that is the only object of the class. Then the object is members of a class. The members can be the data
known as singleton object. But only if that object is the members or the member functions of the class.
only object of the class and no other object is created 12. Which among the following is not a use of object?
for that class. a) Defining a member function
8. Object cout and cin _________________ b) Accessing data members
a) Can be used directly with << and >> symbols c) Creating instance of a class
respectively d) Using class members
b) Can be used directly with >> and << symbols
respectively Answer: a
c) Must be used as a function which accepts 2 Explanation: The objects can’t be used to define any
arguments member function. Member functions must be defined
d) Must be used as a function which accepts 3 by the class only. Objects can only access the members
arguments and use them.
13. Which object can be used to access the standard
Answer: a input?
Explanation: The cin and cout objects can be used a) System.inner
directly with the >> and << operators respectively. The b) cin
objects are of iostream class. Class iostream is an c) System.stdin
inbuilt class. d) console.input
9. Objects type ____________________
a) Can be changed in runtime Answer: b
b) Can’t be changed in runtime Explanation: Object cin can be used to take input from
c) Can be changed in compile time the standard input. It is used in C++. In java we can use
d) May or may not get changed System.in for the standard input stream. The syntax
changes from language to language.
Answer: b 14. A single object can be used __________________
Explanation: The object types are always fixed. Once a) As only two class types at a time
the object is created of a specific type then it can’t be b) As only three class types at a time
changed. Neither at runtime nor at compile time. c) As only one class type at a time
59
d) As of as many class types as required Answer: b
Explanation: This is a property of abstract class. It can
Answer: c define main() function inside it. T . is no restriction on
Explanation: The object can be of only one type. The its definition and implementation.
type of an object can’t be changed. Object type is 3. If t . is an abstract method in a class then,
mandatory to be of one class type to ensure the type ________________
and number of data members it have. a) Class must be abstract class
15. If same object name is given to different objects of b) Class may or may not be abstract class
different class then _____________ c) Class is generic
a) Its compile time error d) Class must be public
b) Its runtime error
c) It’s not an error Answer: a
d) Program suns smooth Explanation: It is a rule that if a class have even one
abstract method, it must be an abstract class. If this rule
Answer: a was not made, the abstract methods would have got
Explanation: It is a compile time error as the compiler skipped to get defined in some places which is
doesn’t allow same name objects to be declared more undesirable with the idea of abstract class.
than once. Compiler produces multiple declaration 4. If a class is extending/inheriting another abstract
error. Every object must have a different name. class having abstract method, then
. _______________________
To practice MCQs on all areas of Object Oriented a) Either implementation of method or making class
Programming, . abstract is mandatory
b) Implementation of the method in derived class is
Abstract Class - mandatory
c) Making the derived class also abstract is mandatory
“Abstract Class”. d) It’s not mandatory to implement the abstract method
1. Which among the following best describes abstract of parent class
classes?
a) If a class has more than one virtual function, it’s Answer: a
abstract class Explanation: Either of the two things must be done,
b) If a class have only one pure virtual function, it’s either implementation or declaration of class as
abstract class abstract. This is done to ensure that the method
c) If a class has at least one pure virtual function, it’s intended to be defined by other classes gets defined at
abstract class every possible class.
d) If a class has all the pure virtual functions only, then 5. Abstract class A has 4 virtual functions. Abstract
it’s abstract class class B defines only 2 of those member functions as it
extends class A. Class C extends class B and
Answer: c implements the other two member functions of class A.
Explanation: The condition for a class to be called Choose the correct option below.
abstract class is that it must have at least one pure a) Program won’t run as all the methods are not
virtual function. The keyword abstract must be used defined by B
while defining abstract class in java. b) Program won’t run as C is not inheriting A directly
2. Can abstract class have main() function defined c) Program won’t run as multiple inheritance is used
inside it? d) Program runs correctly
a) Yes, depending on return type of main()
b) Yes, always Answer: d
c) No, main must not be defined inside abstract class Explanation: The program runs correctly. This is
d) No, because main() is not abstract function because even class B is abstract so it’s not mandatory
to define all the virtual functions. Class C is not

60
abstract but all the virtual functions have been upon base class pointer and reference to select the
implemented will that class. proper virtual function.
6. Abstract classes can ____________________ 10. The abstract classes in java can
instances. _________________
a) Never have a) Implement constructors
b) Always have b) Can’t implement constructor
c) Have array of c) Can implement only unimplemented methods
d) Have pointer of d) Can’t implement any type of constructor

Answer: a Answer: a
Explanation: When an abstract class is defined, it Explanation: The abstract classes in java can define a
won’t be having implementation of at least one constructor. Even though instance can’t be created. But
function. This will restrict the class to have any in this way, only during constructor chaining,
constructor. When the class doesn’t have constructor, constructor can be called. When instance of concrete
t . won’t be any instance of that class. implementation class is created, it’s known as
7. We ___________________ to an abstract class. constructor chaining.
a) Can create pointers 11. Abstract class can’t be final in java.
b) Can create references a) True
c) Can create pointers or references b) False
d) Can’t create any reference, pointer or instance
Answer: a
Answer: c Explanation: If an abstract class is made final in java, it
Explanation: Even though t . can’t be any instance of will stop the abstract class from being extended. And if
abstract class. We can always create pointer or the class is not getting extended, t . won’t be another
reference to abstract class. The member functions class to implement the virtual functions. Due to this
which have some implementation inside abstract itself, contradicting fact, it can’t be final in java.
can be used with these references. 12. Can abstract classes have static methods (Java)?
8. Which among the following is an important use of a) Yes, always
abstract classes? b) Yes, but depends on code
a) Header files c) No, never
b) Class Libraries d) No, static members can’t have different values
c) Class definitions
d) Class inheritance Answer: a
Explanation: T . is no restriction on declaring static
Answer: b methods. The only condition is that the virtual
Explanation: The abstract classes can be used to create functions must have some definition in program.
a generic, extensible class library that can be used by 13. It is _________________________ to have an
other programmers. This helps us to get some already abstract method.
implemented codes and functions that might have not a) Not mandatory for an static class
been provided by the programming language itself. b) Not mandatory for a derived class
9. Use of pointers or reference to an abstract class c) Not mandatory for an abstract class
gives rise to which among the following feature? d) Not mandatory for parent class
a) Static Polymorphism
b) Runtime polymorphism Answer: c
c) Compile time Polymorphism Explanation: Derived, parent and static classes can’t
d) Polymorphism within methods have abstract method (We can’t say what type of these
classes is). And for abstract class it’s not mandatory to
Answer: b have abstract method. But if any abstract method is t .
Explanation: The runtime polymorphism is supported inside a class, then class must be abstract type.
by reference and pointer to an abstract class. This relies
61
14. How many abstract classes can a single program Answer: c
contain? Explanation: The syntax must start with keyword
a) At most 1 template, case sensitive. Then it should include the
b) At least 1 typename and a variable to denote it. Then whenever
c) At most 127 that variable is used, it replaces it with the data type
d) As many as required needed.
3. Can default arguments be used with the template
Answer: d class?
Explanation: T . is no restriction on the number of a) Yes, in some special cases
abstract classes that can be defined inside a single b) Yes, always
program. The programs can use as many abstract c) No, it must satisfy some specific conditions first
classes as required. But the functions with no body d) No, it can’t be done
must be implemented.
15. Is it necessary that all the abstract methods must be Answer: b
defined from an abstract class? Explanation: The template class can use default
a) Yes, depending on code arguments. This is used to specify the data type to be
b) Yes, always considered if it is not specified while passing to the
c) No, never generic class. The default type will be used.
d) No, if function is not used, no definition is required 4. What is the syntax to use explicit class
specialization?
Answer: b a) template class myClass<>{ }
Explanation: That is rule of programming language b) template class myClass{ }
that each function declared, must have some definition. c) template <> class myClass<>{ }
T . can’t be some abstract method that remains d) template <> class myClass{ }
undefined. Even if it’s t ., it would result in compile
time error. 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
Template Class - the same way as with explicit function specialization.
5. Which is the most significant feature that arises by
“Template Class”. using template classes?
1. A template class can have _____________ a) Code readability
a) More than one generic data type b) Ease in coding
b) Only one generic data type c) Code reusability
c) At most two data types d) Modularity in code
d) Only generic type of integers and not characters
Answer: c
Answer: a Explanation: The code reusability is the feature that
Explanation: The template class can support more than becomes more powerful with use of template classes.
one data type. The only thing is to add all the data You can generate a single code that can be used in
types required in a list separated by comma within variety of programming situation.
template specification. 6. A template class defines the form of a class
2. Which among the following is the proper syntax for _____________________ it will operate.
the template class? a) With full specification of the data on which
a) template ; b) With full specification of the functions on which
b) Template ; c) Without full specification of the data on which
c) template T named(T x, T y){ } d) Without full specification of the functions on which
d) Template T1 named(T1 x, T2 y){ }

62
Answer: c b) Overloading is single function doing different
Explanation: The template classes can accept all types operations, Template is multiple function doing
of data types. T . is no need to specify the data on different operations
which the class has to operate. Hence it gives us c) Overloading is multiple function doing similar
flexibility to code without worrying about the type of operation, Template is multiple function doing
data that might be used in the code. identical operations
7. What are the two specializations of I/O template d) Overloading is multiple function doing same
classes in C++? operation, Template is same function doing different
a) 16-bit character and wide characters operations
b) 8-bit character and wide characters
c) 32-bit character and locale characters Answer: c
d) 64-bit characters and locale characters Explanation: The function overloading is multiple
functions with similar or different functionality but
Answer: b generic class functions perform the same task on given
Explanation: The I/O specialization is made with wide different types of data.
character and 8-bit characters. Wide characters are 11. What if static members are declared inside template
used to store the characters that might take more than 1 classes?
byte of space in memory or any size that is different a) All instances will share the static variable
from the one that the machine is using. b) All instances will have their own static variable
8. Can typeid() function be used with the object of c) All the instances will ignore the static variable
generic classes? d) Program gives compile time error
a) Yes, only if default type is given
b) Yes, always Answer: b
c) No, generic data can’t be determined Explanation: The generic class have a special case with
d) No, never possible static members. Each instance will have its own static
member. The static members are not shared usually.
Answer: b 12. What is the output of following program?
Explanation: The typeid() function can be used with template <typename T>
the objects of generic classes. An instance of a void test(const T&x)
template class will take the type of data that is being {
used with it. Hence when typeid() function is used, the static int count = 0;
data type would have already been defined and hence cout &lt;&lt; "x = " &lt;&lt; x &lt;&lt; " count = "
we can get desired result from typeid() function. &lt;&lt; count &lt;&lt; endl;
9. The _____________ class is a specialization of a ++count;
more general template class. return;
a) String }
b) Integer
c) Digit void main()
d) Math {
test<int> (2);
Answer: a test<int>(2);
Explanation: The string class is more specialized. test<double>(2.2);
Since the string must be able to store any kind of data }
that is given to the string. Hence it need maximum a) x = 2 count = 0
specialization. x = 2.2 count = 0
10. How is function overloading different from x = 2.2 count = 0
template class? b) x = 2 count = 0
a) Overloading is multiple function doing same x = 2 count = 0
operation, Template is multiple function doing x = 2.2 count = 0
different operations c) x = 2 count = 0
63
x = 2 count = 1 Base Class -
x = 2.2 count = 0
d) x = 2 count = 0 “Base Class”.
x = 2 count = 1 1. Which is most appropriate definition of a base class?
x = 2.2 count = 2 a) It is parent of any of its derived class
b) It is child of one of the parent class
Answer: c c) It is most basic class of whole program
Explanation: For each new type, the class will have d) It is class with maximum number of members
separate instance. . two instances will be created and
hence counter for integer goes to 1. And for float Answer: a
value, the count remains 0 for the output. Explanation: A class which is parent of another class,
13. If template class is defined, is it necessary to use or from which other classes can be derived, is known
different types of data for each call? as a base class. It is mandatory that a class must have at
a) No, not necessary least one derived class to be called as a base class.
b) No, but at least two types must be t . 2. A base class is also known as _____________ class.
c) Yes, to make proper use of template a) Basic
d) Yes, for code efficiency b) Inherited
c) Super
Answer: a d) Sub
Explanation: It is not necessary to use different type
with each call to the generic function. Data may be of Answer: c
same type with each call but still the function works. Explanation: A class which is being derived by other
We don’t consider other properties like efficiency with classes, is called as super class. This concept is clearly
this concept because it is made generic to all data type, used in java as we call the functions of a base class by
hence always works. using the keyword super as required.
14. How many generic types can be given inside a 3. An abstract class is always a __________ class.
single template class? a) Base
a) Only 1 b) Derived
b) Only 3 c) Template
c) Only 7 d) Nested
d) As many as required
Answer: a
Answer: d Explanation: Every abstract class is a base class. It
Explanation: T . is no restriction on the number of must be so, because the functions which are not
types to be used for making the class generic. T . can defined inside the abstract class, must be defined in the
be any number of generic types with a single class. derived classes. Hence it becomes a base class.
Hence giving flexibility to code with all the data types. 4. How many base classes can a single class inherit in
15. Template classes must have at least one static java?
member. a) 1
a) True b) 2
b) False c) 3
d) As many as required
Answer: b
Explanation: T . is no mandatory condition to have Answer: a
static members inside template class. Not only Explanation: In java, multiple inheritance is not
template, it is not mandatory to have static members supported, which leads to the fact that a class can have
anyw .. We can use them as required in the code. only 1 parent class if inheritance is used. Only if
. interfaces are used then the class can implement more
. than one base class.

64
5. How to make a derived class a base class? a) True
a) Change name of the class b) False
b) Use keyword base
c) Make a class derive from it Answer: b
d) Can’t be done Explanation: When a class have more than one nested
classes, it is known as enclosing class. It can’t be
Answer: c called as parent or base class since t . is no inheritance
Explanation: Making another class derive from it will involved.
make that class as base class. It is not necessary that 10. Base class have ________________ of abstraction.
we have to write a different code for it. If at least one a) Higher degree
class derives that class, it becomes the base class for b) Lower degree
the new class. c) Intermediate
6. If a base class is being derived by two other classes, d) Minimum degree
which inheritance will that be called?
a) Single Answer: b
b) Multiple Explanation: A base class will have lesser information
c) Multi-level as compared to those of derived classes. Since derived
d) Hierarchical classes inherit the base class properties and then add on
their own features, they elaborate more hence have
Answer: d lower degree of abstraction.
Explanation: When more than one classes are being 11. Always the base class constructors are called
derived from a single parent class, the inheritance is ___________ constructor of derived class.
known as hierarchical inheritance. This is usually a) Before
useful when the base class is higher abstraction of its b) After
derived classes. c) Along
7. Which among the following must be in a base class? d) According to priority of
a) Data members
b) Member functions Answer: a
c) Access specifiers Explanation: When the base class object is created, its
d) Nothing constructor will be called for sure. But if derived class
constructor is called, first base class constructor is
Answer: d called and then derived class constructor is taken into
Explanation: Even a class which doesn’t have any consideration.
members can be a base class. It is not mandatory to 12. Can we call methods of base class using
have any member or attribute in base class. constructor of the derived class?
8. Which type of members can’t be accessed in derived a) Yes, always
classes of a base class? b) Yes, but not always
a) Protected c) No, never
b) Private d) No, but we can call in some cases
c) Public
d) All can be accessed Answer: a
Explanation: If the function is defined in the base class,
Answer: b it can always be called from the constructor of its
Explanation: The private members can be accessed derived class. Since the constructors are not private,
only inside the base class. If the class is derived by they can be accessed in derived class even if those are
other classes. Those members will not be accessible. protected.
This concept of OOP is made to make the members 13. If a base class is inherited from another class and
more secure. then one class derives it, which inheritance is shown?
9. If a class is enclosing more than one class, than it a) Multiple
can be called as base class of those classes. b) Single
65
c) Hierarchical a) Data members only
d) Multi-level b) Member functions only
c) All the members except private members
Answer: d d) All the members of base class
Explanation: If a base class is inherited from another
class, single inheritance is shown. But when one more Answer: c
class inherits the derived class, this becomes a multi- Explanation: The class inheriting another class, inherits
level inheritance. all the data members and member functions which are
14. How many base classes can a single derived class not private. This is done to ensure the security features
have in C++? with maximum flexibility.
a) 1 3. If t . is a derived class in a program, how many
b) 2 classes must be in that program?
c) 3 a) 1
d) As many as required b) 2
c) 3
Answer: d d) 4
Explanation: This is because C++ allows the multiple
inheritance. A derived class can have more than one Answer: b
base class and hence can derive all of their features. Explanation: If t . is a derived class in a program, t .
15. If a base class is added with few new members, its must be at least 2 classes in that program. One is a base
subclass must also be modified. (True/False) class and another derived class. Hence at least 2 classes
a) True must be t ..
b) False 4. Which members can never be accessed in derived
class from the base class?
Answer: b a) Private
Explanation: The base class can be added with new b) Protected
members without affecting the subclasses. This is c) Public
because the subclasses may get some more features d) All except private
inherited but it won’t use them. But the base class will
be able to use the new members as would be required. Answer: d
. Explanation: T . 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
Derived Class - so that those members are not accessible outside the
class.
“Derived Class”. 5. How many types of inheritance are supported in C+
1. Which among the following is best definition of a + for deriving a class?
derived class? a) 1
a) A child class b) 2
b) A class which inherits one or more classes c) 3
c) A class with keyword derived d) 4
d) A class with more than one constructor
Answer: c
Answer: b Explanation: T . are three types of inheritance possible.
Explanation: Any class which inherits one or more Private inheritance, protected inheritance, and public
classes is a derived class. The only condition is it must inheritance. The inheritance defines the access
inherit at least one class in order to be called as a specifier to be used with the inherited members in the
derived class. derived class.
2. Which among the following is inherited by a derived 6. How many derived class can a single base class
class from base class? have?
66
a) 1 derived classes. This is used while denoting all the
b) 2 derived classes subsequent to a superclass.
c) 3 10. If class A is derived from another derived class B
d) As many are required which is derived from class C, which class will have
maximum level of abstraction?
Answer: d a) Class A
Explanation: T . is no restriction on how many classes b) Class B
can inherit a single base class. Hence t . can be as c) Class C
many derived classes as required in a program from a d) All have same level of abstraction
single base class.
7. Which among the following is correct? Answer: c
a) Friend function of derived class can access non- Explanation: The abstraction level of class C will be
private members of base class maximum. This is because the parent class have higher
b) Friend function of base class can access derived level of abstraction. Hence the parent of all other class
class members will have maximum level of abstraction.
c) Friend function of derived class can access members 11. If base class is an abstract class then derived class
of only derived class ______________ the undefined functions.
d) Friend function can access private members of base a) Must define
class of a derived class b) Must become another abstract class or define
c) Must become parent class for
Answer: a d) Must implement 2 definitions of
Explanation: The friend function of a class can access
the non-private members of base class. The reason Answer: b
behind is that the members of base class gets derived Explanation: The function must be defined in the
into the derived class and hence become members of program which are not defined in the base class. Hence
derived class too. Hence a friend function can access the class must be defined as abstract of implement the
all of those. function definition in it.
8. If a class is being derived using more than two base 12. How many classes can be derived from a derived
classes, which inheritance will be used? class?
a) Single a) Only 1
b) Multi-level b) At most 1
c) Hierarchical c) At least 1
d) Multiple d) As many as required

Answer: d Answer: d
Explanation: The statement given is definition of Explanation: When a class is to be derived from
multiple inheritance with respect to the derived class. another derived class, the derived class behaves as a
The concept can be illustrated with many other normal base class hence t . are no restriction on how
samples but the main aspects are base class and derived many class can be derived from a derived class. The
class only. derived class again behaves as a normal superclass.
9. Derived class is also known as ______________ 13. The members of a derived class can never be
class. derived.
a) Subclass a) True
b) Small class b) False
c) Big class
d) Noticeable class Answer: b
Explanation: This is not true that the members of a
Answer: a derived class can’t be derived. All the classes are
Explanation: It is just another name given to the considered to be a normal class when used for

67
derivation. The members can be derived with respect to a) Class
their access specifiers. b) Structure
14. Which feature is not related to the derived classes c) Function
among the following? d) Object
a) Inheritance
b) Encapsulation Answer: a
c) Run time memory management Explanation: A class can be used to group all the
d) Compile time function references brands of cars. The Different brands may have
different properties but all will have some common
Answer: c properties like number of wheels and doors,
Explanation: The memory management is the feature accessories etc. All of those properties can be grouped
that is not necessary for derived classes that will be a into a single class representing all the cars.
part of whole program. The functions references must 3. If a class have two data members and two functions
be resolved for their proper use if inheritance is used. to add those two numbers and to subtract them, which
15. Deriving a class in such a way that that the base among the following is most efficient if the
class members are not available for further inheritance programmer wants to implement multiplication too?
is known as ___________________ a) Define a public function which multiplies two
a) Public inheritance numbers
b) Protected inheritance b) Define a public function that returns values of two
c) Protected or private inheritance data members
d) Private inheritance c) Define a private function which multiplies two
numbers
Answer: d d) Define a private function that returns values of two
Explanation: The private members of a class can never data members
be derived to another class. When a class derives
another class using private inheritance, all the members Answer: b
become private members of the derived class. Hence Explanation: The best choice would be to define a
these member won’t be available for further public member function that returns the values of two
inheritance. data members of the class. This way we can implement
. any operation on those data members. Also t . won’t be
. any need to modify the program and to add new
functions for each new operation.
Class Use - 4. If a database have to be maintained using OOP, to
store the details of each employee in a company, which
“Class Use”. would be the best choice among the following?
1. Which among the following is the main a) Define a class to store details of each employee
characteristic of class? b) Define a structure to store details of each employee
a) Inheritance c) Define separate variable for each detail
b) Encapsulation d) Define a generic type to store string and number
c) Polymorphism details
d) Object creation
Answer: a
Answer: b Explanation: A single class can be defined that
Explanation: The classes are made to encapsulate the represents a single employee of a company. The
data and properties that are most similar and can be specific type of functions that can be applied to
grouped together inside a single class. This single class specific employees can be put into another class that is
can represent all of those features by creating its derived from the existing class.
instances. 5. Which class represents the most abstracted
2. To group all the brands of cars, what should be information?
used? a) Nested
68
b) Derived d) To define a blueprint that shows memory location of
c) Enclosed data
d) Base
Answer: c
Answer: d Explanation: The classes would be more suitable to use
Explanation: The base classes are the most abstracted in case w . we need to group the most similar data and
part of information. A base class having many other operations. The data can be represented as data
derived classes would have a bigger overview of all the members of class and operations as member functions
other derived classes. Hence the base classes have the of class. This is indirectly encapsulation feature.
most abstract information. 10. What is the use of inbuilt classes?
6. Which among the following is an advantage of using a) Provide predefined data
classes over structures of C? b) Provide predefined functions
a) Functions are restricted c) Provide predefined data and functions
b) Functions can’t be defined d) Provide predeclared data to be overridden
c) Functions can also be encapsulated
d) Functions can have more security Answer: c
Explanation: The data that is constant or is always the
Answer: c same initially for use can be provided by some inbuilt
Explanation: The functions are also made easy to be classes. The functions that are mostly used are also
encapsulated inside a class. In structures, only the data provided by the inbuilt classes. The data and functions
members were allowed to be encapsulated. Hence can be used by including the corresponding header file
classes can represent an entity in a better way. or library.
7. Which among the follow is a feature of class? 11.Which feature is exhibited by the inbuilt classes?
a) Object orientation a) Code reusability
b) Procedure orientation b) Code efficiency
c) Both object and procedure orientation c) Code readability
d) Neither object nor procedure orientation d) Code reusability, efficiency and readability

Answer: a Answer: d
Explanation: Thee use of classes feature the object Explanation: The code is reusable as the functions
oriented programming. The OOP concept can be which are already written, can be used anytime
implemented by using class and their objects. required. The code becomes easier to read. Also, the
Procedures orientation is not feature of OOP. code is efficient as t . is no need to assign any external
8. Class is _____________ of an object. code.
a) Basic function definition 12. Why do we use user defined classes?
b) Detailed description with values a) To design a user intended code
c) Blueprint b) To model real world objects
d) Set of constant values c) To design the interfaces
d) To model the functions
Answer: c
Explanation: The class is an overview for an object. It Answer: b
contains the basic details map of data that an object Explanation: The primitive classes are not sufficient
will contain. An object is independent representation of for the programming complex algorithms. Some user
class. defined classes are required to represent a real world
9. In which case the classes can be used to make more object and to define a blueprint of what the class
efficient program? should actually contain. The user defined classes are as
a) To define a function that is called frequently in a per the requirements and need of user.
program 13. Why do classes use accessor methods?
b) To structure data that is most similar a) To make public data accessible to client
c) To group the most similar data and operations b) To make public data private to client
69
c) To make private data public for whole program same as copying. Using already defined functions is
d) To make private data accessible to the client not inheritance as we are not adding any of our own
features.
Answer: d 2. How many basic types of inheritance are provided as
Explanation: The private data of a class is usually not OOP feature?
accessible. But the data can be accessed by the user a) 4
using accessor functions. These functions allows the b) 3
user to get the data stored as private in a class. c) 2
14. Why do classes use mutator methods? d) 1
a) Allows client to modify the program
b) Allows client to modify/write the private members Answer: a
c) Allows servers to access all the private data Explanation: T . are basically 4 types of inheritance
d) Allows servers to access only protected members provided in OOP, namely, single level,multilevel,
multiple and hierarchical inheritance. We can add one
Answer: b more type as Hybrid inheritance but that is actually the
Explanation: The client can have rights to access a file combination any types of inheritance from the 4 basic
and write something to it. This is mandatory to keep ones.
the private data updated. Also it is an advantage over 3. Which among the following best defines single level
the use of hidden class members. inheritance?
15. Which among the following is the most abstract a) A class inheriting a derived class
form of class? b) A class inheriting a base class
a) Cars c) A class inheriting a nested class
b) BMW cars d) A class which gets inherited by 2 classes
c) Big cars
d) Small cars Answer: b
Explanation: A class inheriting a base class defines
Answer: a single level inheritance. Inheriting an already derived
Explanation: The most abstract class is class Cars. The class makes it multilevel inheritance. And if base class
class Cars is most general form of all other cars. If it is is inherited by 2 other classes, it is multiple
a brand of car, it comes under car. If it is type of car inheritance.
then also it comes under Cars. 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{ };
Inheritance - b) class student{int marks;}; class stream{ }; class
topper: public student{ };
“Inheritance”. c) class student{int marks;}; class stream:public
1. Which among the following best describes the student{ };
Inheritance? d) class student{ }; class stream{ }; class topper{ };
a) Copying the code already written
b) Using the code already written once Answer: a
c) Using already defined functions in programming Explanation: Class topper is getting derived from 2
language other classes and hence it is multiple inheritance.
d) Using the data and functions into derived segment Topper inherits class stream and class student publicly
and hence can use its features. If only few classes are
Answer: d defined, t . we are not even using inheritance (as in
Explanation: It can only be indicated by using the data option d).
and functions that we use in derived class, being 5. Which programming language doesn’t support
provided by parent class. Copying code is now . similar multiple inheritance?
to this concept, also using the code already written is a) C++ and Java
70
b) C and C++ Answer: c
c) Java and SmallTalk Explanation: When 2 or more classes inherit the same
d) Java class using multiple inheritance and then one more
class inherits those two base classes, we get a diamond
Answer: d like structure. ., ambiguity arises when same function
Explanation:Java doesn’t support multiple inheritance. gets derived into 2 base classes and finally to 3rd level
But that feature can be implemented by using the class, because same name functions are being
interfaces concept. Multiple inheritance is not inherited.
supported because of diamond problem and similar 9. Which access type data gets derived as private
issues. member in derived class:
6. Which among the following is correct for a) Private
hierarchical inheritance? b) Public
a) Two base classes can be used to be derived into one c) Protected
single class d) Protected and Private
b) Two or more classes can be derived into one class
c) One base class can be derived into other two derived Answer: a
classes or more Explanation: It is a rule, that when a derived class
d) One base class can be derived into only 2 classes inherits the base class in private access mode, all the
members of base class gets derived as private members
Answer: c of the derived class.
Explanation: One base class can be derived into other 10. If a base class is inherited in protected access mode
two derived classes or more. If only one class gets then which among the following is true?
derived by only 2 other classes, it is also hierarchical a) Public and Protected members of base class
inheritance, but it is not a mandatory condition, becomes protected members of derived class
because any number of derived classes can be t .. b) Only protected members become protected members
7. Which is the correct syntax of inheritance? of derived class
a) class derived_classname : base_classname{ /*define c) Private, Protected and Public all members of base,
class body*/ }; become private of derived class
b) class base_classname : derived_classname{ /*define d) Only private members of base, become private of
class body*/ }; derived class
c) class derived_classname : access
base_classname{ /*define class body*/ }; Answer: a
d) class base_classname :access derived_classname{ Explanation: As the programming language rules
/*define class body*/ }; apply, all the public and protected members of base
class becomes protected members of derived class in
Answer: c protected access mode. It can’t be changed because it
Explanation: Firstly, keyword class should come, would hinder the security of data and may add
followed by the derived class name. Colon is must vulnerability in program.
followed by access in which base class has to be 11. Members which are not intended to be inherited are
derived, followed by the base class name. And finally declared as:
the body of class. Semicolon after the body is also a) Public members
must. b) Protected members
8. Which type of inheritance leads to diamond c) Private members
problem? d) Private or Protected members
a) Single level
b) Multi-level Answer: c
c) Multiple Explanation: Private access specifier is the most secure
d) Hierarchical access mode. It doesn’t allow members to be inherited.
Even Private inheritance can only inherit protected and
public members.
71
12. While inheriting a class, if no access mode is d) It can be done both by making the visibility mode
specified, then which among the following is true? (in public or protected
C++)
a) It gets inherited publicly by default Answer: d
b) It gets inherited protected by default Explanation: It is not mandatory that you have to make
c) It gets inherited privately by default the visibility mode either public or protected. You can
d) It is not possible do either of those. That will give you permission to
inherit the private members of base class.
Answer: c .
Explanation: If the access mode is not specified during .
inheritance, the class is inherited privately by default.
This is to ensure the security of data and to maintain Inheritance Types -
OOP features. Hence it is not mandatory to specify the
access mode if we want the class to be inherited “Types of Inheritance”.
privately. 1. How many types of inheritance are possible in C++?
13. If a derived class object is created, which a) 2
constructor is called first? b) 3
a) Base class constructor c) 4
b) Derived class constructor d) 5
c) Depends on how we call the object
d) Not possible Answer: d
Explanation: T . are five types of inheritance that are
Answer: a possible in C++. Single level, Multilevel, multiple,
Explanation: First the base class constructor is hierarchical and hybrid. . we count hybrid also
invoked. When we create a derived class object, the because it sometimes can bring up a new form of
system tries to invoke its constructor but the class is inheritance, Like inheritance using multiple and
derived so first the base class must be initialized, hence hierarchical, which sometimes results in diamond
in turn the base class constructor is invoked before problem.
derived class constructor. 2. Which among the following is true?
14. The private members of the base class are visible in a) Java supports all types of inheritance
derived class but are not accessible directly. b) Java supports multiple inheritance
a) True c) Java doesn’t support multiple inheritance
b) False d) Java doesn’t support inheritance

Answer: a Answer: c
Explanation: Consider that a variable is private in base Explanation: Java doesn’t support multiple inheritance.
class and the derived class uses public inheritance to This is done to avoid the diamond problem that
inherit that class. Now if we also have a global variable sometimes arises with inherited functions. Though,
of same name as that of base class private variable, multiple inheritance can be implemented in java using
neither the global variable nor the base class private interfaces.
variable will be accessible from derived class. This is 3. Which type of inheritance is illustrated by the
because we can’t have 2 variable with same name in following code?
same local scope. Hence the private members are class student{ public: int marks; };
accessible but not directly. class topper: public student { public: char grade; };
15. How can you make the private members class average{ public: int makrs_needed; };
inheritable? class section: public average{ public: char
a) By making their visibility mode as public only name[10]; };
b) By making their visibility mode as protected only class overall: public average{ public: int students; };
c) By making their visibility mode as private in derived a) Single level
class b) Multilevel and single level
72
c) Hierarchical 7. If 6 classes uses single level inheritance with pair
d) Hierarchical and single level classes (3 pairs), which inheritance will this be called?
a) Single
Answer: c b) Multiple
Explanation: It is hierarchical inheritance and single c) Hierarchical
level inheritance. Since class topper is inheriting class d) Multilevel
student, it is single level inheritance. And then average
is inherited by section and overall, so it is hierarchical Answer: a
inheritance. But both of them are separate. Hence it is Explanation: . all the pairs are using single inheritance.
not hybrid inheritance. And no different pairs are inheriting same classes.
4. Which among the following best describes multiple Hence it can’t be called hybrid or multilevel
inheritance? inheritance. You can say the single inheritance is used
a) Two classes being parent of any other classes 3 times in that program.
b) Three classes being parent of other classes 8. Which among the following is correct for the
c) More than one class being parent of other child following code?
classes class A
d) More than one class being parent of single child {
public : class B
Answer: d {
Explanation: If a class inherits more than one class, it public : B(int i): data(i)
is known as multiple inheritance. This should not be {
referred with only two or three classes being inherited. }
But t . must be one class which inherits more than one int data;
class to be called as multiple inheritance. }
5. How many types of inheritance can be used at a time };
in single program? class C: public A
a) Any two types {
b) Any three types class D:public A::B{ };
c) Any 4 types };
d) Any type, any number of times a) Multi-level inheritance is used, with nested classes
b) Multiple inheritance is used, with nested classes
Answer: d c) Single level inheritance is used, with enclosing
Explanation: Any type of inheritance can be used in classes
any program. T . is no rule to use only few types of d) Single level inheritance is used, with both enclosing
inheritance. Only thing that matters is how the classes and nested classes
are inherited and used.
6. Which type of inheritance results in diamond Answer: d
problem? Explanation: Class C is inheriting Class A. Class D is
a) Single level inheriting class B, both are nested. Hence it is single
b) Hybrid inheritance. For multiple inheritance, class C or D
c) Hierarchical should have inherited both class A and class B.
d) Multilevel 9. Which among the following is false?
a) If one class inherits the inherited class in single level
Answer: b inheritance, it is multi-level inheritance
Explanation: In diamond problem, hierarchical b) Hybrid inheritance always contains multiple
inheritance is used first, w . two different classes inheritance
inherit the same class and then in turn a 4th class c) Hierarchical inheritance involves inheriting same
inherits the two classes which had inherited the first class into more than one classes
class. Using more than one type of inheritance ., it is d) Hybrid inheritance can involve any types of
known as hybrid inheritance.
73
inheritance together Answer: d
Explanation: Any class can inherit any number of
Answer: b classes. T . is no limit defined for the number of classes
Explanation: It is not necessary to have multiple being inherited by a single class.
inheritance in hybrid type. It can have any type 14. How many classes can be inherited by a single
together. This doesn’t have to be of specific type class in java?
always. a) Only 1
10. If class A has two nested classes B and C. Class D b) Only 27
has one nested class E, and have inherited class A. If E c) Only 255
inherits B and C, then: d) Only 1024
a) It shows multiple inheritance
b) It shows hierarchical inheritance Answer: a
c) It shows multiple inheritance Explanation: Since java doesn’t support multiple
d) Multiple inheritance among nested classes, and inheritance, it is not possible for a class to inherit more
single level for enclosing classes than 1 class in java. This is the same case in C# also.
15. If multi-level inheritance is used, First class B
Answer: d inherits class A, then C inherits B and so on. Till how
Explanation: This involves the same concept of many classes can this go on?
inheritance, w . the nested classes also follow the a) Only till class C
inheritance rules. The Enclosing classes are having b) Only till class J
single inheritance. Nested classes involves multiple. c) Only till class Z
11. In hierarchical inheritance, all the classes involve d) T . is no limit
some kind of inheritance. (True/ False)
a) True Answer: d
b) False Explanation: In this case t . is no limit. All the classes
going on like this will inherit the members of base
Answer: b class, and hence the upper level inheritance won’t
Explanation: This is so because few classes might not affect the number of classes that can go on inheriting in
be involved in any type of inheritance in whole this pattern.
program w .as other classes might be participating in .
more than one type of inheritance at the same time. .
12. Which type of inheritance cannot involve private
inheritance? Single Level Inheritance -
a) Single level
b) Multiple “Single Level Inheritance”.
c) Hybrid 1. Which among the following defines single level
d) All types can have private inheritance inheritance?
a) One base class derives another class
Answer: d b) One derived class inherits from one base class
Explanation: This is a common type of inheritance w . c) One base class inherits from one derived class
the protected and public members of parent class d) One derived class derives from another derived class
become private members in child class. T . is no type
which doesn’t support private inheritance. Answer: b
13. How many classes can be inherited by a single Explanation: If only one base class is used to derive
class in multiple inheritance (C++)? only one subclass, it is known as single level
a) Only 2 inheritance. The reason of this name is that we inherit
b) Only 27 the base class to one more level and stop the
c) Only 1024 inheritance any further.
d) Any number of classes can be inherited 2. If class A and class B are derived from class C and
class D, then ________________
74
a) Those are 2 pairs of single inheritance }
b) That is multilevel inheritance };
c) Those is enclosing class class C: private A, public B
d) Those are all independent classes {
void avg()
Answer: a {
Explanation: Since class A is derived from class C and cout&lt;&lt;(a+b)/2;
then class B is derived from class D, t . are two pairs of }
classes which shows single inheritance. Those two };
pairs are independent of each other though. a) Class A
3. If single inheritance is used, program will contain b) Class B
________________ c) Class C
a) At least 2 classes d) None
b) At most 2 classes
c) Exactly 2 classes Answer: b
d) At most 4 classes Explanation: Class B is using single level inheritance.
Class C is using multiple inheritance. And class A is
Answer: a parent of other two classes.
Explanation: The program will contain at least 2 6. If single level inheritance is used and an abstract
classes in the sense of base and derived classes. At class is created with some undefined functions, can its
least one base class and one derived class must be t .. derived class also skip some definitions?
Types of inheritance remains the same though. a) Yes, always possible
4. Single level inheritance supports _____________ b) Yes, possible if only one undefined function
inheritance. c) No, at least 2 undefined functions must be t .
a) Runtime d) No, the derived class must implement those methods
b) Compile time
c) Multiple inheritance Answer: d
d) Language independency Explanation: The derived class must implement those
methods. This is because the parent class is abstract
Answer: a and hence will have some undefined functions which
Explanation: The runtime inheritance is done when has to be defined in derived classes. Since we are using
object of a class is created to call a method. At runtime single level inheritance, if derived class doesn’t
the function is searched if it is in class of object. If not, implement those functions then one more class has to
it will search in its parent classes and hierarchy for that be t . which will become multi-level inheritance.
method. 7. Which among the following is false for single level
5. Which method in the code below is single level inheritance?
inherited? a) T . can be more than 2 classes in program to
class A implement single inheritance
{ b) T . can be exactly 2 classes to implement single
protected int a, b; inheritance in a program
public: void show() c) T . can be more than 2 independent classes involved
{ in single inheritance
cout&lt;&lt;a&lt;&lt;b; d) The derived class must implement all the abstract
} method if single inheritance is used
};
class B: public A Answer: c
{ Explanation: If more than 2 independent classes are
public: void disp() involved to implement the single level inheritance, it
{ won’t be possible as t . must be only one child and one
cout&lt;&lt;a++&lt;&lt;b++; parent class and none other related class.
75
8. Which concept will result in derived class with more };
features (consider maximum 3 classes)? class B:public A
a) Single inheritance {
b) Multiple inheritance int x,y;
c) Multilevel inheritance };
d) Hierarchical inheritance a) Garbage value
b) Compile time error
Answer: b c) Runtime error
Explanation: If single inheritance is used then only d) Runs but gives random values as output
feature of a single class are inherited, and if multilevel
inheritance is used, the 2nd class might have use Answer: b
private inheritance. Hence only multiple inheritance Explanation: The compiler doesn’t find the main
can result in derived class with more features. This is function and hence will throw an error main() missing.
not mandatory but in a case if we consider same This program is using single level inheritance but the
number of features in each class, it will result the same. program is incomplete. Every program must implement
9. Single level inheritance is safer than main function.
_____________ 12. Output of following program?
a) Multiple inheritance class A
b) Interfaces {
c) Implementations float sal=40000;
d) Extensions }
class B extends A
Answer: a {
Explanation: Interfaces also represent a way of int salBonus=10000;
inheritance but is a wide word to decide which public static void main(String args[])
inheritance we are talking about in it, hence can’t be {
considered. Implementation and extensions also B p=new B();
doesn’t match that level of specific idea. And multiple System.out.println("B salary
inheritance not so safe as it might result in some is:"+p.sal);
ambiguity. System.out.println("Bonus of B
10. Which language doesn’t support single level is:"+p.bonus);
inheritance? }
a) Java }
b) C++ a) B salary is: 4000.0
c) Kotlin Bonus of B is: 10000
d) All languages support it b) B salary is 10000
Bonus of B is: 4000.0
Answer: d c) Compile time error
Explanation: All the languages support single level d) Runtime error
inheritance. Since any class can inherit other classes as
required, if single level inheritance was not allowed it Answer: a
would result in failing a lot of features of OOP. Explanation: The program gives output as in option a.
11. Output of following program? The program have used single level inheritance and
class A hence have access to the parent class methods and
{ variables. This program simply prints the value from
protected: int a,b; parent class and from the child class.
public: void disp() 13. Single level inheritance will be best
{ for___________
cout&lt;&lt;a&lt;&lt;b; a) Inheriting a class which performs all the calculations
} b) Inheriting a class which can print all the calculation
76
results d) Class which have more than one parent
c) Inheriting a class which can perform and print all
calculations Answer: b
d) Inheriting all the classes for different calculations Explanation: Only if the class is being derived from
other derived class, it can be called as multilevel
Answer: b inheritance. If a class is derived from another class, it
Explanation: Inheriting a class which can perform the is single level inheritance. T . must be more than one
most common task will be more efficient. If class level of inheritance.
which can perform all the calculations is inherited then 2. If t . are 5 classes, E is derived from D, D from C, C
t . won’t be any problem to print the result too. But if a from B and B from A. Which class constructor will be
class which can do the most common task for all the called first if the object of E or D is created?
other tasks, it will make real use of inheritance. a) A
14. Which constructor will be called first from the b) B
classes involved in single inheritance from object of c) C
derived class? d) A and B
a) Base class constructor
b) Derived class constructor Answer: a
c) Both class constructors at a time Explanation: A is parent of all other classes indirectly.
d) Runtime error 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.
Answer: a 3. If t . are 3 classes. Class C is derived from class B
Explanation: The base class constructor will be called and B is derived from A, Which class destructor will
first from the object of derived class constructor. When be called at last if object of C is destroyed.
the derived class members are to be initialized and a) A
allocated memory, the base class members should also b) B
be confirmed with the memory allocation. c) C
15. If base class contains 2 nested classes, will it be d) All together
possible to implement single level inheritance?
a) Yes, always Answer: a
b) Yes, only if derived class also have nested classes Explanation: The destructors are called in the reverse
c) No, it will use more than 2 classes which is wrong order of the constructors being called. Hence in
d) No, never multilevel inheritance, the constructors are created
from parent to child, which leads to destruction from
Answer: a child to parent. Hence class A destructor will be called
Explanation: The nested classes are also members of a at last.
class. They behave as a used defined data type with 4. Which Class is having highest degree of abstraction
some methods implementation. So the inheritance will in multilevel inheritance of 5 levels?
be as usual with the nested classes being member of a) Class at 1st level
base class and of derived class if not private. b) Class 2nd last level
. c) Class at 5th level
. d) All with same abstraction

Multilevel Inheritance - Answer: a


Explanation: The class with highest degree of
“Multilevel Inheritance”. abstraction will be the class at the 1st level. You can
1. Which among the following best defines multilevel look at a simple example like, a CAR is more abstract
inheritance? than SPORTS CAR class. The level of abstraction
a) A class derived from another derived class decrease with each level as more details comes out.
b) Classes being derived from other derived classes 5. If all the classes use private inheritance in multilevel
c) Continuing single level inheritance inheritance then ______________
77
a) It will not be called multilevel inheritance This continues as each class inherits only one class. T .
b) Each class can access only non-private members of should not be any class which inherits from two or
its parent more classes or which have more than one subclass.
c) Each subsequent class can access all members of 9. All the classes must have all the members declared
previous level parent classes private to implement multilevel inheritance.
d) None of the members will be available to any other a) True
class b) False

Answer: b Answer: b
Explanation: The classes will be able to access only the Explanation: T . is no mandatory rule to make the
non-private members of its parent class. The classes members private for multilevel inheritance. Moreover
are using private inheritance, hence all the members of if all the classes have only the private members then t .
parent class become private in the derived class. In turn won’t be any member to get inherited. Hence the
those won’t be allowed for further inheritance or direct working will be of no use.
access outside the class. 10. Can abstract classes be used in multilevel
6. Multilevel inheritance allows _________________ inheritance?
in the program. a) Yes, always
a) Only 7 levels of inheritance b) Yes, only one abstract class
b) At least 7 levels of inheritance c) No, abstract class doesn’t have constructors
c) At most 16 levels of inheritance d) No, never
d) As many levels of inheritance as required
Answer: a
Answer: d Explanation: The abstract classes can always be used
Explanation: The multilevel inheritance allows any in multilevel inheritance. The only condition that may
number of levels of inheritance. This is the maximum arise is that all the undefined functions must be defined
flexibility feature to make the members available to all in subclasses. T . must not be any undefined function.
the new classes and to add their own functionalities. 11. How many abstract classes can be used in
The code reusability is used too. multilevel inheritance?
7. What is minimum number of levels for a a) Only 1
implementing multilevel inheritance? b) Only 2
a) 1 c) At least one less than number of levels
b) 2 d) Can’t be used
c) 3
d) 4 Answer: c
Explanation: At least one class must implement all the
Answer: c undefined functions. Hence t . must be at least one
Explanation: T . must be at least 3 levels of class which is not abstract. That is at least one less than
inheritance. Otherwise if less, it will be single level number of levels.
inheritance or would have got no inheritance 12. If all the classes used parameterized constructors
implemented. T . must be a derived class from which and no default constructor then, ___________
another class is derived. a) The object of lower level classes can’t be created
8. In multilevel inheritance one class inherits b) Object of lower level classes must call parent class
_______________ constructors explicitly
a) Only one class c) Object of lower level classes must define all the
b) More than one class default constructors
c) At least one class d) Only object of first class can be created, which is
d) As many classes as required first parent

Answer: a Answer: b
Explanation: The classes inherit only from one class. Explanation: Each class constructor must be called
78
before creating object of any subclass. Hence it will be 15. Is it compulsory for all the classes in multilevel
mandatory to call the constructors of parent classes inheritance to have constructors defined explicitly if
explicitly with parameters. This will make all the only last derived class object is created?
previous class member be initialized and then the class a) Yes, always
in use will be able to create the object. b) Yes, to initialize the members
13. In multilevel inheritance, which is the most c) No, it not necessary
significant feature of OOP used? d) No, Constructor must not be defined
a) Code readability
b) Flexibility Answer: c
c) Code reusability Explanation: It’s not mandatory to define the
d) Code efficiency constructors explicitly. Default constructor will always
be provided by the compiler itself if none another
Answer: c constructor is defined in those classes. If explicit
Explanation: The classes using multilevel inheritance default constructor is defined it will be used.
will use the code in all the subsequent subclasses if .
available. Hence the most significant feature among .
the options given is code reusability. This feature is
generally intended to use the data values and reuse the Multiple Inheritance -
redundant functions.
14. Does following code show multiple inheritance? “Multiple Inheritance”.
1. Multiple inheritance is ____________________
class A a) When a class is derived from another class
{ b) When a class is derived from two or more classes
int a; c) When a class is derived from other two derived
}; classes
class B d) When a class is derived from exactly one class
{
int b; Answer: b
}; Explanation: The multiple inheritance is used when a
class C:public A, public B class is being derived using two base classes or more.
{ This way a single class can have features of more than
int c; one classes inherited into a single unit. This lets us
}; combine two class members into a single class.
class D:public C 2. Which problem arises due to multiple inheritance, if
{ hierarchical inheritance is used previously for its base
int d; classes?
}; a) Diamond
a) Yes, class C and class D b) Circle
b) Yes, All together it’s multilevel c) Triangle
c) No, 4 classes are used d) Loop
d) No, multiple inheritance is used with class A, B and
C Answer: a
Explanation: The diamond problem arises when
Answer: d multiple inheritance is used. This problem arises
Explanation: Since multiple inheritance is used to because the same name member functions get derived
derive class C and then class D is derived from class C. into a single class. Which in turn creates ambiguity in
This is not multilevel inheritance. The classes should calling those methods.
derive from single class. This is actually hybrid 3. How many classes should a program contain to
inheritance. implement the multiple inheritance?
a) Only 1
79
b) At least 1 class definition inheritance. Since class B is mentioned
c) At least 3 first for inheritance, its constructor will be called first.
d) Exactly 3 7. Why does diamond problem arise due to multiple
inheritance?
Answer: c a) Methods with same name creates ambiguity and
Explanation: For the implementation of multiple conflict
inheritance, t . must be at least 3 classes in a program. b) Methods inherited from the superclass may conflict
At least 2 base classes and one class to inherit those c) Derived class gets overloaded with more than two
two classes. If lesser, it becomes single level class methods
inheritance. d) Derived class can’t distinguish the owner class of
4. Which programming language restricts the use of any derived method
multiple inheritance?
a) C++ Answer: a
b) PHP Explanation: All the derived classes can distinguish the
c) SmallTalk base class members, but if a method is being inherited
d) Java to the base classes from another class which again gets
inherited into same class (diamond shape), that may
Answer: d create conflict in using the function from two available.
Explanation: Java doesn’t allow use of multiple 8. How many base classes can a derived class have
inheritance with classes. But this can be done by using which is implementing multiple inheritance?
the interfaces. This is more secure and unambiguous a) Only 2
way to implement multiple inheritance. b) At least 2
5. Is it possible to have all the abstract classes as base c) At most 2
classes of a derived class from those? d) As many as required
a) Yes, always
b) Yes, only if derived class implements all the Answer: d
methods Explanation: The classes can derive from as many
c) No, because abstract classes doesn’t have classes as required since the multiple inheritance
constructors feature is made to combine or group together the
d) No, never functions that are from different classes. This make the
derived class stronger in terms of its flexibility.
Answer: b 9. How to overcome diamond problem ?
Explanation: The condition for abstract class applies a) Using alias name
same . too. All the undefined functions must be b) Using seperate derived class
defined. Hence all the base classes can be abstract but c) Using virtual keyword with same name function
derived class must implement all those undefined d) Can’t be done
functions.
6. If class A inherits class B and class C as “class A: Answer: c
public class B, public class C {// class body ;}; ”, Explanation: To overcome the ambiguity and conflict
which class constructor will be called first? we can use keyword virtual. This will help us to
a) Class A differentiate the functions with same name that came to
b) Class B last derived class in diamond problem.
c) Class C 10. When multiple inheritance is used, which class
d) All together object should be used in order to access all the
available members of parent and derived class ?
Answer: b a) Derived class object
Explanation: The constructors of parent class will be b) Parent class objects
called first. In that, the constructor of the classes will c) Use Abstract derived class
be called in the same sequence as that mentioned in d) Derive a class from derived class

80
Answer: a d) All the members of base
Explanation: The derived class object can access all of
its own members. It can also access the available Answer: a
members of the parent classes, because the members Explanation: The private member’s are available for
are derived into the derived class. only the class containing those members. Derived
11. If all the members of all the base classes are private classes will have access to protected and public
then, members only.
a) T . won’t be any use of multiple inheritance 15. Can the derived class be made abstract if multiple
b) It will make those members public inheritance is used ?
c) Derived class can still access them in multiple a) No, because other classes must be abstract too
inheritance b) Yes, if all the functions are implemented
d) Compile time error c) Yes, if all the methods are predefined
d) No, since constructors won’t be t .
Answer: a
Explanation: The derived class will not be able to Answer: d
access any members of the base classes. Since private Explanation: The derived class must not be abstract.
member’s are not inheritable. It leads to no use of This is because the abstract classes doesn’t have
multiple inheritance. constructor and hence we won’t be having capability to
12. Is it compulsory to have constructor for all the have instances. This will restrict use of multiple
classes involved in multiple inheritance? inheritance.
a) Yes, always .
b) Yes, only if no abstract class is involved .
c) No, only classes being used should have a
constructor Hierarchical Inheritance -
d) No, they must not contain constructors
“Hierarchical Inheritance”.
Answer: b 1. Which among the following is best to define
Explanation: The constructors must be defined in every hierarchical inheritance?
class. If class is abstract, it won’t have any constructor a) More than one classes being derived from one class
but other classes must have constructor. Either implicit b) More than 2 classes being derived from single base
or explicit. class
13. If a class contains 2 nested class and is being c) At most 2 classes being derived from single base
inherited by another class, will t . be any multiple class
inheritance? d) At most 1 class derived from another class
a) No, only single level inheritance is used
b) No, only multilevel inheritance is used Answer: a
c) Yes, because 3 classes are involved Explanation: When two or more classes get derived
d) Yes, because more than 1 classes are being derived from a single base class, it is known as hierarchical
inheritance. This gives us freedom to use same code
Answer: a with different scopes and flexibility into different
Explanation: When a class having nested classes is classes.
being derived into another class. It indirectly means a 2. Do members of base class gets divided among all of
simple class is being inherited to another class. This is its child classes ?
single level inheritance. a) Yes, equally
14. Which members can’t be accessed in derived class b) Yes, depending on type of inheritance
in multiple inheritance ? c) No, it’s doesn’t get divided
a) Private members of base d) No, it may or may not get divided
b) Public members of base
c) Protected members of base Answer: c
Explanation: The class members doesn’t get divided
81
among the child classes. All the members get derived private in derived classes. Hence none of the members
to each of the subclasses as whole. The only restriction of base class will be available for further inheritance.
is from the access specifiers used. 7. Which class uses hierarchical inheritance in
3. Each class can inherit the base class following code?
________________ class A
a) Independently using any inheritance {
b) Independently with private inheritance only int a;
c) With same type of inheritance };
d) With each class using different inheritance only class B:class A
{
Answer: a int b;
Explanation: The classes can inherit the base class };
using any type of inheritance. T . is no mandatory class C:class A,class B
condition to use same private,public or protected {
inheritance only. int c;
4. How many classes must be t . to implement };
hierarchical inheritance ? class D:class A
a) Exactly 3 {
b) At least 3 int d;
c) At most 3 };
d) At least 1 a) Class A,B,C
b) Class B,C,D
Answer: b c) Class A,C,D
Explanation: At least 3 classes must be t .. Two derived d) Class D,A,B
classes and one base class. This lets us implement two
classes that have common characteristics from base Answer: d
class. Explanation: Class A is base class and B and D are
5. Base class _______________ derived classes. If class C is considered, it shows
a) Can be made abstract hybrid inheritance, involving single level and multiple
b) Can’t be made abstract inheritance.
c) Must be abstract 8. Which among the following is correct for following
d) If made abstract, compile time error code ?
abstract class A
Answer: a {
Explanation: The base class may or may not be public Int a;
declared abstract. It depends on the need of program. If public void disp();
it is made abstract, it can contain undefined functions };
too. In turn, those functions will have to be class B:public A
implemented by each of the derived classes. {
6. Which access specifiers should be used so that all public: void dis()
the derived classes restrict further inheritance of base {
class members? court&lt;&lt;a;
a) Private }
b) Public };
c) Protected class C:private A
d) Any inheritance type can be used {
public void incr()
Answer: a {
Explanation: All the derived classes must use private a++;
inheritance. This will make the members of base class }
82
} Answer: d
void main() Explanation: Hierarchical inheritance is used to make
{ all the inherited classes have some common features
B b.disp(); obtained from a single base class. This allows all the
} classes to maintain a group or to be classified under
a) Compile time error one class.
b) Runtime error 12. Hierarchical inheritance can be a subset of
c) Program runs and o/p is 0 _________________
d) Program runs and o/p is garbage value a) Hybrid inheritance
b) Multiple inheritance
Answer: a c) Single level inheritance
Explanation: The derived class D have not d) Multilevel inheritance
implemented the undefined function. . the main
concept involves hierarchical inheritance with abstract Answer: a
base class. Explanation: When we use hybrid inheritance, it can
9. How many classes can be derived from the base contain any type of inheritance or combination or more
class using hierarchical inheritance? than two types. Hence it may contain Hierarchical
a) As many as required inheritance too, hence it can be subset of hybrid
b) Only 7 inheritance.
c) Only 3 13. Which type of inheritance is most suitable for
d) Up to 127 inheriting Same syllabus into different colleges with
different streams?
Answer: a a) Multiple
Explanation: The number of classes that can be derived b) Single
from a base class doesn’t have any restriction and c) Hierarchical
hence will be able to derive as many classes as d) Multilevel
required. This feature gives more flexibility and code
reusability. Answer: c
10. If one class have derived the base class privately Explanation: When hierarchical inheritance is used, the
then another class can’t derive the base class common syllabus can be adopted into different college
publically. classes w . the same syllabus is applicable. For
a) True changing the syllabus only the details of base class will
b) False have to changed.
14. Which class constructor is called first when an
Answer: b object of derived class is created?
Explanation: The classes are independent and can a) Base class constructor
access the base class and inherit it in whichever way it b) Derived class constructor
is required. The classes can use the base base class c) Firstly created derived class constructor
members privately or publically maintaining the d) Last created derived class constructor
security of data and methods.
11. Which among the following is true ? Answer: a
a) Hierarchical inheritance is subset of multiple Exolanation: The base class must be initialised first
inheritance hence the constructor of base class is called first. This
b) Hierarchical inheritance is strongest inheritance type makes everything ready for the new object being
c) Hierarchical inheritance uses only 2 classes for created.
implementation 15. All the derived classes can access only few
d) Hierarchical inheritance allows inheritance of members of base class that other derived classes can’t
common features to more than one class access at same time, in hierarchical inheritance.
a) True

83
b) False idea, one can’t differentiate whether two type of
inheritance are used. Hence it is multilevel inheritance.
Answer: b 4. Diamond problem includes ____________________
Explanation: The derived classes have full access to all hybrid inheritance
the non private member’s of base class. Every derived a) Hierarchical and Multiple
class have equal access, none of the class can have b) Hierarchical and Hierarchical
special access to specific members of base class in c) Multiple and Multilevel
general cases. d) Single, Hierarchical and Multiple
.
. Answer:a
Explanation: The diamond problem arises when more
Hybrid Inheritance - than one classes are derived from one class and then
those classes are used to derive single clas. Resulting
“Hybrid Inheritance”. in ambiguity of same functions from each class.
1. Which among the following best defines the hybrid 5. If __________________ inheritance is done
inheritance? continuously, it is similar to tree structure.
a) Combination of two or more inheritance types a) Hierarchical
b) Combination of same type of inheritance b) Multiple
c) Inheritance of more than 7 classes c) Multilevel
d) Inheritance involving all the types of inheritance d) Hierarchical and Multiple

Answer: a Answer: a
Explanation: When more than one type of inheritance Explanation: Hierarchical inheritance is deriving more
are used together, it results in new type of inheritance than one classes from a base class, it it is done
which is in general known as hybrid inheritance. This continuously and subsequently, it results forming a tree
may of may not have better capabilities. like structure of classes being linked.
2. How many types of inheritance should be used for 6. Which amongst the following is true for hybrid
hybrid ? inheritance?
a) Only 1 a) Constructor calls are in reverse
b) At east 2 b) Constructor calls are priority based
c) At most two c) Constructor of only derived class is called
d) Always more than 2 d) Constructor calls are usual

Answer: b Answer: d
Explanation: T . must be combination of at least 2 Explanation: The constructors will be called in usual
types of inheritance. The inheritance should be of way. First the parent class Constructor and then the
different type. derived class Constructors. This is done to initialise all
3. If single inheritance is used with class A and B. A is the members properly.
base class. Then class C,D and E w . C is base class 7. Which type of inheritance must be used so that the
and D is derived from C, then E is derived from D. resultant is hybrid?
Class C is made to inherit from class B. Which is the a) Multiple
resultant type ? b) Hierarchical
a) Single level c) Multilevel
b) Multilevel d) None
c) Hybrid
d) Multiple Answer: d
Explanation: The use of any specific type is not
Answer: b necessary. Though the final structure should not be the
Explanation: The statement represents multilevel same, it should represent more than one type of
inheritance. It is not hybrid since looking at complete inheritance if class diagram is drawn.
84
8. The private member’s are made public to all the constructors are called.
classes in inheritance. a) Few
a) True b) All
b) False c) Only parent and parent
d) Base and Derived
Answer: b
Explanation: The private member’s scope can’t be Answer: c
changed and those can never be accessed in other Explanation: When derived class object is created, all
classes. Only the class containing private member’s of its successor parent classes constructors are called.
can access its own members. Constructor of all the connected classes is not created.
9. If hierarchical inheritance requires to inherit more Since the parent members have to be initialised, but
than one class to single class, which syntax is correct? ( other derived classes are not needed.
A,B,C are class names ) 13. If hybrid inheritance is used, it mostly shows
a) hierarchical class A: public B, public C _______________ feature of OOP.
b) multiple class A: public B, public C a) Flexibility
c) many class A: public B, public C b) Reusability
d) class A: public B, public C c) Efficiency
d) Code readability
Answer: d
Explanation: The syntax is as same as declaration of Answer: b
other classes. T . is no specific keyword defined for Explanation: The code is reusable in most of the
using hybrid inheritance in programming. Only thing is classes and the data becomes more linked to other
to specify the class name separated by commas. classes. Other features are also exhibited, but the code
10. What is the maximum number of classes allowed in reusability is used the most. Code readability becomes
hybrid inheritance? relatively less. Flexibility increases but it depends on
a) 7 how the hybrid inheritance is used.
b) 127 14. The sequence of destructors being called while
c) 255 using hybrid inheritance is ____________
d) As many as required a) Reverse of constructors being called
b) Reverse of classes being made
Answer: d c) Reverse of objects being created
Explanation: The classes in any type of inheritance can d) Reverse of code calling objects
inherit as many classes as required. The only condition
that may arise is memory management. The classes can Answer: a
inherit most of the features from more than one class. Explanation: The destructors are always called in
11. What is the minimum number of classes to be t . in reverse order of constructors being called always. The
a program implementing hybrid inheritance? type of inheritance doesn’t matter. The only important
a) 2 concept is the sequence of classes being inherited.
b) 3 15. Overloading operators are possible only by using
c) 4 hybrid inheritance.
d) No limit a) True
b) False
Answer: d
Explanation: The answer is no limit. T . is no condition Answer: b
defined for limit of classes that has to be used in Explanation: The overloading concept is not related to
hybrid. Though you must have at least 4 classes so that the types of inheritance being used. Overloading
one set of multiple or hierarchical inheritance is t . and operators can be done without using inheritance. You
one more class to use single level inheritance. don’t even have to use more than one class for operator
12. If object of lowest level class is created ( last overloading.
derived class ), _________________ of its parent class .
85
. Answer: c
Explanation: It is used to achieve runtime
Object Oriented Programming Questions for polymorphism. The functions which are inherited and
Entrance Exams - overridden, so at runtime the correct function is
executed. The correct function call is made from the
This set of Object Oriented Programming . for intended class.
Entrance exams focuses on “Virtual Functions”. 4. Which keyword is used to declare virtual functions?
1. Virtual function is ______ class function which a) virtual
expected to be redefined in ______ class, so that when b) virt
reference is made to derived class object using pointer c) anonymous
then we can call virtual function to execute ________ d) virtually
class definition version.
a) Base, derived, derived Answer: a
b) Derived, Derived, Derived Explanation: The virtual keyword is used to declare
c) Base, derived, base virtual functions. Anonymous keyword is used with
d) Base, base, derived classes and have a different meaning. The virtual
functions are used to call the intended function of
Answer: a derived class.
Explanation: The functions which may give rise to 5. W . the virtual function should be defined?
ambiguity due to inheritance, can be declared virtual. a) Twice in base class
So that whenever derived class object is referred using b) Derived class
pointer or reference to the base class methods, we can c) Base class and derived class
still call the derived class methods using virtual d) Base class
function. Hence this differentiates those methods from
each other. Answer: d
2. What does a virtual function ensure for an object, Explanation: The virtual function should be declared in
among the following? base class. So that when the derived class inherits from
a) Correct method is called, regardless of the class the base class, the functions can be differentiated from
defining it the one in base class and another in derived class.
b) Correct method is called, regardless of the object 6. The resolving of virtual functions is done at
being called ______________
c) Correct method is called, regardless of the type of a) Compile time
reference used for function call b) Interpret time
d) Correct method is called, regardless of the type of c) Runtime
function being called by objects d) Writing source code

Answer: c Answer: c
Explanation: It is property of the virtual function and Explanation: The resolving of virtual functions which
one of their main use. Its use ensure that the correct are to be called is done at run time. The base class and
method is called even though it is been called from the derived classes may contain different definitions
different pointer or references. This also decreases and different variables, so all these things are resolved
chance of mistakes in program. at run time and decided which function is to be called.
3. Virtual functions are mainly used to achieve 7. In which access specifier should a virtual function
_____________ be defined?
a) Compile time polymorphism a) Private
b) Interpreter polymorphism b) Public
c) Runtime polymorphism c) Protected
d) Functions code polymorphism d) Default

86
Answer: b derived class
Explanation: The virtual functions must be defined in
public section of a class. This is to ensure that the Answer: b
virtual function is available everyw . in the program. Explanation: The prototype must be the same. Because
Also to avoid any error while resolving the method. the function is to be overridden in the derived class. If
8. Virtual functions can never be made the function prototype is different in derived class then
_______________ it will not override the base class function and hence
a) Static function virtual function concept won’t work ..
b) Parameterized function 12. The virtual functions must be declared and defined
c) Default argument function in _____________ class and overridden in
d) Zero parameter function ___________ class.
a) Base, base
Answer: a b) Derived, derived
Explanation: The virtual function must not be static. c) Derived, base
Those functions are property of individual objects and d) Base, derived
not of a class as a whole. The functions should not be
made common for all the objects of that class. Answer: d
9. Virtual functions can’t be made friend function of Explanation: The virtual functions must be declared
other classes. and defined in base class. The functions can be
a) True redefined in derived class. If redefined in derived class
b) False then it overrides the base class function definition.
13. It is __________ to redefine the virtual function in
Answer: a derived class?
Explanation: The friend functions can access the a) Necessary
private members also. This may hinder the security of b) Not necessary
class members. This is why the functions should not be c) Not acceptable
made friend functions of other class. d) Good practice
10. Which is must condition for virtual function to
achieve runtime polymorphism? Answer: b
a) Virtual function must be accessed with direct name Explanation: It is not necessary to redefine the virtual
b) Virtual functions must be accessed using base class function in derived class. If not defined, the base class
object function definition is used but if defined, the intended
c) Virtual function must be accessed using pointer or definition is used according to need. It is not about
reference good coding practice as it should be redefined only if
d) Virtual function must be accessed using derived needed.
class object only 14. Which among the following is true?
a) A class may have virtual destructor but not virtual
Answer: c constructor
Explanation: The virtual functions must be called using b) A class may have virtual constructor but not virtual
pointer or reference. This is mandatory so that the destructor
intended function gets executed while resolving the c) A class may have virtual constructor and virtual
method at runtime. The must not be any ambiguity constructor
between the method of parent class and derived class. d) A class may have either virtual destructor or virtual
11. Which among the following is true for virtual constructor
functions?
a) Prototype must be different in base and derived class Answer: a
b) Prototype must be same in base class and derived Explanation: Any class can contain virtual destructor.
class But is not possible to define a virtual constructor. The
c) Prototype must be given only in base class reason behind is that the destructors can be overridden
d) Prototype must have different signature in base and but constructors should not be.
87
15. If virtual function of base class is redefined in virtual function can be defined
derived class then, ________________________ b) Either of those must be defined in base class
a) It must be declared virtual in derived class also c) Different according to definition
b) It may or may not be declared virtual in derived d) Abstract functions are faster
class
c) It can must not be declared virtual in derived class Answer: a
d) It must be declared normally in derived class Explanation: The abstract functions are only declared
in base class. Derived classes have to implement those
Answer: b functions in order to inherit that base class. The
Explanation: The virtual functions may or may not be functions are always defined in derived classes only.
declared virtual in derived class. This is because if the 4. Which among the following is correct?
overriding function defined in derived class is not a) Abstract functions should not be defined in all the
declared virtual explicitly, the compiler makes it derived classes
virtual implicitly. b) Abstract functions should be defined only in one
. derived class
To practice all areas of Object Oriented Programming c) Abstract functions must be defined in base class
for Entrance exams, . d) Abstract functions must be defined in all the derived
classes
Abstract Function -
Answer: d
” Abstract Function”. Explanation: The abstract function are only declared in
1. Which among the following best defines the abstract base classes and then has to be defined in all the
methods? derived classes. This allows all the derived classes to
a) Functions declared and defined in base class define own definition of any function whose
b) Functions only declared in base class declaration in base class might be common to all the
c) Function which may or may not be defined in base other derived classes.
class 5. It is _________________________ to define the
d) Function which must be declared in derived class abstract functions.
a) Mandatory for all the classes in program
Answer: b b) Necessary for all the base classes
Explanation: The abstract functions must only be c) Necessary for all the derived classes
declared in base class. Their definitions are provided d) Not mandatory for all the derived classes
by the derived classes. It is a mandatory condition.
2. Which among the following is true? Answer: c
a) The abstract functions must be only declared in Explanation: The derived classes must define the
derived classes abstract function of base class in their own body. This
b) The abstract functions must not be defined in is a necessary condition. Because the abstract functions
derived classes doesn’t contain any definition in base class and hence
c) The abstract functions must be defined in base and becomes mandatory for the derived class to define
derived class them. All the functions in a program must have some
d) The abstract functions must be defined either in base definition.
or derived class 6. The abstract function definitions in derived classes is
enforced at _________
Answer: a a) Runtime
Explanation: The abstract functions can’t be defined in b) Compile time
base class. They are to be defined in derived classes. It c) Writing code time
is a rule for abstract functions. d) Interpreting time
3. How are abstract functions different from the
abstract functions? Answer: b
a) Abstract must not be defined in base class w .as Explanation: When the program is compiled, these
88
definitions are checked if properly defined. This static in a program. If those are made static then the
compiler also ensure that the function is being defined function will be a property of class rather than each
by all the derived classes. Hence we get a compile time object. In turn ever object or derived class must use the
error if not done. common definition given in the base class. But abstract
7. What is this feature of enforcing definitions of functions can’t be defined in the base class. Hence not
abstract function at compile time called? possible.
a) Static polymorphism 11. Which among the following is true?
b) Polymorphism a) Abstract methods can be static
c) Dynamic polymorphism b) Abstract methods can be defined in derived class
d) Static or dynamic according to need c) Abstract methods must not be static
d) Abstract methods can be made static in derived class
Answer: c
Explanation: The feature is known as Dynamic Answer: c
polymorphism. Because the definitions are resolved at Explanation: The abstract methods can never be made
runtime. Even though the definitions are checked at static. Even if it is in derived class, it can’t be made
compile time, they are resolved at runtime only. static. If this happens, then all the subsequent sub
8. What is the syntax for using abstract method? classes will have a common definition of abstract
a) <access-modifier>abstract<return- function which is not desirable.
type>method_name (parameter) 12. Which among the following is correct for abstract
b) abs<return-type>method name (parameter) methods?
c) <access-modifier>abstract return-type method name a) It must have different prototype in the derived class
(parameter) b) It must have same prototype in both base and
d) <access-modifier>abstract <returning> method derived class
name (parameter) c) It must have different signature in derived class
d) It must have same return type only
Answer: a
Explanation: The syntax must firstly contain the access Answer: b
modifier. Then the keyword abstract is written to Explanation: The prototype must be the same. This is
mention clearly to the compiler that it is an abstract to override the function declared as abstract in base
method. Then prototype of the function with return class. Or else it will not be possible to override the
type, function name and parameters. abstract function of base class and hence we get a
9. If a function declared as abstract in base class compile time error.
doesn’t have to be defined in derived class then ______ 13. If a class have all the abstract methods the class
a) Derived class must define the function anyhow will be known as ___________
b) Derived class should be made abstract class a) Abstract class
c) Derived class should not derive from that base class b) Anonymous class
d) Derived class should not use that function c) Base class
d) Derived class
Answer: b
Explanation: If the function that is not to be defined in Answer: a
derived class but is declared as abstract in base class Explanation: The classes containing all the abstract
then the derived class must be made an abstract class. methods are known as abstract classes. And the
This will make the concept mandatory that the derived abstract classes can never have any normal function
class must have one subclass to define that method. with definition. Hence known as abstract class.
10. Static methods can’t be made abstract in java. 14. The abstract methods can never be ___________ in
a) True a base class.
b) False a) Private
b) Protected
Answer: a c) Public
Explanation: The abstract functions can’t be made
89
d) Default function and just a simple syntax is used with the
return type, function name and parameter list then
Answer: a those are known as simple member functions. This is a
Explanation: The base class must not contain the general definition of simple members.
abstract methods. The methods have to be derived and 3. What are static member functions?
defined in derived class. But if it is made private it a) Functions which use only static data member but
can’t be inherited. Hence we can’t declare it as a can’t be accessed directly
private member. b) Functions which uses static and other data members
15. The abstract method definition can be made c) Functions which can be access outside the class with
___________ in derived class. the data members
a) Private d) Functions using only static data and can be accessed
b) Protected directly in main() function
c) Public
d) Private, public, or protected Answer: d
Explanation: The static member functions can be
Answer: d accessed directly in the main function. T . is no
Explanation: The derived class implements the restriction on direct use. We can call them with use of
definition of the abstract methods of base class. Those objects also. But the restriction is that the static
can be made private in derived class if security is member functions can only use the static data members
needed. T . won’t be any problem in declaring it as of the class.
private. 4. How can static member function can be accessed
. directly in main() function?
. a) Dot operator
b) Colon
Object Oriented Programming Problems - c) Scope resolution operator
d) Arrow operator
This set of Object Oriented Programming Problems
focuses on “Types of Member Functions”. Answer: c
1. How many types of member functions are possible Explanation: The static member functions can be
in general? accessed directly in the main() function. The only
a) 2 restriction is that those must use only static data
b) 3 members of the class. These functions are property of
c) 4 class rather than each object.
d) 5 5. Correct syntax to access the static member functions
from the main() function is:
Answer: d a) classObject::functionName();
Explanation: T . are basically 5 types of member b) className::functionName();
functions possible. The types include simple, static, c) className:classObject:functionName();
const, inline, and friend member functions. Any of d) className.classObject:functionName();
these types can be used in a program as per
requirements. Answer: b
2. Simple member functions are Explanation: The syntax in option b must be followed
______________________ in order to call the static functions directly from the
a) Ones defined simply without any type main() function. That is a predefined syntax. Scope
b) Ones defined with keyword simple resolution helps to spot the correct function in the
c) Ones that are implicitly provided correct class.
d) Ones which are defined in all the classes 6. What are const member functions?
a) Functions in which none of the data members can be
Answer: a changed in a program
Explanation: When t . is no type defined for any b) Functions in which only static members can be
90
changed order to declare a const function in a class is as in
c) Functions which treat all the data members as option a. The syntax may vary in different
constant and doesn’t allow changes programming languages.
d) Functions which can change only the static members 10. Which keyword is used to make a nonmember
function as friend function of a class?
Answer: c a) friendly
Explanation: The const member functions are intended b) new
to keep the value of all the data members of a class c) friend
same and doesn’t allow any changes on them. The data d) connect
members are treated as constant data and any
modification inside the const function is restricted. Answer: c
7. Which among the following best describes the inline Explanation: The keyword friend is provided in
member functions? programming languages to use it whenever a functions
a) Functions defined inside the class only is to be made friend of one class or other. The keyword
b) Functions with keyword inline only indicates that the function is capable of new
c) Functions defined outside the class functionalities like accessing private members.
d) Functions defined inside the class or with the 11. Member functions _____________________
keyword inline a) Must be defined inside class body
b) Can be defined inside class body or outside
Answer: d c) Must be defined outside the class body
Explanation: The functions which are defined with the d) Can be defined in another class
keyword inline or are defined inside the class are
treated to be inline functions. Definitions inside the Answer: c
class are implicitly made inline if none of the complex Explanation: The functions definitions can be given
statements are used in the definition. inside or outside the body of class. If defined inside,
8. What are friend member functions (C++)? general syntax is used. If defined outside then the class
a) Member function which can access all the members name followed by scope resolution operator and then
of a class function name must be given for the definition.
b) Member function which can modify any data of a 12. All type of member functions can’t be used inside a
class single class.
c) Member function which doesn’t have access to a) True
private members b) False
d) Non-member functions which have access to all the
members (including private) of a class Answer: b
Explanation: T . is no restriction on the use of type of
Answer: d member functions inside a single class. Any type any
Explanation: A non-member function of a class which number of times can be defined inside a class. The
can access even the private data of a class is a friend member functions can be used as required.
function. It is an exception on access to private 13. Which among the following is true?
members outside the class. It is sometimes considered a) Member functions can never be private
as a member functions since it has all the access that a b) Member functions can never be protected
member function in general have. c) Member functions can never be public
9. What is the syntax of a const member function? d) Member functions can be defined in any access
a) void fun() const {} specifier
b) void fun() constant {}
c) void const fun() {} Answer: d
d) const void fun(){} Explanation: The member functions can be defined
inside any specifier. T . is no restriction. The
Answer: a programmer can apply restrictions on its use by
Explanation: The general syntax to be followed in specifying the access specifier with the functions.
91
14. Which keyword is used to define the static member the left operand
functions? b) Overloaded operator must be member function of
a) static the right operand
b) stop c) Overloaded operator must be member function of
c) open either left or right operand
d) state d) Overloaded operator must not be dependent on the
operands
Answer: a
Explanation: The static keyword is used to declare any Answer: a
static member function in a class. The static members Explanation: The operator to be overloaded must be
become common to each object of the class being made the member function of the operand on left side
created. They share the same values. of expressions to be used. This allows the compiler to
15. Which keyword is used to define the inline member identify whether the overloading has to be used or not.
function? This rule also reduces the ambiguity in code.
a) No keyword required 3. When the operator to be overloaded becomes the left
b) inline operand member then ______________
c) inlined a) The right operand acts as implicit object represented
d) line by *this
b) The left operand acts as implicit object represented
Answer: b by *this
Explanation: The inline keyword is used to defined the c) Either right or left operand acts as implicit object
inline member functions in a class. The functions are represented by *this
implicitly made inline if defined inside the class body, d) *this pointer is not applicable in that member
but only if they doesn’t have any complex statement function
inside. All functions defined outside the class body
must be mentioned with explicit inline keyword. Answer: b
. Explanation: The left operand becomes the object that
To practice all areas of Object Oriented Programming is referred by *this pointer in the member function that
Problems, . will be called while using operator overloading. This is
done to point to a specific object on which the
Object Oriented Programming Aptitude Test - overloading will be applied.
4. If the left operand is pointed by *this pointer, what
This set of Object Oriented Programming . for happens to other operands?
Aptitude test focuses on “Member Operator Function”. a) Other operands are passed as function return type
1. Which among the following are valid ways of b) Other operands are passed to compiler implicitly
overloading the operators? c) Other operands must be passed using another
a) Only using friend function member function
b) Only using member function d) Other operands are passed as function arguments
c) Either member functions or friend functions can be
used Answer: d
d) Operators can’t be overloaded Explanation: The operands that are used during
overloading expect the left operand, can be passed as
Answer: c function arguments. Those are then referred in function
Explanation: The operators can be overloaded by using definition with the names specified in the argument
the member function or even the friend functions can list.
be used. This is because both of these can access all the 5. If a friend overloaded operator have to be changed to
data members of a class. member overloaded operator, which operator should be
2. Which among the following is mandatory condition used with the class name?
for operators overloading? a) Scope resolution operator
a) Overloaded operator must be member function of b) Colon
92
c) Arrow operator 9. If left operand member is specified directly in the
d) Dot operator function definition, which is the correct implicit
conversion of that syntax?
Answer: a a) *this className
Explanation: The scope resolution operator can be used b) *this parameterObject
followed by the class name. Then the operator keyword c) *this returnedObject
with the operator symbol that should be overloaded. d) *this object
This is done to use member function instead of friend
function. Answer: d
6. What is the syntax to overload an operator? Explanation: Since the left operands are passed
a) className::operator(parameters) implicitly, those object members can be accessed
b) className:operator(parameters) directly in the function definition. The compiler
c) className.operator(paramteres) converts the syntax into the syntax that can be
d) className->operator(parameters) processed. The implicitly converted syntax contains
*this pointer followed by the objectName that is left
Answer: a operand in the expression.
Explanation: The class name is followed by the scope 10. When the friend operator overloading is converted
resolution operator. This is done to specify the class to into member operator overloading _______________
which the function should belong to. Then the keyword a) Two parameters of friend function remains same
operator should be used in order to indicate the parameters in member operator overloading
operator that is to be overloaded. Then come the b) Two parameters of friend function becomes only
parameters list to specify other operands. one parameter of member function
7. Why the left parameter is removed from parameter c) Two parameters of friend function are removed
list? while using member function
a) Because it is of no use d) Two parameters of friend function are made 4 in
b) Because it is never used in definitions member operator overloading
c) Because it becomes parameter pointed by *this
d) Because it can’t be referred by *this pointer Answer: b
Explanation: The friend function would accept two
Answer: c arguments if some binary operator is overloaded.
Explanation: The left object is removed from being When we try to convert that definition to member
passed as a parameter, because it is implicitly passed. It operator overloading then it becomes only one
is passed implicitly because it is considered the object parameter. The reason behind is that the left operand is
with respect to which the overloading function is being passed implicitly while using the member functions.
called. 11. W . in the parameter list is the implicit *this is
8. Which object’s members can be called directly while added?
overloading operator function is used (In function a) Right most parameter
definition)? b) Anyw . in parameter list
a) Left operand members c) Left most parameter
b) Right operand members d) Not added to parameter list
c) All operand members
d) None of the members Answer: c
Explanation: The left operand is passed implicitly by
Answer: a the compiler to the member function. But this is done,
Explanation: This is because the left operand is passed when the compiler adds the calling object as *this to
implicitly. It is pointed by *this. This in turn means we the parameter list. It is always added as the left most
can use the direct member names of the object because parameter, i.e. the first parameter of the function.
those are again converted to a syntax containing *this 12. Which operator among the following can be
pointer implicitly. overloading using only member function?
a) Assignment operator
93
b) Addition operator some other class and the right operand is usually of
c) Subtraction operator some different type.
d) Multiplication and division operator .
To practice all areas of Object Oriented Programming
Answer: a for Aptitude test, .
Explanation: Only the assignment operator among the
options given must be overloaded using the member Object Oriented Programming . for Freshers -
functions. The assignment operator can’t be overloaded
using friend function. This is a restriction in the This set of Object Oriented Programming . for
programming languages to make the programs more Freshers focuses on “Overloading Member Functions”.
resistant towards errors. 1. What does memory allocation for objects mean?
13. Which operator among the following can be a) Actual creation and memory allocation for object
overloaded using both friend function and member members
function? b) Creation of member functions
a) Assignment operator c) Creation of data members for a class
b) Subscript d) Actual creation and data declaration for object
c) Member selection (arrow operator) members
d) Modulus operator
Answer: a
Answer: d Explanation: The memory allocated for the object
Explanation: Only the modulus operator among the members indicates actual creation of the object
given operators can be overloaded using either friend members. This is known as memory allocation for
function or member function. Other operators must be object.
overloaded using only the member functions. 2. W . is the memory allocated for the objects?
14. All the operators can be overloaded using the a) HDD
member function operator overloading. b) Cache
a) True c) RAM
b) False d) ROM

Answer: b Answer: c
Explanation: It is not the case that all the operators can Explanation: The memory for the objects or any other
be overloaded using the member operator overloading. data is allocated in RAM initially. This is while we run
T . are some cases w . the operators must be a program all the memory allocation takes place in
overloaded using the friend function only. The reason some RAM segments. Arrays in heap and local
behind is that the left operand should be passed *this members in stack etc.
pointer, but the left operand in these cases might be 3. When is the memory allocated for an object?
object of some other class. Hence can’t be done. a) At declaration of object
15. Which operator among the following must be b) At compile time
overloaded using the friend function? c) When object constructor is called
a) << operator only d) When object is initialized to another object
b) >> operator only
c) Both << and >> operators Answer: c
d) It’s not mandatory to use friend function in any case Explanation: The object memory allocation takes place
when the object constructor is called. Declaration of an
Answer: c object doesn’t mean that memory is allocated for its
Explanation: In some cases it is mandatory to use the members. If object is initialized with another object, it
friend functions for overloading the operators. . both may just get a reference to the previously created
the << and >> operators must be overloaded using object.
friend function because the left operand is object of 4. Using new is type safe as
_______________________
94
a) It require to be specified with type of data an array of objects in java. The syntax must be
b) It doesn’t require to be specified with type of data specified with an object pointer which is assigned with
c) It requires the name of data a memory space containing the required number of
d) It allocated memory for the data object space. Even initialization can be done directly.
8. When is the memory allocated for an object gets
Answer: b free?
Explanation: The new is type safe because we don’t a) At termination of program
have to specify the type of data that have to be b) When object goes out of scope
allocated with memory. We can directly use it with c) When main function ends
data name. Name of the data doesn’t matter though for d) When system restarts
type of memory allocation though.
5. Which of the following function can be used for Answer: b
dynamic memory allocation of objects? Explanation: Whenever an object goes out of scope,
a) malloc() the deletion of allocation memory takes place. Actually
b) calloc() the data is not deleted, instead the memory space is
c) create() flagged to be free for further use. Hence whenever an
d) both malloc() and calloc() object goes out of scope the object members become
useless and hence memory is set free.
Answer: d 9. Which among the following keyword can be used to
Explanation: The malloc() function can be used to free the allocated memory for an object?
allocate dynamic memory for objects. Function calloc() a) delete
can also be use. These functions differ in the way they b) free
allocate memory for objects. c) either delete or free
6. How much memory will be allocated for an object of d) only delete
class given below?
class Test{ Answer: c
int mark1; Explanation: The memory allocated for an object is
int mark2; usually automatically made free. But if explicitly
float avg; memory has to be made free then we can use either
char name[10]; free or delete keywords depending on programming
}; languages.
a) 22 Bytes 10. Which function is called whenever an object goes
b) 24 Bytes out of scope?
c) 20 Bytes a) Destructor function
d) 18 Bytes b) Constructor function
c) Delete function
Answer: a d) Free function
Explanation: The size of an object of the class given in
question will be of size 22 bytes. This is because the Answer: a
size of an object is always equal to the sum of sizes of Explanation: The destructor function of the class is
the data members of the class, except static members. called whenever an object goes out of scope. This is
7. Which keyword among the following can be used to because the destructor set all the resources, acquired by
declare an array of objects in java? the object, free. This is an implicit work of compiler.
a) new 11. Which operator can be used to check the size of an
b) create object?
c) allocate a) sizeof(objectName)
d) arr b) size(objectName)
c) sizeofobject(objectName)
Answer: a d) sizedobject(objectName)
Explanation: The keyword new can be used to declare
95
Answer: a b) delete
Explanation: The sizeof operator is used to get the size c) Unallocate
of an already created object. This operator must d) Collect
constail keyword sizeof(objectName). The output will
give the number of bytes acquired by a single object of Answer: b
some class. Explanation: The delete operator in C++ can be used to
12. The memory allocated for an object free the memory and resources held by an object. The
____________________ function can be called explicitly whenever required. In
a) Can be only dynamic C++ memory management must be done by the
b) Can be only static programmer. T . is no automatic memory management
c) Can be static or dynamic in C++.
d) Can’t be done using dynamic functions .
To practice all areas of Object Oriented Programming
Answer: c for Freshers, .
Explanation: The memory allocation for an object can
be static or dynamic. The static memory allocation is Object Oriented Programming Interview . for
when an object is declared directly without using any Freshers -
function usually. And dynamic allocation is when we
use some dynamic allocation function to allocate This set of Object Oriented Programming Interview .
memory for data member of an object. for freshers focuses on “Overriding Member
13. If an object is declared in a user defined function Functions”.
__________________ 1. Which among the following best describes member
a) Its memory is allocated in stack function overriding?
b) Its memory is allocated in heap a) Member functions having same name in base and
c) Its memory is allocated in HDD derived classes
d) Its memory is allocated in cache b) Member functions having same name in base class
only
Answer: a c) Member functions having same name in derived
Explanation: The memory for any data or object that class only
are used in a user defined function are always allocated d) Member functions having same name and different
in the stack. This is to ensure that the object is signature inside main function
destroyed as soon as the function is returned. Also this
ensures that the correct memory allocation and Answer: a
destruction is performed. Explanation: The member function which is defined in
14. In java, ____________________ takes care of base class and again in the derived class, is overridden
managing memory for objects dynamically. by the definition given in the derived class. This is
a) Free collector because the preference is given more to the local
b) Dust collector members. When derived class object calls that
c) Memory manager function, definition from the derived class is used.
d) Garbage collector 2. Which among the following is true?
a) Inheritance must not be using when overriding is
Answer: d used
Explanation: The garbage collector in java takes care b) Overriding can be implemented without using
of the memory allocations and their deletions inheritance
dynamically. When an object is no more required then c) Inheritance must be done, to use overriding are
the garbage collector deletes the object and free up all overridden
the resources that were held by that object. d) Inheritance is mandatory only if more than one
15. Which operator can be used to free the memory functions
allocated for an object in C++?
a) Free()
96
Answer: c class B
Explanation: The inheritance must be used in order to {
use function overriding. If inheritance is not used, the int j;
functions can only be overloaded. T . must be a base void show()
class and a derived class to override the function of {
base class. cout&lt;&lt;j;
3. Which is the correct condition for function }
overriding? };
a) The declaration must not be same in base and a) show()
derived class b) print()
b) The declaration must be exactly the same in base c) show() and print()
and derived class d) Compile time error
c) The declaration should have at least 1 same
argument in declaration of base and derived class Answer: a
d) The declaration should have at least 1 different Explanation: The declaration must be exactly same in
argument in declaration of base and derived class the derived class and base class. The derived class have
defined show() function with exactly same declaration.
Answer: b This then shows that the function in base class is being
Explanation: For a function to be over ridden, the overridden if show() is called from object of class B.
declaration must be exactly the same. T . must not be 6. How to access the overridden method of base class
any different syntax used. This will ensure that the from the derived class?
function to be overridden is only the one intended from a) Using arrow operator
to be overridden from the derived class. b) Using dot operator
4. Exactly same declaration in base and derived class c) Using scope resolution operator
includes______________ d) Can’t be accessed once overridden
a) Only same name
b) Only same return type and name Answer: c
c) Only same return type and argument list Explanation: Scope resolution operator :: can be used
d) All the same return type, name and parameter list to access the base class method even if overridden. To
access those, first base class name should be written
Answer: d followed by the scope resolution operator and then the
Explanation: Declaration includes the whole prototype method name.
of the function. The return type name and the 7. The functions to be overridden _____________
parameter list must be same in order to confirm that the a) Must be private in base class
function is same in derived and the base class. And b) Must not be private base class
hence can be overridden. c) Must be private in both derived and base class
5. Which among function will be overridden from the d) Must not be private in both derived and base class
function defined in derived class below:
class A Answer: b
{ Explanation: If the function is private in the base class,
int i; derived class won’t be able to access it. When the
void show() derived class can’t access the function to be overridden
{ then it won’t be able to override it with any definition.
cout&lt;&lt;i; 8. Which language doesn’t support method overriding
} implicitly?
void print() a) C++
{ b) C#
cout &lt;&lt;i; c) Java
} d) SmallTalk
};
97
Answer: b d) Abstract
Explanation: The feature of method overriding is not
provided in C#. To override the methods, one must use Answer: b
override or virtual keywords explicitly. This is done to Explanation: The function to be overridden must be
remove accidental changes in program and open. This is a condition in Kotlin for any function to
unintentional overriding. be overridden. This avoids accidental overriding.
9. In C# ____________________ 13. Abstract functions of a base class
a) Non- virtual or static methods can’t be overridden _________________
b) Non- virtual and static methods only can be a) Are overridden by the definition in same class
overridden b) Are overridden by the definition in parent class
c) Overriding is not allowed c) Are not overridden generally
d) Overriding must be implemented using C++ code d) Are overridden by the definition in derived class
only
Answer: d
Answer: a Explanation: The functions declared to be abstract in
Explanation: The non-virtual and static methods can’t base class are redefined in derived classes. That is, the
be overridden in C# language. The restriction is made functions are overridden by the definitions given in the
from the language implicitly. Only the methods that are derived classes. This must be done to give at least one
abstract, virtual or override can be overridden. definition to each undefined function.
10. In Delphi ______________ 14. If virtual functions are defined in the base class
a) Method overriding is done implicitly then _______________
b) Method overriding is not supported a) It is not necessary for derived classes to override
c) Method overriding is done with directive override those functions
d) Method overriding is done with the directive b) It is necessary for derived classes to override those
virtually functions
c) Those functions can never be derived
Answer: c d) Those functions must be overridden by all the
Explanation: This is possible but only if the method to derived classes
be overridden is marked as dynamic or virtual. It is
inbuilt restriction of programming language. This is Answer: a
done to reduce the accidental or unintentional Explanation: The derived classes doesn’t have to
overriding. redefine and override the base class functions. If one
11. What should be used to call the base class method definition is already given it is not mandatory for any
from the derived class if function overriding is used in derived class to override those functions. The base
Java? class definition will be used.
a) Keyword super 15. Which feature of OOP is exhibited by the function
b) Scope resolution overriding?
c) Dot operator a) Inheritance
d) Function name in parenthesis b) Abstraction
c) Polymorphism
Answer: a d) Encapsulation
Explanation: The keyword super must be used to
access base class members. Even when overriding is Answer: c
used, super must be used with the dot operator. The Explanation: The polymorphism feature is exhibited by
overriding is possible. function overriding. Polymorphism is the feature which
12. In Kotlin, the function to be overridden must be basically defines that same named functions can have
______________ more than one functionalities.
a) Private .
b) Open To practice all areas of Object Oriented Programming
c) Closed for Interviews, .
98
b) Can be called only from const object
Object Oriented Programming Quiz - c) Can be called both by const and non-const object
d) Can’t be called with object
This set of Object Oriented Programming Quiz focuses
on “Constant Member Functions”. Answer: a
1. What are constant member functions? Explanation: The non-const functions are able to
a) Functions which doesn’t change value of calling modify the values of object which called the function.
object So only the non-const functions can be called. If const
b) Functions which doesn’t change value of any object object is used then the compiler produces an error as
inside definition the const object is being given to a function which can
c) Functions which doesn’t allow modification of any modify its values.
object of class 5. Which is a correct condition on const member
d) Functions which doesn’t allow modification of functions?
argument objects a) Const member functions can’t call non-const
member functions
Answer: a b) Const member functions can’t call any other
Explanation: The constant member functions are function
special type of member functions. These are intended c) Const member functions can call only the functions
to restrict any modification in to the values of object which are neither const nor non-const
which is used to invoke that function. This is done to d) Const member functions can call only data members
ensure that t . are no accidental modifications to the of call not member functions
object.
2. Which keyword must be used to declare a member Answer: a
function as a constant member function? Explanation: The const member functions are restricted
a) Constant to call any other non-const member functions. This is
b) Const to ensure that the const function doesn’t have any code
c) FunctionConst that might modify the calling object.
d) Unchanged 6. If a const object calls a non-const member function
then ____________________
Answer: b a) Run time error may get produced
Explanation: The keyword const is provided in most of b) Compile time error may get produced
the programming languages. This indicates that the c) Either compile time or run time error is produced
member on which it is specified, remains constant with d) The program can’t be compiled
the respective values of members. The keyword must
be mentioned so as to declare a member function to be Answer: b
constant. Explanation: The program gets compiled but produces
3. Which objects can call the const functions? an error. The error is produced because a constant
a) Only const objects value is being changed. Even if t . is no code that can
b) Only non-const objects change any object value, but non-const member
c) Both const and non-const objects functions are assumed to change the values.
d) Neither const not non-const objects 7. Can a constructor function be constant?
a) Yes, always
Answer: c b) Yes, only if permissions are given
Explanation: All the objects of a class can call const c) No, because objects are not involved
functions for its use. Const objects can call the const d) No, never
functions to since those values are already constant.
And the non- const objects can call the const functions Answer: d
to keep their values constant. Explanation: The constructors can’t be made const.
4. Non-const functions _______________________ This is to ensure that the constructor is capable of
a) Can be called only from non-const object initializing the values to the members of the object. If it
99
is made constant then it won’t be able to initialize any given after the declaration of function and before
data member values. definition.
8. A function can have both the const and non-const 12. Functions which differ in const-ness are considered
version in the same program. ______________________
a) True a) To have same signature
b) False b) To have different signature
c) To produce compile time error
Answer: a d) To produce runtime error
Explanation: The functions in a program can be made
both const and non-const. This feature is made Answer: b
available to make programming more flexible. This Explanation: The functions are considered to have
ensures the security too as we can call const function different signature. This is because the const-ness also
whenever required. defines the type of function or the working of
9. How is it possible to have both const and non-const functions. And hence the functions can be considered
version of a function? different. This is the reason that we can use function
a) Function overriding overloading for const and non-const version of same
b) Function prototyping function.
c) Function overloading 13. If const version of a function when overloading is
d) Function declaring used, the function ___________________
a) Returns reference to object
Answer: c b) Returns volatile reference
Explanation: The functions can be declared const and c) Returns mutable reference
non-const in the same program. The technique used is d) Returns const reference
function overloading. We can define a const function
and then a non-const version of same function using Answer: d
overloading. Explanation: The function returns a const reference.
10. When both the const and non-const version of a This is to ensure that the value of object calling the
functions are required? function is not modified. This is a security feature.
a) Return value have to be different in const 14. Which among the following is recommended for
b) Return value have to be same in const const functions?
c) Return values have to be ignored a) Const function use should be reduced in a program
d) Return values have to be suppressed b) Const function use should be more in a program
c) Const function use should not matter in a program
Answer: a d) Const function use should be able to modify the
Explanation: The return values can help to overload the values
functions. Also this will allow us to use a non-const
function to be called inside both the const and non- Answer: b
const version of functions. Explanation: The const member functions should be
11. If a function is to be made const, which is the used more in a program. The reason behind is to ensure
correct syntax? t . is no accidental modification of data of object. Also
a) const functionName(parameters); to ensure any unintended modification which may
b) const returnType functionName(parameters); result in unexpected termination of program.
c) const functionName(returnType)(Parameters); 15. Use of const member function in a program
d) const (functionName(parameters)); _________________________
a) Is mandatory, always
Answer: b b) Is optional, always
Explanation: The function declaration must contain the c) Is mandatory, if objects are used
keyword const. The const keyword makes the function d) Is optional, if const objects are used
const type. The usual function declaration can be given
followed by the keyword. The keyword const can be
100
Answer: b Answer: a
Explanation: The use of const member functions is not Explanation: The private member functions can never
mandatory. If const objects are involved then t . is a be accessed in the derived classes. The access
high use of const member functions too. But t . is no specifiers is of maximum security that allows only the
mandatory condition. members of self class to access the private member
. functions.
To practice all areas of Object Oriented Programming 4. Which syntax among the following shows that a
for Quizzes, . member is private in a class?
a) private: functionName(parameters)
Object Oriented Programming - b) private(functionName(parameters))
c) private functionName(parameters)
This set of Object Oriented Programming .Questions d) private::functionName(parameters)
& Answers focuses on “Private Member Functions”.
1. Which is private member functions access scope? Answer: c
a) Member functions which can only be used within Explanation: The function declaration must contain
the class private keyword follower by the return type and
b) Member functions which can used outside the class function name. Private keyword is followed by normal
c) Member functions which are accessible in derived function declaration.
class 5. If private member functions are to be declared in C+
d) Member functions which can’t be accessed inside + then _____________
the class a) private:
b) private
Answer: a c) private(private member list)
Explanation: The member functions can be accessed d) private :- <private members>
inside the class only if they are private. The access is
scope is limited to ensure the security of the private Answer: a
members and their usage. Explanation: The private members doesn’t have to
2. Which among the following is true? have the keyword with each private member. We only
a) The private members can’t be accessed by public have to specify the keyword private followed by single
members of the class colon and then private member’s are listed.
b) The private members can be accessed by public 6. In java, which rule must be followed?
members of the class a) Keyword private preceding list of private member’s
c) The private members can be accessed only by the b) Keyword private with a colon before list of private
private members of the class member’s
d) The private members can’t be accessed by the c) Keyword private with arrow before each private
protected members of the class member
d) Keyword private preceding each private member
Answer: b
Explanation: The private members are accessible Answer: d
within the class. T . is no restriction on use of private Explanation: The private keyword must be mentioned
members by public or protected members. All the before each private member. Unlike the rule in C++ to
members can access the private member functions of specify private once and list all other private
the class. member’s, in java all member declarations must be
3. Which member can never be accessed by inherited preceded by the keyword private.
classes? 7. How many private member functions are allowed in
a) Private member function a class ?
b) Public member function a) Only 1
c) Protected member function b) Only 7
d) All can be accessed c) Only 255

101
d) As many as required private function
d) Not possible
Answer: d
Explanation: T . are no conditions applied on the Answer: a
number of private member functions that can be Explanation: The private member functions can be
declared in a class. Though the system may restrict use accessed within the class. A public member function
of too many functions depending on memory. can be called which in turn calls the private member
8. How to access a private member function of a class? function. This maintains the security and ad .s to the
a) Using object of class rules of private members.
b) Using object pointer 12. A private function of a derived class can be
c) Using address of member function accessed by the parent class.
d) Using class address a) True
b) False
Answer: c
Explanation: Even the private member functions can be Answer: b
called outside the class. This is possible if address of Explanation: If private functions get accessed even by
the function is known. We can use the address to call the parent class that will violate the rules of private
the function outside the class. members. If the functions can be accessed then the
9. Private member functions ____________ derived class security is hindered.
a) Can’t be called from enclosing class 13. Which error will be produced if private members
b) Can be accessed from enclosing class are accessed?
c) Can be accessed only if nested class is private a) Can’t access private message
d) Can be accessed only if nested class is public b) Code unreachable
c) Core dumped
Answer: a d) Bad code
Explanation: The nested class members can’t be
accessed in the enclosed class even though other Answer: a
members can be accessed. This is to ensure the class Explanation: The private members access from outside
members security and not to go against the rules of the class produce an error. The error states that the
private members. code at some line can’t access the private members.
10. Which function among the following can’t be And denies the access terminating the program.
accessed outside the class in java in same package? 14. Can main() function be made private?
a) public void show() a) Yes, always
b) void show() b) Yes, if program doesn’t contain any classes
c) protected show() c) No, because main function is user defined
d) static void show() d) No, never

Answer: c Answer: d
Explanation: The protected members are available Explanation: The reason given in c option is wrong.
within the class. And are also available in derived The proper reason that the main function should not be
classes. But these members are treated as private private is that it should be accessible in whole
members for outside the class and inheritance program. This makes the program flexible.
structure. Hence can’t be accessed. 15. If a function in java is declared private then it
11. If private members are to be called outside the __________________
class, which is a good alternative? a) Can’t access the standard output
a) Call a public member function which calls private b) Can access the standard output
function c) Can’t access any output stream
b) Call a private member function which calls private d) Can access only the output streams
function
c) Call a protected member function which calls
102
Answer: b d) All member functions
Explanation: The private members can access any
standard input or output. T . is no restriction on access Answer: c
to any input or output stream. And since standard input Explanation: The public member functions gets into
can also be used hence only accessing the output the same specifier in which the inheritance is done. If
stream is not true. protected members are involved in public inheritance,
. still those remain protected in the derived class but
To practice all areas of Object Oriented public members become public on public inheritance
Programming, . and protected in protected inheritance.
4. Which syntax among the following is correct for
Public Member Functions - public member functions?
a) public::void functionName(parameters)
“Public Member Functions”. b) public void functionName(parameters)
1. What are public member functions? c) public(void functionName(parameters))
a) Functions accessible outside the class but not in d) public:-void functionName(Parameters)
derived class
b) Functions accessible outside the class directly Answer: b
c) Functions accessible everyw . using object of class Explanation: The public member functions declaration
d) Functions that can’t be accessed outside the class must be mentioned with the keyword public. The
syntax given is used in java. Keyword public is
Answer: c followed by the usual function declaration.
Explanation: The most suitable definition would be 5. Which syntax is applicable to declare public member
that public member functions are accessible everyw . functions in C++?
using object of the class. If derived classes are using a) public:
those, derived class object can be used to call those b) public()
functions. c) public void
2. Which among the following is true for public d) public::
member functions?
a) Public member functions doesn’t have a return type Answer: a
b) Public member functions doesn’t have any security Explanation: The syntax in C++ must contain the
c) Public member functions are declared outside the public keyword followed by a colon. T .after, all the
class public members can be declared. But in few other
d) Public member functions can be called using object language, public have to be mentioned explicitly with
of class each member.
6. In java, which rule among the following is
Answer: d applicable?
Explanation: The public member functions can be a) Keyword public can’t be preceded with all the
called using object of the class. The members can’t be public members
declared outside the class as those would become non- b) Keyword public must be preceded with all the
member functions of the class. The functions have public members
security as those can be accessed using the class object c) Keyword public must be post mentioned the
only. function declaration
3. Which type of member functions get inherited in the d) Keyword public is not mandatory
same specifier in which the inheritance is done? (If
private inheritance is used, those become private and if Answer: b
public used, those become public) Explanation: The public members in java must be
a) Private member functions preceded with the keyword public. It must be
b) Protected member functions mentioned with each public member, unlike C++ w .
c) Public member functions we mention it only once. In java, each member must
have explicit declaration of specifier type.
103
7. How many public members are allowed in a class? object name should be mentioned with a dot operator
a) Only 1 to access its class members. Then the function name
b) At most 7 with parameters, if required, can be given.
c) Exactly 3 11. If public members are to be restricted from getting
d) As many as required inherited from the subclass of the class containing that
function, which alternative is best?
Answer: d a) Make the function private
Explanation: The number of public members that can b) Use private inheritance
be defined in a class doesn’t have any limit. Though c) Use public inheritance
the programmer should not use too many functions, d) Use protected inheritance
instead should use another class for more specific
functions to reduce the readability complexity. Answer: b
8. Which is not a proper way to access public members Explanation: If private inheritance is used then the
of a class? class containing the function will be able to use the
a) Using object pointer with arrow operator function with rules of whichever specifier is used.
b) Using object of class in main function Then the derived class makes those function the private
c) Using object of class with arrow operator members of itself. This restricts the public members of
d) Using object anyw . in the program parent class from further inheritance.
12. A derived class object can access the public
Answer: c members of the base class.
Explanation: The public members can be accessed a) True
anyw . in the program using the object of the class. b) False
And if object pointer is used, then arrow operator is
used to access class members. If normal object is used Answer: b
with arrow operator, an error will be generated. Explanation: The public members of the base class can
9. Which call is correct for public members of a nested be accessed from the derived class object only if public
class? inheritance is used. If protected or private inheritance
a) Can be called from object of enclosing class is used then those members become public/protected in
b) Can be called within enclosing class only with direct derived class and hence won’t be able to be called from
names object of derived class.
c) Direct names should be used for the nested classes 13. If a class have a public member function and is
d) Only with help of nested class object pointer called directly in the main function then
___________________________
Answer: a a) Undeclared function error will be produced
Explanation: The closest definition is that any public b) Out of memory error is given
member function of the nested class can be accessed c) Program gives warning only
with the help of enclosing class object. The nested d) Program shut down the computer
class object pointer can be used only within the
enclosing class. It’s not mandatory to use the members Answer: a
of nested class only within the enclosing class. Explanation: If the function is called directly without
10. Which public function call among the following is using any object then the compiler doesn’t gets to
correct outside the class, if return type is void (C++)? know that the function have to be called from a
a) object.void functionName(parameters); specific class. And if t . are no global or in-scope
b) object.functionName(parameters); function with same name then the compiler produces
c) object.functionName void (parameters) an error stating that the called function is undeclared.
d) object.void functionName(); 14. The function main() must always be public.
a) True
Answer: b b) False
Explanation: The condition given says that t . is no
return type hence we can call the function directly. The
104
Answer: a error occurred and then retry the process or just
Explanation: The main() function must always be continue the program further.
public. This is because the whole function and the 3. An exception may arise when _______________
operating system that is out of the package have to a) Input is fixed
access the main function throughout the program b) Input is some constant value of program
execution. Hence the main() function should be public c) Input given is invalid
so as to be available everyw . in the program. d) Input is valid
15. All the public member functions
___________________ Answer: c
a) Can’t access the private members of a class Explanation: The exceptions may arise because the
b) Can’t access the protected members of a class input given by the user might not be of the same type
c) Can access only public members of a class that a program can manage. If the input is invalid the
d) Can access all the member of its class program gets terminated.
4. If a file that needs to be opened is not found in the
Answer: d target location then _____________
Explanation: The public member function can access a) Exception will be produced
any private, protected and public member of its class. b) Exceptions are not produced
Not only public member function, any member c) Exception might get produced because of syntax
function of a class can access each and every other d) Exceptions are not produced because of logic
member declared inside the class. Hence are flexible to
program. Answer: a
. Explanation: The exceptions are produced when
. anything unexpected happened. The program might not
be able to find a file in the target location and hence
Exception Handling - program produces an exceptions. The exception
produced, then terminates the program.
“Exception Handling”. 5. Which is the universal exception handler class?
1. What is an exception? a) Object
a) Problem arising during compile time b) Math
b) Problem arising during runtime c) Errors
c) Problem in syntax d) Exceptions
d) Problem in IDE
Answer: d
Answer: b Explanation: Any type of exception can be handled by
Explanation: The problems that might occur during using class Exceptions. An object of this class is
execution of a program are known as exceptions. The created which can manipulate the exception data. The
exceptions are unexpected sometimes and can be data can be used to display the error or to run the
predicted. Also, the exceptions should be always program further based on error produced.
considered for a better program. 6. What are two exception classes in hierarchy of java
2. Why do we need to handle exceptions? exceptions class?
a) To prevent abnormal termination of program a) Runtime exceptions only
b) To encourage exception prone program b) Compile time exceptions only
c) To avoid syntax errors c) Runtime exceptions and other exceptions
d) To save memory d) Other exceptions

Answer: a Answer: c
Explanation: The exceptions should be handled to Explanation: The exceptions class is having two other
prevent any abnormal termination of a program. The derived classes which are of runtime exception handler
program should keep running even if it gets interrupted and for other type of exceptions handling. The runtime
in between. The program should preferable show the exception handler is used to handle the exceptions
105
produced during run time and same with case of other is wrong. The function toString returns string value.
exceptions. Hence the return type must be a String and not a char.
7. Which are the two blocks that are used to check 11. To catch the exceptions ___________________
error and handle the error? a) An object must be created to catch the exception
a) Try and catch b) A variable should be created to catch the exception
b) Trying and catching c) An array should be created to catch all the
c) Do and while exceptions
d) TryDo and Check d) A string have to be created to store the exception

Answer: a Answer: a
Explanation: Two blocks that are used to check for Explanation: The object must be created of a specific
errors and to handle the errors are try and catch block. class of which the error has occurred. If the type of
The code which might produce some exceptions is error is unknown then we can use an object of class
placed inside the try block and then the catch block is Exceptions. This object will be able to handle any kind
written to catch the error that is produced. The error of exception that a program might produce.
message or any other processing can be done in catch 12. Multiple catch blocks __________________
block if the error is produced. a) Are mandatory for each try block
8. T . can be a try block without catch block but vice b) Can be combined into a single catch block
versa is not possible. c) Are not possible for a try block
a) True d) Can never be associated with a single try block
b) False
Answer: b
Answer: a Explanation: The separate catch blocks for a single try
Explanation: The try block may or may not have any block can be combined into a single catch block. All
catch block. But a catch block can’t be t . in a program type of errors can be then handled in s single block.
if t . is no try block. It is like else-block can only be The type still have to be specified for the errors that
written if and only if if-block is present in the program. might be produced.
9. How many catch blocks can a single try block can 13. Which symbol should be used to separate the type
have? of exception handler classes in a single catch block?
a) Only 1 a) ?
b) Only 2 b) ,
c) Maximum 127 c) –
d) As many as required d) |

Answer: d Answer: d
Explanation: T . is no limit on the number of catch Explanation: A pipe symbol can be used to separate
blocks corresponding to a try block. This is because the different type of exceptions. The exceptions should
error can be of any type and for each type, a new catch always be given in proper sequence to ensure that no
block can be defined. This is to make sure all type of code remains unreachable. If not done properly the
exceptions can be handled. code might never be used in a program.
10. Which among the following is not a method of 14. Which class is used to handle the input and output
Throwable class? exceptions?
a) public String getMessage() a) InputOutput
b) public Throwable getCause() b) InputOutputExceptions
c) public Char toString() c) IOExceptions
d) public void printStackTrace() d) ExceptionsIO

Answer: c Answer: c
Explanation: Actually all the functions are available in Explanation: T . is a specific class to handle each type
throwable class. But the return type given in the option of exceptions that might be produced in a program.
106
The input and output exceptions can be handled by an blocks for base and derived classes. It is mandatory to
object of class IOExcceptions. This class handles all write derived class catch block first because the errors
type of input and output exceptions. produced by the derived class must be handled first.
15. Why do we use finally block? 3. Which among the following is true?
a) To execute the block if exception occurred a) If catch block of base class is written first, it is
b) To execute a code when exception is not occurred compile time error
c) To execute a code whenever required b) If catch block of base class is written first, it is run
d) To execute a code with each and every run of time error
program c) If catch block of base class is written first, derived
class catch block can’t be reached
Answer: d d) If catch block of base class is written first, only
Explanation: Sometimes t . is a need to execute a set of derived class catch block is executed
code every time the program runs. Even if the
exception occurs and even if it doesn’t, t . can be some Answer: c
code that must be executed at end of the program. That Explanation: If the catch block of the base class is
code is written in finally block. This block is always defined first and then the derived class catch block is
executed regardless of exceptions occurring. given. The code becomes unreachable. Hence the
. derived class catch block must be written first.
. 4. The catching of base class exception
___________________________ in java.
Catching Class Types - a) After derived class is not allowed by compiler
b) Before derived class is not allowed by compiler
“Catching Class Types”. c) Before derived class is allowed
1. Which among the following is true for class d) After derived class can’t be done
exceptions?
a) Only base class can give rise to exceptions Answer: b
b) Only derived class can give rise to exceptions Explanation: The catching of base class exception
c) Either base class or derived class may produce before derived class is not allowed in java. The
exceptions compiler itself doesn’t allow this declaration. It
d) Both base class and derived class may produce produces an error.
exceptions 5. If catching of base class exception is done before
derived class in C++ ________________
Answer: d a) It gives compile time error
Explanation: It’s not mandatory that either base class b) It doesn’t run the program
or derived class can give rise to exceptions. The c) It may give warning but not error
exceptions might get produced from any class. The d) It always gives compile time error
exceptions depends on code.
2. If both base and derived class caught exceptions Answer: c
______________ Explanation: The compiler in C++ doesn’t identify this
a) Then catch block of derived class must be defined as compile time error and allows the execution of the
before base class program. But, the compiler may give some warning
b) Then catch block of base class must be defined related to the catch block sequence or code
before the derived class unreachable.
c) Then catch block of base and derived classes doesn’t 6. How many catch blocks can a class have?
matter a) Only 1
d) Then catch block of base and derived classes are not b) 2
mandatory to be defined c) 3
d) As many as required
Answer: a
Explanation: It is a condition for writing the catch
107
Answer: d the exceptions when the class used is not found in the
Explanation: T . are many type of exceptions that may program. This is done to handle all the undefined class
arise while running a code. And each catch block can exceptions. This can be due to a command line error.
handle only one exception. Hence t . can be as many 11. If classes produce some exceptions, then
catch blocks as required. ______________________
7. Since which version of java is multiple exception a) Their respective catch block must be defined
catch was made possible? b) Their respective catch blocks are not mandatory
a) Java 4 c) Their catch blocks should be defined inside main
b) Java 5 function
c) Java 6 d) Their catch blocks must be defined at the end of
d) Java 7 program

Answer: d Answer: a
Explanation: None of the languages used to support Explanation: The catch blocks must be defined. This is
multiple exception catch in a single catch block. Since to ensure that all the exceptions related to the classes
java 7 the feature was added to catch more than one are handled by the program code and the program
exceptions in one catch block. doesn’t terminate unexpectedly.
8. To catch more than one exception in one catch 12. Which among the following is true?
block, how are the exceptions separated in the syntax? a) Only the base class catch blocks are important
a) Vertical bar b) Only the derived class catch blocks are important
b) Hyphen c) Both the base and derived class catch blocks are
c) Plus important
d) Modulus d) If base and derived classes both produce exceptions,
program doesn’t run
Answer: a
Explanation: Just the way we separate the arguments in Answer: c
a function definition using comma. . we separate the Explanation: The purpose of exception handling is to
exceptions by using a vertical bar or we call it pipe handle the unexpected errors in the program. If base
symbol sometimes. This is just a convention followed class might produce some error then its catch block
to separate different exception list. must be given and if the derived class might produce
9. If a catch block accepts more than one exceptions some error then it must be given a specific catch block
then __________________ too.
a) The catch parameters are not final 13. Which is the necessary condition to define the base
b) The catch parameters are final and derived class catch blocks?
c) The catch parameters are not defined a) Base class catch should be defined first
d) The catch parameters are not used b) Derived class catch should be defined first
c) Catch block for both the classes must not be defined
Answer: b d) Catch block must be defined inside main function
Explanation: The catch parameters are made final. This
is to ensure that the parameters are not changed inside Answer: b
the catch block. Hence those retain their values. Explanation: The derived class catch blocks must be
10. Which among the following handles the undefined defined prior to the base class catch block. This is to
class in program? ensure that all the catch boxes are reachable. If not
a) ClassNotFound done, the code might become unreachable which in
b) NoClassException turn makes the program prone to errors.
c) ClassFoundException 14. Only the base class catch box can handle more than
d) ClassNotFoundException one exception in single block.
a) True
Answer: d b) False
Explanation: It is the exception handler that handles
108
Answer: b 3. Any changes made to static data member from one
Explanation: T . is no specific condition that states that member function _____________
only base class catch box can handle more than one a) Is reflected to only the corresponding object
exception in single box. Even the derived class catch b) Is reflected to all the variables in a program
clocks can handle more than one exceptions. c) Is reflected to all the objects of that class
15. Which condition among the following might result d) Is constant to that function only
in memory exception?
a) False if conditions Answer: c
b) Nested if conditions that are all false Explanation: The changes made from any function to
c) Infinite loops static data member will be a common change for all the
d) Loop that runs exactly 99 times other objects also. If the change is made with respect to
one object and change is printed from another object,
Answer: c the result will be same.
Explanation: The infinite loops doesn’t stop running 4. Which is the correct syntax for declaring static data
once started. T . must be a way to stop the loop but that member?
is always an improper termination. Infinite loops may a) static mamberName dataType;
keep on using more memory and hence would result in b) dataType static memberName;
memory error. c) memberName static dataType;
. d) static dataType memberName;
.
Answer: d
Static Data Members - Explanation: The syntax must firstly be mentioned
with the keyword static. Then the data type of the
“Static Data Members”. member followed by the member name should be
1. Which among the following best defines static given. This is general form of declaring static data
variables members? members.
a) Data which is allocated for each object separately 5. The static data member ______________________
b) Data which is common to all the objects of a class a) Must be defined inside the class
c) Data which is common to all the classes b) Must be defined outside the class
d) Data which is common to a specific method c) Must be defined in main function
d) Must be defined using constructor
Answer: b
Explanation: The static data members are made Answer: b
common to all the object of a class. They doesn’t Explanation: The static data members must be defined
change from object to object. Those are property of outside the class. Since these are common to all the
class rather than of any individual object. objects and should be created only once, they must not
2. Which keyword should be used to declare static be defined in the constructor.
variables? 6. The syntax for defining the static data members is:
a) static a) dataType className :: memberName =value;
b) stat b) dataType className : memberName =value;
c) common c) dataType className . memberName =value;
d) const d) dataType className -> memberName =value;

Answer: a Answer: a
Explanation: The keyword used to declare static Explanation: The syntax doesn’t contain the static
variables is static. This is must be used while declaring keyword. Since it is already been declared as static
the static variables. The compiler can make variables inside the class. The data type and the corresponding
static if and only if they are mentioned with static class name must be t . to allocate the variable to a
keyword. class. The value is assigned using scope resolution
operator for the member name.
109
7. If static data members have to be used inside a class, made static by default. This is an implicit meaning
those member functions _______________ given by the compiler to the member. Since const
a) Must not be static member functions values won’t change from object to object, hence are
b) Must not be member functions made static instead.
c) Must be static member functions 11. What is the output of the following program?
d) Must not be member function of corresponding class class Test
{
Answer: c private: static int x;
Explanation: Only the static member functions can public: static void fun()
access the static data members. The definition of static {
members is made common and hence the member cout &lt;&lt; ++x &lt;&lt; “ ”;
function should be capable of manipulating the static }
data members. };
8. The static data member int Test :: x =20;
__________________________ void main()
a) Can be accessed directly {
b) Can be accessed with any public class name Test x;
c) Can be accessed with dot operator x.fun();
d) Can be accessed using class name if not using static x.fun();
member function }
a) 20 22
Answer: d b) 20 21
Explanation: The static data members can be accessed c) 21 22
using the class name also. If the member functions is d) 22 23
not used or is not to be used then we can call the static
data members directly by using its corresponding class Answer: c
name. Explanation: The static member is initialized with 20.
9. Which among the following is correct syntax to Then the function is called which used pre-increment
access static data member without using member and printed value of x. The function is called twice.
function? Hence we get 21 22 as output.
a) className -> staticDataMember; 12. Whenever any static data member is declared in a
b) className :: staticDataMember; class ______________________
c) className : staticDataMember; a) Only one copy of the data is created
d) className . staticDataMember; b) New copy for each object is created
c) New memory location is allocated with each object
Answer: b d) Only one object uses the static data
Explanation: For accessing the static data members
without using the static member functions, the class Answer: a
name can be used. The class name followed by scope Explanation: The static data is same for all the objects.
resolution, indicating that static data members is Instead of creating the same data each time an object is
member of this class, and then the data member name. created, the compiler created only one data which is
10. Which data members among the following are accessed by all the objects of the class. This saves
static by default? memory and reduces redundancy.
a) extern 13. If object of class are created, then the static data
b) integer members can be accessed ____________
c) const a) Using dot operator
d) void b) Using arrow operator
c) Using colon
Answer: c d) Using dot or arrow operator
Explanation: The const data members of any class are
110
Answer: d d) className : dataType -> memberName;
Explanation: The static data members can be accessed
in usual way as other members are accessed using the Answer: d
objects. The dot operator is used generally. Arrow can Explanation: The syntax given in option d doesn’t
be used with the pointers. belong to any particular declaration or definition. First
14. What will be the output of the following program? one is to access the static members using the class
class Test name. Second is to define the static data outside the
{ class. Third syntax id to declare a data member as
public: Test() static in a class.
{ .
cout &lt;&lt; "Test's Constructor .
is Called " &lt;&lt; endl;
} Static Member Functions -
};
“Static Member Functions”.
class Result 1. Which among the following is correct definition for
{ static member functions?
static Test a; a) Functions created to allocate constant values to each
public: object
Result() b) Functions made to maintain single copy of member
{ functions for all objects
cout &lt;&lt; "Result's c) Functions created to define the static members
Constructor is Called " &lt;&lt; endl; d) Functions made to manipulate static programs
}
}; Answer: b
Explanation: The functions which are made common,
void main() with respect to definition and data usage, to all the
{ objects. These functions are able to access the static
Result b; data members of a class.
} 2. The static member functions __________________
a) Test’s Constructor is Called a) Have access to all the members of a class
b) Result’s Constructor is Called b) Have access to only constant members of a class
c) Result’s Constructor Called Test’s Constructor is c) Have access to only the static members of a class
Called d) Have direct access to all other class members also
d) Test’s Constructor Called Result’s Constructor is
Called Answer: c
Explanation: The static member functions are common
Answer: b for all the objects. These functions can use only the
Explanation: The output is the message printed from static members of a class in which those are defined.
the constructor of class Result. T . is no inheritance This is because other members change with respect to
used hence only one constructor is called. Since static each object created.
members are declared once in class declaration and are 3. The static member functions
not defined. The constructor of class Test will not be ____________________
called. a) Can be called using class name
15. Which among the following is wrong syntax b) Can be called using program name
related to static data members? c) Can be called directly
a) className :: staticDataMember; d) Can’t be called outside the function
b) dataType className :: memberName =value;
c) static dataType memberName; Answer: a
Explanation: The static members can be accessed using
111
class name also. This is because the static members Then those members are reused whenever called or
remain common to all the objects. Hence objects are invoked. Memory is allocated only once.
not required. 8. Which among the following is true?
4. Which is correct syntax to access the static member a) Static member functions can be overloaded
functions with class name? b) Static member functions can’t be overloaded
a) className . functionName; c) Static member functions can be overloaded using
b) className -> functionName; derived classes
c) className : functionName; d) Static member functions are implicitly overloaded
d) className :: functionName;
Answer: b
Answer: d Explanation: The static member functions can’t be
Explanation: The scope resolution operator must be overloaded because the definition must be the same for
used to access the static member functions with class all the instances of a class. If an overloaded function
name. This indicates that the function belongs to the have many definitions, none of them can be made
corresponding class. static.
5. Which among the following is not applicable for the 9. The static member functions _______________
static member functions? a) Can’t be declared const
a) Variable pointers b) Can’t be declared volatile
b) void pointers c) Can’t be declared const or volatile
c) this pointer d) Can’t be declared const, volatile or const volatile
d) Function pointers
Answer: d
Answer: c Explanation: The static member functions can’t be
Explanation: Since the static members are not property made const, since any object or class itself should be
of objects, they doesn’t have this pointer. Every time capable of making changes to the function. And the
the same member is referred from all the objects, hence function must retain all changes common to all the
use of this pointer is of no use. objects.
6. Which among the following is true? 10. Which keyword should be used to declare the static
a) Static member functions can’t be virtual member functions?
b) Static member functions can be virtual a) static
c) Static member functions can be declared virtual if it b) stat
is pure virtual class c) const
d) Static member functions can be used as virtual in d) common
Java
Answer: a
Answer: a Explanation: The member functions which are to be
Explanation: The static member functions can’t be made static, must be preceded with the keyword static.
virtual. This is a restriction on static member functions, This indicates the compiler to make the functions
since the definition should not change or should not be common to all the objects. And a new copy is not
overridden by any other function of derived class. The created with each of the new object.
static members must remain same for all the objects. 11. The keyword static is used _______________
7. The static members are ______________________ a) With declaration inside class and with definition
a) Created with each new object outside the class
b) Created twice in a program b) With declaration inside class and not with definition
c) Created as many times a class is used outside the class
d) Created and initialized only once c) With declaration and definition w .ver done
d) With each call to the member function
Answer: d
Explanation: The static members are created only once. Answer: b
Explanation: The keyword is used only inside the class
112
while declaring the static member. Outside the class, .
only definition with proper syntax is given. T . is no
need of specifying the keyword static again. Object Oriented Programming Interview . for
12. Which among the following can’t be used to access Experienced -
the members in any way?
a) Scope resolution This set of Object Oriented Programming Interview .
b) Arrow operator for Experienced people focuses on “Passing Object to
c) Single colon Functions”.
d) Dot operator 1. Passing object to a function _______________
a) Can be done only in one way
Answer: c b) Can be done in more than one ways
Explanation: The single colon can’t be used in any way c) Is not possible
in order to access the static members of a class. Other d) Is not possible in OOP
symbols can be used according to the code and need.
13. We can use the static member functions and static Answer: b
data member __________________ Explanation: The objects can be passed to the functions
a) Even if class object is not created and this requires OOP concept because objects are
b) Even if class is not defined main part of OOP. The objects can be passed in more
c) Even if class doesn’t contain any static member than one way to a function. The passing depends on
d) Even if class doesn’t have complete definition how the object have to be used.
2. The object ________________
Answer: a a) Can be passed by reference
Explanation: The static members are property of class b) Can be passed by value
as a whole. T . is no need of specific objects to call c) Can be passed by reference or value
static members. Those can be called directly or with d) Can be passed with reference
class name.
14. The static data member _________________ Answer: c
a) Can be mutable Explanation: The objects can be passed by reference if
b) Can’t be mutable required to use the same object. The values can be
c) Can’t be integer passed so that the main object remains same and no
d) Can’t be characters changes are made to it if the function makes any
changes to the values being passed.
Answer: b 3. Which symbol should be used to pass the object by
Explanation: The static data members can never be reference in C++?
mutable. T . copies are not made. Since those are a) &
common and created only once. b) @
15. If static data member are made inline, c) $
______________ d) $ or &
a) Those should be initialized outside the class
b) Those can’t be initialized with the class Answer: a
c) Those can be initialized within the class Explanation: The object to be passed by reference to
d) Those can’t be used by class members the function should be preceded by & symbol in the
argument list syntax of the function. This indicates the
Answer: c compiler not to use new object. The same object which
Explanation: Since the members are created once and is being passed have to be used.
are common for all the instances, those can be 4. If object is passed by value, ________________
initialized inside the class. Those doesn’t change with a) Copy constructor is used to copy the values into
each object being created hence can be defined inside another object in the function
the class once for all. b) Copy constructor is used to copy the values into
. temporary object
113
c) Reference to the object is used to access the values b) Local object in the function is used
of the object c) Only the data member values are used
d) Reference to the object is used to created new object d) The values are accessible from the original object
in its place
Answer: b
Answer: a Explanation: When an object is called by values, copy
Explanation: The copy constructor is used. This constructor is called and object is copied to the local
constructor is used to copy the values into a new object object of the function which is mentioned in the
which will contain all the values same as that of the argument list. The values gets copied and are used
object being passed but any changes made to the newly from the local object. T . is no need to access the
created object will not affect the original object. original object again.
5. Pass by reference of an object to a function 9. Can data members be passed to a function using the
_______________ object?
a) Affects the object in called function only a) Yes, it can be passed only inside class functions
b) Affects the object in prototype only b) Yes, only if the data members are public and are
c) Affects the object in caller function being passed to a function outside the class
d) Affects the object only if mentioned with & symbol c) No, can’t be passed outside the class
with every call d) No, can’t be done

Answer: c Answer: b
Explanation: The original object in the caller function Explanation: The data members can be passed with
will get affected. The changes made in the called help of object but only if the member is public. The
function will be same in the caller function object also. object will obviously be used outside the class. The
6. Copy constructor definition requires object must have access to the data member so that its
__________________ value or reference is used outside the class which is
a) Object to be passed by value possible only if the member is public.
b) Object not to be passed to it 10. What exactly is passed when an object is passed by
c) Object to be passed by reference reference?
d) Object to be passed with each data member value a) The original object name
b) The original object class name
Answer: c c) The exact address of the object in memory
Explanation: The object must be passed by reference to d) The exact address of data members
a copy constructor. This is to avoid the out of memory
error. The constructors keeps calling itself, if not Answer: c
passed by reference, and goes out of memory. Explanation: The location of the object, that is, the
7. What is the type of object that should be specified in exact memory location is passed, when the object is
argument list? passed by reference. The pass by reference is actually a
a) Function name reference to the object that the function uses with
b) Object name itself another name to the same memory location as the
c) Caller function name original object uses.
d) Class name of object 11. If the object is not to be passed to any function but
the values of the object have to be used then:
Answer: d a) The data members should be passed separately
Explanation: The type of object is the class itself. The b) The data members and member functions have to be
class name have to be specified in order to pass the passed separately
objects to a function. This allows the program to create c) The values should be present in other variables
another object of same class or to use the same object d) The object must be passed
that was passed.
8. If an object is passed by value, _________________ Answer: a
a) Temporary object is used in the function Explanation: The data members can be passed
114
separately. T . is no need to pass whole object, instead another object is created and new memory space is
we can use the object to pass only the required values. allocated for it.
12. Which among the following is true? 15. Pass by reference and pass by value can’t be done
a) More than one object can’t be passed to a function simultaneously in a single function argument list.
b) Any number of objects can be passed to a function a) True
c) Objects can’t be passed, only data member values b) False
can be passed
d) Objects should be passed only if those are public in Answer: b
class Explanation: T . is no condition which specifies that
only the reference pass or values pass is allowed. The
Answer: b argument list can contain one reference pass and
Explanation: T . is no restriction on passing the number another value pass. This helps to manipulate the
of objects to a function. The operating system or the objects with functions more easily.
compiler or environment may limit the number of .
arguments. But t . is no limit on number of objects till To practice all areas of Object Oriented Programming
that limit. for Interviews, .
13. What will be the output if all necessary code is
included (Header files and main function)? Returning Objects . -
void test (Object &y)
{ “Returning Objects”.
y = "It is a string"; 1. In which of the following way(s) can the object be
} returned from a function?
void main() a) Can only be returned by value
{ b) Can only be returned by reference
Object x = null; c) Can be returned either by value or reference
test (x); d) Can neither be returned by value nor by reference
System.out.println (x);
} Answer: c
a) Run time error Explanation: The objects can be returned either by
b) Compile time error value or reference. T . is no mandatory condition for
c) Null the way it should be used. The way of returning object
d) It is a string can be decided based on the requirement.
2. Whenever an object is returned by value
Answer: d ____________________
Explanation: This is because the x object is passed by a) A temporary object is created
reference. The changes made inside the function will b) Temporary object is not created
be applicable to original function too. c) Temporary object may or may not be created
14. In which type is new memory location will be d) New permanent object is created
allocated?
a) Only in pass by reference Answer: a
b) Only in pass by value Explanation: A temporary object is created when an
c) Both in pass by reference and value object is returned by value. The temporary object is
d) Depends on the code used to copy the values to another object or to be used
in some way. The object holds all the values of the data
Answer: b members of the object.
Explanation: The new memory location will be 3. W . the temporary objects (created while return by
allocated only if the object is passed by value. value) are created?
Reference uses the same memory address and is a) Outside the function scope
denoted by another name also. But in pass by value, b) Within the function
c) Inside the main function
115
d) Inside the calling function d) 4

Answer: b Answer: a
Explanation: The temporary object are created within Explanation: Only one object can be returned at a time.
the function and are intended to return the value for This is because a function is only capable of returning
specific use. Either the object can be assigned to a single value at a time. Though array of objects can be
another object or be used directly if possible. returned from a function.
4. Which is the correct syntax for returning an object 8. Which error will be produced if a local object is
by value? returned by reference outside a function?
a) void functionName ( ){ } a) Out of memory error
b) object functionName( ) { } b) Run time error
c) class object functionName( ) { } c) Compile time error
d) ClassName functionName ( ){ } d) No error

Answer: d Answer: c
Explanation: The class name itself should be the return Explanation: If the local object is returned outside the
type of the function. This notifies that the function will function then the compile-time error arises. While the
return an object of specified class type. Only the class program is being converted and the processes
name should be specified. happening during compile time, the compiler won’t be
5. Which is the correct syntax for defining a function able to resolve the statement.
which passes an object by reference? 9. If object is passed by reference
a) className& functionName ( ) ____________________
b) className* functionName( ) a) Temporary object is created
c) className-> functionName( ) b) Temporary object is created inside the function
d) &className functionName() c) Temporary object is created for few seconds
d) Temporary object is not created
Answer: a
Explanation: The function declaration must contain the Answer: d
class name as return type. But, a & symbol should be Explanation: The temporary object is not created. If
followed by the class name. & indicates that the object object is returned by reference, a particular memory
being returned will be returned by reference. location will be denoted with another name and hence
6. If an object is declared inside the function then same address values will be used.
____________________ outside the function 10. Which among the following is correct?
a) It can be returned by reference a) Individual data members can’t be returned
b) It can’t be returned by reference b) Individual data members can be returned
c) It can be returned by address c) Individual member functions can be returned from
d) It can’t be returned at all another function
d) Individual data members can only be passed by
Answer: b reference
Explanation: The object which is declared inside the
class can never be returned by reference. This is Answer: b
because the object will be destroyed as it goes out of Explanation: It is not mandatory to return the whole
scope when the function is returned. The local object. Instead we can return a specific data member
variables get destroyed when function is returned value. But the return type given must match with the
hence the local objects can’t be returned by reference. data type of the data being returned.
7. How many independent objects can be returned at 11. Can we return an array of objects?
same time from a function? a) Yes, always
a) 1 b) Ye, only if objects are having same values
b) 2 c) No, because objects contain many other values
c) 3
116
d) No, because objects are single entity c) Because return by reference just make the objects
points to values memory location
Answer: a d) Because return by reference just make the object
Explanation: The Object array can be returned from a point to null
function. This can be done by putting a className* as
the return type of the function. This makes the return Answer: c
type to accept an array of objects in return. Explanation: A reference to the memory location w .
12. If an object is passed by reference to a function the returned object is stored is made. This allows the
then it must be returned by reference. new object which takes the return value, point to the
a) True memory location and hence access the same values.
b) False .
.
Answer: b
Explanation: It is not compulsory to return the object in Assigning Objects . -
the same way as it was passed. If the object is passed
by reference then t . is actually no need to return the “Assigning Objects”.
object. Because the changes made inside the function 1. When value of an object is assigned to another
will be visible on the original object of caller function object, ___________________
also. a) It becomes invalid statement
13. Which among the following is true? b) Its values gets copied into another object
a) Two objects can point to the same memory location c) Its values gets address of the existing values
b) Two objects can never point to the same memory d) The compiler doesn’t execute that statement
location
c) Objects not allowed to point at a location already Answer: b
occupied Explanation: The values get copied to another object.
d) Objects can’t point to any address No address is assigned to the object values. This is uses
copy constructor to copy the values.
Answer: a 2. If an object is created and another object is assigned
Explanation: When an object is created and instead of to it, then ________________
calling a constructor, another object is assigned to it. a) Copy constructor is called to copy the values
Both the objects point to the same memory location. b) Object is copied directly to the object
This can be illustrated with help of return by reference. c) Reference to another object is created
14. If an object is being returned by value then d) The new object is initialized to null values
__________________________
a) Its member values are made constant Answer: c
b) Its member values have to be copied individually Explanation: The new object created, refers to the same
c) Its member values are not used address of the previously created object. Now
d) Its member values are copied using copy constructor whenever new object changes any data member value,
it will affect the previously existing object.
Answer: d 3. How the argument passed to a function get
Explanation: When an object is returned by value, it initialized?
will be returned to another object or will be directly a) Assigned using copy constructor at time of passing
used. Still in all the conditions the copy constructor b) Copied directly
will be used to copy all the values from the temporary c) Uses addresses always
object that gets created. d) Doesn’t get initialized
15. Why temporary object is not created in return by
reference? Answer: a
a) Because compiler can’t create temporary objects Explanation: The arguments get initialized using the
b) Because the temporary object is created within the copy constructor. T . is need of assigning the value of
function
117
all the members of an object to the local object of the still generates a copy assignment operator. It doesn’t
function. gets suppressed.
4. Predict the output of the program. 7. In copy constructor definition, if non const values
class A are accepted only ________
{ a) Only const objects will be accepted
public int i; b) Only non – const objects are accepted
}; c) Only const members will not get copied
void main() d) Compiler generates an error
{
A x; Answer: b
A y=x; Explanation: Only the non – const objects will be
x.i=10; accepted by the compiler. If a const object is passed,
y.i=20; the compiler produces an error. To reduce that, we use
y.i++; const argument in definition, so that both const and
y.i=20; non – const objects are accepted.
cout&l;&lt;tx.i; 8. How many objects can be assigned to a single
} address?
a) 10 a) Only 1
b) 20 b) At most 7
c) 21 c) At most 3
d) 0 d) As many as required

Answer: b Answer: d
Explanation: The expected output may be 10 because Explanation: The memory address can be referenced
the value of member of object x is printed. But when by more than one object. T . is no maximum number
object x is assigned to y, y points to the same address defined. Any number of objects can reference to the
w . x is stored. So actually both objects x and y point to same address.
the same location and refers to the same object. 9. Use of assignment operator ____________________
5. If programmer doesn’t define any copy assignment a) Changes its use, when used at declaration and in
operator then ____________________ normal assignment
a) Compiler gives an error b) Doesn’t changes its use, whatever the syntax might
b) Program fails at run time be
c) Compiler gives an implicit definition c) Assignment takes place in declaration and
d) Compiler can’t copy the member values assignment syntax
d) Doesn’t work in normal syntax, but only with
Answer: c declaration
Explanation: While defining a copy constructor, we
use reference const parameter, those are used for the Answer: a
assignment. The assignment operator may or may not Explanation: The assignment operator if used at
be defined by the programmer, if not, compiler declaration then it uses copy constructor for the
implicitly defines member wise copy assignment copying of objects. If used in simple assignment syntax
operator. then it uses copy assignment function.
6. Declaring a copy constructor doesn’t suppresses the 10. If more than one object refer to the same address,
compiler generated copy assignment operator. any changes made
a) True a) Can be made visible to specific objects
b) False b) Will be specific to one object only
c) From any object will be visible in all
Answer: a d) Doesn’t changes the values of all objects
Explanation: Even if the programmer doesn’t define or
even if they define the copy constructor. The compiler
118
Answer: c But better if the program have a predefined copy
Explanation: At a memory address, only one object can assignment operator.
be referenced. All the other objects which refer to the 15. Assigning objects takes place while passing the
same memory address make changes for all of the arguments.
objects referring that address. a) True
11. How to make more than one object refer to the b) False
same object?
a) Initialize it to null Answer: b
b) Initialize the object with another at declaration Explanation: The actual assignment doesn’t take place
c) Use constructor to create new object as the object might have got passed by reference. Also
d) Assign the address directly even if not by reference, the copy constructor is called
to copy the values into the new object and not exactly
Answer: b the assignment operator.
Explanation: The object must get initialized with .
another object at time of declaration only. We don’t .
have to create a new object we just have to get name of
new object because t . after same address will be Object Oriented Programming Assessment
referred. Questions -
12. We can assign ______________________
a) Value of one reference variable to another This set of Object Oriented Programming
b) Value of any object to another Assessment . focuses on “Pointer to Objects”.
c) Value of any type to any object 1. Which language among the following doesn’t allow
d) Value of non – reference to another reference pointers?
a) C++
Answer: a b) Java
Explanation: Only the reference value can be assigned c) Pascal
to another reference value. This is because both deal d) C
with the address. T . is no type mismatch hence we can
assign them. Answer: b
13. Assigning reference to an object Explanation: The concept of pointers is not supported
_________________ in Java. The feature is not given in the language but
a) Will create another copy of the object can be used in some ways explicitly. Though this
b) Will create two different copies of the object pointer is supported by java too.
c) Will not create any other copy of the object 2. Which is correct syntax for declaring pointer to
d) Will not refer to the object object?
a) className* objectName;
Answer: c b) className objectName;
Explanation: When an object is assigned with another c) *className objectName;
object. Same memory location is used. T . is no other d) className objectName();
copy of the object created.
14. Which among the following is true? Answer: a
a) We can use direct assignment for any object Explanation: The syntax must contain * symbol after
b) We can use direct assignment only for different the className as the type of object. This declares an
class objects object pointer. This can store address of any object of
c) We must not use direct assignment the specified class.
d) We can use direct assignment to same class objects 3. Which operator should be used to access the
members of the class using object pointer?
Answer: d a) Dot operator
Explanation: The direct assignment can be used with b) Colon to the member
the same class objects. T . is no restriction on them. c) Scope resolution operator
119
d) Arrow operator d) Can point to whole class objects at a time

Answer: d Answer: a
Explanation: The members can be accessed from the Explanation: The object pointer can point to only one
object pointer by using arrow operator. The arrow object at a time. The pointer will be able to store only
operator can be used only with the pointer of class one address at a time. Hence only one object can be
type. If simple object is declared, it must use dot referred.
operator to access the members. 8. Pointer to a base class can be initialized with the
4. How does compiler decide the intended object to be address of derived class, because of _________
used, if more than one object are used? a) derived-to-base implicit conversion for pointers
a) Using object name b) base-to-derived implicit conversion for pointers
b) Using an integer pointer c) base-to-base implicit conversion for pointers
c) Using this pointer d) derived-to-derived implicit conversion for pointers
d) Using void pointer
Answer: a
Answer: c Explanation: It is an implicit rule defined in most of
Explanation: This pointer denotes the object, in which the programming languages. It permits the programmer
it is being used. If member function is called with to declare a pointer to the derived class from a base
respect to one object then this pointer refers to the class pointer. In this way the programmer doesn’t have
same object members. It can be used when members to declare object for derived class each time it is
with same name are involved. required.
5. If pointer to an object is declared, ___________ 9. Can pointers to object access the private members of
a) It can store any type of address the class?
b) It can store only void addresses a) Yes, always
c) It can only store address of integer type b) Yes, only if it is only pointer to object
d) It can only store object address of class type c) No, because objects can be referenced from another
specified objects too
d) No, never
Answer: d
Explanation: The address of only the specified class Answer: d
type can get their address stored in the object pointer. Explanation: The pointers to an object can never access
The addresses doesn’t differ but they do differ for the the private members of the class outside the class. The
amount and type of memory required for objects of object can indirectly use those private members using
different classes. Hence same class object pointer member functions which are public in the class.
should be used. 10. Is name of an array of objects is also a pointer to
6. What is the size of object pointer? object?
a) Equal to size of any usual pointer a) Yes, always
b) Equal to size of sum of all the members of object b) Yes, in few cases
c) Equal to size of maximum sized member of object c) No, because it represents more than one object
d) Equal to size of void d) No, never

Answer: a Answer: a
Explanation: The size of object pointer is same as that Explanation: The array name represents a pointer to the
of any usual pointer. This is because only the address object. The name alone can represent the starting
have to be stored. T . are no values to be stored in the address of the array. But that also represents an array
pointer. which is in turn stored in a pointer.
7. A pointer _________________ 11. Which among the following is true?
a) Can point to only one object at a time a) The pointer to object can hold address only
b) Can point to more than one objects at a time b) The pointer can hold value of any type
c) Can point to only 2 objects at a time c) The pointer can hold only void reference
120
d) The pointer can’t hold any value d) Pointer should be made const

Answer: a Answer: d
Explanation: The pointer to an object can hold only the Explanation: The pointer should be declared as a const
addresses. Address of any other object of same class. type. This prevents the pointer to change any value that
This allows the programmer to link more than one is being pointed from it. This is a feature that is made
objects if required. to access the values using pointer but to make sure that
12. Which is the correct syntax to call a member pointer doesn’t change those values accidently.
function using pointer? 16. References to object are same as pointers of object.
a) pointer->function() a) True
b) pointer.function() b) False
c) pointer::function()
d) pointer:function() Answer: b
Explanation: The references are made to object when
Answer: a the object is created and initialized with another object
Explanation: The pointer should be mentioned without calling any constructor. But the object pointer
followed by the arrow operator. Arrow operator is must be declared explicitly using * symbol that will be
applicable only with the pointers. Then the function capable of storing some address. Hence both are
name should be mentioned that is to be called. different.
13. If pointer to an object is created and the object gets .
deleted without using the pointer: To practice all areas of Object Oriented Programming
a) It becomes void pointer Assessment Questions, .
b) It becomes dangling pointer
c) It becomes null pointer This Pointer -
d) It becomes zero pointer
“This Pointer”.
Answer: b 1. Which is the pointer which denotes the object
Explanation: When the address pointed by the object calling the member function?
pointer gets deleted, the pointer now points to an a) Variable pointer
invalid address. Hence it becomes a dangling pointer. b) This pointer
It can’t be null or void pointer since it doesn’t point to c) Null pointer
any specific location. d) Zero pointer
14. How can the address stored in the pointer be
retrieved? Answer: b
a) Using * symbol Explanation: The pointer which denotes the object
b) Using $ symbol calling the member function is known as this pointer.
c) Using & symbol The this pointer is usually used when t . are members
d) Using @ symbol in the function with same name as those of the class
members.
Answer: c 2. Which among the following is true?
Explanation: The & symbol must be used. This should a) this pointer is passed implicitly when member
be done such that the object should be preceded by & functions are called
symbol and then the address should be stored in b) this pointer is passed explicitly when member
another variable. This is done to get the address w . the functions are called
object is stored. c) this pointer is passed with help of pointer member
15. What should be done to prevent changes that may functions are called
be made to the values pointed by the pointer? d) this pointer is passed with help of void pointer
a) Usual pointer can’t change the values pointed member functions are called
b) Pointer should be made virtual
c) Pointer should be made anonymous
121
Answer: a d) Address is specified as return type of the function
Explanation: When an object calls some member
function, it implicitly passes itself as an argument. This Answer: a
allows the compiler to know which member should be Explanation: The address is passed implicitly as an
used for the purposes. This also allows to reduce the argument to the function. This doesn’t have to be
ambiguity among the variable and data member names. passed explicitly. The address is passed, of the object
3. The this pointer is accessible __________________ which is calling the non-static member function.
a) Within all the member functions of the class 7. Which is the correct interpretation of the member
b) Only within functions returning void function call from an object,
c) Only within non-static functions object.function(parameter);
d) Within the member functions with zero arguments a) object.function(&this, parameter)
b) object(&function,parameter)
Answer: c c) function(&object,&parameter)
Explanation: The this pointer is available only within d) function(&object,parameter)
the non-static member functions of a class. If the
member function is static, it will be common to all the Answer: d
objects and hence a single object can’t refer to those Explanation: The function name is specified first and
functions independently. then the parameter lists. The parameter list is included
4. An object’s this pointer _____________________ with the object name along with & symbol. This
a) Isn’t part of class denotes that the address of the object is being passed as
b) Isn’t part of program an argument.
c) Isn’t part of compiler 8. The address of the object _________________
d) Isn’t part of object itself a) Can’t be accessed from inside the function
b) Can’t be accessed in the program
Answer: d c) Is available inside the member function using this
Explanation: The object’s this pointer being called are pointer
not part of the object itself. This can be cross verified d) Can be accessed using the object name inside the
by checking that it doesn’t take up any space for the member function
data to be stored or pointed.
5. The result of sizeof() function __________________ Answer: c
a) Includes space reserved for this pointer Explanation: The address of the object with respect to
b) Includes space taken up by the address pointer by which the member functions are being called, are
this pointer stored in this pointer. This pointer is hence used
c) Doesn’t include the space taken by this pointer whenever t . are members with same name as those of
d) Doesn’t include space for any data member the variables inside the function.
9. Which among the following is true?
Answer: c a) This pointer can be used to guard against any kind of
Explanation: The space taken by this pointer is not reference
reflected in by the sizeof() operator. This is because b) This pointer can be used to guard against self-
object’s this pointer is not part of object itself. This is a reference
cross verification for the concept stating that this c) This pointer can be used to guard from other
pointer doesn’t take any space in the object. pointers
6. Whenever non-static member functions are called d) This pointer can be used to guard from parameter
_______________ referencing
a) Address of the object is passed implicitly as an
argument Answer: b
b) Address of the object is passed explicitly as an Explanation: The this pointer can be used to guard
argument itself whenever self-reference is used. This allows
c) Address is specified globally so that the address is accidental address access. And accidental modification
not used again of data.
122
10. Which syntax doesn’t execute/is false when 14. Which among the following is/are type(s) of this
executed? pointer?
a) if(&object != this) a) const
b) if(&function !=object) b) volatile
c) this.if(!this) c) const or volatile
d) this.function(!this) d) int

Answer: a Answer: c
Explanation: The condition becomes false when Explanation: The this pointer can be declared const or
executed and hence doesn’t executes. This is the case volatile. This depends on need of program and type of
w . this pointer can guard itself from the self-reference. code. This is just an additional feature.
. if the address of the object doesn’t match with this 15. Which is the correct syntax for declaring type of
pointer that means the object doesn’t refer itself. this in a member function?
11. The this pointers _____________________ a) classType [cv-qualifier-list] *const this;
a) Are modifiable b) classType const[cv-qualifier-list] *this;
b) Can be assigned any value c) [cv-qualifier-list]*const classType this;
c) Are made variables d) [cv-qualifier-list] classType *const this;
d) Are non-modifiable
Answer: d
Answer: d Explanation: The syntax contains the cv-qualifier-list
Explanation: The this pointer is non modifiable. This is that can be determined from the member function
because the address of any object remains constant declaratory that can be either const or volatile or can be
throughout its life time. Hence the address must not be made both. Hence we write it as list. classType denotes
changed otherwise wrong members of invalid the name of class to mention to which class does the
addresses might get accessed. object belong to. And *const this denotes that the this
12. Earlier implementations of C++ pointer is having a constant value.
___________________ .
a) Never allowed assignment to this pointer .
b) Allowed no assignment to this pointer
c) Allowed assignments to this pointer Default Arguments -
d) Never allowed assignment to any pointer
“Default Arguments”.
Answer: c 1. What are default arguments?
Explanation: The earlier, most initial versions of C++ a) Arguments which are not mandatory to be passed
used to allow assignments to this pointers. That used to b) Arguments with default value that aren’t mandatory
allow modifications of this pointer. Later that feature to be passed
got disabled. c) Arguments which are not passed to functions
13. This pointer can be used directly to ___________ d) Arguments which always take same data value
a) To manipulate self-referential data structures
b) To manipulate any reference to pointers to member Answer: b
functions Explanation: The arguments which are assigned with
c) To manipulate class references some default value. Since some value is already given,
d) To manipulate and disable any use of pointers it is not mandatory to pass those arguments. They can
be used directly.
Answer: a 2. Which is correct condition for the default
Explanation: This is a feature provided, that can be arguments?
used directly. The manipulation of self-referential data a) Those must be declared as last arguments in
structures is just an application of this feature. Other argument list
conditions fails as this pointer doesn’t deal with those b) Those must be declared first in the argument list
things. c) Those can be defined anyw . in the argument list
123
d) Those are declared inside the function definition b) void test(int x=0,int=0)
c) void test(int x,int y=0)
Answer: a d) void test(int x=’c’,int y)
Explanation: The default arguments must be declared
at last in the argument list. This is to ensure that the Answer: c
arguments doesn’t create ambiguity. The normal Explanation: The default arguments must be mentioned
arguments should be passed first. at last in the argument list. Also the type of values
3. If a member function have to be made both zero assigned must match with the argument type. All the
argument and parameterized constructor, which among default arguments must be mentioned at last, none of
the following can be the best option? the normal arguments should come in between the
a) Two normal and one default argument default arguments list.
b) At least one default argument 7. What function will be called with the independent
c) Exactly one default argument syntax “test(5,6,7);”?
d) Make all the arguments default a) void test(int x, int y)
b) void test(int x=0,int y,int z)
Answer: d c) int test(int x=0,y=0,z=0)
Explanation: All the arguments must be made default. d) void test(int x,int y, int z=0)
This will make sure that none of the arguments are
mandatory to be passed. Which in turn means that the Answer: d
function can work without any argument and can be Explanation: T . are three arguments that are getting
passed with arguments too. passed to the function test(). Only the last option have
4. Which among the following function can be called all the default argument at last in the argument list.
without arguments? And the total number of the arguments is three. The
a) void add(int x, int y=0) third option is wrong because the return type is int and
b) void add(int=0) the syntax given is independent which means it doesn’t
c) void add(int x=0, int y=0) return any value.
d) void add(char c) 8. Which among the following is wrong call to the
function void test(int x, int y=0, int z=0)?
Answer: c a) test(5,6,7);
Explanation: For the function to be called without b) test(5);
arguments, either it must have zero arguments or it c) test();
must have all the default arguments. . the function in d) test(5,6);
option c have all the default arguments and hence can
be called directly with zero argument. Answer: c
5. If a function have all the default arguments but still Explanation: The function must be passed with at least
some values are passed to the function then one argument. T . is two default arguments and one
______________ normal argument which must be passed with some
a) The function will use the values passed to it value. Hence third call to the function is wrong as it
b) The function will use the default values as those are doesn’t pass even a single parameter to the function
local 9. Default arguments are
c) The function can use any value whichever is higher _________________________
d) The function will choose the minimum values a) Only allowed in the parameter list of the function
declaration
Answer: a b) Only allowed in the return type of the function
Explanation: The function will use the values passed declaration
explicitly to it. The default values will be ignored. The c) Only allowed with the class name definition
default values are used only in case the values are not d) Only allowed with the integer type values
passed explicitly to the function.
6. Which among the following is correct? Answer: a
a) void test(int x=0, int y, int z=0) Explanation: The default arguments are only allowed
124
in the parameter list of the function arguments. This arguments are added. This comes under flexible
rule was not applicable in the beginning versions of c+ programming.
+ but later from c++ 14th version it has been 13. The names given to the default arguments are only
implemented. This is the only way to use default looked up and ________________. And are bound
arguments. during declaration.
10. Which among the following is false for default a) Checked for availability
arguments? b) Checked for random access
a) Those are not allowed with declaration of pointer to c) Checked for accessibility
functions d) Checked for feasibility
b) Those are not allowed with the reference to
functions Answer: c
c) Those are not allowed with the typedef declarations Explanation: The names given to the default arguments
d) Those are allowed with pointer and reference to are bound at time of declaration but are only checked
function declaration for accessibility and to get bounded. This is mainly to
bind those members during declaration.
Answer: d 14. The default argument get bound during declaration
Explanation: The statements given are true because ________________
that is a feature given to make the programming more a) And are never executed
flexible and have some security with accidental b) And are executed simultaneously
changes at same time. The last option is false because c) But are executed only if priority is given
it is not a rule defined. It is an opposite statement to the d) But are executed during function call
rules defined for default arguments.
11. The non-template functions can be added with Answer: d
default arguments to already declared functions Explanation: The default argument are bound at time
____________________ of declaration. That is an implicit functioning. But
a) If and only if the function is declared again in the those are executed only when the function is called.
same scope Otherwise those will never get executed.
b) If and only if the function is declared only once in 15. The virtual function overrides ____________
the same scope a) Do not acquire base class declaration of default
c) If and only if the function is declared in different arguments
scope b) Do acquire base class declaration of default
d) If and only if the function is declared twice in the arguments
program c) Do not link with the default arguments of base class
d) Do link with the default argument but only of
Answer: a derived classes
Explanation: The non-template functions can also be
added with default arguments. This can be done even if Answer: a
the functions were defined earlier. This is because the Explanation: The virtual function overrides do not
call to the function won’t be affected. The function can acquire the base class declaration of default arguments.
still be used in the same way as it was used earlier. Even if a call to the virtual function is made, static type
12. The using declaration __________ of the object decides the default arguments to be used.
a) Doesn’t carry over the default values .
b) Carries over the known default arguments .
c) Carries over only the normal arguments
d) Carries over only few default arguments Constructors Overloading -

Answer: b “Constructors Overloading”.


Explanation: The using-declaration carries over all the 1. Which among the following is true for constructors
known default arguments. This is a common feature as overloading?
the usage doesn’t gets affected even if the default a) Constructors can’t be overloaded
125
b) Constructors can be overloaded using different the constructor functions doesn’t have any return type.
signatures Also, for overloading all the functions must have the
c) Constructors can be overloaded with same same name.
signatures 5. Which among the following can be used in place of
d) Constructors can be overloaded with different return default constructor?
types a) constructorName(int x,int y=0)
b) constructorName(int x=0,int y=0)
Answer: b c) constructorName(int x=0,int y)
Explanation: The constructors can be overloaded only d) constructorName(int x,int y)
if the definitions have different signatures.
Constructors doesn’t have any return type so can’t be Answer: b
overloaded using return type. If constructors have same Explanation: For a parameterized constructor to be
signature then it will produce a compile time error. used as a default constructor, it must have all the
2. If a constructors should be capable of creating default arguments. This makes the constructor to have
objects without argument and with arguments, which is optional arguments which are not mandatory to be
a good alternative for this purpose? passed.
a) Use zero argument constructor 6. Can a class have more than one function with all the
b) Use constructor with one parameter default arguments?
c) Use constructor with all default arguments a) Yes, always
d) Use default constructor b) Yes, if argument list is different
c) No, because constructors overloading doesn’t
Answer: c depend on argument list
Explanation: The constructor should use all the default d) No, never
arguments. This will allow the constructor to be called
even if no arguments are passed. And if arguments are Answer: d
passed, those will be accepted instead of the default Explanation: A single class can never have more than
values. once constructor with all the default arguments. This is
3. The Constructors with all the default arguments are because it will make all those constructors as a default
similar as default constructors. (True/False) constructor. And when an object is created with zero
a) True arguments then it will create ambiguity.
b) False 7. Which is the correct syntax for using default
arguments with the constructor?
Answer: a a) default constructorName(default int x=0)
Explanation: The constructors with all the default b) constructorName(default int x=0)
arguments are similar as the default constructors. c) constructorName(int x=0)
Those can be used instead of the default constructors. d) constructorName()
So defining the default constructor is not mandatory.
4. Which among the following is true? Answer: c
a) The constructors overloading can be done by using Explanation: The constructors using the default
different names arguments must initialize the arguments in the
b) The constructors overloading can be done by using argument list. This is to make the constructor use the
different return types default value when no arguments are passed. If no
c) The constructors can be overloaded by using only arguments are listed then it is a default constructor.
one argument 8. How many parameters must be passed if only the
d) The constructors must have same name as that of following prototype is given to a constructor?
class Prototype: className(int x, int y, int z=0);
a) 1
Answer: d b) 2
Explanation: The constructors must have same name as c) 3
that of the class name. This is mandatory because only
126
d) Compile time error be called. T . is only one option which first accepts
integer value and then a character value. Hence that
Answer: b constructor will be called.
Explanation: In the prototype given, only 2 arguments 12. Which constructor definition will produce a
are mandatory. Since the third argument is default compile time error?
argument, so it is not mandatory to pass the argument. a) className(int x=0);
9. If the constructors are overloaded by using the b) className(char c);
default arguments, which problem may arise? c) className(int x=0,char c);
a) The constructors might have all the same arguments d) className(char c,int x=0);
except the default arguments
b) The constructors might have same return type Answer: c
c) The constructors might have same number of Explanation: The default arguments, just like with
arguments member functions, must be listed at last in the
d) The constructors can’t be overloaded with respect to argument list. Hence this will produce a compile time
default arguments error. The compiler doesn’t allow the definition to be
executed.
Answer: a 13. If t . is a constructor with all the default arguments
Explanation: The constructors having same arguments and arguments are not passed then
except the default arguments can give rise to errors. If _________________
only the mandatory arguments are passed, it will create a) The default values given will not be used
ambiguity in calling the correct constructor. Hence the b) Then all the null values will be used
mandatory arguments must be different. c) Then all the default values given will be used
10. Which among the following is true? d) Then compiler will produce an error
a) More than one constructors with all default
arguments is allowed Answer: c
b) More than one constructors with all default Explanation: The constructors will use the default
arguments can be defined outside the class values listed for use. The null values are not used
c) More than one constructors can be used with same because those are not specified. Though if it was
argument list compiler provided default constructor, then it would
d) More than one constructors with all default have initialized the members to null or zero values.
arguments can’t exist in same class 14. Which is the correct statement for default
constructors?
Answer: d a) The constructors with all the default arguments
Explanation: The constructors must have different b) The constructors with all the null and zero values
argument list. Along that, if all the arguments are c) The constructors which can’t be defined by
default arguments, t . can’t be more than once programmer
constructor like that in the same class as that will d) The constructors with zero arguments
create ambiguity while constructors are being called.
11. Which constructor among the following will be Answer: d
called if a call is made like className(5,’a’);? Explanation: The closest answer to the question is that
a) className(int x=5,char c=’a’); a default constructor is a constructor with zero
b) int className(int x, char c, char d); arguments. But this is not the actual case. Actually the
c) className(int x, char c, int y); constructors provided by the compiler are default
d) char className(char c,int x); constructors. And the constructors with zero arguments
defined by the programmer are zero argument
Answer: a constructors.
Explanation: The syntax given is passing two 15. Which is a good alternative instead of having one
parameters to the constructor call. One value is of zero argument constructor and one single argument
integer type and another of character type. Hence the constructor with default argument?
constructor with arguments of int and char type should a) No constructor defined
127
b) One default value constructor base type or the larger size. This results in allocating a
c) Defining the default constructor smaller size data into bigger type data. No data is lost
d) Using one constructor with two arguments in casting, hence safe.
4. Which among the following is best situation to use
Answer: b upcasting?
Explanation: The constructor with one default a) For general code dealing with only subtype
argument can be the best alternative. This is because b) For general code dealing with only supertype
the constructor with one default value will do the work c) For general code dealing with both the supertype
for both the default constructor and one argument and subtype
constructor. d) For writing a rigid code with respect to subtype
.
. Answer: b
Explanation: When a general code has to be written w .
Upcasting - we use only the supertype object or the data of bigger
size, then upcasting would be the best option. Since the
“Upcasting”. whole code will require only the supertype name
1. What is upcasting? references.
a) Casting subtype to supertype 5. Which property is shown most when upcasting is
b) Casting super type to subtype used?
c) Casting subtype to super type and vice versa a) Code reusability
d) Casting anytype to any other type b) Code efficiency
c) Complex code simple syntax
Answer: a d) Encapsulation
Explanation: The upcasting concept includes only the
casting of subtypes to the super types. This casting is Answer: c
generally done implicitly. Smaller size types can fit Explanation: The code written using upcasting mostly
into larger size types implicitly. shows complex code in simpler syntax feature. This is
2. Which among the following is true for upcasting in because the upcasting concept can be applied as
inheritance? polymorphism and to group the similar type of objects.
a) Downward to the inheritance tree 6. Upcasting and downcasting objects is same as
b) Upward to the inheritance tree casting primitive types.
c) Either upward or downward a) True
d) Doesn’t apply on inheritance b) False

Answer: b Answer: b
Explanation: The upcasting concept in inheritance is Explanation: It is a bit confusing concept since both
always applied upward the inheritance tree. The casting concepts are different. Primitive casting
derived class objects can be type casted to any of its depends on the type and size of data being typecast.
parent class type. Since is a relationship applies in W .as in objects casting, the classes and inheritance
general inheritance. order plays a big role.
3. Which among the following is safe? 7. Which casting among the following is allowed for
a) Upcasting the code given below?
b) Downcasting class A
c) Both upcasting and downcasting {
d) If upcasting is safe then downcasting is not, and vice public :int a;
versa }
class B:public A
Answer: a {
Explanation: The upcasting is always safe since the int b;
derived type or the smaller type is converted into the }
128
main() Answer: a
{ Explanation: The public inheritance shows the most
B b=new A(); //casting 1 flexible is-a relationship. Hence explicit type casting is
A a=new B(); //casting 2 not required. Implicit type casting is done by the
} compiler.
a) Casting 1 11. Which concept is needed because of implicit type
b) Casting 2 casting use?
c) casting 1 and casting 2 a) Static binding
d) casting 1 nor casting 2 b) Dynamic binding
c) Compile time binding
Answer: b d) Source code binding
Explanation: The casting 2 is correct. The objects
casting must be done from derived class object to a Answer: b
parent class object. That is, the object of the superclass Explanation: Since the implicit type casting allows
can be made an object of subclass only. Vice versa is casting of a base class pointer to refer to its derived
not possible. class object or even base class object. We need
8. If multiple inheritance is implemented, which dynamic type casting so that the references can be
upcasting will be correct? resolved during execution of program.
a) Upcast to first base class listed in inheritance 12. When are the pointer types known for upcasting the
b) Upcast to send base class listed in inheritance objects?
c) Upcast to any base class a) Compile time
d) Upcast is not possible b) Runtime
c) Source code build time
Answer: c d) Doesn’t apply to pointer types
Explanation: The upcasting of derived class object is
possible to any base class. This is because the base Answer: a
class object can represent any of its derived classes Explanation: The pointer or reference types are known
using upcasting. at compile time for the upcasting of an object. This is
9. If class C inherits class B and class B inherits class because the addresses must be known for casting the
A ________________ derived class to base class object.
a) Class C object can be upcasted to object of class B 13. When are the object type known for upcasting the
only objects?
b) Class C object can be upcasted to object of class A a) Compile time
only b) Runtime
c) Class C object can be upcasted to object of either c) Source code build time
class A or B d) Doesn’t apply to objects directly
d) Class C object can’t be upcasted
Answer: b
Answer: c Explanation: The upcasting with objects directly
Explanation: Both class A and B are parent classes of requires runtime resolving. The objects are fixed and
class C. Class C object hence can be upcasted to any of address are allocated at compile time. But the
those class objects. It is not compulsory to upcast to execution of a program requires runtime knowledge of
nearest parent. object types, for implicit type cast.
10. Upcasting is _____________________ without an 14. If two classes are defined “Parent” and “Child”
explicit type cast. then which is the correct type upcast syntax in C++?
a) Always allowed for public inheritance a) Parent *p=child;
b) Always allowed for protected inheritance b) Parent *p=*child;
c) Always allowed for private inheritance c) Parent *p=&child;
d) Not allowed d) Parent *p=Child();

129
Answer: c c) Safe sometimes
Explanation: The syntax must contain the base class d) Safe, depending on code
name first. So that the parent class object pointer can
be declared. Then the object is assigned with the Answer: b
derived class object with & symbol. & symbol is added Explanation: The downcasting concept is made for
to get the address of derived class object. exception cases. When t . is a need to represent an
15. Which among the following is true? entity in the form which is not suitable for it.
a) Upcasting is possible only for single level Representing a base type in derived type is not right
inheritance but can be done for special cases.
b) Upcasting is possible only for multilevel inheritance 4. Downcasting ____________________
c) Upcasting is possible only for multiple inheritance a) Can result in unexpected results
d) Upcasting is possible for any type of inheritance b) Can’t result in unexpected result
c) Can result only in out of memory error
Answer: d d) Can’t result in any error
Explanation: The type of inheritance doesn’t matter
with the upcasting concept. Upcasting applies to all Answer: a
types of inheritance. Any derived class object can be Explanation: The result of downcasting can be
upcasted to any of its base class object. 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?
Downcasting - a) Static cast
b) Dynamic cast
“Downcasting”. c) Manual cast
1. What is downcasting? d) Implicit cast
a) Casting subtype to supertype
b) Casting supertype to subtype Answer: b
c) Casting subtype to supertype and vice versa Explanation: The dynamic cast can be done using the
d) Casting anytype to any other type operator dynamic_cast. This converts one type to
another type in safe way.
Answer: b 6. What does dynamic_cast return after successful type
Explanation: The downcasting concept includes only casting?
the casting of supertypes to the sub types. This casting a) Address of object which is converted
is generally done explicitly. Larger size types are made b) Address of object that is used for conversion
to fit into small size types explicitly. c) Address of object that is mentioned in the syntax
2. Which among the following is a mandatory d) Doesn’t return any address
condition for downcasting?
a) It must not be done explicitly Answer: a
b) It must be done implicitly Explanation: The address of the object which is
c) It must be done explicitly converted is returned by the dynamic_cast operator.
d) It can’t be done explicitly This is done to safely convert the subtype to supertype.
This ensure the proper assignment and conversion from
Answer: c one type to another.
Explanation: The downcasting of any object must be 7. If dynamic_cast fails, which value is returned?
done explicitly. This is because the compilers don’t a) void
support the implicit conversion of a supertype to b) null
subtype. c) void pointer
3. Downcasting is _______________________ d) null pointer
a) Always safe
b) Never safe
130
Answer: d 12. Which way the downcasting is possible with
Explanation: The null pointer is returned by the respect to inheritance?
dynamic_cast, if it fails. The conversion sometimes a) Upward the inheritance order
fails because of too complex type conversion. The b) Downward the inheritance order
conversion may also fail due to memory or some c) Either upward or downward the inheritance order
related issues. d) Order of inheritance doesn’t matter
8. Which is the proper syntax of dynamic_cast?
a) dynamic_cast(object) Answer: b
b) dynamic_cast new (object) Explanation: The downcasting is always downward the
c) dynamic_cast(object) inheritance order. Since the base class object have to
d) dynamic_cast(object) be casted into derived class type. This is basic
definition of downcasting.
Answer: c 13. What happens when downcasting is done but not
Explanation: The dynamic_cast is the name of explicitly defined in syntax?
operator, which is followed by the new type in which a) Compile time error
the object have to be converted. Then the object name b) Runtime error
is given. This object name is then used after the type c) Code write time error
conversion. d) Conversion error
9. Which is the exception handler for the exceptions of
downcasting? Answer: a
a) CastException Explanation: The implicit downcasting is not possible.
b) ClassCastingExeption If tried, the compiler produces an error. Since the
c) ClassCasting compiler doesn’t allow coasting to a type which is not
d) ClassCastException compatible.
14. When is downcating used?
Answer: d a) To separate inherited class from base class
Explanation: The exception handler for the exceptions b) To write a more complex code
produced during the downcasting exception. This c) To compare two objects
handler can be called during runtime to handle any d) To disable one class in inheritance
exception thrown.
10. How to prevent the ClassCastExceptions? Answer: c
a) By using instanceof Explanation: The downcasting can be used whenever
b) By using is-a check t . is a need to compare one object to another. Equals()
c) By using arrow operator with check function function can be used to compare whether the objects
d) By checking type of conversion were of same age. We can use getClass() function too.
15. Why is downcasting possible in any language?
Answer: a a) Because inheritance follows has-a relationship
Explanation: The instanceof operator can be used to b) Because inheritance follows is-a relationship
check the compatibility of the conversion. This has to c) Because inheritance doesn’t follow any relationship
be done to check whether the casting would be safe or d) Because inheritance is not involved in casting
not.
11. Java supports direct downcasting. Answer: b
a) True Explanation: The downcasting is possible because the
b) False classes in inheritance follow is-a relationship. Hence
the derived class is a base class. Which in turn make
Answer: b the downcasting possible.
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. New Operator -
131
programmer to give a custom definition for handling
“New Operator”. new operator failure.
1. What is new operator? 5. In C++, if new operator is used, when is the
a) Allocates memory for an object or array constructor called?
b) Allocates memory for an object or array and returns a) Before the allocation of memory
a particular pointer b) After the allocation of memory
c) Used as return type when an object is created c) Constructor is called to allocate memory
d) Used to declare any new thing in a program d) Depends on code

Answer: b Answer: b
Explanation: The new keyword is used to allocate Explanation: The constructor function is called after
memory of an object or array. The new object or array the allocation of memory. In C++ the feature works in
can be of any type. Then it return a suitable non zero a bit different way. The memory for all the data
pointer to the object. members is allocated first and then the constructor
2. Microsoft C++ Components extensions support new function is called to finalize the memory allocated.
keyword to _____________ 6. Which among the following is correct syntax to
a) Modify a vtable declare a 2D array using new operator?
b) Replace a vtable slot entry a) char (*pchar)[10] = new char[][10];
c) Add new vtable slot entries b) char (pchar) = new char[][10];
d) Rearrange vtable slot entries c) char (*char) = new char[10][];
d) char (*char)[][10]= new char;
Answer: c
Explanation: The new keyword is used for adding new Answer: a
vtable slot entries. This is an additional feature in Explanation: The new operator usage to declare a 2D
Microsoft C++. It can use predefined class object for array requires a pointer and size of array to be
this work. declared. Data type and then the pointer with size of
3. What happens when new fails? array. The left index can be left blank or any variable
a) Returns zero always can be assigned to it.
b) Throws an exception always 7. For declaring data by using new operator
c) Either throws an exception or returns zero ____________________
d) Terminates the program a) Type name can’t contain const
b) Type name can’t contain volatile
Answer: c c) Type name can’t contain class declarations
Explanation: While creating new objects, the new d) Type name can’t contain const, volatile, class
operator may fail because of memory errors or due to declaration or enumerations
permissions. At that moment the new operator returns
zero or it may throw an exception. The exception can Answer: d
be handled as usual. Explanation: The declaration of any data w . we use
4. If new throws an error, which function can be called new operator, any of the mentioned types are not
to write a custom exception handler? allowed. This is because the new operator allocated
a) _set_handler memory based on the type of data which can be
b) _new_handler allocated dynamically.
c) _handler_setter 8. The new operator _____________
d) _set_new_handler a) Can allocate reference types too
b) Doesn’t allocate reference types
Answer: d c) Can allocate reference to objects
Explanation: If the default exception handler have to d) Doesn’t allocate any data
be replaced by a user defined handler, we can call
_set_new_handler run-time library function with the Answer: b
function name as an argument. This lets the Explanation: The new operator doesn’t allocate
132
reference types. This is because the reference types are Answer: b
not objects. The new operator is used to allocate Explanation: It is not necessary that the objects get
memory to the direct objects. destroyed when they go out of scope if allocated by
9. Which among the following is true? using new operator. This is because new operator
a) New operator can’t allocate functions but pointer to returns a pointer to object that it had allocated. A
functions can be allocated suitable pointer with proper scope should be defined by
b) New operator can allocate functions as well as the programmer explicitly.
pointer to functions 13. The new operator _________________
c) New operator can allocate any type of functions a) Invokes function operator new
d) New operator is not applicable with functions b) Doesn’t invoke function operator new
allocation c) Invokes function operator only if required
d) Can’t invoke function operator new implicitly
Answer: a
Explanation: The new operator can’t allocate functions Answer: a
but can allocate pointer to the functions. This is a Explanation: The new operator invokes function
security feature as well as to reduce the ambiguity in operator new. This is done to allocate the storage to an
code. The new keyword is not given functionality to object. ::operator new is called for storage allocation
directly allocate any function. implicitly.
10. Which among the following is added in grammar 14. If new operator is defined for a class and still
of new operator? global new operator have to be used, which operator
a) Finalize should be used with the keyword new?
b) Arg a) Colon
c) Initializer b) Arrow
d) Allocator c) Dot
d) Scope resolution
Answer: c
Explanation: The new operator grammar is added with Answer: d
an initializer field. This can be used to initialize an Explanation: As usual, scope resolution operator is
object with a user defined constructor. Hence can used to get the scope of parent or the global entities.
allocate memory as intended by the programmer. Hence we can use scope resolution operator with the
11. Initializers __________________ new operator to call the global new operator even if
a) Are used for specifying arrays new operator is defined for the class explicitly.
b) Are used to defined multidimensional arrays 15. How does compiler convert “::operator new”
c) Can’t be specified for arrays implicitly?
d) Can’t be specified for any data a) ::operator new( sizeof( type ) )
b) ::operator new( sizeof( ) )
Answer: c c) new operator :: type sizeof( type )
Explanation: The initializers can’t be specified for d) new sizeof( type ) operator
arrays. The initializers can create arrays of object if
and only if the class has a default constructor. That is a Answer: a
zero argument constructor so that it can be called Explanation: The compiler implicitly converts the
without any argument. syntax so that the instruction can be understood by the
12. The objects allocated using new operator processor and proper machine code can be generated.
________________ The conversion is done implicitly and no explicit
a) Are destroyed when they go out of scope syntax is required.
b) Are not destroyed even if they go out of scope .
c) Are destroyed anytime .
d) Are not destroyed throughout the program execution
Delete Operator -

133
“Delete Operator”. 5. If delete is used to delete an object which was not
1. What is delete operator? allocated using new _______________
a) Deallocates a block of memory a) Then out of memory error arises
b) Deallocates whole program memory b) Then unreachable code error arises
c) Deallocates only primitive data memory c) Then unpredictable errors may arise
d) Deallocates all the data reserved for a class d) Then undefined variable error arises

Answer: a Answer: c
Explanation: The delete operator is reverse process of Explanation: When the delete operator is used with the
new operator. It deallocates all the memory allocated objects that were not allocated using new operator then
for an object. The object can be of any type. The delete unpredictable errors may arise. This is because the
operator completely destroys an object so that the delete can’t perform the required actions on the type of
resources can be used for other purposes. memory allocated for the object.
2. If an object is allocated using new operator 6. Delete operator _________________
____________ a) Can be used on pointers with null value
a) It should be deleted using delete operator b) Can be used on pointers with void value
b) It can’t be deleted using delete operator c) Can be used on pointer with value 0
c) It may or may not be deleted using delete operator d) Can be used on pointer with any value
d) The delete operator is not applicable
Answer: c
Answer: a Explanation: The delete operator can be used on
Explanation: The new operator allocates an object in pointers with the value 0. This actually means that
memory and hence the memory allocation is bit when new operator fails and return value 0 then
different from usual allocation of an object. The delete deleting the result of failed new remains harmless.
operator can be used to delete the memory allocated for Hence the deletion is possible.
an object. 7. When delete operator is used
3. Does delete return any value? ___________________ (If object has a destructor)
a) Yes, positive value a) Object destructor is called after deallocation
b) Yes, negative value b) Object destructor is called before deallocation
c) Yes, zero value c) Object destructor is not used
d) No d) Object destructor can be called anytime during
destruction
Answer: d
Explanation: The delete operator doesn’t return any Answer: b
value. Its function is to delete the memory allocated for Explanation: The destructor is called before the
an object. This is done in reverse way as that new memory is deallocated for any object. The destructor
operator works. call initiates the destruction process and the
4. Which type of value is resulted from the delete deallocation of memory takes place.
operator? 8. If delete is applied to an object whose l-value is
a) void modifiable, then _______________ after the object is
b) void pointer deleted.
c) null pointer a) Its value is defined as null
d) null b) Its value is defined as void
c) Its value is defined as 0
Answer: a d) Its value is undefined
Explanation: The result of delete operator is void. The
values returned is of no use to the program or any other Answer: d
system function hence the return type is not defined for Explanation: After performing delete operation on an
the delete operator. object whole l-value is modifiable, its values becomes
undefined. This is done so as to denote that the
134
memory space is available to be used for other b) Invokes function defined by user to delete
purposes. c) Invokes function defined in global scope to delete
9. How many variants of delete operator are available? object
a) Only 1 d) Doesn’t invoke any function
b) Only 2
c) Only 3 Answer: a
d) Only 4 Explanation: The delete operator invokes the function
operator delete. This function in turn performs all the
Answer: b delete operations on the mentioned object. This is
Explanation: T . are two variants of delete operator. ensures safe deletion.
One is for object deletion. Other is for deletion of 14. For objects that are not of class type
object array. ______________
10. Which is the correct syntax to delete a single a) Global delete operator is invoked
object? b) Local delete operator is invoked
a) delete *objectName; c) Global user defined function is invoked
b) objectName delete; d) Local function to delete object is called
c) delete objectName;
d) objectName *delete; Answer: a
Explanation: The global delete operator is called to
Answer: c delete the objects that are not of class type. Class type
Explanation: The object to be deleted is mentioned includes class, union or struct. All objects of these
after the keyword delete. This deletes the object from types can be deleted using the global delete operator.
memory and free up the memory that was acquired by 15. The delete operator
the object. __________________________
11. Which is the correct syntax to delete array of a) Can be defined for each class
objects? b) Can’t be defined for each class
a) delete [] objectName; c) Can be defined globally only
b) delete * objectName; d) Can’t be defined in a program explicitly
c) objectName[] delete;
d) delete objectName[]; Answer: a
Explanation: The delete operator can be defined for
Answer: a each class explicitly. If t . is a class for which delete is
Explanation: The object array that has to be deleted is not defined then the global delete operator is used. The
mentioned after the keyword delete. But after delete, definition of delete operator for each class is not
empty square brackets have to be given to denote that necessary.
the deletion have to be done on array of objects. .
12. Which cases among the following produces the .
undefined result?
a) delete [] on an independent object Automatic Variable -
b) delete on an object array
c) delete [] on an object and delete on object array “Automatic Variable”.
d) Undefined result is never produced 1. What are automatic variables?
a) Global variables
Answer: c b) Implicit/temporary variables
Explanation: The undefined result is always produced c) Local variables
when we try to use delete [] with a single object. d) System variables
Because the type of deletion mismatches. Same in case
w . we try to apply delete to an object array. Answer: c
13. The delete operator __________________ Explanation: The local variables are also known as
a) Invokes function operator delete automatic variables. The variables in any local scope
135
that are created and destroyed as the program executes called function gets executed. This is done so as to
its scope. ensure that the program can continue its execution after
2. The memory for automatic variables the called function is returned. The automatic variables
___________________ gets destroyed only if those go out of scope.
a) Have to be allocated and deallocated explicitly 6. W . are the automatic variables stored if another
b) Are allocated and deallocated automatically function is called in between the execution of
c) Is never actually allocated program?
d) Are never safe a) Heap
b) Queue
Answer: b c) Stack
Explanation: The memory is allocated and deallocated d) Temp variable
automatically for the automatic variables. As soon as
the variable comes in scope, the memory is allocated. Answer: c
The variables are destroyed as soon as those go out of Explanation: All the automatic variables are stored in a
scope. new stack entry as soon as their scope is created. If
3. Scope of an automatic variable _______________ another function is called, the present data is saved in
a) Is actually the whole program stack and new entry in stack is made for the called
b) Is actually never fixed function. When the function returns, the automatic
c) Is always equal to the whole program execution variables are used again from w . those were left.
d) Is actually function or block in which it is defined 7. The static variables of a function
________________
Answer: d a) Are also automatic variables
Explanation: The automatic variables scope is limited b) Are not automatic variables
only within the block or the function w . those are c) Are made automatic by default
defined. This is property of all the automatic variables. d) Can be made automatic explicitly
4. Which among the following is true for automatic
variables in general? Answer: b
a) Automatic variables are invisible to called function Explanation: The static members can’t be automatic.
b) Automatic variables are always visible to the called This is because the automatic variables are created and
function destroyed with each call to a specific function. But the
c) Automatic variables can’t interact with the called static members remain throughout the execution of
function program once created.
d) Automatic variables can’t be variable 8. All variables declared within a block
____________________
Answer: a a) Are not always automatic
Explanation: The automatic variables are hidden from b) Can be made non-automatic
the called function. Even if passed by reference or c) Are static by default
address, the address of the variable is used and not the d) Are automatic by default
actual variable of calling function. Automatic variables
can be const or variable. Answer: d
5. If an automatic variable is created and then a Explanation: The variables declared inside a block, are
function is called then ________________ make automatic by default. This is to ensure that the
a) The automatic variable created gets destroyed variables get destroyed when not required. The
b) The automatic variable doesn’t get destroyed variables remain live only till those are required, the
c) The automatic variable may or may not get life is dependent on the scope of variable.
destroyed 9. What values does uninitialized automatic variables
d) The automatic variable can’t be used in this case contain?
a) Null value
Answer: b b) Void value
Explanation: The automatic variables are saved till the c) Undefined/Garbage
136
d) Zero value 13. Which error is produced if the automatic variables
are used without declaration?
Answer: c a) Undefined symbol
Explanation: The automatic variable which are not b) Memory error
initialized, contain garbage value. If we just declare a c) Type mismatch
variable and try to print its value, the result is some d) Statement missing
unknown value. The value is garbage as that was not
expected value. Answer: a
10. Constructor of automatic variables is called Explanation: If the automatic variables are used
____________________ without declaration or are used before the declaration
a) When execution reaches the place of declaration of then the compiler throws an error. The error that the
automatic variables symbol is undefined. The compiler must know
b) When the program is compiled everything before that can be used.
c) When the execution is just started 14. In perl, using which operator are the local variables
d) Just before the execution of program created?
a) Dot
Answer: a b) Arrow
Explanation: Only when the execution reaches the c) Scope resolution
place w . the automatic variable was declared, the d) my
constructor is called. This is to ensure that the memory
is not allocated if not needed. The memory is allocated Answer: d
and then destroyed as soon as it goes out of scope. Explanation: The language perl supports local
11. Does java contain auto or register keywords? variables but the concept is bit different. And if the
a) Yes, for declaring every type of variable values are not assigned to the local variables then it
b) Yes, only to declare cache registers contains undef value.
c) No, because java doesn’t support automatic 15. How are automatic variables different from the
variables instance variables?
d) No, java supports local variable concept a) Automatic variables are initialized automatically but
instances are not
Answer: d b) Automatic variables are given zero values initially
Explanation: The auto and register keywords are not and not instances
supported in java. Though the same is allowed in java c) Instance variables have to be initialized explicitly
without specifying any of those keywords. The and automatic implicitly
variables are local variables. But java makes it d) Instance variables are initialized implicitly while
mandatory to initialize all of the local variables in a automatic are not
program.
12. The automatic variables Answer: d
_________________________ Explanation: The automatic variables have to be
a) Must be declared after its use initialized explicitly. But in case of instances, those are
b) Must be declared before using initialized automatically during execution of program.
c) Must be declared, can be anytime The conventions are mandatory.
d) Must not be initialized .
.
Answer: b
Explanation: All the automatic variables in a program Object Oriented Programming Questions for
must be declared before their use. The compiler won’t Campus Interviews -
allow any use of variable if those are not declared
before their use.

“Extern Variable”.
137
1. What is extern variable? Answer: c
a) Variables to be used that are declared in another Explanation: The source file must be included in the
object file file which needs to use the extern variable. This is
b) Variables to be used that are declared in another done to ensure that the variables that are already
source file declared can be used again. Only the declarations are
c) Variables to be used that are declared in another used from one file to another.
executable file 5. What does a header file contain for an extern
d) Variables to be used that are declared in another variable?
program a) Only declaration of variables
b) Only definition of variables
Answer: b c) Both declaration and definition of variables
Explanation: The variables that are declared in d) Neither declaration nor definition
another source file can be accessed in other files
using extern variables. The extern variables must be Answer: a
mentioned explicitly. The source file is included to Explanation: The header file only contains the
use its variables. declaration of variables that are extern. It doesn’t
2. Which among the following is a correct statement contain any static variable definitions.
for variables? 6. Which condition is true if extern variable is used
a) Variable can be declared many times in a file?
b) Variable can be declared only one time a) All the header files declare it
c) Variable declaration can’t be done more than ones b) Only few required files declare it
d) Variable declaration is always done more than one c) All header files declared it if required
time d) Only one header file should declare it

Answer: a Answer: d
Explanation: The variables can be declared any Explanation: Only one header file should declare the
number of times. T . is no restriction on how many extern variable to be used. T . must not be more than
times a single variables can be declared. Declaration one file declaring the same extern variable. This is to
is just an indication that the variable will be used in ensure that t . is no ambiguity in using the extern
the program. variable.
3. Which among the following is true for the 7. Whenever a function is declared in a program
variables? _____________________
a) Variable can be defined only once a) extern can be used only in some special cases
b) Variable can be defined any number of times b) extern can’t be used
c) Variable must be defined more than one time c) function is extern by default
d) Variable can be defined in different files d) it can’t be made extern

Answer: a Answer: c
Explanatoin: The variables can be defined only once. Explanation: Even if we don’t specify a function to
Once the variable is defined, then it can’t be declared be extern, by default all the functions are exter. The
again. The definition of a variable is actual allocation compiler adds the keyword at the beginning of the
of memory for the variable. function declaration. If t . is an extern function to be
4. To use extern variable _____________________ used then it will be used otherwise the new function
a) The source file must not be included in the new only will be used.
file code 8. Even if a variable is not declared as extern, it is
b) The source file itself must be used for new extern by default. (True/False)
program a) True
c) The source file must be included in the new file b) False
d) The source file doesn’t matter for extern variables

138
Answerr: b {
Explanation: The statement is false. The variables are var = 10;
not extern by default. If those are made extern by var++;
default, then the memory will never be allocated for cout<<var;
those extern variables. Hence we make the variables }
extern explicitly. a) 10
9. Due to what, is the memory allocated for the b) 11
extern variables? c) Run time error
a) Declaration d) Compile time error
b) Definition
c) Including file Answer: d
d) Memory is not allocated for extern variables Explanation: The program gives the compiler time
error. T . is no definition given for the extern
Answer: b variables. This is not allowed and hence we get a
Explanation: The memory for the extern variables are compile time error.
allocated due to their definition. When the variables 13. If the definition is given in the header file that we
are declared, it only indicates the compiler that the include then ________________
variable is going to be used somew .. But definition a) The program can run successfully
makes the compiler to allocate the memory for the b) Also the program should define the extern variable
variables. c) The extern variable must contain two definitions
10. Which is the correct syntax for extern variable d) Extern variable can’t be used in the program
declaration?
a) extern data_type variable_name; Answer: a
b) extern variable_name; Explanation: The program runs successfully. This is
c) data_type variable_name extern; because only one definition of any variable is
d) extern (data_type)variable_name; allowed. And hence the definition from the source
file that is included will be used.
Answer: a 14. If extern variable is initialized with the
Explanation: The syntax firstly contains the keyword declaration then _______________________
extern. Then the data type of the variable is given. a) Also the header file with definition is required
Then the variabel name is mentioned by which it will b) The header file with definition must be included
be used in the program. c) T . is no need to include any other header file for
11. Which is the correct syntax for extern function definition
declaration? d) The extern variable produces compile time error
a) extern function_name(argument_list);
b) extern return_type function_name(argument_list); Answer: c
c) extern Explanation: When the value for the extern variable
(return_type)function_name(argument_list); is defined with its declaration, then t . is no need to
d) return_type extern function_name(argument_list); include any file for the definition of the variable. The
Initialization acts as a definition for the extern
Answer: b variable in the file itself.
Explanation: The syntax must contain the keyword 15. Why are functions extern by default?
extern first, to denote that the function is extern. a) Because functions are always private
Though the function are extern by default but among b) Because those are not visible throughout the
the given choices, it should contain the keyword, for program
explicit declaration. Then the usual function c) Because those can’t be accessed in all parts of
declaration follows. program
12. What will be the output of the program? d) Because those are visible throughout the program
extern int var;
int main(void)
139
Answer: a c) String
Explanation: The program have all of its functions d) Functions
visible throughout the program usually. Also t . is no
specific value that a function must contain. Hence the Answer: d
functions are extern by default. Explanation: T . is no inbuilt class named function in
. java. The others are classes already provided in java.
. All those classes contain some special functions to be
used in programming.
Inbuilt Classes - 5. What is InputStream class meant for?
a) To handle all input streams
“Inbuilt Classes”. b) To handle all output streams
1. What are inbuilt classes? c) To handle all input and output streams
a) The predefined classes in a language d) To handle only input from file
b) The classes that are defined by the user
c) The classes which are meant to be modified by the Answer: a
user Explanation: The InputStream is an inbuilt class
d) The classes which can’t be used by the user which is used to handle all the tasks related to input
handling. This class extends input from keyboard or
Answer: a file or any other possible input stream.
Explanation: The classes that are already provided in 6. Which statement is true for Array class?
a programming language for use are inbuilt classes. a) Arrays can have variable length
These classes provide some functions or objects that b) The length array can be changed
can be used by the programmer for easier code. c) Each class has an associated Array class
2. Inbuilt class __________________________ d) Arrays can contain different type of values
a) Must be included before use
b) Are not necessary to be included for use Answer: c
c) Are used by the compiler only Explanation: The Array class is associated with all
d) Can be modified by programmer always the other classes. This gives us flexibility to declare
an array of any type. The index goes from 0 to n, w .
Answer: a n is some fixed size for array.
Explanation: The inbuilt classes must be included in 7. What is the use of Math class?
the program. Whenever some functions are used, a) To use the mathematical functions with strings
they must have a declaration before use. The same is b) To use the mathematical functions
case with classes. c) To suppress the use of mathematical functions
3. What doesn’t inbuilt classes contain? d) To complex the calculations
a) Function prototype
b) Function declaration Answer: b
c) Function definitions Explanation: The Math class is provided with some
d) Objects special functions. These functions can be used to
calculate and get result of some special and usual
Answer: c mathematical functions. We don’t have to write the
Explanation: The classes contain the definitions of code to calculate the trigonometric function results,
the special functions that are provided for the instead we can use Math functions.
programmers use. Those functions can be used to 8. DataInputStream is derived from
make the programming easy and to reuse the already ______________________
existing code. a) StreamingInput
4. Which among the following not an inbuilt class in b) StreamedInput
C++? c) StreameInput
a) System d) StreamInput
b) Color
140
Answer: d finalize an object which undergo destruction. The
Explanation: The DataInputStream is more specific action is required to terminate the object properly.
class for operating on specific type of data inputs. 13. What does load(String)::= function do, in System
This is used to read data of specific type. The same class?
can be used to read data in a specific format. a) Loads dynamic library for a path name
9. Which attribute can be used to get the size of an b) Loads all the dynamic libraries
array? c) Loads all the Number in string format
a) Size.Array d) Loads the processor with calculations
b) Array.Size
c) Array_name.length Answer: a
d) length.Array_name Explanation: Only the specified path named dynamic
libraries are loaded. All the dynamic libraries can’t
Answer: c be loaded at a time. Hence we use this function for
Explanation: The array name is given of which the specific libraries.
length have to be calculated. The array length is 14. Which is not a System class variable?
stored in the attribute length. Hence we access it a) err
using dot operator. b) out
10. Number class can’t manipulate c) in
____________________ d) put
a) Integer values
b) Float values Answer: d
c) Byte values Explanation: Put is not a System class variable. The
d) Character values most general and basic variables are err, out and in.
The variables can handle most of the tasks performed
Answer: d in a program.
Explanation: The Number class is used to work with 15. Which package contains the utility classes?
all the number type of values. The integers, float, a) java.lang
double, byte etc. are all number type values. b) java.utility
Character is not a number value. c) java.util
11. Which function should be used to exit from the d) java.io
program that is provided by System class?
a) exit(int); Answer: c
b) gc(); Explanation: The package java.util contains all the
c) terminate(); utility classes. This package also contains generic
d) halt(); data structures, date, time etc. These can be used in
any java program, you just have to include java.util
Answer: a package.
Explanation: The exit function should be used to .
terminate the program. The function is passed with .
an argument. The argument indicated the type of
error occurred. IO Class -
12. Which class contain runFinalization() method?
a) Finalize “IO Class”.
b) System 1. What is the use of IO class?
c) Final a) To handle all the input operations
d) SystemFinal b) To handle all the output operations
c) To handle all the input and output operations
Answer: b d) To handle all the input and output to the standard
Explanation: The runFinalization() Function is input
defined in the System class. The function is used to
141
Answer: c c) A handle for compiler specific structure of an open
Explanation: The IO class provides functions that can file
be used to handle input and output operations. All the d) A handle for representing device files structure
inputs from standard input and standard output, and
also from the files can be handled. This gives Answer: a
flexibility to make the programs more user friendly. Explanation: The machine specific structure of an
2. IO class provides input and output through open file have to be handled in some special ways.
______________________ FileDescriptor class can handle those files. The
a) Data streams FileDescriptor can also handle open socket, another
b) Serialization source, sink of bytes.
c) File system 6. FileInputStream _________________________
d) Data streams, serialization and file system a) Gets the input stream from any device file
b) Gets the input stream from any open socket
Answer: d c) Gets the input stream from any cache
Explanation: The IO classes are made such that those d) Gets the input stream from any open file only
can support the input and output from any type of
source or destination. The input can be taken from Answer: d
system file and standard input and also some special Explanation: The most specific answer is that the
devices if conned. Same is case to show the output. FileInputStream can only be used for the opened
3. Which among the following class contains the files. The class can work only for the file type. No
methods to access character based console device? socket or another source are allowed to be accessed.
a) Console 7. What does FilePermission class do?
b) File a) This class is used to give permission rights to a file
c) Device b) This class is used to restrict use of permissions
d) Pipe c) This class is used to represent device access
permissions
Answer: a d) This class is used to represent file access
Explanation: The Console class contains the methods permissions
to access the character based devices. The devices
which can stream the data as character set. All those Answer: d
devices can be made use of by using the methods of Explanation: The FilePermission can’t get access to
class Console. the device access permissions. The Permission is
4. File class is ____________________________ given to a file when it is created or otherwise when a
a) An abstract of file representation only privileged user changes it. Then these permission
b) An abstract of path names only rights can be accessed using the FilePermission class.
c) An abstract which can be used to represent path 8. Which class among the following makes incorrect
names or file assumptions?
d) An abstract which can represent a file in any a) LineNumberInputStream
format b) LineNumberReader
c) LineReader
Answer: c d) LineBuffer
Explanation: The File class is made to operate with
the files. The file can be of any type. All the input Answer: a
and output operations that have to be performed on a Explanation: The LineNumberInputStream class
file can be done using File class object. makes false assumptions. The false assumption is
5. What is a FileDescriptor? that it assumes, all the byte data is a character. Which
a) A handle for machine specific structure of an open is actually not the case, instead the character have
file one byte memory space.
b) A handle for program specific structure of an open 9. Reader class is _________________
file a) Used to read from files
142
b) Abstract class to read character streams Answer: a
c) Abstract class to input character streams Explanation: The PushBackReader allows the
d) Used to take input from standard input stream character streams handling. The main feature is that
the stream can be pushed back to the stream. This is
Answer: b used in special cases of handling input stream.
Explanation: The Reader class is an abstract class 14. RandomAccessFile can be used to
which can be used to read characters stream. It can’t _______________________
be used for any kind of input. It can just read the a) Read from a random access file
existing data. b) Write to a random access file
10. Which class can handle IO class interrupt? c) Read and write to a random access file
a) ExceptionIO d) Restricts read and write to a random access file
b) InteruptedIO
c) InteruptedIOException Answer: c
d) IOInteruptException Explanation: The RandomAccessFile class instance
can be created to handle input and output operations
Answer: c to a random access file. It first checks the
Explanation: The only class which handles the IO permissions on the file and then any required
class interrupts is InteruptedIOException class. This operation can be done on a random access file.
class is specially provided to handle any case that Comparatively faster than other files access.
involves the execution interrupt. 15. Which among the following is a serialization
11. StringReader handles _____________________ descriptor for any class?
a) Any character stream a) StreamClass
b) A character stream whose source is an array b) ObjectStreamClass
c) A character stream whose source is character array c) ObjectStream
d) A character stream whose source is String only d) StreamObjectClass

Answer: d Answer: b
Explanation: The StringReader can only work with Explanation: The ObjectStreamClass object can be
the string type data. Even if a character array is created to handle serializations. The class is provided
given, it might produce some errors in code. Hence specially for the serializations. It is descriptor like we
only the string values can be handled properly. have a file descriptor to handle/access files.
12. Which exception handler can be used when .
character encoding is not supported? .
a) UnsupportedException
b) UnsupportedEncodingException String Class -
c) SupportException
d) EncodingException “String Class”.
1. Which is a true statement for object of String
Answer: b class?
Explanation: The encoding that is unsupported in a a) Object are immutable
system can be handled. The exception handler is b) Object are mutable
UnSupportedEncodingException class. An object of c) Object are created only once
this class can be created which will catch the d) Object can’t be created
exception and handle it.
13. PushBackReader allows the streams to be pushed Answer: a
back to the stream. Explanation: The object of string class are mostly
a) True immutable. This means that the String objects are
b) False constant. These can’t be changed once created.
2. How to declare an object of class String?
a) String object_Name = value;
143
b) String object_name = new; specified index till the end. The substring function
c) String object_name= new value; have to be called with the object of string class.
d) String object_name= value new; 6. If two index are given as argument to substring
function then ___________________
Answer: a a) String of length equal to sum of two arguments is
Explanation: The class name String is given. And returned
then the object name is mentioned. T . are two ways b) String starting from first index and of length equal
to declare and initialize the string. Either by giving to send argument
direct string value or by using new keyword. But if c) String starting from first index and of length equal
new operator is used, constructor of String class have to sum of two arguments
to be called. From the given options, the direct string d) String starting from first index and ending at
value declaration is correct. second index position
3. What does function length do in String class?
a) Returns length of string including null character Answer: d
b) Returns length of string excluding null character Explanation: A value of string type is returned from
c) Returns length of substring this function. The returned string is a substring that
d) Returns size of string in bytes starts from the first argument position, till the second
index position. The indices must be less than the
Answer: b length of actual string.
Explanation: The length function returns the length 7. String class have a concat() function that is used to
of string. The length is the number of characters in _____________________
the string but the last null character is not counted. a) Replace old string by new string
The string length can be used to loop through each b) Add two strings
character in the string. c) Append one string at end of another string
4. Which is the function to get the character present d) Remove a string from end of one string
at a particular index in the string?
a) char charAt(index); Answer: c
b) char charIn(StringName); Explanation: The concat function is used to append
c) char charAt(StringName); string into another string. The new string is always
d) char charIn(index); appended at the end of source string. The target
string is appended as it is and the whole string is then
Answer: a ended by null character.
Explanation: The function can be called using dot 8. The function lastIndexOf() is used to
operator with the string object. Char is the return ___________________
type of the function to return the character at a) Get the index of last occurrence of specified
specified position. The index must be an integer character in argument
value, less than the length of string. b) Get the index of first occurrence of specified
5. If only one parameter is passed to substring character in argument
function then __________________ c) Get the index of last occurrence of first character
a) It returns the character at the specified position in string
b) It returns the string of length 1 from the specified d) Get the index of last occurrence of last character
index of string
c) It returns the string from specified index till the
end Answer: a
d) It returns the string from starting of string till the Explanation: The function is used to get the last
specified index occurrence index of a character present in a string.
The return type is char. Single character is returned.
Answer: c The function is used with a string object and the
Explanation: The substring function returns a string target character is passed as its argument.
value. The string is the substring starting from the
144
9. Function equals() is _______________ and white space from both the ends of a given string. The
equalIgnoreCase() is _________________ white space include space, tab, next line etc. It will
a) Case Insensitive, case insensitive be removed both from the starting of string and from
b) Case sensitive, Case insensitive end of string.
c) Case sensitive, case sensitive 13. Function replace() accepts _____________
d) Case insensitive, case sensitive arguments.
a) 1
Answer: b b) 2
Explanation: Both the functions return Boolean c) 3
value. The function equal() is case sensitive and d) 4
returns false even if a single character is case
different in two strings. The other function ignores Answer: b
the case sensitivity and only checks if the spellings Explanation: The first argument is the target
are same. character. This target character will be replaced by
10. The compareTo() function is used to another character. The new character is the second
________________ argument to the function. Only the characters can be
a) Compare strings value to string object passed as argument, not a string.
b) Compare string value to string value 14. If two arguments are passed to the indexOf()
c) Compare string object to another string object function then ___________________
d) Compare string object to another string value a) Second argument indicates the occurrence number
of specified character from starting
Answer: c b) Second argument indicates the occurrence number
Explanation: The source and target must be objects of specified character from end
of the string class. The compare is always case c) Second argument indicates the index of the
sensitive. To compare two string objects without case character in first argument
sensitivity then we can use compareToIgnoreCase() d) Second argument indicates the index of the
function. character from the last of the string
11. String class provides function toUpper() to
_____________________ Answer: a
a) Convert first character to uppercase Explanation: The string may have more than one
b) Convert last character to uppercase occurrence of a character. We use this function to get
c) Convert the whole string characters to uppercase the index at which the specified number of times a
d) Convert uppercase to lower and lower to specific character has occurred in a string. For
uppercases example, we can get the index of 5th occurrence of
character “j” in a string.
Answer: c 15. The string class deals with string of only
Explanation: The function is used to convert each character type.
character of the string. If the character is already a) True
uppercase then it remains the same. But if some b) False
character is in lowercase then it will be converted to
uppercase. Answer: a
12. String trim() function is used to Explanation: The string class objects can be used for
_______________________ any string consisting of characters. The characters
a) Remove all the white spaces from the string include numbers, alphabets and few special
b) Remove white space from start of string characters. String class is not necessary to be used
c) Remove white space at end of string but provides a huge set of inbuilt functions to make
d) Remove white space from both the ends of string the string operations easier.
.
Answer: d .
Explanation: The function is used to remove any
145

You might also like