MCQS Set Oop PDF
MCQS Set Oop PDF
MCQS SET_OOP
a) Code reusability
b) Modularity
c) Duplicate/Redundant data
d) Efficient Code
------------
Answer: c
-. Pure OOP can be implemented without using class in a program. (True or False)
a) True
b) False
------------
Answer: b
Explanation: It’s false because for a program to be pure OO, everything must be written inside classes. If
this rule is violated, the program can’t be labelled as purely OO.
Answer: b
Explanation: Java doesn’t support all 4 types of inheritance. It doesn’t support multiple inheritance. But
the multiple inheritance can be implemented using interfaces in Java.
a) Only 1
b) Only 100
c) Only 999
Answer: d
Explanation: Any number of classes can be defined inside a program, provided that their names are
different. In java, if public class is present then it must have the same name as that of file.
a) 1970’s
b) 1980’s
c) 1993
d) 1995
------------
Answer: a
Explanation: OOP first came into picture in 1970’s by Alan and his team. Later it was used by some
programming languages and got implemented successfully, SmallTalk was first language to use pure
OOP and followed all rules strictly.
------------
Answer: a
Explanation: As Java supports usual declaration of data variables, it is partial implementation of OOP.
Because according to rules of OOP, object constructors must be used, even for declaration of variables.
------------
Answer: b
Explanation: In C++, it’s not necessary to use classes, and hence codes can be written without using OOP
concept. Classes may or may not contain member functions, so it’s not a necessary condition in C++.
And, an object can only be declared in a code if its class is defined/included via header file.
a) iostream.h
b) stdio.h
c) stdlib.h
------------
Answer: d
Explanation: We need not include any specific header file to use OOP concept in C++, only specific
functions used in code need their respective header files to be included or classes should be defined if
needed.
------------
Answer: c
Explanation: Encapsulation and Abstraction are similar features. Encapsulation is actually binding all the
properties in a single class or we can say hiding all the features of object inside a class. And Abstraction
is hiding unwanted data (for user) and showing only the data required by the user of program.
------------
Answer: a
Explanation: Use of this pointer allows an object to call data and methods of itself whenever needed.
This helps us call the members of an object recursively, and differentiate the variables of different
scopes.
a) Abstract Class
b) Final Class
c) Start Class
d) String Class
------------
Answer: c
Explanation: Only 9 types of classes are provided in general, namely, abstract, final, mutable, wrapper,
anonymous, input-output, string, system, network. We may further divide the classes into parent class
and subclass if inheritance is used.
a) Value
b) Reference
d) Copy
------------
Answer: b
Explanation: Classes are pass by reference, and the structures are pass by copy. It doesn’t depend on the
program.
class Student
int a;
public : float a;
};
d) It is correct
------------
Answer: a
Explanation: Same variable can’t be defined twice in same scope. Even if the data types are different,
variable name must be different. There is no rule like Public member should come first or last.
a) Abstract class
b) Final class
c) Template class
d) Efficient Code
------------
Answer: c
Explanation: Template classes are known to be generic classes because those can be used for any data
type value and the same class can be used for all the variables of different data types.
a) Sum of the size of all the variables declared inside the class
b) Sum of the size of all the variables along with inherited variables in the class
------------
Answer: d
Explanation: Classes doesn’t have any size, actually the size of object of the class can be defined. That is
done only when an object is created and its constructor is called.
a) Protected scope
b) Private scope
c) Global scope
------------
Answer: d
Explanation: It depends on the access specifier and the type of inheritance used with the class, because
if the class is inherited then the nested class can be used by subclass too, provided it’s not of private
type.
a) True
b) False
------------
Answer: a
Explanation: The class containing main function can be inherited and hence the program can be
executed using the derived class names also in java.
d) Member functions can be made friend to another class using friend keyword
------------
Answer: c
Explanation: Member functions must be declared inside class body, thought the definition can be given
outside the class body. There is no way to declare the member functions inside the class.
a) class student{ };
b) student class{ };
------------
Answer: b
Explanation: Keyword class should come first. Class name should come after keyword class.
Parameterized constructor definition depends on programmer so it can be left empty also.
------------
Answer: b
Explanation: Class and structure are similar to each other. Only major difference is that a structure
doesn’t have member functions whereas the class can have both data members and member functions.
a) Classes may/may not have both data members and member functions
------------
Answer: b
Explanation: Class definition must end with a semicolon, not colon. Class can have only member
functions in its body with no data members.
a) Anonymous class
b) Nested class
c) Parent class
d) Abstract class
------------
Answer: d
Explanation: Instance of abstract class can’t be created as it will not have any constructor of its own,
hence while creating an instance of class, it can’t initialize the object members. Actually the class
inheriting the abstract class can have its instance because it will have implementation of all members.
a) Instance of a class
b) Instance of itself
c) Child of a class
d) Overview of a class
------------
Answer: a
Explanation: An object is instance of its class. It can be declared in the same way that a variable is
declared, only thing is you have to use class name as the data type.
-. Which among the following is false?
------------
Answer: c
Explanation: Objects can be passed by reference. Objects can be passed by value also. If the object of a
class is not created, we can’t use members of that class.
------------
Answer: c
Explanation: The array must be specified with a size. You can’t declare object array, or any other linear
array without specifying its size. It’s a mandatory field.
a) Passed by reference
b) Passed by value
c) Passed by copy
d) Passed as function
------------
Answer: d
Explanation: Object can’t be passed as function as it is an instance of some class, it’s not a function.
Object can be passed by reference, value or copy. There is no term defined as pass as function for
objects.
a) 20
b) 22
c) 24
d) 28
------------
Answer: c
Explanation: The size of any object of student class will be of size 4+20=24, because static members are
not really considered as property of a single object. So static variables size will not be added.
-. If a local class is defined in a function, which of the following is true for an object of that class?
------------
Answer: d
Explanation: For an object which belongs to a local class, it is mandatory to declare and use the object
within the function because the class is accessible locally within the class only.
Answer: b
Explanation: We can never create instance of an abstract class. Abstract classes doesn’t have
constructors and hence when an instance is created there is no facility to initialize its members. Option d
is correct because topper class is inheriting the base abstract class student, and hence topper class
object can be created easily.
------------
Answer: c
Explanation: The object declared in main() have local scope inside main() function only. It can’t be used
outside main() function. Scope resolution operator is used to access globally declared variables/objects.
d) Object are returned implicitly, we can’t say how it happens inside program
------------
Answer: a
Explanation: A temporary object is created to return the value. It is created because the object used in
function is destroyed as soon as the function is returned. The temporary variable returns the value and
then gets destroyed.
------------
Answer: c
Explanation: Only if the objects are of same class then their data can be copied from to another using
assignment operator. This actually comes under operator overloading. Class constructors can’t be
assigned any explicit value as in option class student{ }s1; class topper{ }t1; s1=t1; and class student{ }s1;
class topper{ }t1; s1.student()=s2.topper();.
class student{
int marks;
int cgpa;
marks=I;
cgpa=j
};
------------
Answer: b
Explanation: It is the way we can initialize the data members for an object array using parameterized
constructor. We can do this to pass our own intended values to initialize the object array data.
-. Object can’t be used with pointers because they belong to user defined class, and compiler can’t
decide the type of data may be used inside the class.
a) True
b) False
------------
Answer: b
Explanation: The explanation given is wrong because object can always be used with pointers like with
any other variables. Compiler doesn’t have to know the structure of the class to use a pointer because
the pointers only points to a memory address/stores that address.
a) Encapsulation
b) Inheritance
c) Abstraction
d) Polymorphism
------------
Answer: b
Explanation: Inheritance indicates the code reusability. Encapsulation and abstraction are meant to
hide/group data into one element. Polymorphism is to indicate different tasks performed by a single
entity.
-. If a function can perform more than 1 type of tasks, where the function name remains same, which
feature of OOP is used here?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
------------
Answer: c
Explanation: For the feature given above, the OOP feature used is Polymorphism. Example of
polymorphism in real life is a kid, who can be a student, a son, a brother depending on where he is.
-. If different properties and functions of a real world entity is grouped or embedded into a single
element, what is it called in OOP language?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
------------
Answer: d
Explanation: It is Encapsulation, which groups different properties and functions of a real world entity
into single element. Abstraction, on other hand, is hiding of functional or exact working of codes and
showing only the things which are required by the user.
b) Inheritance
d) Functions Overloading
------------
Answer: c
Explanation: Data must be declared using objects. Object usage is mandatory because it in turn calls its
constructors, which in turn must have a class defined. If object is not used, it is a violation of pure OOP
concept.
a) Platform independent
b) Data binding
c) Message passing
d) Data hiding
------------
Answer: a
Explanation: Platform independence is not feature of OOP. C++ supports OOP but it’s not a platform
independent language. Platform independence depends on programming language.
a) Inheritance
b) Polymorphism
------------
Answer: d
Explanation: Encapsulation is indicated by use of classes. Inheritance is shown by inheriting the student
class into topper class. Polymorphism is not shown here because we have defined the constructor in the
topper class but that doesn’t mean that default constructor is overloaded.
------------
Answer: d
Explanation: All the features are violated because Inheritance and Encapsulation won’t be implemented.
Polymorphism and Abstraction are still possible in some cases, but the main features like data binding,
object use and etc won’t be used hence the use of class is must for OOP concept.
-. How many basic features of OOP are required for a programming language to be purely OOP?
a) 7
b) 6
c) 5
d) 4
------------
Answer: a
Explanation: There are 7 basic features that define whether a programing language is pure OOP or not.
The 4 basic features are inheritance, polymorphism, encapsulation and abstraction. Further, one is,
object use is must, secondly, message passing and lastly, Dynamic binding.
-. Which feature in OOP is used to allocate additional function to a predefined operator in any language?
a) Operator Overloading
b) Function Overloading
c) Operator Overriding
d) Function Overriding
------------
Answer: a
Explanation: The feature is operator overloading. There is not a feature named operator overriding
specifically. Function overloading and overriding doesn’t give addition function to any operator.
a) Function overloading
b) Function overriding
c) Operator overloading
d) Virtual function
------------
Answer: b
Explanation: Function overriding doesn’t illustrate polymorphism because the functions are actually
different and theirs scopes are different. Function and operator overloading illustrate proper
polymorphism. Virtual functions show polymorphism because all the classes which inherit virtual
function, define the same function in different ways.
-. Exception handling is a feature of OOP.
a) True
b) False
------------
Answer: a
Explanation: Exception handling is a feature of OOP as it includes classes concept in most of the cases.
Also it may come handy while using inheritance.
------------
Answer: d
Explanation: The language must follow all the rules of OOP to be called a purely OOP language. Even if a
single OOP feature is not followed, then it’s known to be a partially OOP language.
------------
Answer: a
Explanation: It is always true as we have the facility of private and protected access specifiers. Also, only
the public and global data are available globally or else the program should have proper permission to
access the private data.
-. Which among the following best describes polymorphism?
------------
Answer: a
Explanation: It is actually the ability for a message / data to be processed in more than one form. The
word polymorphism indicates many-forms. So if a single entity takes more than one form, it is known as
polymorphism.
-. What do you call the languages that support classes but not polymorphism?
c) Object-based language
------------
Answer: c
Explanation: The languages which support classes but doesn’t support polymorphism, are known as
object-based languages. Polymorphism is such an important feature, that is a language doesn’t support
this feature, it can’t be called as a OOP language.
-. Which among the following is the language which supports classes but not polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
------------
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t support the
polymorphism feature. It is an object-based programming language. Note that it’s not an OOP language.
-. If same message is passed to objects of several different classes and all of those can respond in a
different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
------------
Answer: c
Explanation: The feature defined in question defines polymorphism features. Here the different objects
are capable of responding to the same message in different ways, hence polymorphism.
calc_grade();
public : calc_grade()
return 10;
};
public : calc_grade()
{
return 20;
};
c) All class student, topper and average together can show polymorphism
d) Class failed should also inherit class student for this code to work for polymorphism
------------
Answer: c
Explanation: Since Student class is abstract class and class topper and average are inheriting student,
class topper and average must define the function named calc_grade(); in abstract class. Since both the
definition are different in those classes, calc_grade() will work in different way for same input from
different objects. Hence it shows polymorphism.
a) Inline function
b) Virtual function
c) Undefined functions
------------
Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class member functions can
show polymorphism too but we should be sure that the same function is being overloaded or is a
function of abstract class or something like this, since we are not sure about all these, we can’t say
whether it can show polymorphism or not.
-. In case of using abstract class or function overloading, which function is supposed to be called first?
a) Local function
d) Function with lowest priority because it might have been halted since long time, because of low
priority
------------
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread scheduling in CPU,
but it focuses on whether the function in local scope is present or not, or if scope resolution is used in
some way, or if the function matches the argument signature. So all these things define which function
has the highest priority to be called in runtime. Local function could be one of the answer but we can’t
say if someone have used pointer to another function or same function name.
d) Constructor overloading
------------
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t be considered for
overloading/overriding. For polymorphism, function must be property of object, not only of class.
class education
char name[10];
public : disp()
};
void main()
school s;
s.disp();
------------
Answer: a
Explanation: Notice that the function name in derived class is different from the function name in base
class. Hence when we call the disp() function, base class function is executed. No polymorphism is used
here.
a) True
b) False
------------
Answer: a
-. Which problem may arise if we use abstract class functions for polymorphism?
------------
Answer: c
Explanation: The undefined functions must be defined is a problem, because one may need to
implement few undefined functions from abstract class, but he will have to define each of the functions
declared in abstract class. Being useless task, it is a problem sometimes.
a) It is feature of OOP
------------
Answer: d
Explanation: It never increases function definition overhead, one way or another if you don’t use
polymorphism, you will use the definition in some other way, so it actually helps to write efficient codes.
-. If 2 classes derive one base class and redefine a function of base class, also overload some operators
inside class body. Among these two things of function and operator overloading, where is polymorphism
used?
d) Either function overloading or operator overloading because polymorphism can be applied only once
in a program
------------
Answer: d
Explanation: Both of them are using polymorphism. It is not necessary that polymorphism can be used
only once in a program, it can be used anywhere, any number of times in a single program.
c) It is a way of combining various data members and member functions into a single unit which can
operate on any data
d) It is a way of combining various data members and member functions that operate on those data
members into a single unit
------------
Answer: d
Explanation: It is a way of combining both data members and member functions, which operate on
those data members, into a single unit. We call it a class in OOP generally. This feature have helped us
modify the structures used in C language to be upgraded into class in C++ and other languages.
-. If data members are private, what can we do to access them from the class object?
d) Private data members can never be accessed from outside the class
------------
Answer: a
Explanation: We can define public member functions to access those private data members and get
their value for use or alteration. They can’t be accessed directly but is possible to be access using
member functions. This is done to ensure that the private data doesn’t get modified accidentally.
b) Data member’s data type can be changed without changing any other code
d) Member functions can be used to change the data type of data members
------------
Answer: b
Explanation: Data member’s data type can be changed without changing any further code. All the
members using that data can continue in the same way without any modification. Member functions
can never change the data type of same class data members.
a) Inheritance
b) Abstraction
c) Polymorphism
d) Overloading
------------
Answer: b
Explanation: Data abstraction can be achieved by using encapsulation. We can hide the operation and
structure of actual program from the user and can show only required information by the user.
------------
Answer: c
Explanation: It is the class which uses both the data members and member functions being declared
inside a single unit. Only data members can be there in structures also. And the encapsulation can only
be illustrated if some data/operations are associated within class.
a) Mutable
b) Abstract
c) Wrapper
d) Immutable
------------
Answer: d
Explanation: Immutable classes are used for caching purpose generally. And it can be created by making
the class as final and making all its members private.
------------
Answer: a
Explanation: The data prone to change in near future is usually encapsulated so that it doesn’t get
changed accidentally. We encapsulate the data to hide the critical working of program from outside
world.
c) Using inheritance
d) Using Abstraction
------------
Answer: a
Explanation: Using access specifiers we can achieve encapsulation. Using this we can in turn implement
data abstraction. It’s not necessary that we only use private access.
------------
Answer: d
Explanation: If we use any kind of array or pointer as data member which should not be changed, but in
some case its address is passed to some other function or similar variable. There are chances to modify
its whole data easily. Hence Against encapsulation.
class student
int marks;
return &marks;
};
main()
student s;
int *ptr=c.fun();
return 0;
}
------------
Answer: d
Explanation: This code violates the encapsulation. By this code we can get the address of the private
member of the class, hence we can change the value of private member, which is against the rules.
class hero
char name[10];
cout<<name;
};
------------
Answer: a
Explanation: This code maintains encapsulation. Here the private member is kept private. Outside code
can’t access the private members of class. Only objects of this class will be able to access the public
member function at maximum.
-. Encapsulation is the way to add functions in a user defined structure.
a) True
b) False
------------
Answer: b
Explanation: False, because we can’t call these structures if member functions are involved, it must be
called class. Also, it is not just about adding functions, it’s about binding data and functions together.
a) Not ensured
c) Purely ensured
d) Very low
------------
Answer: b
Explanation: The encapsulation can only ensure data security to some extent. If pointer and addresses
are misused, it may violate encapsulation. Use of global variables also makes the program vulnerable,
hence we can’t say that encapsulation gives pure security.
------------
Answer: d
Explanation: It includes hiding the implementation part and showing only the required data and features
to the user. It is done to hide the implementation complexity and details from the user. And to provide a
good interface in programming.
------------
Answer: a
Explanation: It can make programming easy. The programming need not know how the inbuilt functions
are working but can use those complex functions directly in the program. It doesn’t provide more
number of features or better features.
a) Object
b) Logical
c) Real
d) Hypothetical
------------
Answer: b
Explanation: Class is logical abstraction because it provides a logical structure for all of its objects. It
gives an overview of the features of an object.
a) Object
b) Logical
c) Real
d) Hypothetical
------------
Answer: c
Explanation: Object is real abstraction because it actually contains those features of class. It is the
implementation of overview given by class. Hence the class is logical abstraction and its object is real.
a) Class usage
b) Program complexity
c) Idealized interface
d) Unstable interface
------------
Answer: c
Explanation: It is to idealize the interface. In this way the programmer can use the programming
features more efficiently and can code better. It can’t increase the program complexity, as the feature
itself is made to hide it.
b) Only data
c) Only control
d) Classes
------------
Answer: a
Explanation: Abstraction applies to both. Control abstraction involves use of subroutines and control
flow abstraction. Data abstraction involves handling pieces of data in meaningful ways.
-. Which among the following can be viewed as combination of abstraction of data and code.
a) Class
b) Object
c) Inheritance
d) Interfaces
------------
Answer: b
Explanation: Object can be viewed as abstraction of data and code. It uses data members and their
functioning as data abstraction. Code abstraction as use of object of inbuilt class.
------------
Answer: c
Explanation: Abstraction principle includes use of abstraction to avoid duplication (usually of code). It
this way the program doesn’t contain any redundant functions and make the program efficient.
a) True
b) False
------------
Answer: b
Explanation: Higher the level of abstraction, lower are the details. The best way to understand this is to
consider a whole system that is highest level of abstraction as it hides everything inside. And next lower
level would contain few of the computer components and so on.
------------
Answer: a
Explanation: Abstraction is hiding the complex code. For example, we directly use cout object in C++ but
we don’t know how is it actually implemented. Encapsulation is data binding, as in, we try to combine a
similar type of data and functions together.
-. A phone is made up of many components like motherboard, camera, sensors and etc. If the processor
represents all the functioning of phone, display shows the display only, and the phone is represented as
a whole. Which among the following have highest level of abstraction?
a) Motherboard
b) Display
c) Camera
d) Phone
------------
Answer: d
Explanation: Phone as a whole have the highest level of abstraction. This is because the phone being a
single unit represents the whole system. Whereas motherboard, display and camera are its components.
a) Logical level
b) Physical level
c) View level
d) External level
------------
Answer: d
Explanation: Abstraction is generally divided into 3 different levels, namely, logical, physical and view
level. External level is not defined in terms of abstraction.
c) Can be safer
d) Can increase vulnerability
------------
Answer: c
Explanation: It will make the code safer. One may think it reduces the readability, but the fact is, it
actually helps us understand the code better. We don’t have to read the complex code which is of no
use in understanding the program.
-. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger
------------
Answer: b
Explanation: Constructors are the member functions which are called automatically whenever an object
is created. It is a mandatory functions to be called for an object to be created as this helps in initializing
the object to a legal initial value for the class.
------------
Answer: c
Explanation: Constructors are predefined implicitly, even if the programmer doesn’t define any of them.
Even if the programmer declares a constructor, it’s not necessary that it must contain some definition.
------------
Answer: d
Explanation: The constructors must not have any return type. Also, the body may or may not contain
any body. Defining default constructor is optional, if you are not using any other constructor.
-. In which access should a constructor be defined, so that object of the class can be created in any
function?
a) Public
b) Protected
c) Private
------------
Answer: a
Explanation: Constructor function should be available to all the parts of program where the object is to
be created. Hence it is advised to define it in public access, so that any other function is able to create
objects.
-. How many types of constructors are available for use in general (with respect to parameters)?
a) 2
b) 3
c) 4
d) 5
------------
Answer: a
Explanation: Two types of constructors are defined generally, namely, default constructor and
parameterized constructor. Default constructor is not necessary to be defined always.
-. If a programmer defines a class and defines a default value parameterized constructor inside it.
He has not defined any default constructor. And then he try to create the object without passing
arguments, which among the following will be correct?
b) It will create the object (as the default arguments are passed)
c) It will not create the object (as the default constructor is not defined)
------------
Answer: b
Explanation: It will create the object without any problem, because the default arguments use the
default value if no value is passed. Hence it is equal to default constructor with zero parameters. But it
will not create the object if signature doesn’t match.
-. Default constructor must be defined, if parameterized constructor is defined and the object is to be
created without arguments.
a) True
b) False
------------
Answer: a
Explanation: If the object is create without arguments and only parameterized constructors are used,
compiler will give an error as there is no default constructor defined. And some constructor must be
called so as to create an object in memory.
-. If class C inherits class B. And B has inherited class A. Then while creating the object of class C, what
will be the sequence of constructors getting called?
------------
Answer: d
Explanation: While creating the object of class C, its constructor would be called by default. But, if the
class is inheriting some other class, firstly the parent class constructor will be called so that all the data is
initialized that is being inherited.
-. In multiple inheritance, if class C inherits two classes A and B as follows, which class constructor will be
called first?
class A{ };
class B{ };
a) A()
b) B()
c) C()
d) Can’t be determined
------------
Answer: a
Explanation: Constructor of class A will be called first. This is because the constructors in multiple
inheritance are called in the sequence in which they are written to be inherited. Here A is written first,
hence it is called first.
------------
Answer: b
Explanation: It can’t be defined with zero number of arguments. This is because to copy one object to
another, the object must be mentioned so that compiler can take values from that object.
-. Which among the following helps to create a temporary instance?
------------
Answer: d
Explanation: Explicit call to a constructor can let you create a temporary instance. This is because the
temporary instances doesn’t have any name. Those are deleted from memory as soon as their reference
is removed.
-. Which among the following is correct for the class defined below?
class student
int marks;
public: student(){}
student(int x)
marks=x;
};
main()
student s1(100);
student s2();
student s3=100;
return 0;
}
a) Object s3, syntax error
------------
Answer: c
Explanation: It is a special case of constructor with only 1 argument. While calling a constructor with one
argument, you are actually implicitly creating a conversion from the argument type to the type of class.
Hence you can directly specify the value of that one argument with assignment operator.
-. For constructor overloading, each constructor must differ in ___________ and __________
------------
Answer: a
Explanation: Each constructor must differ in the number of arguments it accepts and the type of
arguments. This actually defines the constructor signature. This helps to remove the ambiguity and
define a unique constructor as required.
a) 2
b) 3
c) 4
d) 5
------------
Answer: b
Explanation: There are 3 types of constructors in general, namely, default constructors, parameterized
constructors and copy constructors. Default one is called whenever an object is created without
arguments.
class student
int marks;
student s1;
student s2=2;
------------
Answer: d
Explanation: The object s2 can be assigned with one value only if a single argument constructor is
defined in class, but here, it can’t be done as no constructor is defined. Hence every object must be
declare or created without using arguments.
a) Default
b) Parameterized
c) Copy
------------
Answer: c
Explanation: Copy constructor is used while an object is assigned with another. This is mandatory since
we can’t decide which member should be assigned to which member value. By using copy constructor,
we can assign the values in required form.
------------
Answer: a
Explanation: Object must be passed by reference to copy constructor because constructor is not called
in pass by reference. Otherwise, in pass by value, a temporary object will be created which in turn will
try to call its constructor that is already being used. This results in creating infinite number of objects
and hence memory shortage error will be shown.
a) Public
b) Protected
c) Implicit
d) Explicit
------------
Answer: d
Explanation: The keyword explicit can be used while defining the constructor only. This is used to
suppress the implicit call to the constructor. It ensures that the constructors are being called with the
default syntax only (i.e. only by using object and constructor name).
Answer: c
Explanation: Default constructors can be called explicitly anytime. They are specifically used to allocate
memory space for the object in memory, in general. It is not necessary that these should always be
called implicitly.
a) Default
b) Parameterized
c) Copy
------------
Answer: d
Explanation: Constructors don’t return any value. Those are special functions, whose return type is not
defined, not even void. This is so because the constructors are meant to initialize the members of class
and not to perform some task which can return some value to newly created object.
------------
Answer: a
Explanation: Static constructors help in initializing the static members of the class. This is provided
because the static members are not considered to be property of the object, rather they are considered
as the property of class.
b) Called at first time when an object is created and only one time
c) Called at first time when an object is created and called with every new object creation
------------
Answer: b
Explanation: Those are called at very first call of object creation. That is called only one time because the
value of static members must be retained and continued from the time it gets created.
------------
Answer: c
Explanation: Static constructors can’t be parameterized constructors. Those are used to initialize the
value of static members only. And that must be a definite value. Accepting arguments may make it
possible that static members loses their value with every new object being created.
------------
Answer: c
Explanation: The static constructor is called before creation of the first instance of that class. This is
done so that even the first instance can use the static value of the static members of the class and
manipulate as required.
------------
Answer: a
Explanation: If the constructors are defined in private access, then the class can’t be inherited by other
classes. This is useful when the class contains static members only. The instances can never be created.
-. Which among the following is correct, based on the given code below?
class student
int marks;
public : student()
};
student s1;
------------
Answer: c
Explanation: This program will work fine. This is because it is not mandatory that a constructor must
contain only initialization only. If you want to perform a task on each instance being created, that code
can be written inside the constructor.
This set of Basic Object Oriented Programming Questions and Answers focuses on “Copy Constructor”.
a) Creates an object by copying values from any other object of same class
b) Creates an object by copying values from first object created for that class
d) Creates an object by initializing it with another previously created object of same class
------------
Answer: d
Explanation: The object that has to be copied to new object must be previously created. The new object
gets initialized with the same values as that of the object mentioned for being copied. The exact copy is
made with values.
c) Initialize more than one object from another object of same type at a time
------------
Answer: a
Explanation: The copy constructor has the most basic function to initialize the members of an object
with same values as that of some previously created object. The object must be of same class.
-. If two classes have exactly same data members and member function and only they differ by class
name. Can copy constructor be used to initialize one class object with another class object?
a) Yes, possible
------------
Answer: c
Explanation: The restriction for copy constructor is that it must be used with the object of same class.
Even if the classes are exactly same the constructor won’t be able to access all the members of another
class. Hence we can’t use object of another class for initialization.
------------
Answer: b
Explanation: When an object is passed to a function, actually its copy is made in the function. To copy
the values, copy constructor is used. Hence the object being passed and object being used in function
are different.
a) Default constructor
c) Parameterized constructor
d) Copy constructor
------------
Answer: d
Explanation: While returning an object we can use the copy constructor. When we assign the return
value to another object of same class then this copy constructor will be used. And all the members will
be assigned the same values as that of the object being returned.
------------
Answer: a
Explanation: The compiler provides an implicit copy constructor. It is not mandatory to always create an
explicit copy constructor. The values are copied using implicit constructor only.
-. If a class implements some dynamic memory allocations and pointers then _____________
------------
Answer: a
Explanation: In the case where dynamic memory allocation is used, the copy constructor definition must
be given. The implicit copy constructor is not capable of manipulating the dynamic memory and
pointers. Explicit definition allows to manipulate the data as required.
------------
Answer: c
Explanation: The syntax must contain the class name first, followed by the classname as type and
&object within parenthesis. Then comes the constructor body. The definition can be given as per
requirements.
------------
Answer: a
Explanation: This is mandatory to pass the object by reference. Otherwise, the object will try to create
another object to copy its values, in turn a constructor will be called, and this will keep on calling itself.
This will cause the compiler to give out of memory error.
-. Out of memory error is given when the object _____________ to the copy constructor.
b) Is passed by reference
------------
Answer: d
Explanation: All the options given, directly or indirectly indicate that the object is being passed by
reference. And if object is not passed by reference then the out of memory error is produced. Due to
infinite constructor call of itself.
------------
Answer: c
Explanation: Whenever the compiler creates a temporary object, copy constructor is used to copy the
values from existing object to the temporary object.
-. The deep copy is possible only with the help of __________
c) Parameterized constructor
d) Default constructor
------------
Answer: b
Explanation: While using explicit copy constructor, the pointers of copied object point to the intended
memory location. This is assured since the programmers themselves manipulate the addresses.
a) True
b) False
------------
Answer: a
Explanation: The copy constructors are always overloaded constructors. They have to be. All the classes
have a default constructor and other constructors are basically overloaded constructors.
c) Defining more than one constructor in single class with different signature
------------
Answer: c
Explanation: If more than one constructors are defined in a class with same signature, then that results
in error. The signatures must be different. So that the constructors can be differentiated.
a) Yes, always
b) Yes, if derived class has no constructor
d) No, never
------------
Answer: d
Explanation: The constructor must be having the same name as that of a class. Hence a constructor of
one class can’t even be defined in another class. Since the constructors can’t be defined in derived class,
it can’t be overloaded too, in derived class.
-. Does constructor overloading include different return types for constructors to be overloaded?
------------
Answer: d
Explanation: The constructors doesn’t have any return type. When we can’t have return type of a
constructor, overloading based on the return type is not possible. Hence only parameters can be
different.
c) Define default constructor, and 2 other parameterized constructors with same signature
------------
Answer: a
Explanation: All the constructors defined in a class must have a different signature in order to be
overloaded. Here one default and other parameterized constructors are used, wherein one is of only
one parameter and other accepts two. Hence overloading is possible.
-. Which constructor will be called from the object created in the code below?
class A
int i;
A()
i=0; cout<<i;
A(int x=0)
i=x; cout<<I;
};
A obj1;
a) Default constructor
b) Parameterized constructor
------------
Answer: c
Explanation: When a default constructor is defined and another constructor with 1 default value
argument is defined, creating object without parameter will create ambiguity for the compiler. The
compiler won’t be able to decide which constructor should be called, hence compile time error.
------------
Answer: b
Explanation: The constructors are not always user defined. The construct will be provided implicitly from
the compiler if the used doesn’t defined any constructor. The implicit constructor makes all the string
values null and allocates memory space for each data member.
------------
Answer: d
Explanation: The constructor is called as soon as the object is created. The overloading comes into
picture as to identify which constructor have to be called depending on arguments passed in the
creation of object.
-. Which among the following function can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()
------------
Answer: a
Explanation: The function this() can be used to call the default constructor from inside any other
constructor. This helps to further reuse the code and not to write the redundant data in all the
constructors.
------------
Answer: c
Explanation: The constructors are overloaded to initialize the objects of a class in different ways. This
allows us to initialize the object with either default values or used given values. If data members are not
initialized then program may give unexpected results.
------------
Answer: a
Explanation: When the programmer doesn’t specify any default constructor and only defines some
parameterized constructor. The compiler doesn’t provide any default constructor implicitly. This is
because it is assumed that the programmer will create the objects only with constructors.
a) Constructor overloading
------------
Answer: b
Explanation: Java doesn’t provide the feature to recursively call the constructor. This is to eliminate the
out of memory error in some cases that arises unexpectedly. That is an predefined condition for
constructors in java.
-. Which constructor will be called from the object obj2 in the following program?
class A
int i;
A()
i=0;
A(int x)
i=x+1;
A(int y, int x)
i=x+y;
};
A obj1(10);
A obj2(10,20);
A obj3;
a) A(int x)
b) A(int y)
c) A(int y, int x)
d) A(int y; int x)
------------
Answer: c
Explanation: The two argument constructor will be called as we are passing 2 arguments to the object
while creation. The arguments will be passed together and hence compiler resolves that two argument
constructor have to be called.
------------
Answer: c
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 object name. The keyword new must be used and
then the constructor call with or without the parameters as required.
a) A special function that is called to free the resources, acquired by the object
------------
Answer: a
Explanation: It is used to free the resources that the object might had used in its lifespan. The
destructors are called implicitly whenever an object’s life ends.
------------
Answer: d
Explanation: The destructor is called just before the object go out of scope or just before its life ends.
This is done to ensure that all the resources reserved for the object are used and at last, are made free
for others.
b) It has destructors
------------
Answer: a
Explanation: It doesn’t have destructors. Since an abstract class don’t have constructors, and hence
can’t have instances. Having this case, the abstract classes don’t have destructors too, because that
would be of no use here.
-. If in multiple inheritance, class C inherits class B, and Class B inherits class A. In which sequence are
their destructors called if an object of class C was declared?
------------
Answer: a
Explanation: The destructors are always called in the reverse order of how the constructors were called.
Here class A constructor would have been created first if Class C object is declared. Hence class A
destructor is called at last.
-. Choose the correct sequence of destructors being called for the following code.
class A{ };
class B{ };
------------
Answer: d
Explanation: In multiple inheritance, the constructors are called in the sequence of how they are written
in inheritance sequence. And the destructors will be called in the reverse order. This can be cross
verified just by printing a message from each destructor defined in classes.
------------
Answer: a
Explanation: This is because the lifespan of global object is from start of the program, till the end of the
program. And hence program end is the end of global object too. Just before the end of program, the
destructor will be called to free the acquired resources by the objects.
c) Destructors are preceded with a tilde (~) symbol, and constructor doesn’t
------------
Answer: c
Explanation: The destructors are preceded with the tilde (~) symbol. The name is same as that of the
class. These also doesn’t have any return type.
a) True
b) False
------------
Answer: a
Explanation: The destructors doesn’t accept the arguments. Those are just used to free up the
resources.
a) Abstract type
b) Virtual
c) Void
------------
Answer: b
Explanation: The destructors can be virtual. It is actually advised to keep the destructors virtual always.
This is done to suppress the problems that may arise if inheritance is involved.
a) Sequential
b) Random
c) Reverse
d) Depending on priority
------------
Answer: c
Explanation: The destructors are always called in reverse order no matter which destructor it is. This is
done to ensure that all the resources are able to get free. And no resource is kept busy.
------------
Answer: a
Explanation: This is always advised to have user defined destructor when pointers are involved in class.
This is usually done to ensure that the memory, that was allocated dynamically, gets free after use and
doesn’t cause memory leak.
------------
Answer: d
Explanation: This is so because the destructors can’t be overloaded. And the destructor must have the
same name as that of class with a tilde symbol preceding the name of the destructor. Hence there can
be only one destructor in a class. Since more than one function with same name and signature can’t be
present in same scope.
------------
Answer: a
Explanation: The destructor is never called in this situation. The concept is that when an object is passed
by reference to the function, the constructor is not called, but only the main object will be used. Hence
no destructor will be called at end of function.
a) 1
b) 2
c) 3
d) 4
------------
Answer: c
Explanation: Only 3 types of access specifiers are available. Namely, private, protected and public. All
these three can be used according to the need of security of members.
a) Only private
------------
Answer: d
Explanation: All the classes can use any of the specifiers as needed. There is no restriction on how many
of them can be used together.
-. Which among the following can restrict class members to get inherited?
a) Private
b) Protected
c) Public
d) All three
------------
Answer: a
Explanation: Private members of a class can’t be inherited. These members can only be accessible from
members of its own class only. It is used to secure the data.
-. Which access specifier is used when no access specifier is used with a member of class (java)?
a) Private
b) Default
c) Protected
d) Public
------------
Answer: b
Explanation: Default access is used if the programmer doesn’t specify the specifier. This acts in a similar
way as that of private. But since nothing is specified we call it to default access.
-. Which specifier allows a programmer to make the private members which can be inherited?
a) Private
b) Default
c) Protected
------------
Answer: c
Explanation: Protected access is used to make the members private. But those members can be
inherited. This gives both security and code reuse capability to a program.
Answer: c
Explanation: The default members can be inherited. Provided that they are in same package. It works in
a little different way from private access specifier.
-. If a class has all the private members, which specifier will be used for its implicit constructor?
a) Private
b) Public
c) Protected
d) Default
------------
Answer: b
Explanation: The implicit constructor will always be public. Otherwise the class wouldn’t be able to have
instances. In turn, no objects will be created and the class can only be used for inheritance.
-. If class A has add() function with protected access, and few other members in public. Then class B
inherits class A privately. Will the user will not be able to call _________ from the object of class B.
------------
Answer: d
Explanation: Class B object will not be able to call any of the private, protected and public members of
class A. It is not only about the function add(), but all the members of class A will become private
members of class B.
-. Which access specifier should be used in a class where the instances can’t be created?
------------
Answer: b
Explanation: All the constructors must be made private. This will restrict the instance of class to be made
anywhere in the program. Since the constructors are private, no instance will be able to call them and
hence won’t be allocated with any memory space.
------------
Answer: a
Explanation: All the data members are counted to calculate the size of an object of a class. The data
member access specifier doesn’t play any role here. Hence all the data size will be added.
-. If class B inherits class A privately. And class B has a friend function. Will the friend function be able to
access the private member of class A?
c) No, because friend function can only access private members of friend class
d) No, because friend function can access private member of class A also
------------
Answer: c
Explanation: The friend function of class B will not be able to access private members of class A. Since B
is inheriting class A privately, the members will become private in class B. But private members of class
A won’t be inherited at all. Hence it won’t be accessible.
------------
Answer: a
Explanation: The classes which inherit the abstract class, won’t be able to implement the members of
abstract class. The private members will not be inherited. This will restrict the subclasses to implement
those members.
-. Which access specifier should be used so that all the parent class members can be inherited and
accessed from outside the class?
a) Private
b) Default or public
c) Protected or private
d) Public
------------
Answer: d
Explanation: All the members must be of public access. So that the members can be inherited easily.
Also, the members will be available from outside the class.
-. If a function has to be called only by using other member functions of the class, what should be the
access specifier used for that function?
a) Private
b) Protected
c) Public
d) Default
------------
Answer: a
Explanation: The function should be made private. In this way, the function will be available to be called
only from the class member functions. Hence the function will be secure from the outside world.
-. Which among the following is correct for the code given below?
class student
private: student()
marks =x;
};
------------
Answer: c
Explanation: For creating object without parameters, the default constructor must be defined in public
access. But here, only parameterized constructor is public, hence the objects being created with only
one parameter will only be allowed.
-. Which among the following is true for the code given below?
class A
public :
A(int x=100)
marks=x;
};
------------
Answer: a
Explanation: The constructor here has a default argument constructor. Hence we can pass one
parameter, but that is optional. If an object is created without parameter, the default value will be used
in the constructor definition.
-. Which among the following is correct to call a private member from outside the class?
a) object.memberfunction( parameters );
b) object->memberfunction( parameters );
d) Not possible
------------
Answer: d
Explanation: The private member function will not be accessible from outside the class. Hence any
syntax will not work to access the private members. If you have the address of the member, may be you
can access those members, but that is a totally different case and concept.
-. If private members have to be accessed directly from outside the class but the access specifier must
not be changed, what should be done?
d) It is not possible
------------
Answer: b
Explanation: For calling the function directly, we can’t use another function because that will be indirect
call. Using friend function, we can access the private members directly.
a) Private
b) Default
c) Protected
------------
Answer: a
Explanation: The private members are most secure in inheritance. The default members can still be in
inherited in special cases, but the private members can’t be accessed in any case.
------------
Answer: c
Explanation: The constructor is using a static member to keep the count of the number of objects
created. This is done because the variable c is static and hence the value will be common for all the
objects created.
return x+y;
public: A()
A(int x, int y)
cout<<sum(x,y);
};
b) Constructor prints sum, if two parameters are passed with object creation
------------
Answer: d
Explanation: Constructor is not having any default arguments hence no default value will be given to any
parameters. Only integer values must be passed to the constructor if we need the sum as output,
otherwise if float values are passed, type mismatch will be shown as error.
class A()
{
int marks; char name[20];
public : A()
marks=100;
void disp()
cout<<”Marks= ”<'<marks;
cout<<”Student”;
};
------------
Answer: a
Explanation: Variable name will never be used. It is a private member. None other than class members
can access name, also, neither the constructor nor the disp() function are accessing the variable name.
Hence it will never be accessible.
a) True
b) False
------------
Answer: a
Explanation: The private functions can also be overloaded. This can be done in usual way by having the
same name of the member function and having different signature. Only thing is, they must be accessed
from members of class only.
-. Which among the following is true?
------------
Answer: d
Explanation: The private member functions can be overloaded but they can’t be overridden. This is
because, overriding means a function with same name in derived class, gets more priority when called
from object of derived class. Here, the member function is private so there is no way that it can be
overridden.
-. Which data member in following code will be used whenever an object is created?
Class A
public : A()
y=100; x=100*y;
};
a) x will be used
b) y will be used
c) z will be used
------------
Answer: c
Explanation: Whenever an object will be created, the constructor will be called. Inside constructor we
are using the data members x and y. Hence these two will always be used with each object creation.
class A()
int a;
private : int b;
protected : int c;
public : int d;
};
a) a
b) b
c) c
d) d
------------
Answer: b
Explanation: The default variables can be inherited in some special cases but the public members can
never be inherited. Hence the most secure data member in the class is b.
------------
Answer: c
Explanation: The members which are made protected, are most secure if inheritance is not used. But,
this facility is provided to keep those members private and with that, they can be inherited by other
classes. This is done to make the code more flexible.
c) It’s instance can be created inside the subclasses and main() function
------------
Answer: a
Explanation: The instances will be allowed to be created only inside the sub classes. This is because the
protected members will be inherited and hence the constructor too. This will allow the subclasses to call
the constructor whenever an object is created.
class A
int marks;
protected : A()
marks=100;
public : A( int x)
marks=x;
};
------------
Answer: d
Explanation: The instances can be created anywhere in the program. The only restriction will be on
which constructor will have to be called. The instances with zero arguments will be allowed to be
created only inside the subclasses, but the instances with one argument can be created anywhere in the
program.
-. If the protected members are to be made accessible only to the nearest subclass and no further
subclasses, which access specifier should be used in inheritance?
------------
Answer: a
Explanation: The sub class should use private inheritance. This will allow only the nearest sub classes to
inherit the protected members and then those members will become private. Hence further inheritance
of those members will not be possible.
-. What will be the output of the following code (all header files and required things are included)?
class A
int marks;
protected : A(int x)
marks=x;
public : A()
{
marks=100;
class B
A a;
A b=100;
};
main()
A a, b=100;
B c;
------------
Answer: c
Explanation: The objects being created with assignment value are allowed, if the constructor accepts
only 1 argument. But main() function will not be able to create the object here with assignment, as the
constructor which accepts one argument is in protected mode in the class.
-. Which among the following is true for the given code below?
class A
A()
marks=100;
disp()
cout<<”marks=”<<marks;
};
class B: protected A
};
B b;
b.disp();
------------
Answer: a
Explanation: The object of class B can’t access the members of A outside the class. This is because the
class is being inherited in protected access, so all the members will become protected in subclass B.
a) Protected members can be accessed outside package using inheritance, but default can’t
b) Default members can be accessed outside package using inheritance, but protected can’t
c) Protected members are allowed for inheritance but Default members are not allowed
Answer: a
Explanation: The protected members are allowed in the same package but can also be accessed in other
packages using inheritance. But the default members can never be accessible in other packages.
-. If all the members are defined in protected specifier then? (Constructors not considered)
------------
Answer: b
Explanation: The instances can be created anywhere in the program. This is because the constructors
are not considered among the members defined in protected mode. Hence the default implicit
constructor will be used whenever an object is created.
class A
A()
Public : disp()
cout<< marks;
};
class B: public A
{
B b;
------------
Answer: a
Explanation: Instance of B will not be created. When you try to create an instance of B, First the
constructor of parent class will be called, but the parent class constructor is private, hence it won’t be
able to initialize and allocate memory for parent class members. In turn, the object of B will not be
created.
------------
Answer: d
Explanation: The protected and public members of the parent class will become the protected members
in subclass. This is predefined rule of inheritance. The reason behind is to maintain the level of security
in subclass too.
Answer: d
Explanation: The members must be made public, otherwise it is not possible. In every case, the
protected members will act as private members if it’s about access specifier. It will only be inherited,
that too will lead to make those members protected again, in subclasses.
d) Members which have to be as secure as private but can be used by main() function
------------
Answer: b
Explanation: The members which have to be secure and might get used in other packages or subclasses
can use protected access. This also allows the members to be safe from accidental modification.
-. Protected access is same as default access that is given implicitly in java if no specifier is mentioned.
a) True
b) False
------------
Answer: b
Explanation: The statement given is true. The clear difference is protected members are available in
other packages also, but the default members are available within the package only.
-. Which among the following have least security according to the access permissions allowed?
a) Private
b) Default
c) Protected
d) Public
------------
Answer: d
Explanation: The public members are available to the whole program. This makes the members most
vulnerable to accidental changes, which may result in unwanted modification and hence unstable
programming.
-. Which among the following can be used for outermost class access specifier in java?
a) Private
b) Public
c) Default
d) Default or Public
------------
Answer: d
Explanation: Either default or public access specifier must be used for outermost classes. Private can be
used with inner classes. This is done so that all the members can access and use the utmost class and
that program execution can be done from anywhere. Inner classes can be made private for security.
a) True
b) False
------------
Answer: b
Explanation: The statement given is false. This is because when we inherit the members they can either
be made more secure or be at same access. But the visibility reduction is not possible, for example, if a
member is protected in parent class, then it can only be made protected or private in subclass and not
public in any case.
Answer: a
Explanation: All the members will be available in subclasses. Though it is not guaranteed whether the
members will be available in subsequent subclasses from the first subclass.
a) 1
b) 2
c) -
d) As required
------------
Answer: a
Explanation: There can be only one public class in a java program. The public class name must match the
name of file. And there can’t be more than one class with same name in a single program in same scope.
Hence it is not possible to have more than one public class in java program.
package pack1;
class A
public A()
System.out.print(“object created”);
package pack2;
import pack1.*;
class B
{
A a=new A();
------------
Answer: c
Explanation: The program will give compile time error. Class A is defined with default access specifier.
This directly means that class A will be available within package only. Even if the constructor is public,
the object will not be created.
------------
Answer: a
Explanation: The static members are not property of any object of the class. Instead, those are treated
as property of class. This allows us to have public static members too.
-. A class has its default constructor defined as public. Class B inherits class A privately. The class
___________
------------
Answer: d
Explanation: Class A can have instances as it has public default constructor. Class will have its own
constructors defined. Hence both classes can have instances.
-. Which specifier can be used to inherit protected members as protected in subclass but public as public
in subclass?
a) Private
b) Default
c) Public
d) Protected
------------
Answer: c
Explanation: The specifier that can make protected member’s protected in subclass and public
member’s public in subclass, is public. This is done to maintain the security level of protected members
of parent class.
d) Public classes can be accessed from any other class using instance
------------
Answer: d
Explanation: The public class is a usual class. There is no special rule but the members of the class can be
accessed from other classes using instance of the class. This is usually useful to define main() function.
Answer: c
Explanation: According to rule of private, protected and default access specifiers, none of the members
under these specifiers will be able to get invoked outside the class. We are not sure about the members
of class specifically so other options doesn’t give a fixed answer.
------------
Answer: a
Explanation: The public inheritance makes the public members of the base class, public in derived
classes. This can be used when the same feature have to be redefined with each new class inheriting the
base class.
-. Which specifier allows to secure the public members of base class in inherited classes?
a) Private
b) Protected
c) Public
------------
Answer: d
Explanation: Both the private and protected specifiers can make the public members of the base class
more secure. This is useful if we stop using the parent class members and use the classes which
inherited the parent class, so as to secure data better.
------------
Answer: d
Explanation: Static members are not property of instances of classes. Those are shared by all the object
of classes. Hence those are defined outside the constructor, so as to make them common for all the
objects.
-. What should be done for data member to be of user defined structure type?
b) The structure must have been defined after the class definition
------------
Answer: a
Explanation: The structure must have been defined prior to its use. If the structure is not defined, then
the memory space will not be allocated for its members. This leads to undefined use of new data types.
a) 27
b) 255
c) 1024
d) As many as required
------------
Answer: d
Explanation: Any class can have as many data members as required. The only restriction that may arise
is when there is not enough memory space. This gives flexibility to define a class with best properties
possible.
b) Arrow operator
------------
Answer: c
Explanation: The data members can never be called directly. Dot operator is used to access the
members with help of object of class. Arrow is usually used if pointers are used.
-. To create a pointer to a private data member of a class, outside the class, which among the following
is correct?
a) Return the address of the private data member using a member function
------------
Answer: a
Explanation: We can call a public member function and return the address of any private data member.
Though the pointer being returned must be defined inside class itself. And the returned address can be
stored in a pointer.
-. Which among the following is true for use of setter() and getter() function?
d) Considered a red flag, and not recommended for large scale use
------------
Answer: d
Explanation: This concept of getter and setter functions is not acceptable if used too much. This is
considered to be inappropriate in OOP perspective. Though it is commonly used, it doesn’t work
according to OOP concepts at some higher level of understanding.
class A()
private : int n;
public : int m;
A()
n=100; m=50;
void disp()
cout<<”n”<<m<<n;
};
a) 1050100
b) 1005010
c) n5010
d) n50100
------------
Answer: d
Explanation: In cout we have specified n as a string to be printed. And m is a variable so its value gets
printed. And global variable will not be used since local variable have more preference.
------------
Answer: a
Explanation: The static member functions can only access static data members. This is because the static
member function can’t work with the properties that change object to object. It is mandatory that only
the common properties of all the objects be used. And only static data members are common to all as
those are property of class.
a) True
b) False
------------
Answer: b
Explanation: The data members in a class can never refer to own class type. This is not possible because
the data members should have some memory allocated for its object before the self-reference is used,
but class must call constructor for that. Hence not possible.
-. What is the keyword used to make data members have same value?
a) static
b) const
c) double
d) abstract
------------
Answer: b
Explanation: The keyword const can be used anywhere to make the variable have same value all the
time. This restriction is made to use the same value whenever required. Also, this can restrict accidental
changes.
a) Private
b) Protected
d) Privately inherited
------------
Answer: b
Explanation: Static members inheritance also depends on the type of specifier they have. Only the
protected members can be inherited but remain private to class. If static members are defined in private
access, they won’t be allowed for inheritance.
-. The arguments passed to member functions by reference are considered as data members of class.
a) True
b) False
------------
Answer: b
Explanation: This is a wrong statement. As only the data defined inside class is considered as its
member. But even if a variable is passed by reference it would be the same variable that is outside the
class. Hence it can’t be considered class member.
-. Which among the following is not allowed for data member declaration?
a) int a;
b) static int a;
c) abstract a;
d) Boolean a;
------------
Answer: c
Explanation: The abstract keyword in the declaration of data members is not allowed. This is because
the abstract keyword features can’t be used with the data members of the class. We can have all other
syntax given, but not abstract.
d) Using dot, arrow or using scope resolution operator with class name
------------
Answer: d
Explanation: The member functions can be called using only the dot operator or the arrow operator. But
the static members can be called using directly the class name followed by the scope resolution
operator and static member function name. This is useful when you don’t have any object to call the
member.
------------
Answer: b
Explanation: The member functions whose definition is expanded at the call, and no jump to function
and return happened, are termed as inline functions. This is used to make the program faster and more
efficient.
-. What happens if non static members are used in static member function?
b) Runtime error
c) Executes fine
------------
Answer: a
Explanation: There must be specific memory space allocated for the data members before the static
member functions uses them. But the space is not reserved if object is not declared. Hence only if static
members are not used, it leads to compile time error.
------------
Answer: c
Explanation: The static member functions doesn’t contain “this” pointer. Static member functions can’t
be defined as const or volatile also. These are restrictions on static member functions.
------------
Answer: d
Explanation: The members of a class can be used directly inside a member function. We can use this
pointer when there is a conflict between data members of class and arguments/local function variable
names.
------------
Answer: c
Explanation: For overloading those operators for a class, the class must use non-static member function
so that doesn’t remain common to all the objects, and each object can use it independently. The friend
functions is also restricted so as to keep the security of data.
------------
Answer: a
Explanation: The virtual functions are defined using virtual keyword. These are made in order to make
all the classes to define them as the class gets inherited. Increases code understanding.
a) Not generic
b) Automatically generic
------------
Answer: b
Explanation: When generic type is used in a class, the functions are automatically generic. This is so
because the functions would use the same type as defined to make the class generic. The functions will
get to know the type of data as soon as the generic class is used. It’s inbuilt feature.
------------
Answer: a
Explanation: The member functions has access to all the members of the class. Whenever data members
of a class, which might be private, have to be modified, we make use of these member functions. This is
more secure way to manipulate data.
-. Which among the following is proper syntax for class given below?
class A
int a,b;
a) void disp::A(){ }
b) void A::disp(){ }
------------
Answer: b
Explanation: The syntax in option void A::disp(){ } is correct. We use scope resolution to represent the
member function of a class and to write its definition. It is not necessary for a function to have anything
in its definition.
------------
Answer: a
Explanation: We can call one function inside another function to access some data of class. A public
member function can be used to call a private member function which directly manipulates the private
data of class.
a) Static
b) Constructor
c) Const
------------
Answer: d
Explanation: All the member functions work same as normal functions with syntax. But the constructor
and destructor are also considered as member functions of a class, and they never have any data type.
------------
Answer: c
Explanation: A member function of a class can only have the access to the members of its own class and
parent classes if inheritance used. Otherwise a member function can never access the members of a
subclass. Accessing static members of a class is possible by normal and static member functions.
------------
Answer: b
Explanation: The classes declared inside a package are available to all the functions and classes, hence
can’t be called local. This is somewhat similar concept that we use to denote variables of a function. The
classes declared inside functions will be local to them.
------------
Answer: c
Explanation: There is a restriction on where the member functions of the local class should be define.
Those must be defined inside the class body only. This is to reduce the ambiguity and complexity of
program.
-. Local classes can access the type names and enumerators defined by the enclosing function.
a) True
b) False
------------
Answer: a
Explanation: This is a little tricky part with local classes. Though the local class can’t access the general
variables of the function but can access the types that are defined inside the function. This is because
the whole definition of that type would be existing inside the class.
------------
Answer: d
Explanation: No, the static variables can’t be declared inside a local class. This is because each time the
function is called, all the variables get created again and are destroyed as soon as the function is
returned. This would have been possible id the static variable was of function.
a) Static
b) Inline
c) Abstract
d) Virtual
------------
Answer: c
Explanation: All the members are defined inside the class body. And when the member functions are
defined inside the class body, they are made inline by default. If the definition is too complex, those are
made normal functions.
-. The enclosing function has no special access to the members of the local class.
a) True
b) False
------------
Answer: a
Explanation: This is a rule that the enclosing function doesn’t have any special access to the members of
the local class. This is done to maintain the security of class members. And to adhere to the rules of
OOP.
a) Kotlin
b) Java
c) SmallTalk
d) SAP ABAP
------------
Answer: d
Explanation: Other language might support inheritance with local classes but those doesn’t provide all
the proper features of inheritance. Language SAP ABAP provides a way to implement inheritance with
local classes efficiently.
a) Only 1
b) Only 3
c) Only 5
d) As many as required
------------
Answer: d
Explanation: The local classes can be defined as required. There is no restriction on the number of local
classes that can be defined inside a function. But all those classes must follow the rules and restrictions.
b) Defined in constructor
------------
Answer: b
Explanation: The data members follow the same rules as of simple classes. Hence the data members
must be declared first. Then their definition must be given using the constructors.
-. Can two different functions have local class with same name?
------------
Answer: a
Explanation: The local classes can have same name if they belong to different functions. The classes
would be local to those specific functions and hence can have same name. This is same as that of local
variables concept.
------------
Answer: b
Explanation: The scope of a local class is limited only within the function definition. The function can use
the class as usual as local variables. The class gets destroyed as soon as the function is returned.
-. Can a function, other than the enclosing function of local class, access the class members?
------------
Answer: d
Explanation: The local classes are local to the specific enclosing function. Other functions can’t access
the class. Even if the pointers are used, the class must be alive when the pointer is used. But this will not
happen if the enclosing function is returned.
-. Which among the following is the main advantage of using local classes?
a) Make program more efficient
------------
Answer: c
Explanation: The closest answer is to add more functionalities to a function or to make some specific
functions to be generic. Adding more members to a function can be done directly but to add some
special functionality that are encapsulated, can be done using local classes.
a) 1
b) 2
c) 3
d) 4
------------
Answer: c
Explanation: The objects can be passed in three ways. Pass by value, pass by reference and pass by
address. These are the general ways to pass the objects to a function.
------------
Answer: a
Explanation: When an object is passed by value, a new object is created implicitly. This new object uses
the implicit values assignment, same as that of the object being passed.
-. Constructor function is not called when an object is passed to a function, will its destructor be called
when its copy is destroyed?
------------
Answer: b
Explanation: Even though the constructor is not called when the object is passed to a function, the copy
of the object is still created, where the values of the members are same. When the object have to be
destroyed, the destructor is called to free the memory and resources that the object might have
reserved.
a) Temporary object
b) Virtual object
c) New object
d) Data member
------------
Answer: a
Explanation: The temporary object is created. It holds the return value. The values gets assigned as
required, and the temporary object gets destroyed.
------------
Answer: c
Explanation: The destruction of temporary variable may give rise to unexpected logical errors. Consider
the destructor which may free the dynamically allocated memory. But this may abort the program if
another is still trying to copy the values from that dynamic memory.
------------
Answer: d
Explanation: The problem can be solved by overloading the assignment operator to get the values that
might be getting returned while the destructor free the dynamic memory. Defining copy constructor can
help us to do this in even simpler way.
a) Only 1
b) Only 2
c) Only 16
d) As many as required
------------
Answer: a
Explanation: Like any other value, only one object can be returned at ones. The only possible way to
return more than one object is to return address of an object array. But that again comes under
returning object pointer.
Class A
int i;
public : A(int n)
~A()
cout<<”destroying ”<<i;
void seti(int n)
i=n;
int geti()
return I;
};
cout<<”something ”;
int main()
A a(1);
t(a);
cout<<”this is i in main ”;
cout<<a.geti();
------------
Answer: a
Explanation: Although the object constructor is called only ones, the destructor will be called twice,
because of destroying the copy of the object that is temporarily created. This is the concept of how the
object should be passed and manipulated.
------------
Answer: c
Explanation: Having the address being passed to the function, the changes are automatically made to
the main function. In all the cases if the address is being used, the same memory location will be
updated with new values.
a) Only 1
b) Only an array
c) Only 1 or an array
d) As many as required
------------
Answer: d
Explanation: There is no limit to how many objects can be passed. This works in same way as that any
other variable gets passed. Array and object can be passed at same time also.
-. If an object is passed by address, will be constructor be called?
------------
Answer: c
Explanation: A copy of all the values is created. If the constructor is called, there will be a compile time
error or memory shortage. This happens because each time a constructor is called, it try to call itself
again and that goes infinite times.
-. Is it possible that an object of is passed to a function, and the function also have an object of same
name?
------------
Answer: a
Explanation: There can’t be more than one variable or object with the same name in same scope. The
scope is same, since the object is passed, it becomes local to function and hence function can’t have one
more object of same name.
-. Passing an object using copy constructor and pass by value are same.
a) True
b) False
------------
Answer: b
Explanation: The copy constructor is used to copy the values from one object to other. Pass by values is
not same as copy constructor method. Actually the pass by value method uses a copy constructor to
copy the values in a local object.
a) It is address of an object
------------
Answer: b
Explanation: Reference indicates the address where the object’s variables and methods are stored. It is
not actual address of the object. This is done to directly use the variables and methods whenever
required.
------------
Answer: a
-. Is there any explicit use of pointers in java that would be applicable to objects?
------------
Answer: c
Explanation: The question clearly asks if there is any explicit use of pointers related to objects. Pointers
are not applicable in java first of all. Secondly, the pointing in java is achieved implicitly using the
references and object arrays.
b) Maybe, it is possible
------------
Answer: c
Explanation: The object of a super class can never refer to methods of a subclass. Whereas vice versa is
possible. If there is an overridden function in subclass, then the object of super class will execute the
method of itself and not from the subclass.
import java.awt.Point;
class Testing
Point t1,t2,t3;
t1=new Point(100,100);
t2=t1;
t3=t1;
t1.x=200;
t1.y=200;
t2.x=300;
t3.y=500;
b) Point 1: 100,100
------------
Answer: d
Explanation: When references are used, all the variables point to the same object. Whenever any of the
variable changes any values, it will be reflected to all the variables pointing to the same object.
------------
Answer: a
Explanation: Since the variable is declared final. It will have a constant value throughout the program. It
can refer to only one object at a time. And if it was made to refer to none of the object, it would have
got no use.
------------
Answer: a
Explanation: We use this pointer to differentiate the members of the class where this is used to the
other inherited or passed variables. The local variables are denoted with this. Or specifically the
members of class only.
a) enclosingClassObject.innerClassObject.method();
b) innerClassObject.method();
c) method();
------------
Answer: d
Explanation: This depends on where the method is being called. If the method is called inside the
enclosing class 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) One at a time
b) Many at a time
------------
Answer: a
Explanation: There should not be any confusion in how many references can be made from a single
variable. A single variable can only point to one object at a time. Even if it’s an array, the name of the
array is used and is considered one object name only (representing first array element).
-. Java handles memory dynamically and references are deleted as soon as they are out of scope.
a) True
b) False
------------
Answer: a
Explanation: In Java, it is inbuilt feature that handles all the memory dynamically. It is not necessary to
free or destroy all the references made from a function which is going out of scope. You can call destroy
or free methods explicitly but there is no mandatory rule.
------------
Answer: c
Explanation: The object referencing will point to the same address if variables are assigned. All the
variables might have a different name but they will point to the same memory location. This is most
basic concept of references.
a) Different from
b) Same as
c) Somewhat similar
d) Part of
------------
Answer: b
Explanation: The methods invoked on a particular object is same as sending a message with same values
to that object. Message would contain values in a particular format that can be used by the object. And
calling a method would be just another way to do the same task.
------------
Answer: b
Explanation: This is possible but not always, since the reference being returned may get destroyed with
the return of method. This is an undesirable condition, hence it is not always possible to return
references. But it is always possible if the referred element is not local to the method.
------------
Answer: a
Explanation: The memory allocated for the object members indicates actual creation of the object
members. This is known as memory allocation for object.
a) HDD
b) Cache
c) RAM
d) ROM
------------
Answer: c
Explanation: The memory for the objects or any other data is allocated in RAM initially. This is while we
run a program all the memory allocation takes place in some RAM segments. Arrays in heap and local
members in stack etc.
-. When is the memory allocated for an object?
a) At declaration of object
b) At compile time
------------
Answer: c
Explanation: The object memory allocation takes place when the object constructor is called.
Declaration of an object doesn’t mean that memory is allocated for its members. If object is initialized
with another object, it may just get a reference to the previously created object.
------------
Answer: b
Explanation: The new is type safe because we don’t have to specify the type of data that have to be
allocated with memory. We can directly use it with data name. Name of the data doesn’t matter though
for type of memory allocation though.
-. Which keyword among the following can be used to declare an array of objects in java?
a) new
b) create
c) allocate
d) arr
------------
Answer: a
Explanation: The keyword new can be used to declare an array of objects in java. The syntax must be
specified with an object pointer which is assigned with a memory space containing the required number
of object space. Even initialization can be done directly.
a) At termination of program
------------
Answer: b
Explanation: Whenever an object goes out of scope, the deletion of allocation memory takes place.
Actually the data is not deleted, instead the memory space is flagged to be free for further use. Hence
whenever an object goes out of scope the object members become useless and hence memory is set
free.
-. Which among the following keyword can be used to free the allocated memory for an object?
a) delete
b) free
d) only delete
------------
Answer: c
Explanation: The memory allocated for an object is usually automatically made free. But if explicitly
memory has to be made free then we can use either free or delete keywords depending on
programming languages.
a) Destructor function
b) Constructor function
c) Delete function
d) Free function
------------
Answer: a
Explanation: The destructor function of the class is called whenever an object goes out of scope. This is
because the destructor set all the resources, acquired by the object, free. This is an implicit work of
compiler.
a) sizeof(objectName)
b) size(objectName)
c) sizeofobject(objectName)
d) sizedobject(objectName)
------------
Answer: a
Explanation: The sizeof operator is used to get the size of an already created object. This operator must
constail keyword sizeof(objectName). The output will give the number of bytes acquired by a single
object of some class.
------------
Answer: c
Explanation: The memory allocation for an object can be static or dynamic. The static memory allocation
is when an object is declared directly without using any function usually. And dynamic allocation is when
we use some dynamic allocation function to allocate memory for data member of an object.
------------
Answer: a
Explanation: The memory for any data or object that are used in a user defined function are always
allocated in the stack. This is to ensure that the object is destroyed as soon as the function is returned.
Also this ensures that the correct memory allocation and destruction is performed.
a) Free collector
b) Dust collector
c) Memory manager
d) Garbage collector
------------
Answer: d
Explanation: The garbage collector in java takes care of the memory allocations and their deletions
dynamically. When an object is no more required then the garbage collector deletes the object and free
up all the resources that were held by that object.
-. Which operator can be used to free the memory allocated for an object in C++?
a) Free()
b) delete
c) Unallocate
d) Collect
------------
Answer: b
Explanation: The delete operator in C++ can be used to free the memory and resources held by an
object. The function can be called explicitly whenever required. In C++ memory management must be
done by the programmer. There is no automatic memory management in C++.
-. Which among the following best describes abstract classes?
a) If a class has more than one virtual function, it’s abstract class
b) If a class have only one pure virtual function, it’s abstract class
c) If a class has at least one pure virtual function, it’s abstract class
d) If a class has all the pure virtual functions only, then it’s abstract class
------------
Answer: c
Explanation: The condition for a class to be called abstract class is that it must have at least one pure
virtual function. The keyword abstract must be used while defining abstract class in java.
b) Yes, always
------------
Answer: b
Explanation: This is a property of abstract class. It can define main() function inside it. There is no
restriction on its definition and implementation.
c) Class is generic
------------
Answer: a
Explanation: It is a rule that if a class have even one abstract method, it must be an abstract class. If this
rule was not made, the abstract methods would have got skipped to get defined in some places which
are undesirable with the idea of abstract class.
------------
Answer: a
Explanation: Either of the two things must be done, either implementation or declaration of class as
abstract. This is done to ensure that the method intended to be defined by other classes gets defined at
every possible class.
------------
Answer: c
Explanation: Even though there can’t be any instance of abstract class. We can always create pointer or
reference to abstract class. The member functions which have some implementation inside abstract
itself can be used with these references.
a) Header files
b) Class Libraries
c) Class definitions
d) Class inheritance
------------
Answer: b
Explanation: The abstract classes can be used to create a generic, extensible class library that can be
used by other programmers. This helps us to get some already implemented codes and functions that
might have not been provided by the programming language itself.
-. Use of pointers or reference to an abstract class gives rise to which among the following feature?
a) Static Polymorphism
b) Runtime polymorphism
------------
Answer: b
Explanation: The runtime polymorphism is supported by reference and pointer to an abstract class. This
relies upon base class pointer and reference to select the proper virtual function.
a) Implement constructors
------------
Answer: a
Explanation: The abstract classes in java can define a constructor. Even though instance can’t be
created. But in this way, only during constructor chaining, constructor can be called. When instance of
concrete implementation class is created, it’s known as constructor chaining.
a) True
b) False
------------
Answer: a
Explanation: If an abstract class is made final in java, it will stop the abstract class from being extended.
And if the class is not getting extended, there won’t be another class to implement the virtual functions.
Due to this contradicting fact, it can’t be final in java.
a) Yes, always
c) No, never
------------
Answer: a
Explanation: There is no restriction on declaring static methods. The only condition is that the virtual
functions must have some definition in the program.
------------
Answer: c
Explanation: Derived, parent and static classes can’t have abstract method (We can’t say what type of
these classes is). And for abstract class it’s not mandatory to have abstract method. But if any abstract
method is there inside a class, then class must be abstract type.
a) At most 1
b) At least 1
c) At most 127
d) As many as required
------------
Answer: d
Explanation: There is no restriction on the number of abstract classes that can be defined inside a single
program. The programs can use as many abstract classes as required. But the functions with no body
must be implemented.
-. Is it necessary that all the abstract methods must be defined from an abstract class?
b) Yes, always
c) No, never
------------
Answer: b
Explanation: That is the rule of programming language that each function declared, must have some
definition. There can’t be some abstract method that remains undefined. Even if it’s there, it would
result in compile time error.
------------
Answer: a
Explanation: The template class can support more than one data type. The only thing is to add all the
data types required in a list separated by comma within template specification.
-. Which among the following is the proper syntax for the template class?
a) template <typename T1, typename T2>;
------------
Answer: c
Explanation: The syntax must start with keyword template, case sensitive. Then it should include the
typename and a variable to denote it. Then whenever that variable is used, it replaces it with the data
type needed.
b) Yes, always
------------
Answer: b
Explanation: The template class can use default arguments. This is used to specify the data type to be
considered if it is not specified while passing to the generic class. The default type will be used.
------------
Answer: d
Explanation: The class specialization is creation of explicit specialization of a generic class. We have to
use template<> constructor for this to work. It works in the same way as with explicit function
specialization.
------------
Answer: b
Explanation: The I/O specialization is made with wide character and 8-bit characters. Wide characters
are used to store the characters that might take more than 1 byte of space in memory or any size that is
different from the one that the machine is using.
b) Yes, always
------------
Answer: b
Explanation: The typeid() function can be used with the objects of generic classes. An instance of a
template class will take the type of data that is being used with it. Hence when typeid() function is used,
the data type would have already been defined and hence we can get desired result from typeid()
function.
a) String
b) Integer
c) Digit
d) Math
------------
Answer: a
Explanation: The string class is more specialized. Since the string must be able to store any kind of data
that is given to the string. Hence it needs maximum specialization.
a) Overloading is multiple function doing same operation, Template is multiple function doing different
operations
b) Overloading is single function doing different operations, Template is multiple function doing
different operations
c) Overloading is multiple function doing similar operation, Template is multiple function doing identical
operations
d) Overloading is multiple function doing same operation, Template is same function doing different
operations
------------
Answer: c
Explanation: The function overloading is multiple functions with similar or different functionality but
generic class functions perform the same task on given different types of data.
------------
Answer: b
Explanation: The generic class have a special case with static members. Each instance will have its own
static member. The static members are not shared usually.
cout << "x = " << x << " count = " << count << endl;
++count;
return;
void main()
test<int> (2);
test<int>(2);
test<double>(2.2);
a)
x = 2 count = 0
x = 2.2 count = 0
x = 2.2 count = 0
b)
x = 2 count = 0
x = 2 count = 0
x = 2.2 count = 0
c)
x = 2 count = 0
x = 2 count = 1
x = 2.2 count = 0
d)
x = 2 count = 0
x = 2 count = 1
x = 2.2 count = 2
------------
Answer: c
Explanation: For each new type, the class will have separate instance. Here two instances will be created
and hence counter for integer goes to 1. And for float value, the count remains 0 for the output.
-. If template class is defined, is it necessary to use different types of data for each call?
------------
Answer: a
Explanation: It is not necessary to use different type with each call to the generic function. Data may be
of same type with each call but still the function works. We don’t consider other properties like
efficiency with this concept because it is made generic to all data type, hence always works.
-. How many generic types can be given inside a single template class?
a) Only 1
b) Only 3
c) Only 7
d) As many as required
------------
Answer: d
Explanation: There is no restriction on the number of types to be used for making the class generic.
There can be any number of generic types with a single class. Hence giving flexibility to code with all the
data types.
a) True
b) False
------------
Answer: b
Explanation: There is no mandatory condition to have static members inside template class. Not only
template, it is not mandatory to have static members anywhere. We can use them as required in the
code.
------------
Answer: a
Explanation: A class which is parent of another class, or from which other classes can be derived, is
known as a base class. It is mandatory that a class must have at least one derived class to be called as a
base class.
a) Basic
b) Inherited
c) Super
d) Sub
------------
Answer: c
Explanation: A class which is being derived by other classes, is called as super class. This concept is
clearly used in java as we call the functions of a base class by using the keyword super as required.
a) Base
b) Derived
c) Template
d) Nested
------------
Answer: a
Explanation: Every abstract class is a base class. It must be so, because the functions which are not
defined inside the abstract class, must be defined in the derived classes. Hence it becomes a base class.
a) 1
b) 2
c) -
d) As many as required
------------
Answer: a
Explanation: In java, multiple inheritance is not supported, which leads to the fact that a class can have
only 1 parent class if inheritance is used. Only if interfaces are used then the class can implement more
than one base class.
d) Can’t be done
------------
Answer: c
Explanation: Making another class derive from it will make that class as base class. It is not necessary
that we have to write different code for it. If at least one class derives that class, it becomes the base
class for the new class.
-. If a base class is being derived by two other classes, which inheritance will that be called?
a) Single
b) Multiple
c) Multi-level
d) Hierarchical
------------
Answer: d
Explanation: When more than one classes are being derived from a single parent class, the inheritance is
known as hierarchical inheritance. This is usually useful when the base class is higher abstraction of its
derived classes.
-. If a class is enclosing more than one class, than it can be called as base class of those classes.
a) True
b) False
------------
Answer: b
Explanation: When a class have more than one nested classes, it is known as enclosing class. It can’t be
called as parent or base class since there is no inheritance involved.
a) Higher degree
b) Lower degree
c) Intermediate
d) Minimum degree
------------
Answer: b
Explanation: A base class will have lesser information as compared to those of derived classes. Since
derived classes inherit the base class properties and then add on their own features, they elaborate
more hence have lower degree of abstraction.
-. Always the base class constructors are called ___________ constructor of derived class.
a) Before
b) After
c) Along
d) According to priority of
------------
Answer: a
Explanation: When the base class object is created, its constructor will be called for sure. But if a derived
class constructor is called, first base class constructor is called and then derived class constructor is
taken into consideration.
-. Can we call methods of base class using the constructor of the derived class?
a) Yes, always
c) No, never
------------
Answer: a
Explanation: If the function is defined in the base class, it can always be called from the constructor of its
derived class. Since the constructors are not private, they can be accessed in derived class even if those
are protected.
-. If a base class is inherited from another class and then one class derives it, which inheritance is
shown?
a) Multiple
b) Single
c) Hierarchical
d) Multi-level
------------
Answer: d
Explanation: If a base class is inherited from another class, single inheritance is shown. But when one
more class inherits the derived class, this becomes a multi-level inheritance.
-. How many base classes can a single derived class have in C++?
a) 1
b) 2
c) 3
d) As many as required
------------
Answer: d
Explanation: This is because C++ allows multiple inheritance. A derived class can have more than one
base class and hence can derive all of their features.
-. If a base class is added with a few new members, its subclass must also be modified.
a) True
b) False
------------
Answer: b
Explanation: The base class can be added with new members without affecting the subclasses. This is
because the subclasses may get some more features inherited but it won’t use them. But the base class
will be able to use the new members as would be required.
a) A child class
------------
Answer: b
Explanation: Any class which inherits one or more classes is a derived class. The only condition is it must
inherit at least one class in order to be called as a derived class.
-. Which among the following is inherited by a derived class from base class?
------------
Answer: c
Explanation: The class inheriting another class, inherits all the data members and member functions that
are not private. This is done to ensure the security features with maximum flexibility.
-. If there is a derived class in a program, how many classes must be in that program?
a) 1
b) 2
c) 3
d) 4
------------
Answer: b
Explanation: If there is a derived class in a program, there must be at least 2 classes in that program.
One is a base class and another derived class. Hence at least 2 classes must be there.
4. Which members can never be accessed in derived class from the base class?
a) Private
b) Protected
c) Public
d) All except private
------------
Answer: d
Explanation: There is no restriction for a derived class to access the members of the base class until and
unless the members are private. Private member are declared so that those members are not accessible
outside the class.
-. How many types of inheritance are supported in C++ for deriving a class?
a) 1
b) 2
c) 3
d) 4
------------
Answer: c
Explanation: There are three types of inheritance possible. Private inheritance, protected inheritance,
and public inheritance. The inheritance defines the access specifier to be used with the inherited
members in the derived class.
a) 1
b) 2
c) 3
------------
Answer: d
Explanation: There is no restriction on how many classes can inherit a single base class. Hence there can
be as many derived classes as required in a program from a single base class.
a) Subclass
b) Small class
c) Big class
d) Noticeable class
------------
Answer: a
Explanation: It is just another name given to the derived classes. This is used while denoting all the
derived classes subsequent to a superclass.
-. If class A is derived from another derived class B which is derived from class C, which class will have
maximum level of abstraction?
a) Class A
b) Class B
c) Class C
------------
Answer: c
Explanation: The abstraction level of class C will be maximum. This is because the parent class have
higher level of abstraction. Hence the parent of all other class will have maximum level of abstraction.
-. If base class is an abstract class then derived class ______________ the undefined functions.
a) Must define
------------
Answer: b
Explanation: The function must be defined in the program which are not defined in the base class.
Hence the class must be defined as abstract of implement the function definition in it.
b) At most 1
c) At least 1
d) As many as required
------------
Answer: d
Explanation: When a class is to be derived from another derived class, the derived class behaves as a
normal base class hence there are no restriction on how many class can be derived from a derived class.
The derived class again behaves as a normal superclass.
a) True
b) False
------------
Answer: b
Explanation: This is not true that the members of a derived class can’t be derived. All the classes are
considered to be a normal class when used for derivation. The members can be derived with respect to
their access specifiers.
-. Which feature is not related to the derived classes among the following?
a) Inheritance
b) Encapsulation
------------
Answer: c
Explanation: The memory management is the feature that is not necessary for derived classes that will
be a part of whole program. The functions references must be resolved for their proper use if
inheritance is used.
-. Deriving a class in such a way that that the base class members are not available for further
inheritance is known as ___________________
a) Public inheritance
b) Protected inheritance
d) Private inheritance
------------
Answer: d
Explanation: The private members of a class can never be derived to another class. When a class derives
another class using private inheritance, all the members become private members of the derived class.
Hence these member won’t be available for further inheritance.
------------
Answer: d
Explanation: It can only be indicated by using the data and functions that we use in derived class, being
provided by parent class. Copying code is nowhere similar to this concept, also using the code already
written is same as copying. Using already defined functions is not inheritance as we are not adding any
of our own features.
a) 4
b) 3
c) 2
d) 1
------------
Answer: a
Explanation: There are basically 4 types of inheritance provided in OOP, namely, single level, multilevel,
multiple and hierarchical inheritance. We can add one more type as Hybrid inheritance but that is
actually the combination any types of inheritance from the 4 basic ones.
------------
Answer: b
Explanation: A class inheriting a base class defines single level inheritance. Inheriting an already derived
class makes it multilevel inheritance. And if base class is inherited by 2 other classes, it is multiple
inheritance.
a) class student{public: int marks;}s; class stream{int total;}; class topper:public student, public stream{ };
------------
Answer: a
Explanation: Class topper is getting derived from 2 other classes and hence it is multiple inheritance.
Topper inherits class stream and class student publicly and hence can use its features. If only few classes
are defined, there we are not even using inheritance (as in option class student{ }; class stream{ }; class
topper{ };).
b) C and C++
------------
Answer: d
Explanation: Java doesn’t support multiple inheritance. But that feature can be implemented by using
the interfaces concept. Multiple inheritance is not supported because of diamond problem and similar
issues.
a) Two base classes can be used to be derived into one single class
c) One base class can be derived into other two derived classes or more
------------
Answer: c
Explanation: One base class can be derived into the other two derived classes or more. If only one class
gets derived by only 2 other classes, it is also hierarchical inheritance, but it is not a mandatory
condition, because any number of derived classes can be there.
-. Which access type data gets derived as private member in derived class?
a) Private
b) Public
c) Protected
------------
Answer: a
Explanation: It is a rule, that when a derived class inherits the base class in private access mode, all the
members of base class gets derived as private members of the derived class.
-. If a base class is inherited in protected access mode then which among the following is true?
a) Public and Protected members of base class becomes protected members of derived class
b) Only protected members become protected members of derived class
c) Private, Protected and Public all members of base, become private of derived class
------------
Answer: a
Explanation: As the programming language rules apply, all the public and protected members of base
class becomes protected members of derived class in protected access mode. It can’t be changed
because it would hinder the security of data and may add vulnerability in the program.
a) Public members
b) Protected members
c) Private members
------------
Answer: c
Explanation: Private access specifier is the most secure access mode. It doesn’t allow members to be
inherited. Even Private inheritance can only inherit protected and public members.
-. While inheriting a class, if no access mode is specified, then which among the following is true? (in
C++)
d) It is not possible
------------
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 OOP features. Hence it is not mandatory to
specify the access mode if we want the class to be inherited privately.
-. If a derived class object is created, which constructor is called first?
d) Not possible
------------
Answer: a
Explanation: First the base class constructor is invoked. When we create a derived class object, the
system tries to invoke its constructor but the class is derived so first the base class must be initialized,
hence in turn the base class constructor is invoked before the derived class constructor.
-. The private members of the base class are visible in derived class but are not accessible directly.
a) True
b) False
------------
Answer: a
Explanation: Consider that a variable is private in base class and the derived class uses public inheritance
to inherit that class. Now if we also have a global variable of same name as that of base class private
variable, neither the global variable nor the base class private variable will be accessible from derived
class. This is because we can’t have 2 variables with same name in same local scope. Hence the private
members are accessible but not directly.
------------
Answer: d
Explanation: It is not mandatory that you have to make the visibility mode either public or protected.
You can do either of those. That will give you permission to inherit the private members of base class.
a) 2
b) 3
c) 4
d) 5
------------
Answer: d
Explanation: There are five types of inheritance that are possible in C++. Single level, Multilevel,
multiple, hierarchical and hybrid. Here we count hybrid also because it sometimes can bring up a new
form of inheritance, Like inheritance using multiple and hierarchical, which sometimes results in
diamond problem.
------------
Answer: c
Explanation: Java doesn’t support multiple inheritance. This is done to avoid the diamond problem that
sometimes arises with inherited functions. Though, multiple inheritance can be implemented in java
using interfaces.
a) Single level
c) Hierarchical
------------
Answer: c
Explanation: It is hierarchical inheritance and single level inheritance. Since class topper is inheriting
class student, it is single level inheritance. And then average is inherited by section and overall, so it is
hierarchical inheritance. But both of them are separate. Hence it is not hybrid inheritance.
------------
Answer: d
Explanation: If a class inherits more than one class, it is known as multiple inheritance. This should not
be referred with only two or three classes being inherited. But there must be one class which inherits
more than one class to be called as multiple inheritance.
c) Any - types
------------
Answer: d
Explanation: Any type of inheritance can be used in any program. There is no rule to use only few types
of inheritance. Only thing that matters is how the classes are inherited and used.
a) Single level
b) Hybrid
c) Hierarchical
d) Multilevel
------------
Answer: b
Explanation: In diamond problem, hierarchical inheritance is used first, where two different classes
inherit the same class and then in turn a 4th class inherits the two classes which had inherited the first
class. Using more than one type of inheritance here, it is known as hybrid inheritance.
-. If 6 classes uses single level inheritance with pair classes (3 pairs), which inheritance will this be called?
a) Single
b) Multiple
c) Hierarchical
d) Multilevel
------------
Answer: a
Explanation: Here all the pairs are using single inheritance. And no different pairs are inheriting same
classes. Hence it can’t be called hybrid or multilevel inheritance. You can say the single inheritance is
used 3 times in that program.
class A
public : class B
{
public : B(int i): data(i)
int data;
};
class C: public A
};
d) Single level inheritance is used, with both enclosing and nested classes
------------
Answer: d
Explanation: Class C is inheriting Class A. Class D is inheriting class B, both are nested. Hence it is single
inheritance. For multiple inheritance, class C or D should have inherited both class A and class B.
a) True
b) False
------------
Answer: b
Explanation: This is so because few classes might not be involved in any type of inheritance in whole
program whereas other classes might be participating in more than one type of inheritance at the same
time.
a) Single level
b) Multiple
c) Hybrid
------------
Answer: d
Explanation: This is a common type of inheritance where the protected and public members of parent
class become private members in child class. There is no type which doesn’t support private inheritance.
-. How many classes can be inherited by a single class in multiple inheritance (C++)?
a) Only 2
b) Only 27
c) Only 1024
------------
Answer: d
Explanation: Any class can inherit any number of classes. There is no limit defined for the number of
classes being inherited by a single class.
a) Only 1
b) Only 27
c) Only 255
d) Only 1024
------------
Answer: a
Explanation: Since java doesn’t support multiple inheritance, it is not possible for a class to inherit more
than 1 class in java. This is the same case in C# also.
-. If multi-level inheritance is used, First class B inherits class A, then C inherits B and so on. Till how
many classes can this go on?
a) Only till class C
d) There is no limit
------------
Answer: d
Explanation: In this case, there is no limit. All the classes going on like this will inherit the members of
base class, and hence the upper level inheritance won’t affect the number of classes that can go on
inheriting in this pattern.
------------
Answer: b
Explanation: If only one base class is used to derive only one subclass, it is known as single level
inheritance. The reason of this name is that we inherit the base class to one more level and stop the
inheritance any further.
-. If class A and class B are derived from class C and class D, then ________________
------------
Answer: a
Explanation: Since class A is derived from class C and then class B is derived from class D, there are two
pairs of classes which shows single inheritance. Those two pairs are independent of each other though.
a) At least 2 classes
b) At most 2 classes
c) Exactly 2 classes
d) At most 4 classes
------------
Answer: a
Explanation: The program will contain at least 2 classes in the sense of base and derived classes. At least
one base class and one derived class must be there. Types of inheritance remains the same though.
a) Runtime
b) Compile time
c) Multiple inheritance
d) Language independency
------------
Answer: a
Explanation: The runtime inheritance is done when object of a class is created to call a method. At
runtime the function is searched if it is in class of object. If not, it will search in its parent classes and
hierarchy for that method.
class A
protected int a, b;
{
cout<<a<<b;
};
class B: public A
cout<<a++<<b++;
};
void avg()
cout<<(a+b)/2;
};
a) Class A
b) Class B
c) Class C
d) None
------------
Answer: b
Explanation: Class B is using single level inheritance. Class C is using multiple inheritance. And class A is
parent of other two classes.
-. If single level inheritance is used and an abstract class is created with some undefined functions, can
its derived class also skip some definitions?
------------
Answer: d
Explanation: The derived class must implement those methods. This is because the parent class is
abstract and hence will have some undefined functions which has to be defined in derived classes. Since
we are using single level inheritance, if derived class doesn’t implement those functions then one more
class has to be there which will become multi-level inheritance.
d) The derived class must implement all the abstract method if single inheritance is used
------------
Answer: c
Explanation: If more than 2 independent classes are involved to implement the single level inheritance,
it won’t be possible as there must be only one child and one parent class and none other related class.
-. Which concept will result in derived class with more features (consider maximum 3 classes)?
a) Single inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Hierarchical inheritance
------------
Answer: b
Explanation: If single inheritance is used then only feature of a single class are inherited, and if multilevel
inheritance is used, the 2nd class might have use private inheritance. Hence only multiple inheritance
can result in derived class with more features. This is not mandatory but in a case if we consider same
number of features in each class, it will result the same.
class A
cout<<a<<b;
};
class B:public A
int x,y;
};
a) Garbage value
c) Runtime error
------------
Answer: b
Explanation: The compiler doesn’t find the main function and hence will throw an error main() missing.
This program is using single level inheritance but the program is incomplete. Every program must
implement main function.
class A
{
float sal=40000;
class B extends A
int salBonus=10000;
B p=new B();
System.out.println("Bonus of B is:"+p.bonus);
a)
b)
B salary is 10000
d) Runtime error
------------
Answer: a
Explanation: The program gives output as in option a. The program have used single level inheritance
and hence have access to the parent class methods and variables. This program simply prints the value
from parent class and from the child class.
------------
Answer: b
Explanation: Inheriting a class which can perform the most common task will be more efficient. If class
which can perform all the calculations is inherited then there won’t be any problem to print the result
too. But if a class which can do the most common task for all the other tasks, it will make real use of
inheritance.
-. Which constructor will be called first from the classes involved in single inheritance from object of
derived class?
d) Runtime error
------------
Answer: a
Explanation: The base class constructor will be called first from the object of derived class constructor.
When the derived class members are to be initialized and allocated memory, the base class members
should also be confirmed with the memory allocation.
-. If base class contains 2 nested classes, will it be possible to implement single level inheritance?
a) Yes, always
d) No, never
------------
Answer: a
Explanation: The nested classes are also members of a class. They behave as a used defined data type
with some methods implementation. So the inheritance will be as usual with the nested classes being
member of base class and of derived class if not private.
------------
Answer: b
Explanation: Only if the class is being derived from other derived class, it can be called as multilevel
inheritance. If a class is derived from another class, it is single level inheritance. There must be more
than one level of inheritance.
-. If there are 5 classes, E is derived from D, D from C, C from B and B from A. Which class constructor
will be called first if the object of E or D is created?
a) A
b) B
c) C
d) A and B
------------
Answer: a
Explanation: A is parent of all other classes indirectly. Since A is parent of B and B is parent of C and so
on till E. Class A constructor will be called first always.
-. If there are 3 classes. Class C is derived from class B and B is derived from A, Which class destructor
will be called at last if object of C is destroyed.
a) A
b) B
c) C
d) All together
------------
Answer: a
Explanation: The destructors are called in the reverse order of the constructors being called. Hence in
multilevel inheritance, the constructors are created from parent to child, which leads to destruction
from child to parent. Hence class A destructor will be called at last.
------------
Answer: a
Explanation: The class with highest degree of abstraction will be the class at the 1st level. You can look
at a simple example like, a CAR is more abstract than SPORTS CAR class. The level of abstraction
decrease with each level as more details comes out.
5. If all the classes use private inheritance in multilevel inheritance then ______________
c) Each subsequent class can access all members of previous level parent classes
------------
Answer: b
Explanation: The classes will be able to access only the non-private members of its parent class. The
classes are using private inheritance, hence all the members of the parent class become private in the
derived class. In turn those won’t be allowed for further inheritance or direct access outside the class.
------------
Answer: d
Explanation: The multilevel inheritance allows any number of levels of inheritance. This is the maximum
flexibility feature to make the members available to all the new classes and to add their own
functionalities. The code reusability is used too.
a) 1
b) 2
c) 3
d) 4
------------
Answer: c
Explanation: There must be at least 3 levels of inheritance. Otherwise if less, it will be single level
inheritance or would have got no inheritance implemented. There must be a derived class from which
another class is derived.
------------
Answer: a
Explanation: The classes inherit only from one class. This continues as each class inherits only one class.
There should not be any class that inherits from two or more classes or which have more than one
subclass.
b) Only 2
d) Can’t be used
------------
Answer: c
Explanation: At least one class must implement all the undefined functions. Hence there must be at least
one class which is not abstract. That is at least one less than number of levels.
-. If all the classes used parameterized constructors and no default constructor then ___________
b) Object of lower level classes must call parent class constructors explicitly
c) Object of lower level classes must define all the default constructors
------------
Answer: b
Explanation: Each class constructor must be called before creating the object of any subclass. Hence it
will be mandatory to call the constructors of parent classes explicitly with parameters. This will make all
the previous class member be initialized and then the class in use will be able to create the object.
a) Code readability
b) Flexibility
c) Code reusability
d) Code efficiency
------------
Answer: c
Explanation: The classes using multilevel inheritance 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 redundant functions.
class A
int a;
};
class B
int b;
};
int c;
};
class D:public C
int d;
};
------------
Answer: d
Explanation: Since multiple inheritance is used to derive class C and then class D is derived from class C.
This is not multilevel inheritance. The classes should derive from single class. This is actually hybrid
inheritance.
-. Is it compulsory for all the classes in multilevel inheritance to have constructors defined explicitly if
only last derived class object is created?
a) Yes, always
------------
Answer: c
Explanation: It’s not mandatory to define the constructors explicitly. Default constructor will always be
provided by the compiler itself if none another constructor is defined in those classes. If explicit default
constructor is defined it will be used.
------------
Answer: b
Explanation: The multiple inheritance is used when a class is being derived using two base classes or
more. This way a single class can have features of more than one classes inherited into a single unit. This
lets us combine two class members into a single class.
-. Which problem arises due to multiple inheritance, if hierarchical inheritance is used previously for its
base classes?
a) Diamond
b) Circle
c) Triangle
d) Loop
------------
Answer: a
Explanation: The diamond problem arises when multiple inheritance is used. This problem arises
because the same name member functions get derived into a single class. Which in turn creates
ambiguity in calling those methods.
-. How many classes should a program contain to implement the multiple inheritance?
a) Only 1
b) At least 1
c) At least 3
d) Exactly 3
------------
Answer: c
Explanation: For the implementation of multiple inheritance, there must be at least 3 classes in a
program. At least 2 base classes and one class to inherit those two classes. If lesser, it becomes single
level inheritance.
a) C++
b) PHP
c) SmallTalk
d) Java
------------
Answer: d
Explanation: Java doesn’t allow use of multiple inheritance with classes. But this can be done by using
the interfaces. This is more secure and unambiguous way to implement multiple inheritance.
-. Is it possible to have all the abstract classes as base classes of a derived class from those?
a) Yes, always
d) No, never
------------
Answer: b
Explanation: The condition for abstract class applies same here too. All the undefined functions must be
defined. Hence all the base classes can be abstract but derived class must implement all those undefined
functions.
-. If class A inherits class B and class C as “class A: public class B, public class C {// class body ;}; ”, which
class constructor will be called first?
a) Class A
b) Class B
c) Class C
d) All together
------------
Answer: b
Explanation: The constructors of parent class will be called first. In that, the constructor of the classes
will be called in the same sequence as that mentioned in class definition inheritance. Since class B is
mentioned first for inheritance, its constructor will be called first.
c) Derived class gets overloaded with more than two class methods
d) Derived class can’t distinguish the owner class of any derived method
------------
Answer: a
Explanation: All the derived classes can distinguish the base class members, but if a method is being
inherited to the base classes from another class which again gets inherited into same class (diamond
shape), that may create conflict in using the function from two available.
-. How many base classes can a derived class have which is implementing multiple inheritance?
a) Only 2
b) At least 2
c) At most 2
d) As many as required
------------
Answer: d
Explanation: The classes can derive from as many classes as required since the multiple inheritance
feature is made to combine or group together the functions that are from different classes. This make
the derived class stronger in terms of its flexibility.
d) Can’t be done
------------
Answer: c
Explanation: To overcome the ambiguity and conflict we can use keyword virtual. This will help us to
differentiate the functions with same name that came to last derived class in diamond problem.
-. When multiple inheritance is used, which class object should be used in order to access all the
available members of parent and derived class?
------------
Answer: a
Explanation: The derived class object can access all of its own members. It can also access the available
members of the parent classes, because the members are derived into the derived class.
-. If a class contains 2 nested class and is being inherited by another class, will there be any multiple
inheritance?
------------
Answer: a
Explanation: When a class having nested classes is being derived into another class. It indirectly means a
simple class is being inherited to another class. This is single level inheritance.
------------
Answer: a
Explanation: The private member’s are available for only the class containing those members. Derived
classes will have access to protected and public members only.
------------
Answer: d
Explanation: The derived class must not be abstract. This is because the abstract classes doesn’t have
constructor and hence we won’t be having the capability to have instances. This will restrict the use of
multiple inheritance.
-. Virtual function is ______ class function which expected to be redefined in ______ class, so that when
reference is made to derived class object using pointer then we can call virtual function to execute
________ class definition version.
------------
Answer: a
Explanation: The functions which may give rise to ambiguity due to inheritance, can be declared virtual.
So that whenever derived class object is referred using pointer or reference to the base class methods,
we can still call the derived class methods using virtual function. Hence this differentiates those
methods from each other.
-. What does a virtual function ensure for an object, among the following?
c) Correct method is called, regardless of the type of reference used for function call
d) Correct method is called, regardless of the type of function being called by objects
------------
Answer: c
Explanation: It is property of the virtual function and one of their main use. Its use ensure that the
correct method is called even though it is been called from different pointer or references. This also
decreases chance of mistakes in program.
b) Interpreter polymorphism
c) Runtime polymorphism
------------
Answer: c
Explanation: It is used to achieve runtime polymorphism. The functions which are inherited and
overridden, so at runtime the correct function is executed. The correct function call is made from the
intended class.
a) virtual
b) virt
c) anonymous
d) virtually
------------
Answer: a
Explanation: The virtual keyword is used to declare virtual functions. Anonymous keyword is used with
classes and have a different meaning. The virtual functions are used to call the intended function of the
derived class.
b) Derived class
d) Base class
------------
Answer: d
Explanation: The virtual function should be declared in base class. So that when the derived class
inherits from the base class, the functions can be differentiated from the one in base class and another
in derived class.
b) Interpret time
c) Runtime
------------
Answer: c
Explanation: The resolving of virtual functions that are to be called is done at run time. The base class
and the derived classes may contain different definitions and different variables, so all these things are
resolved at run time and decided which function is to be called.
a) Private
b) Public
c) Protected
d) Default
------------
Answer: b
Explanation: The virtual functions must be defined in public section of a class. This is to ensure that the
virtual function is available everywhere in the program. Also to avoid any error while resolving the
method.
a) Static function
b) Parameterized function
------------
Answer: a
Explanation: The virtual function must not be static. Those functions are the property of individual
objects and not of a class as a whole. The functions should not be made common for all the objects of
that class.
a) True
b) False
------------
Answer: a
Explanation: The friend functions can access the private members also. This may hinder the security of
class members. This is why the functions should not be made friend functions of other class.
------------
Answer: c
Explanation: The virtual functions must be called using pointer or reference. This is mandatory so that
the intended function gets executed while resolving the method at runtime. The must not be any
ambiguity between the method of parent class and derived class.
a) Necessary
b) Not necessary
c) Not acceptable
d) Good practice
------------
Answer: b
Explanation: It is not necessary to redefine the virtual function in the derived class. If not defined, the
base class function definition is used but if defined, the intended definition is used according to need. It
is not about good coding practice as it should be redefined only if needed.
------------
Answer: a
Explanation: Any class can contain virtual destructor. But is not possible to define a virtual constructor.
The reason behind is that the destructors can be overridden but constructors should not be.
------------
Answer: b
Explanation: The virtual functions may or may not be declared virtual in derived class. This is because if
the overriding function defined in derived class is not declared virtual explicitly, the compiler makes it
virtual implicitly.
------------
Answer: b
Explanation: The abstract functions must only be declared in base class. Their definitions are provided by
the derived classes. It is a mandatory condition.
------------
Answer: a
Explanation: The abstract functions can’t be defined in base class. They are to be defined in derived
classes. It is a rule for abstract functions.
a) Abstract must not be defined in base class whereas virtual function can be defined
------------
Answer: a
Explanation: The abstract functions are only declared in base class. Derived classes have to implement
those functions in order to inherit that base class. The functions are always defined in derived classes
only.
Answer: d
Explanation: The abstract function are only declared in base classes and then has to be defined in all the
derived classes. This allows all the derived classes to define own definition of any function whose
declaration in base class might be common to all the other derived classes.
------------
Answer: c
Explanation: The derived classes must define the abstract function of base class in their own body. This
is a necessary condition. Because the abstract functions doesn’t contain any definition in base class and
hence becomes mandatory for the derived class to define them. All the functions in a program must
have some definition.
a) Runtime
b) Compile time
d) Interpreting time
------------
Answer: b
Explanation: When the program is compiled, these definitions are checked if properly defined. This
compiler also ensure that the function is being defined by all the derived classes. Hence we get a
compile time error if not done.
-. What is this feature of enforcing definitions of abstract function at compile time called?
a) Static polymorphism
b) Polymorphism
c) Dynamic polymorphism
------------
Answer: c
Explanation: The feature is known as Dynamic polymorphism. Because the definitions are resolved at
runtime. Even though the definitions are checked at compile time, they are resolved at runtime only.
a) <access-modifier>abstract<return-type>method_name (parameter)
------------
Answer: a
Explanation: The syntax must firstly contain the access modifier. Then the keyword abstract is written to
mention clearly to the compiler that it is an abstract method. Then prototype of the function with return
type, function name and parameters.
-. If a function declared as abstract in base class doesn’t have to be defined in derived class then ______
------------
Answer: b
Explanation: If the function that is not to be defined in derived class but is declared as abstract in base
class then the derived class must be made an abstract class. This will make the concept mandatory that
the derived class must have one subclass to define that method.
-. Static methods can’t be made abstract in java.
a) True
b) False
------------
Answer: a
Explanation: The abstract functions can’t be made static in a program. If those are made static then the
function will be a property of class rather than each object. In turn ever object or derived class must use
the common definition given in the base class. But abstract functions can’t be defined in the base class.
Hence not possible.
-. If a class have all the abstract methods the class will be known as ___________
a) Abstract class
b) Anonymous class
c) Base class
d) Derived class
------------
Answer: a
Explanation: The classes containing all the abstract methods are known as abstract classes. And the
abstract classes can never have any normal function with definition. Hence known as abstract class.
a) Private
b) Protected
c) Public
d) Default
------------
Answer: a
Explanation: The base class must not contain the abstract methods. The methods have to be derived and
defined in derived class. But if it is made private it can’t be inherited. Hence we can’t declare it as a
private member.
-. The abstract method definition can be made ___________ in derived class.
a) Private
b) Protected
c) Public
------------
Answer: d
Explanation: The derived class implements the definition of the abstract methods of base class. Those
can be made private in derived class if security is needed. There won’t be any problem in declaring it as
private.
a) 2
b) 3
c) 4
d) 5
------------
Answer: d
Explanation: There are basically 5 types of member functions possible. The types include simple, static,
const, inline, and friend member functions. Any of these types can be used in a program as per
requirements.
------------
Answer: a
Explanation: When there is no type defined for any function and just a simple syntax is used with the
return type, function name and parameter list then those are known as simple member functions. This is
a general definition of simple members.
a) Functions which use only static data member but can’t be accessed directly
c) Functions which can be accessed outside the class with the data members
d) Functions using only static data and can be accessed directly in main() function
------------
Answer: d
Explanation: The static member functions can be accessed directly in the main function. There is no
restriction on direct use. We can call them with use of objects also. But the restriction is that the static
member functions can only use the static data members of the class.
-. How can static member function can be accessed directly in main() function?
a) Dot operator
b) Colon
d) Arrow operator
------------
Answer: c
Explanation: The static member functions can be accessed directly in the main() function. The only
restriction is that those must use only static data members of the class. These functions are property of
class rather than each object.
-. Correct syntax to access the static member functions from the main() function is ______________
a) classObject::functionName();
b) className::functionName();
c) className:classObject:functionName();
d) className.classObject:functionName();
------------
Answer: b
Explanation: The syntax in option b must be followed in order to call the static functions directly from
the main() function. That is a predefined syntax. Scope resolution helps to spot the correct function in
the correct class.
c) Functions which treat all the data members as constant and doesn’t allow changes
------------
Answer: c
Explanation: The const member functions are intended to keep the value of all the data members of a
class same and doesn’t allow any changes on them. The data members are treated as constant data and
any modification inside the const function is restricted.
-. Which among the following best describes the inline member functions?
------------
Answer: d
Explanation: The functions which are defined with the keyword inline or are defined inside the class are
treated to be inline functions. Definitions inside the class are implicitly made inline if none of the
complex statements are used in the definition.
------------
Answer: d
Explanation: A non-member function of a class which can access even the private data of a class is a
friend function. It is an exception on access to private members outside the class. It is sometimes
considered as a member functions since it has all the access that a member function in general have.
------------
Answer: a
Explanation: The general syntax to be followed in order to declare a const function in a class is as in
option a. The syntax may vary in different programming languages.
a) friendly
b) new
c) friend
d) connect
------------
Answer: c
Explanation: The keyword friend is provided in programming languages to use it whenever a functions is
to be made friend of one class or other. The keyword indicates that the function is capable of new
functionalities like accessing private members.
------------
Answer: c
Explanation: The functions definitions can be given inside or outside the body of class. If defined inside,
general syntax is used. If defined outside then the class name followed by scope resolution operator and
then function name must be given for the definition.
a) True
b) False
------------
Answer: b
Explanation: There is no restriction on the use of type of member functions inside a single class. Any
type any number of times can be defined inside a class. The member functions can be used as required.
a) no keyword required
b) inline
c) inlined
d) line
------------
Answer: b
Explanation: The inline keyword is used to defined the inline member functions in a class. The functions
are implicitly made inline if defined inside the class body, but only if they doesn’t have any complex
statement inside. All functions defined outside the class body must be mentioned with explicit inline
keyword.
-. Which among the following are valid ways of overloading the operators?
------------
Answer: c
Explanation: The operators can be overloaded by using the member function or even the friend
functions can be used. This is because both of these can access all the data members of a class.
------------
Answer: a
Explanation: The operator to be overloaded must be made the member function of the operand on left
side of expressions to be used. This allows the compiler to identify whether the overloading has to be
used or not. This rule also reduces the ambiguity in code.
-. When the operator to be overloaded becomes the left operand member then ______________
------------
Answer: b
Explanation: The left operand becomes the object that is referred by *this pointer in the member
function that will be called while using operator overloading. This is done to point to a specific object on
which the overloading will be applied.
-. If the left operand is pointed by *this pointer, what happens to other operands?
------------
Answer: d
Explanation: The operands that are used during overloading expect the left operand, can be passed as
function arguments. Those are then referred in function definition with the names specified in the
argument list.
-. If a friend overloaded operator have to be changed to member overloaded operator, which operator
should be used with the class name?
b) Colon
c) Arrow operator
d) Dot operator
------------
Answer: a
Explanation: The scope resolution operator can be used followed by the class name. Then the operator
keyword with the operator symbol that should be overloaded. This is done to use member function
instead of friend function.
a) className::operator<operatorSymbol>(parameters)
b) className:operator<operatorSymbol>(parameters)
c) className.operator<operatorSymbol>(paramteres)
d) className->operator<operatorSymbol>(parameters)
------------
Answer: a
Explanation: The class name is followed by the scope resolution operator. This is done to specify the
class to which the function should belong to. Then the keyword operator should be used in order to
indicate the operator that is to be overloaded. Then come the parameters list to specify other operands.
a) Because it is of no use
------------
Answer: c
Explanation: The left object is removed from being passed as a parameter, because it is implicitly
passed. It is passed implicitly because it is considered the object with respect to which the overloading
function is being called.
-. Which object’s members can be called directly while overloading operator function is used (In function
definition)?
------------
Answer: a
Explanation: This is because the left operand is passed implicitly. It is pointed by *this. This in turn
means we can use the direct member names of the object because those are again converted to a
syntax containing *this pointer implicitly.
-. If left operand member is specified directly in the function definition, which is the correct implicit
conversion of that syntax?
a) *this className
b) *this parameterObject
c) *this returnedObject
d) *this object
------------
Answer: d
Explanation: Since the left operands are passed implicitly, those object members can be accessed
directly in the function definition. The compiler converts the syntax into the syntax that can be
processed. The implicitly converted syntax contains *this pointer followed by the objectName that is left
operand in the expression.
-. When the friend operator overloading is converted into member operator overloading
_______________
a) Two parameters of friend function remains same parameters in member operator overloading
b) Two parameters of friend function becomes only one parameter of member function
c) Two parameters of friend function are removed while using member function
------------
Answer: b
Explanation: The friend function would accept two arguments if some binary operator is overloaded.
When we try to convert that definition to member operator overloading then it becomes only one
parameter. The reason behind is that the left operand is passed implicitly while using the member
functions.
------------
Answer: c
Explanation: The left operand is passed implicitly by the compiler to the member function. But this is
done, when the compiler adds the calling object as *this to the parameter list. It is always added as the
left most parameter, i.e. the first parameter of the function.
-. Which operator among the following can be overloading using only member function?
a) Assignment operator
b) Addition operator
c) Subtraction operator
------------
Answer: a
Explanation: Only the assignment operator among the options given must be overloaded using the
member functions. The assignment operator can’t be overloaded using friend function. This is a
restriction in the programming languages to make the programs more resistant towards errors.
-. Which operator among the following must be overloaded using the friend function?
------------
Answer: c
Explanation: In some cases it is mandatory to use the friend functions for overloading the operators.
Here both the << and >> operators must be overloaded using friend function because the left operand is
object of some other class and the right operand is usually of some different type.
------------
Answer: a
Explanation: The memory allocated for the object members indicates actual creation of the object
members. This is known as memory allocation for object.
a) HDD
b) Cache
c) RAM
d) ROM
------------
Answer: c
Explanation: The memory for the objects or any other data is allocated in RAM initially. This is while we
run a program all the memory allocation takes place in some RAM segments. Arrays in heap and local
members in stack etc.
a) At declaration of object
b) At compile time
------------
Answer: c
Explanation: The object memory allocation takes place when the object constructor is called.
Declaration of an object doesn’t mean that memory is allocated for its members. If object is initialized
with another object, it may just get a reference to the previously created object.
------------
Answer: b
Explanation: The new is type safe because we don’t have to specify the type of data that have to be
allocated with memory. We can directly use it with data name. Name of the data doesn’t matter though
for type of memory allocation though.
-. Which of the following function can be used for dynamic memory allocation of objects?
a) malloc()
b) calloc()
c) create()
------------
Answer: d
Explanation: The malloc() function can be used to allocate dynamic memory for objects. Function
calloc() can also be use. These functions differ in the way they allocate memory for objects.
-. How much memory will be allocated for an object of class given below?
class Test{
int mark1;
int mark2;
float avg;
char name[10];
};
a) 22 Bytes
b) 24 Bytes
c) 20 Bytes
d) 18 Bytes
------------
Answer: a
Explanation: The size of an object of the class given in question will be of size 22 bytes. This is because
the size of an object is always equal to the sum of sizes of the data members of the class, except static
members.
-. Which keyword among the following can be used to declare an array of objects in java?
a) new
b) create
c) allocate
d) arr
------------
Answer: a
Explanation: The keyword new can be used to declare an array of objects in java. The syntax must be
specified with an object pointer which is assigned with a memory space containing the required number
of object space. Even initialization can be done directly.
a) At termination of program
------------
Answer: b
Explanation: Whenever an object goes out of scope, the deletion of allocation memory takes place.
Actually the data is not deleted, instead the memory space is flagged to be free for further use. Hence
whenever an object goes out of scope the object members become useless and hence memory is set
free.
-. Which among the following keyword can be used to free the allocated memory for an object?
a) delete
b) free
d) only delete
------------
Answer: c
Explanation: The memory allocated for an object is usually automatically made free. But if explicitly
memory has to be made free then we can use either free or delete keywords depending on
programming languages.
a) Destructor function
b) Constructor function
c) Delete function
d) Free function
------------
Answer: a
Explanation: The destructor function of the class is called whenever an object goes out of scope. This is
because the destructor set all the resources, acquired by the object, free. This is an implicit work of
compiler.
a) sizeof(objectName)
b) size(objectName)
c) sizeofobject(objectName)
d) sizedobject(objectName)
------------
Answer: a
Explanation: The sizeof operator is used to get the size of an already created object. This operator must
constail keyword sizeof(objectName). The output will give the number of bytes acquired by a single
object of some class.
------------
Answer: c
Explanation: The memory allocation for an object can be static or dynamic. The static memory allocation
is when an object is declared directly without using any function usually. And dynamic allocation is when
we use some dynamic allocation function to allocate memory for data member of an object.
-. Which operator can be used to free the memory allocated for an object in C++?
a) Free()
b) delete
c) Unallocate
d) Collect
------------
Answer: b
Explanation: The delete operator in C++ can be used to free the memory and resources held by an
object. The function can be called explicitly whenever required. In C++ memory management must be
done by the programmer. There is no automatic memory management in C++.
d) Member functions having same name and different signature inside main function
------------
Answer: a
Explanation: The member function which is defined in base class and again in the derived class, is
overridden by the definition given in the derived class. This is because the preference is given more to
the local members. When derived class object calls that function, definition from the derived class is
used.
-. Which among the following is true?
------------
Answer: c
Explanation: The inheritance must be used in order to use function overriding. If inheritance is not used,
the functions can only be overloaded. There must be a base class and a derived class to override the
function of base class.
b) The declaration must be exactly the same in base and derived class
c) The declaration should have at least 1 same argument in declaration of base and derived class
d) The declaration should have at least 1 different argument in declaration of base and derived class
------------
Answer: b
Explanation: For a function to be over ridden, the declaration must be exactly the same. There must not
be any different syntax used. This will ensure that the function to be overridden is only the one intended
from to be overridden from the derived class.
------------
Answer: d
Explanation: Declaration includes the whole prototype of the function. The return type name and the
parameter list must be same in order to confirm that the function is same in derived and the base class.
And hence can be overridden.
-. Which among function will be overridden from the function defined in derived class below:
class A
int i;
void show()
cout<<i;
void print()
cout <<i;
};
class B
int j;
void show()
cout<<j;
};
a) show()
b) print()
Answer: a
Explanation: The declaration must be exactly same in the derived class and base class. The derived class
have defined show() function with exactly same declaration. This then shows that the function in base
class is being overridden if show() is called from the object of class B.
-. How to access the overridden method of base class from the derived class?
------------
Answer: c
Explanation: Scope resolution operator :: can be used to access the base class method even if
overridden. To access those, first base class name should be written followed by the scope resolution
operator and then the method name.
------------
Answer: b
Explanation: If the function is private in the base class, derived class won’t be able to access it. When the
derived class can’t access the function to be overridden then it won’t be able to override it with any
definition.
a) C++
b) C#
c) Java
d) SmallTalk
------------
Answer: b
Explanation: The feature of method overriding is not provided in C#. To override the methods, one must
use override or virtual keywords explicitly. This is done to remove accidental changes in program and
unintentional overriding.
-. In C# ____________________
------------
Answer: a
Explanation: The non-virtual and static methods can’t be overridden in C# language. The restriction is
made from the language implicitly. Only the methods that are abstract, virtual or override can be
overridden.
-. In Delphi ______________
------------
Answer: c
Explanation: This is possible but only if the method to be overridden is marked as dynamic or virtual. It is
inbuilt restriction of programming language. This is done to reduce the accidental or unintentional
overriding.
-. What should be used to call the base class method from the derived class if function overriding is used
in Java?
a) Keyword super
b) Scope resolution
c) Dot operator
------------
Answer: a
Explanation: The keyword super must be used to access base class members. Even when overriding is
used, super must be used with the dot operator. The overriding is possible.
a) Private
b) Open
c) Closed
d) Abstract
------------
Answer: b
Explanation: The function to be overridden must be open. This is a condition in Kotlin for any function to
be overridden. This avoids accidental overriding.
------------
Answer: d
Explanation: The functions declared to be abstract in base class are redefined in derived classes. That is,
the functions are overridden by the definitions given in the derived classes. This must be done to give at
least one definition to each undefined function.