Object Oriented Mcqs
Object Oriented Mcqs
class A
{
int a;
};
class B
{
int b;
};
class C:public A, public B
{
int c;
};
class D:public C
{
int d;
};
a) Yes, class C and class D
b) Yes, All together it’s multilevel
c) No, 4 classes are used
d) No, multiple inheritance is used with class A, B and C
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.
15. 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
b) Yes, to initialize the members
c) No, it not necessary
d) No, Constructor must not be defined
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Multiple
Inheritance”.
1. Multiple inheritance is ____________________
a) When a class is derived from another class
b) When a class is derived from two or more classes
c) When a class is derived from other two derived classes
d) When a class is derived from exactly one class
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.
2. Which problem arises due to multiple inheritance, if hierarchical inheritance is used previously for its base classes?
a) Diamond
b) Circle
c) Triangle
d) Loop
Answer: a
Explanation: The diamond problem arises when multiple inheritance is used. This problem arises because the same name
member functions get derived into a single class. Which in turn creates ambiguity in calling those methods.
3. How many classes should a program contain to implement the multiple inheritance?
a) Only 1
b) At least 1
c) At least 3
d) Exactly 3
Answer: c
Explanation: For the implementation of multiple inheritance, there must be at least 3 classes in a program. At least 2 base
classes and one class to inherit those two classes. If lesser, it becomes single level inheritance.
4. Which programming language restricts the use of multiple inheritance?
a) C++
b) PHP
c) SmallTalk
d) Java
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.
5. Is it possible to have all the abstract classes as base classes of a derived class from those?
a) Yes, always
b) Yes, only if derived class implements all the methods
c) No, because abstract classes doesn’t have constructors
d) No, never
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.
6. If class A inherits class B and class C as “class A: public class B, public class C {// class body ;}; ”, which class constructor will
be called first?
a) Class A
b) Class B
c) Class C
d) All together
Answer: b
Explanation: The constructors of parent class will be called first. In that, the constructor of the classes will be called in the same
sequence as that mentioned in class definition inheritance. Since class B is mentioned first for inheritance, its constructor will be
called first.
7. Why does diamond problem arise due to multiple inheritance?
a) Methods with same name creates ambiguity and conflict
b) Methods inherited from the superclass may conflict
c) Derived class gets overloaded with more than two class methods
d) Derived class can’t distinguish the owner class of any derived method
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.
8. How many base classes can a derived class have which is implementing multiple inheritance?
a) Only 2
b) At least 2
c) At most 2
d) As many as required
Answer: d
Explanation: The classes can derive from as many classes as required since the multiple inheritance feature is made to
combine or group together the functions that are from different classes. This make the derived class stronger in terms of its
flexibility.
9. How to overcome diamond problem?
a) Using alias name
b) Using seperate derived class
c) Using virtual keyword with same name function
d) Can’t be done
Answer: c
Explanation: To overcome the ambiguity and conflict we can use keyword virtual. This will help us to differentiate the functions
with same name that came to last derived class in diamond problem.
10. When multiple inheritance is used, which class object should be used in order to access all the available members of parent
and derived class?
a) Derived class object
b) Parent class objects
c) Use Abstract derived class
d) Derive a class from derived class
Answer: a
Explanation: The derived class object can access all of its own members. It can also access the available members of the parent
classes, because the members are derived into the derived class.
11. If all the members of all the base classes are private then _____________
a) There won’t be any use of multiple inheritance
b) It will make those members public
c) Derived class can still access them in multiple inheritance
d) Compile time error
Answer: a
Explanation: The derived class will not be able to access any members of the base classes. Since private member’s are not
inheritable. It leads to no use of multiple inheritance.
12. Is it compulsory to have constructor for all the classes involved in multiple inheritance?
a) Yes, always
b) Yes, only if no abstract class is involved
c) No, only classes being used should have a constructor
d) No, they must not contain constructors
Answer: b
Explanation: The constructors must be defined in every class. If class is abstract, it won’t have any constructor but other classes
must have constructor. Either implicit or explicit.
13. If a class contains 2 nested class and is being inherited by another class, will there be any multiple inheritance?
a) No, only single level inheritance is used
b) No, only multilevel inheritance is used
c) Yes, because 3 classes are involved
d) Yes, because more than 1 classes are being derived
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.
14. Which members can’t be accessed in derived class in multiple inheritance?
a) Private members of base
b) Public members of base
c) Protected members of base
d) All the members of base
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.
15. Can the derived class be made abstract if multiple inheritance is used?
a) No, because other classes must be abstract too
b) Yes, if all the functions are implemented
c) Yes, if all the methods are predefined
d) No, since constructors won’t be there
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Nested
Class”.
1. Which among the following best describes a nested class?
a) Class inside a class
b) Class inside a function
c) Class inside a package
d) Class inside a structure
Answer: a
Explanation: If a class is defined inside another class, the inner class is termed as nested class. The inner class is local to the
enclosing class. Scope matters a lot here.
2. Which feature of OOP reduces the use of nested classes?
a) Encapsulation
b) Inheritance
c) Binding
d) Abstraction
Answer: b
Explanation: Using inheritance we can have the security of the class being inherited. The subclass can access the members of
parent class. And have more feature than a nested class being used.
3. How many categories are nested classes divided into?
a) 2
b) 3
c) 4
d) 5
Answer: a
Explanation: The nested classes are divided into two main categories. Namely, Static and non-static. The categories define how
the classes can be used inside another class.
4. Non-static nested classes have access to _____________ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) All the members
Answer: d
Explanation: The non-static nested class can access all the members of the enclosing class. All the data members and member
functions can be accessed from the nested class. Even if the members are private, they can be accessed.
5. Static nested classes doesn’t have access to _________________ from enclosing class.
a) Private members
b) Protected members
c) Public members
d) Any other members
Answer: d
Explanation: The static nested class doesn’t have access to any other members of the enclosing class. This is a rule that is
made to ensure that only the data which can be common to all the object is being accessed by the static nested class.
6. The nested class can be declared ___________________
a) Public
b) Private
c) Protected
d) Public, Protected, Private or Package private
Answer: d
Explanation: The nested class can be declared with any specifier, unlike the outer classes which can only be declared public or
package private. This is flexibility given for the nested class being a member of enclosing class.
7. Use of nested class ____________ encapsulation.
a) Increases
b) Decreases
c) Doesn’t affect
d) Slightly decreases
Answer: a
Explanation: The use of nested class increases encapsulation as the inner class is getting even more grouped into the enclosing
class. Firstly the class encapsulate the data, having nested classes can increase the encapsulation even further.
8. Which among the following is the correct advantage/disadvantage of nested classes?
a) Makes the code more complex
b) Makes the code unreadable
c) Makes the code efficient and readable
d) Makes the code multithreaded
Answer: c
Explanation: The use of nested classes makes the code more streamed towards a single concept. This allows to group the most
similar and related classes together and makes it even more efficient and readable.
9. How to access static nested classes?
a) OuterClass.StaticNestedClass
b) OuterClass->StaticNestedClass
c) OuterClass(StaticNestedClass)
d) OuterClass[StaticNestedClass]
Answer: a
Explanation: Like any other member of the class, the static nested class uses the dot operator to be accessed. The reason
behind is, the static classes can’t work with instances, hence we use enclosing class name to access static nested class.
10. A nested class can have its own static members.
a) True
b) False
Answer: b
Explanation: The nested classes are associated with the object of the enclosing class. Hence have direct access to the
members of that object. Hence the inner class can’t have any static members of its own. Otherwise the rule of static members
would be violated using enclosing class instance.
11. How to create object of the inner class?
a) OuterClass.InnerClass innerObject = outerObject.new InnerClass();
b) OuterClass.InnerClass innerObject = new InnerClass();
c) InnerClass innerObject = outerObject.new InnerClass();
d) OuterClass.InnerClass = outerObject.new InnerClass();
Answer: a
Explanation: An instance of inner class can exist only within instance of outer class. To instantiate the inner class, one must
instantiate the outer class first. This can be done by the correct syntax above.
12. What will be the output of the following code?
public class Test
{
public int a=0;
class innerClass
{
public int a=1;
void innermethod(int x)
{
System.out.println(“value of x = ” + x);
System.out.println(“value of this.x = ” + this.x);
System.out.println(“value of Test.this.x = ” + Test.T=this.x);
}
}
}
public static void main( String args[] )
{
Test t=new Test();
Test.innerClass im=t.new innerClass();
im.innermethod(55);
}
a)
value of x = 55
value of this.x = 0
value of Test.this.x = 1
b)
value of x = 1
value of this.x = 0
value of Test.this.x = 55
c)
value of x = 55
value of this.x = 1
value of Test.this.x = 0
d)
value of x = 0
value of this.x = 55
value of Test.this.x = 1
Answer: c
Explanation: The variable x denotes the parameter of the function. And this.x is the variable of the inner class. Test.this.x is the
variable of the outer class. Hence we get this output.
13. Instance of inner class can exist only _______________ enclosing class.
a) Within
b) Outside
c) Private to
d) Public to
Answer: a
Explanation: The class defined inside another class is local to the enclosing class. This means that the instance of inner class
will not be valid outside the enclosing class. There is no restriction for instance to be private or public always.
14. If a declaration of a member in inner class has the same name as that in the outer class, then ________________ enclosing
scope.
a) Outer declaration shadows inner declaration in
b) Inner declaration shadows outer declaration in
c) Declaration gives compile time error
d) Declaration gives runtime error
Answer: b
Explanation: The inner class will have more preference for its local members than those of the enclosing members. Hence it will
shadow the enclosing class members. This process is known as shadowing.
15. A static nested class is _____________ class in behavior that is nested in another _________ class.
a) Top level, top level
b) Top level, low level
c) Low level, top level
d) Low level, low level
Answer: a
Explanation: Top level class encloses the other classes or have same preference as that of other top level classes. Having a
class inside the top level class is indirectly having a top level class which higher degree of encapsulation.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “New
Operator”.
1. What is the new operator?
a) Allocates memory for an object or array
b) Allocates memory for an object or array and returns a particular pointer
c) Used as return type when an object is created
d) Used to declare any new thing in a program
Answer: b
Explanation: The new keyword is used to allocate memory of an object or array. The new object or array can be of any type. Then
it returns a suitable non zero pointer to the object.
2. Microsoft C++ Components extensions support new keyword to _____________
a) Modify a vtable
b) Replace a vtable slot entry
c) Add new vtable slot entries
d) Rearrange vtable slot entries
Answer: c
Explanation: The new keyword is used for adding new vtable slot entries. This is an additional feature in Microsoft C++. It can
use predefined class object for this work.
3. What happens when new fails?
a) Returns zero always
b) Throws an exception always
c) Either throws an exception or returns zero
d) Terminates the program
Answer: c
Explanation: While creating new objects, the new operator may fail because of memory errors or due to permissions. At that
moment the new operator returns zero or it may throw an exception. The exception can be handled as usual.
4. If new throws an error, which function can be called to write a custom exception handler?
a) _set_handler
b) _new_handler
c) _handler_setter
d) _set_new_handler
Answer: d
Explanation: If the default exception handler has to be replaced by a user defined handler, we can call _set_new_handler run-
time library function with the function name as an argument. This lets the programmer to give a custom definition for handling new
operator failure.
5. In C++, if new operator is used, when is the constructor called?
a) Before the allocation of memory
b) After the allocation of memory
c) Constructor is called to allocate memory
d) Depends on code
Answer: b
Explanation: The constructor function is called after the allocation of memory. In C++ the feature works in a bit different way. The
memory for all the data members is allocated first and then the constructor function is called to finalize the memory allocated.
6. Which among the following is correct syntax to declare a 2D array using new operator?
a) char (*pchar)[10] = new char[][10];
b) char (pchar) = new char[][10];
c) char (*char) = new char[10][];
d) char (*char)[][10]= new char;
Answer: a
Explanation: The new operator usage to declare a 2D array requires a pointer and size of array to be declared. Data type and
then the pointer with size of array. The left index can be left blank or any variable can be assigned to it.
7. For declaring data by using new operator ____________________
a) Type name can’t contain const
b) Type name can’t contain volatile
c) Type name can’t contain class declarations
d) Type name can’t contain const, volatile, class declaration or enumerations
Answer: d
Explanation: The declaration of any data where we use new operator, any of the mentioned types are not allowed. This is
because the new operator allocated memory based on the type of data which can be allocated dynamically.
8. The new operator _____________
a) Can allocate reference types too
b) Doesn’t allocate reference types
c) Can allocate reference to objects
d) Doesn’t allocate any data
Answer: b
Explanation: The new operator doesn’t allocate reference types. This is because the reference types are not objects. The new
operator is used to allocate memory to the direct objects.
9. Which among the following is true?
a) New operator can’t allocate functions but pointer to functions can be allocated
b) New operator can allocate functions as well as pointer to functions
c) New operator can allocate any type of functions
d) New operator is not applicable with functions allocation
Answer: a
Explanation: The new operator can’t allocate functions but can allocate pointer to the functions. This is a security feature as well
as to reduce the ambiguity in code. The new keyword is not given functionality to directly allocate any function.
10. Which among the following is added in grammar of new operator?
a) Finalize
b) Arg
c) Initializer
d) Allocator
Answer: c
Explanation: The new operator grammar is added with an initializer field. This can be used to initialize an object with a user
defined constructor. Hence can allocate memory as intended by the programmer.
11. Initializers __________________
a) Are used for specifying arrays
b) Are used to defined multidimensional arrays
c) Can’t be specified for arrays
d) Can’t be specified for any data
Answer: c
Explanation: The initializers can’t be specified for arrays. The initializers can create arrays of object if and only if the class has a
default constructor. That is a zero argument constructor so that it can be called without any argument.
12. The objects allocated using new operator ________________
a) Are destroyed when they go out of scope
b) Are not destroyed even if they go out of scope
c) Are destroyed anytime
d) Are not destroyed throughout the program execution
Answer: b
Explanation: It is not necessary that the objects get destroyed when they go out of scope if allocated by using new operator. This
is because new operator returns a pointer to object that it had allocated. A suitable pointer with proper scope should be defined
by the programmer explicitly.
13. The new operator _________________
a) Invokes function operator new
b) Doesn’t invoke function operator new
c) Invokes function operator only if required
d) Can’t invoke function operator new implicitly
Answer: a
Explanation: The new operator invokes function operator new. This is done to allocate the storage to an object. ::operator new is
called for storage allocation implicitly.
14. If a new operator is defined for a class and still global new operator have to be used, which operator should be used with the
keyword new?
a) Colon
b) Arrow
c) Dot
d) Scope resolution
Answer: d
Explanation: As usual, scope resolution operator is used to get the scope of parent or the global entities. Hence we can use
scope resolution operator with the new operator to call the global new operator even if new operator is defined for the class
explicitly.
15. How does compiler convert “::operator new” implicitly?
a) ::operator new( sizeof( type ) )
b) ::operator new( sizeof( ) )
c) new operator :: type sizeof( type )
d) new sizeof( type ) operator
Answer: a
Explanation: The compiler implicitly converts the syntax so that the instruction can be understood by the processor and proper
machine code can be generated. The conversion is done implicitly and no explicit syntax is required.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Object
Array”.
1. What is an array of objects?
a) An array of instances of class represented by single name
b) An array of instances of class represented by more than one name
c) An array of instances which have more than 2 instances
d) An array of instances which have different types
Answer: a
Explanation: The array of objects an array of instances of a class. The array is represented by a single name. The array name is
itself a pointer. Array name represents the first object.
2. Which among the following is a mandatory condition for array of objects?
a) All the objects should be of different class
b) All the objects should be of same program classes
c) All the objects should be of same class
d) All the objects should have different data
Answer: c
Explanation: The objects of an array must be of same class. This is mandatory because array is set of same type of elements.
The objects of same class are considered to be of same type.
3. What is the type of elements of array of objects?
a) Class
b) Void
c) String
d) Null
Answer: a
Explanation: The class itself is the type of elements of array of objects. All the objects possess the same properties. Like any
other primitive data type, the objects are of their respective class type.
4. If array of objects is declared as given below, which is the limitation on objects?
Class_name arrayName[size];
a) The objects will have same values
b) The objects will not be initialized individually
c) The objects can never be initialized
d) The objects will have same data
Answer: b
Explanation: If the syntax given, is used to declare the array of objects, then the objects can’t be initialized individually. All the
objects will have to be initialized after this declaration.
5. Which is the condition that must be followed if the array of objects is declared without initialization, only with size of array?
a) The class should have separate constructor for each object
b) The class must have no constructors
c) The class should not have any member function
d) The class must have a default or zero argument constructor
Answer: d
Explanation: The class must have a default/zero argument constructor. Since the declaration is done by only specifying the size
of array, the class must have default a construct to be called by default to reserve memory for each object. Also, we can’t specify
the arguments in this type of declaration hence the class should provide a default initialization.
6. When are the array of objects without any initialization useful?
a) When object data is not required just after the declaration
b) When initialization of object data is to be made by the compiler
c) When object data doesn’t matter in the program
d) When the object should contain garbage data
Answer: a
Explanation: Sometimes the object data is not mandatory to be used just after the declaration or may be the program requires
the data to be updated according to what user inputs. Hence only declaration us also useful.
7. If constructor arguments are passed to objects of array then ____________ if the constructors are overloaded.
a) It is mandatory to pass same number of arguments to all the objects
b) It is mandatory to pass same type of arguments to all the objects
c) It is not mandatory to call same constructor for all the objects
d) It is mandatory to call same constructor for all the constructors
Answer: c
Explanation: It is not mandatory to call the same constructor for all the objects in an array if initialized with the declaration. The
objects can be passed with different set of arguments in the same syntax, separated by commas.
8. How the objects of array can be denoted?
a) Indices
b) Name
c) Random numbers
d) Alphabets
Answer: a
Explanation: Different objects in an array can be denoted with the indices of array. The first object is denoted by 0. And the
further indices denote the next objects in sequence of array.
9. The objects in an object array _______________________
a) Can be created without use of constructor
b) Can be created without calling default constructor
c) Can’t be created with use of constructor
d) Can’t be created without calling default constructor
Answer: b
Explanation: The objects need some constructor to get the memory spaced reserved for those. If the default constructor is not
used then we can use some arguments constructor which will reserve the memory for the objects. The objects can be passed
with constructor arguments during declaration.
10. The Object array is created in _____________________
a) Heap memory
b) Stack memory
c) HDD
d) ROM
Answer: a
Explanation: If the object arrays are declared dynamically, then the memory will be reserved on heap. The memory for objects will
be on stack only if some constructor or some call and return tasks are happening. The program doesn’t run on HDD and ROM is
not used for the execution of programs.
11. If an array of objects is of size 10 and a data value have to be retrieved from 5th object then ________________ syntax
should be used.
a) Array_Name[4].data_variable_name;
b) Data_Type Array_Name[4].data_variable_name;
c) Array_Name[4].data_variable_name.value;
d) Array_Name[4].data_variable_name(value);
Answer: a
Explanation: The array name with the index of fifth element is called, i.e. index 4. Then the dot operator is used to access the
data member of that object. This Allows us to access the data members of all the objects in an object array.
12. Can we have two dimensional object array?
a) Yes, always
b) Yes, only if primitive type array
c) No, since two indices are impossible
d) No, never
Answer: a
Explanation: A two dimensional array can always be created. There is no rule that only primitive type objects can have more than
one dimension. The object array can also be made 2 dimensional.
13. From which index does the array of objects start?
a) 0
b) 1
c) 2
d) 3
Answer: a
Explanation: The index must start from 0. The index ends at size – 1 index. This is because the index is always till n-1 where n is
the total number of beads.
14. Two dimensional array can’t be initialized with the declaration.
a) True
b) False
Answer: b
Explanation: The two dimensional arrays can also be initialized using curly brackets. For each set, values in curly bracket. And
then another bracket is added at first and end. This ensures that all the code belongs to the user.
15. Is an array of characters always a string?
a) Yes, always
b) Yes, if each character is terminated by null
c) No, since each character is terminated by null
d) No, never
Answer: d
Explanation: The character arrays are not the same as string. The string once created then remains the same. The character
array values may change.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Object
Reference”.
1. What is reference to an object?
a) It is address of an object
b) It is address of where the variables and methods of object are stored
c) It is pointer having address of an object
d) It is address of only variables and not the methods 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.
2. Whenever an object is assigned to a variable or passed to a method ________________
a) Actually the objects aren’t used
b) Actually only the objects are used
c) Actually a pointer to an object is used
d) Actually copy of object is used
Answer: a
Explanation: Whenever an object is assigned to a variable or passed to a method, we aren’t actually using objects there. Actually
the reference to the objects is used. The reference makes a lot of difference as the main object may or may not get affected
depending on the code.
3. Does use of object reference in assignment or passing means copy of the object is being used?
a) No, because the copy would create a new temporary variable
b) No, because the copy would not help to make changes to main object
c) Yes, because the reference directly means using address
d) Yes, because the reference directly means the constructors are involved
Answer: c
Explanation: We can’t say that the reference involves constructors in passing the objects to some method. The reference is used
to denote the addresses and hence to point to the main object itself. There is no copy involved.
4. What will be the output of the following code?
import java.awt.Point;
class Testing
{
public static void main(String[] args)
{
Point p1,p2;
p1=new Point(100,100);
p2=p1;
p1.x=200;
p1.y=200;
System.out.println(“Point 1: ” + p1.x + ”, “ + p1.y);
System.out.println(“Point 2: ” + p2.x + ”, “ + p2.y);
}
}
a)
Point 1: 100, 100
Point 2: 200, 200
b)
Point 1: 200, 200
Point 2: 100, 100
c)
Point 1: 100, 100
Point 2: 100, 100
d)
Point 1: 200, 200
Point 2: 200, 200
Answer: d
Explanation: The expected output would be like p2 with values 100, 100. But this is not the case. The tricky part is assignment
used (p2=p1;). Here a reference is created from object p1 to p2, and not any new object that would copy p1’s values. Hence
when we change the values of p1 object members. There changes are reflected to the object p2 also.
5. Is there any explicit use of pointers in java that would be applicable to objects?
a) Yes, we use reference for this purpose
b) Yes, we use java arrays for this purpose
c) No, implicit pointing is possible
d) No, direct class names should be used
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.
6. Can a super class object give reference to a subclass method?
a) No, it is not possible
b) Maybe, it is possible
c) No, it’s not possible
d) No, It’s not possible in few cases only
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.
7. What will be the output of the following code?
import java.awt.Point;
class Testing
{
public static void main(String[] args)
{
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;
System.out.println(“Point 1: ” + p1.x + ”, “ + p1.y);
}
}
a) Point 1: 200, 200
b) Point 1: 100,100
c) Point 1: 300, 300
d) Point 1: 300, 500
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.
8. If a reference variable is declared final then _________________
a) It can never be reassigned to refer to a different object
b) It can be assigned to refer to any object anytime
c) It can never be assigned with any object
d) It can be assigned with 2 or more objects simultaneously
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.
9. Which of the members are referred by this pointer usually (Java)?
a) Members of class where this is used
b) Member of the parent class where this is used
c) Members that are passed as argument to the object
d) Pointers are not applicable in java
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.
10. How to refer to method of nested class?
a) enclosingClassObject.innerClassObject.method();
b) innerClassObject.method();
c) method();
d) depends on where the method is being called
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.
11. How many objects can be referenced from the same variables?
a) One at a time
b) Many at a time
c) Many using array name
d) 7 at max at same 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).
12. 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.
13. Which among the following is true?
a) Object referencing refers to methods address
b) Object referencing refers to variable of object
c) Object referencing points to same address, if assigned by variables
d) Object referencing is used to point methods
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.
14. Invoking a method on a particular object is ____________ sending a message to that object.
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.
15. Can reference to an object be returned from a method?
a) Yes, always possible
b) Yes, but not always
c) No, never possible
d) No, Not possible because referred element would be destroyed
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on
“Objects”.
1. Which definition best describes an object?
a) Instance of a class
b) Instance of itself
c) Child of a class
d) Overview of a class
Answer: a
Explanation: An object is instance of its class. It can be declared in the same way that a variable is declared, only thing is you
have to use class name as the data type.
2. How many objects can be declared of a specific class in a single program?
a) 32768
b) 127
c) 1
d) As many as you want
Answer: d
Explanation: You can create as many objects of a specific class as you want, provided enough memory is available.
3. Which among the following is false?
a) Object must be created before using members of a class
b) Memory for an object is allocated only after its constructor is called
c) Objects can’t be passed by reference
d) Objects size depends on its class data members
Answer: c
Explanation: Objects can be passed by reference. Objects can be passed by value also. If the object of a class is not created,
we can’t use members of that class.
4. Which of the following is incorrect?
a) class student{ }s;
b) class student{ }; student s;
c) class student{ }s[];
d) class student{ }; student s[5];
Answer: c
Explanation: The array must be specified with a size. You can’t declare object array, or any other linear array without specifying
its size. It’s a mandatory field.
5. The object can’t be __________
a) Passed by reference
b) Passed by value
c) Passed by copy
d) Passed as function
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.
6. What is size of the object of following class (64 bit system)?
class student { int rollno; char name[20]; static int studentno; };
a) 20
b) 22
c) 24
d) 28
Answer: c
Explanation: The size of any object of student class will be of size 4+20=24, because static members are not really considered
as property of a single object. So static variables size will not be added.
7. Functions can’t return objects.
a) True
b) False
Answer: b
Explanation: Functions can always return an object if the return type is same as that of object being returned. Care has to be
taken while writing the prototype of the function.
8. How members of an object are accessed?
a) Using dot operator/period symbol
b) Using scope resolution operator
c) Using member names directly
d) Using pointer only
Answer: a
Explanation: Using dot operator after the name of object we can access its members. It is not necessary to use the pointers. We
can’t use the names directly because it may be used outside the class.
9. If a local class is defined in a function, which of the following is true for an object of that class?
a) Object is accessible outside the function
b) Object can be declared inside any other function
c) Object can be used to call other class members
d) Object can be used/accessed/declared locally in that function
Answer: d
Explanation: For an object which belongs to a local class, it is mandatory to declare and use the object within the function
because the class is accessible locally within the class only.
10. Which among the following is wrong?
a) class student{ }; student s;
b) abstract class student{ }; student s;
c) abstract class student{ }s[50000000];
d) abstract class student{ }; class toppers: public student{ }; topper t;
Answer: b
Explanation: We can never create instance of an abstract class. Abstract classes doesn’t have constructors and hence when an
instance is created there is no facility to initialize its members. Option d is correct because topper class is inheriting the base
abstract class student, and hence topper class object can be created easily.
11. Object declared in main() function _____________
a) Can be used by any other function
b) Can be used by main() function of any other program
c) Can’t be used by any other function
d) Can be accessed using scope resolution operator
Answer: c
Explanation: The object declared in main() have local scope inside main() function only. It can’t be used outside main() function.
Scope resolution operator is used to access globally declared variables/objects.
12. When an object is returned___________
a) A temporary object is created to return the value
b) The same object used in function is used to return the value
c) The Object can be returned without creation of temporary object
d) Object are returned implicitly, we can’t say how it happens inside program
Answer: a
Explanation: A temporary object is created to return the value. It is created because the object used in function is destroyed as
soon as the function is returned. The temporary variable returns the value and then gets destroyed.
13. Which among the following is correct?
a) class student{ }s1,s2; s1.student()=s2.student();
b) class student{ }s1; class topper{ }t1; s1=t1;
c) class student{ }s1,s2; s1=s2;
d) class student{ }s1; class topper{ }t1; s1.student()=s2.topper();
Answer: c
Explanation: Only if the objects are of same class then their data can be copied from to another using assignment operator. This
actually comes under operator overloading. Class constructors can’t be assigned any explicit value as in option class student{
}s1; class topper{ }t1; s1=t1; and class student{ }s1; class topper{ }t1; s1.student()=s2.topper();.
14. Which among following is correct for initializing the class below?
class student{
int marks;
int cgpa;
public: student(int i, int j){
marks=I;
cgpa=j
}
};
a) student s[3]={ s(394, 9); s(394, 9); s(394,9); };
b) student s[2]={ s(394,9), s(222,5) };
c) student s[2]={ s1(392,9), s2(222,5) };
d) student s[2]={ s[392,9], s2[222,5] };
Answer: b
Explanation: It is the way we can initialize the data members for an object array using parameterized constructor. We can do this
to pass our own intended values to initialize the object array data.
15. Object can’t be used with pointers because they belong to user defined class, and compiler can’t decide the type of data
may be used inside the class.
a) True
b) False
Answer: b
Explanation: The explanation given is wrong because object can always be used with pointers like with any other variables.
Compiler doesn’t have to know the structure of the class to use a pointer because the pointers only points to a memory
address/stores that address.
This set of Object Oriented Programming using C++ online quiz focuses on “Types of Constructors”.
1. How many types of constructors are available, in general, in any language?
a) 2
b) 3
c) 4
d) 5
Answer: b
Explanation: There are 3 types of constructors in general, namely, default constructors, parameterized constructors and copy
constructors. Default one is called whenever an object is created without arguments.
2. Choose the correct option for the following code.
class student
{
int marks;
}
student s1;
student s2=2;
a) Object s1 should be passed with argument
b) Object s2 should not be declared
c) Object s2 will not be created, but program runs
d) Program gives compile time error
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.
3. Which constructor is called while assigning some object with another?
a) Default
b) Parameterized
c) Copy
d) Direct assignment is used
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.
4. It’s necessary to pass object by reference in copy constructor because ____________
a) Constructor is not called in pass by reference
b) Constructor is called in pass by reference only
c) It passes the address of new constructor to be created
d) It passes the address of new object to be created
Answer: a
Explanation: Object must be passed by reference to copy constructor because constructor is not called in pass by reference.
Otherwise, in pass by value, a temporary object will be created which in turn will try to call its constructor that is already being
used. This results in creating infinite number of objects and hence memory shortage error will be shown.
5. Which specifier applies only to the constructors?
a) Public
b) Protected
c) Implicit
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).
6. Which among the following is true?
a) Default constructor can’t be defined by the programmer
b) Default parameters constructor isn’t equivalent to the default constructor
c) Default constructor can be called explicitly
d) Default constructor is and always called implicitly only
Answer: c
Explanation: Default constructors can be called explicitly anytime. They are specifically used to allocate memory space for the
object in memory, in general. It is not necessary that these should always be called implicitly.
7. Which type of constructor can’t have a return type?
a) Default
b) Parameterized
c) Copy
d) Constructors don’t have a return type
Answer: d
Explanation: Constructors don’t return any value. Those are special functions, whose return type is not defined, not even void.
This is so because the constructors are meant to initialize the members of class and not to perform some task which can return
some value to newly created object.
8. Why do we use static constructors?
a) To initialize the static members of class
b) To initialize all the members with static value
c) To delete the static members when not required
d) To clear all the static members initialized values
Answer: a
Explanation: Static constructors help in initializing the static members of the class. This is provided because the static members
are not considered to be property of the object, rather they are considered as the property of class.
9. When and how many times a static constructor is called?
a) Created at time of object destruction
b) Called at first time when an object is created and only one time
c) Called at first time when an object is created and called with every new object creation
d) Called whenever an object go out of scope
Answer: b
Explanation: Those are called at very first call of object creation. That is called only one time because the value of static
members must be retained and continued from the time it gets created.
10. Which among the following is true for static constructor?
a) Static constructors are called with every new object
b) Static constructors are used initialize data members to zero always
c) Static constructors can’t be parameterized constructors
d) Static constructors can be used to initialize the non-static members also
Answer: c
Explanation: Static constructors can’t be parameterized constructors. Those are used to initialize the value of static members
only. And that must be a definite value. Accepting arguments may make it possible that static members loses their value with
every new object being created.
11. Within a class, only one static constructor can be created.
a) True
b) False
Answer: a
Explanation: Since the static constructors are can’t be parameterized, they can’t be overloaded. Having this case, only one
constructor will be possible to be created in a local scope, because the signature will always be same and hence it will not be
possible to overload static constructor.
12. Default constructor initializes all data members as ___________
a) All numeric member with some garbage values and string to random string
b) All numeric member with some garbage values and string to null
c) All numeric member with zero and strings to random value
d) All numeric member with zero and strings to null
Answer: d
Explanation: Default constructor, which even the programmer doesn’t define, always initialize the values as zero if numeric and
null if string. This is done so as to avoid the accidental values to change the conditional statements being used and similar
conditions.
13. When is the static constructor called?
a) After the first instance is created
b) Before default constructor call of first instance
c) Before first instance is created
d) At time of creation of first instance
Answer: c
Explanation: The static constructor is called before creation of the first instance of that class. This is done so that even the first
instance can use the static value of the static members of the class and manipulate as required.
14. If constructors of a class are defined in private access, then __________
a) The class can’t be inherited
b) The class can be inherited
c) Instance can be created only in another class
d) Instance can be created anywhere in the program
Answer: a
Explanation: If the constructors are defined in private access, then the class can’t be inherited by other classes. This is useful
when the class contains static members only. The instances can never be created.
15. Which among the following is correct, based on the given code below?
class student
{
int marks;
public : student()
{
cout<<”New student details can be added now”;
}
};
student s1;
a) Cout can’t be used inside the constructor
b) Constructor must contain only initializations
c) This program works fine
d) This program produces errors
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.
To practice all areas of Object Oriented Programming using C++ for online Quizzes, .
This set of Object Oriented Programming using C++ online test focuses on “OOP Basic Concepts”.
1. Which was the first purely object oriented programming language developed?
a) Java
b) C++
c) SmallTalk
d) Kotlin
Answer: c
Explanation: SmallTalk was the first programming language developed which was purely object oriented. It was developed by
Alan Kay. OOP concept came into the picture in 1970’s.
2. Which of the following best defines a class?
a) Parent of an object
b) Instance of an object
c) Blueprint of an object
d) Scope of an object
Answer: c
Explanation: A class is Blueprint of an object which describes/ shows all the functions and data that are provided by an object of
a specific class. It can’t be called as parent or instance of an object. Class in general describes all the properties of an object.
3. Who invented OOP?
a) Alan Kay
b) Andrea Ferro
c) Dennis Ritchie
d) Adele Goldberg
Answer: a
Explanation: Alan Kay invented OOP, Andrea Ferro was a part of SmallTalk Development. Dennis invented C++ and Adele
Goldberg was in team to develop SmallTalk but Alan actually had got rewarded for OOP.
4. What is the additional feature in classes that was not in structures?
a) Data members
b) Member functions
c) Static data allowed
d) Public access specifier
Answer: b
Explanation: Member functions are allowed inside a class but were not present in structure concept. Data members, static data
and public access specifiers were present in structures too.
5. Which is not feature of OOP in general definitions?
a) Code reusability
b) Modularity
c) Duplicate/Redundant data
d) Efficient Code
Answer: c
Explanation: Duplicate/Redundant data is dependent on programmer and hence can’t be guaranteed by OOP. Code reusability
is done using inheritance. Modularity is supported by using different code files and classes. Codes are more efficient because
of features of OOP.
6. 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.
7. Which Feature of OOP illustrated the code reusability?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Inheritance
Answer: d
Explanation: Using inheritance we can reuse the code already written and also can avoid creation of many new functions or
variables, as that can be done one time and be reused, using classes.
8. Which language does not support all 4 types of inheritance?
a) C++
b) Java
c) Kotlin
d) Small Talk
Answer: b
Explanation: Java doesn’t support all 4 types of inheritance. It doesn’t support multiple inheritance. But the multiple inheritance
can be implemented using interfaces in Java.
9. How many classes can be defined in a single program?
a) Only 1
b) Only 100
c) Only 999
d) As many as you want
Answer: d
Explanation: Any number of classes can be defined inside a program, provided that their names are different. In java, if public
class is present then it must have the same name as that of file.
10. When OOP concept did first came into picture?
a) 1970’s
b) 1980’s
c) 1993
d) 1995
Answer: a
Explanation: OOP first came into picture in 1970’s by Alan and his team. Later it was used by some programming languages
and got implemented successfully, SmallTalk was first language to use pure OOP and followed all rules strictly.
11. Why Java is Partially OOP language?
a) It supports usual declaration of primitive data types
b) It doesn’t support all types of inheritance
c) It allows code to be written outside classes
d) It does not support pointers
Answer: a
Explanation: As Java supports usual declaration of data variables, it is partial implementation of OOP. Because according to
rules of OOP, object constructors must be used, even for declaration of variables.
12. Which concept of OOP is false for C++?
a) Code can be written without using classes
b) Code must contain at least one class
c) A class must have member functions
d) At least one object should be declared in code
Answer: b
Explanation: In C++, it’s not necessary to use classes, and hence codes can be written without using OOP concept. Classes
may or may not contain member functions, so it’s not a necessary condition in C++. And, an object can only be declared in a
code if its class is defined/included via header file.
13. Which header file is required in C++ to use OOP?
a) iostream.h
b) stdio.h
c) stdlib.h
d) OOP can be used without using any header file
Answer: d
Explanation: We need not include any specific header file to use OOP concept in C++, only specific functions used in code need
their respective header files to be included or classes should be defined if needed.
14. Which of the two features match each other?
a) Inheritance and Encapsulation
b) Encapsulation and Polymorphism
c) Encapsulation and Abstraction
d) Abstraction and Polymorphism
Answer: c
Explanation: Encapsulation and Abstraction are similar features. Encapsulation is actually binding all the properties in a single
class or we can say hiding all the features of object inside a class. And Abstraction is hiding unwanted data (for user) and
showing only the data required by the user of program.
15. Which feature allows open recursion, among the following?
a) Use of this pointer
b) Use of pointers
c) Use of pass by value
d) Use of parameterized constructor
Answer: a
Explanation: Use of this pointer allows an object to call data and methods of itself whenever needed. This helps us call the
members of an object recursively, and differentiate the variables of different scopes.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “OOP
Features”.
1. Which feature of OOP indicates code reusability?
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.
2. If a function can perform more than 1 type of tasks, where the function name remains same, which feature of OOP is used
here?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
Answer: c
Explanation: For the feature given above, the OOP feature used is Polymorphism. Example of polymorphism in real life is a kid,
who can be a student, a son, a brother depending on where he is.
3. If different properties and functions of a real world entity is grouped or embedded into a single element, what is it called in
OOP language?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
Answer: d
Explanation: It is Encapsulation, which groups different properties and functions of a real world entity into single element.
Abstraction, on other hand, is hiding of functional or exact working of codes and showing only the things which are required by
the user.
4. Which of the following is not a feature of pure OOP?
a) Classes must be used
b) Inheritance
c) Data may/may not be declared using object
d) Functions Overloading
Answer: c
Explanation: Data must be declared using objects. Object usage is mandatory because it in turn calls its constructors, which in
turn must have a class defined. If object is not used, it is a violation of pure OOP concept.
5. Which among the following doesn’t come under OOP concept?
a) Platform independent
b) Data binding
c) Message passing
d) Data hiding
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.
6. Which feature of OOP is indicated by the following code?
class student{ int marks; };
class topper:public student{ int age; topper(int age){ this.age=age; } };
a) Inheritance
b) Polymorphism
c) Inheritance and polymorphism
d) Encapsulation and Inheritance
Answer: d
Explanation: Encapsulation is indicated by use of classes. Inheritance is shown by inheriting the student class into topper class.
Polymorphism is not shown here because we have defined the constructor in the topper class but that doesn’t mean that default
constructor is overloaded.
7. Which feature may be violated if we don’t use classes in a program?
a) Inheritance can’t be implemented
b) Object must be used is violated
c) Encapsulation only is violated
d) Basically all the features of OOP gets violated
Answer: d
Explanation: All the features are violated because Inheritance and Encapsulation won’t be implemented. Polymorphism and
Abstraction are still possible in some cases, but the main features like data binding, object use and etc won’t be used hence the
use of class is must for OOP concept.
8. How many basic features of OOP are required for a programming language to be purely OOP?
a) 7
b) 6
c) 5
d) 4
Answer: a
Explanation: There are 7 basic features that define whether a programing language is pure OOP or not. The 4 basic features are
inheritance, polymorphism, encapsulation and abstraction. Further, one is, object use is must, secondly, message passing and
lastly, Dynamic binding.
9. The feature by which one object can interact with another object is _____________
a) Data transfer
b) Data Binding
c) Message Passing
d) Message reading
Answer: c
Explanation: The interaction between two object is called the message passing feature. Data transfer is not a feature of OOP.
Also, message reading is not a feature of OOP.
10. ___________ underlines the feature of Polymorphism in a class.
a) Nested class
b) Enclosing class
c) Inline function
d) Virtual Function
Answer: d
Explanation: Virtual Functions can be defined in any class using the keyword virtual. All the classes which inherit the class
containing the virtual function, define the virtual function as required. Redefining the function on all the derived classes according
to class and use represents polymorphism.
11. Which feature in OOP is used to allocate additional function to a predefined operator in any language?
a) Operator Overloading
b) Function Overloading
c) Operator Overriding
d) Function Overriding
Answer: a
Explanation: The feature is operator overloading. There is not a feature named operator overriding specifically. Function
overloading and overriding doesn’t give addition function to any operator.
12. Which among doesn’t illustrates polymorphism?
a) Function overloading
b) Function overriding
c) Operator overloading
d) Virtual function
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.
13. 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.
14. Which among the following, for a pure OOP language, is true?
a) The language should follow 3 or more features of OOP
b) The language should follow at least 1 feature of OOP
c) The language must follow only 3 features of OOP
d) The language must follow all the rules of OOP
Answer: d
Explanation: The language must follow all the rules of OOP to be called a purely OOP language. Even if a single OOP feature is
not followed, then it’s known to be a partially OOP language.
15. Does OOP provide better security than POP?
a) Always true for any programming language
b) May not be true with respect to all programming languages
c) It depends on type of program
d) It’s vice-versa is true
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on
“Overloading Constructors”.
1. Which among the following best describes constructor overloading?
a) Defining one constructor in each class of a program
b) Defining more than one constructor in single class
c) Defining more than one constructor in single class with different signature
d) Defining destructor with each constructor
Answer: c
Explanation: If more than one constructors are defined in a class with same signature, then that results in error. The signatures
must be different. So that the constructors can be differentiated.
2. Can constructors be overloaded in derived class?
a) Yes, always
b) Yes, if derived class has no constructor
c) No, programmer can’t do it
d) No, never
Answer: d
Explanation: The constructor must be having the same name as that of a class. Hence a constructor of one class can’t even be
defined in another class. Since the constructors can’t be defined in derived class, it can’t be overloaded too, in derived class.
3. Does constructor overloading include different return types for constructors to be overloaded?
a) Yes, if return types are different, signature becomes different
b) Yes, because return types can differentiate two functions
c) No, return type can’t differentiate two functions
d) No, constructors doesn’t have any return type
Answer: d
Explanation: The constructors doesn’t have any return type. When we can’t have return type of a constructor, overloading based
on the return type is not possible. Hence only parameters can be different.
4. Which among the following is possible way to overload constructor?
a) Define default constructor, 1 parameter constructor and 2 parameter constructor
b) Define default constructor, zero argument constructor and 1 parameter constructor
c) Define default constructor, and 2 other parameterized constructors with same signature
d) Define 2 default constructors
Answer: a
Explanation: All the constructors defined in a class must have a different signature in order to be overloaded. Here one default
and other parameterized constructors are used, wherein one is of only one parameter and other accepts two. Hence overloading
is possible.
5. Which constructor will be called from the object created in the code below?
class A
{
int i;
A()
{
i=0; cout<<i;
}
A(int x=0)
{
i=x; cout<<I;
}
};
A obj1;
a) Default constructor
b) Parameterized constructor
c) Compile time error
d) Run time error
Answer: c
Explanation: When a default constructor is defined and another constructor with 1 default value argument is defined, creating
object without parameter will create ambiguity for the compiler. The compiler won’t be able to decide which constructor should
be called, hence compile time error.
6. Which among the following is false for a constructor?
a) Constructors doesn’t have a return value
b) Constructors are always user defined
c) Constructors are overloaded with different signature
d) Constructors may or may not have any arguments being accepted
Answer: b
Explanation: The constructors are not always user defined. The construct will be provided implicitly from the compiler if the used
doesn’t defined any constructor. The implicit constructor makes all the string values null and allocates memory space for each
data member.
7. When is the constructor called for an object?
a) As soon as overloading is required
b) As soon as class is derived
c) As soon as class is created
d) As soon as object is created
Answer: d
Explanation: The constructor is called as soon as the object is created. The overloading comes into picture as to identify which
constructor have to be called depending on arguments passed in the creation of object.
8. Which among the following function can be used to call default constructor implicitly in java?
a) this()
b) that()
c) super()
d) sub()
Answer: a
Explanation: The function this() can be used to call the default constructor from inside any other constructor. This helps to further
reuse the code and not to write the redundant data in all the constructors.
9. Why do we use constructor overloading?
a) To use different types of constructors
b) Because it’s a feature provided
c) To initialize the object in different ways
d) To differentiate one constructor from another
Answer: c
Explanation: The constructors are overloaded to initialize the objects of a class in different ways. This allows us to initialize the
object with either default values or used given values. If data members are not initialized then program may give unexpected
results.
10. If programmer have defined parameterized constructor only, then __________________
a) Default constructor will not be created by the compiler implicitly
b) Default constructor will be created by the compiler implicitly
c) Default constructor will not be created but called at runtime
d) Compile time error
Answer: a
Explanation: When the programmer doesn’t specify any default constructor and only defines some parameterized constructor.
The compiler doesn’t provide any default constructor implicitly. This is because it is assumed that the programmer will create the
objects only with constructors.
11. Which among the following is not valid in java?
a) Constructor overloading
b) Recursive constructor call
c) Default value constructors
d) String argument constructor
Answer: b
Explanation: Java doesn’t provide the feature to recursively call the constructor. This is to eliminate the out of memory error in
some cases that arises unexpectedly. That is an predefined condition for constructors in java.
12. Which constructor will be called from the object obj2 in the following program?
class A
{
int i;
A()
{
i=0;
}
A(int x)
{
i=x+1;
}
A(int y, int x)
{
i=x+y;
}
};
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.
13. What are we only create an object but don’t call any constructor for it in java?
a) Implicit constructor will be called
b) Object is initialized to some null values
c) Object is not created
d) Object is created but points to null
Answer: d
Explanation: The object becomes a reference object which can be initialized will another object. Then this object will indirectly
become another name of the object being assigned. If not assigned, it simply points to null address.
14. Which among the following is false?
a) Constructor can’t be overloaded in Kotlin
b) Constructors can’t be called recursively in java
c) Constructors can be overloaded in C++
d) Constructors overloading depends on different signatures
Answer: a
Explanation: Kotlin language allows constructor overloading. This is a basic feature for the constructors. The constructor
overloading allows the object to be initialized according to the user.
15. Which is correct syntax?
a) classname objectname= new() integer;
b) classname objectname= new classname;
c) classname objectname= new classname();
d) classname objectname= new() classname();
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “This
Pointer”.
1. Which is the pointer which denotes the object calling the member function?
a) Variable pointer
b) This pointer
c) Null pointer
d) Zero pointer
Answer: b
Explanation: The pointer which denotes the object calling the member function is known as this pointer. The this pointer is usually
used when there are members in the function with same name as those of the class members.
2. Which among the following is true?
a) this pointer is passed implicitly when member functions are called
b) this pointer is passed explicitly when member functions are called
c) this pointer is passed with help of pointer member functions are called
d) this pointer is passed with help of void pointer member functions are called
Answer: a
Explanation: When an object calls some member function, it implicitly passes itself as an argument. This allows the compiler to
know which member should be used for the purposes. This also allows to reduce the ambiguity among the variable and data
member names.
3. The this pointer is accessible __________________
a) Within all the member functions of the class
b) Only within functions returning void
c) Only within non-static functions
d) Within the member functions with zero arguments
Answer: c
Explanation: The this pointer is available only within the non-static member functions of a class. If the member function is static, it
will be common to all the objects and hence a single object can’t refer to those functions independently.
4. An object’s this pointer _____________________
a) Isn’t part of class
b) Isn’t part of program
c) Isn’t part of compiler
d) Isn’t part of object itself
Answer: d
Explanation: The object’s this pointer being called are not part of the object itself. This can be cross verified by checking that it
doesn’t take up any space for the data to be stored or pointed.
5. The result of sizeof() function __________________
a) Includes space reserved for this pointer
b) Includes space taken up by the address pointer by this pointer
c) Doesn’t include the space taken by this pointer
d) Doesn’t include space for any data member
Answer: c
Explanation: The space taken by this pointer is not reflected in by the sizeof() operator. This is because object’s this pointer is
not part of object itself. This is a cross verification for the concept stating that this pointer doesn’t take any space in the object.
6. Whenever non-static member functions are called _______________
a) Address of the object is passed implicitly as an argument
b) Address of the object is passed explicitly as an argument
c) Address is specified globally so that the address is not used again
d) Address is specified as return type of the function
Answer: a
Explanation: The address is passed implicitly as an argument to the function. This doesn’t have to be passed explicitly. The
address is passed, of the object which is calling the non-static member function.
7. Which is the correct interpretation of the member function call from an object, object.function(parameter);
a) object.function(&this, parameter)
b) object(&function,parameter)
c) function(&object,¶meter)
d) function(&object,parameter)
Answer: d
Explanation: The function name is specified first and then the parameter lists. The parameter list is included with the object name
along with & symbol. This denotes that the address of the object is being passed as an argument.
8. The address of the object _________________
a) Can’t be accessed from inside the function
b) Can’t be accessed in the program
c) Is available inside the member function using this pointer
d) Can be accessed using the object name inside the member function
Answer: c
Explanation: The address of the object with respect to which the member functions are being called, are stored in this pointer.
This pointer is hence used whenever there are members with same name as those of the variables inside the function.
9. Which among the following is true?
a) This pointer can be used to guard against any kind of reference
b) This pointer can be used to guard against self-reference
c) This pointer can be used to guard from other pointers
d) This pointer can be used to guard from parameter referencing
Answer: b
Explanation: The this pointer can be used to guard itself whenever self-reference is used. This allows accidental address
access. And accidental modification of data.
10. Which syntax doesn’t execute/is false when executed?
a) if(&object != this)
b) if(&function !=object)
c) this.if(!this)
d) this.function(!this)
Answer: a
Explanation: The condition becomes false when executed and hence doesn’t executes. This is the case where this pointer can
guard itself from the self-reference. Here if the address of the object doesn’t match with this pointer that means the object
doesn’t refer itself.
11. The this pointers _____________________
a) Are modifiable
b) Can be assigned any value
c) Are made variables
d) Are non-modifiable
Answer: d
Explanation: The this pointer is non modifiable. This is because the address of any object remains constant throughout its life
time. Hence the address must not be changed otherwise wrong members of invalid addresses might get accessed.
12. Earlier implementations of C++ ___________________
a) Never allowed assignment to this pointer
b) Allowed no assignment to this pointer
c) Allowed assignments to this pointer
d) Never allowed assignment to any pointer
Answer: c
Explanation: The earlier, most initial versions of C++ used to allow assignments to this pointers. That used to allow modifications
of this pointer. Later that feature got disabled.
13. This pointer can be used directly to ___________
a) To manipulate self-referential data structures
b) To manipulate any reference to pointers to member functions
c) To manipulate class references
d) To manipulate and disable any use of pointers
Answer: a
Explanation: This is a feature provided, that can be used directly. The manipulation of self-referential data structures is just an
application of this feature. Other conditions fails as this pointer doesn’t deal with those things.
14. Which among the following is/are type(s) of this pointer?
a) const
b) volatile
c) const or volatile
d) int
Answer: c
Explanation: The this pointer can be declared const or volatile. This depends on need of program and type of code. This is just
an additional feature.
15. Which is the correct syntax for declaring the type of this in a member function?
a) classType [cv-qualifier-list] *const this;
b) classType const[cv-qualifier-list] *this;
c) [cv-qualifier-list]*const classType this;
d) [cv-qualifier-list] classType *const this;
Answer: d
Explanation: The syntax contains the cv-qualifier-list that can be determined from the member function declaratory that can be
either const or volatile or can be made both. Hence we write it as list. classType denotes the name of class to mention to which
class does the object belong to. And *const this denotes that the this pointer is having a constant value.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on
“Polymorphism”.
1. Which among the following best describes polymorphism?
a) It is the ability for a message/data to be processed in more than one form
b) It is the ability for a message/data to be processed in only 1 form
c) It is the ability for many messages/data to be processed in one way
d) It is the ability for undefined message/data to be processed in at least one way
Answer: a
Explanation: It is actually the ability for a message / data to be processed in more than one form. The word polymorphism
indicates many-forms. So if a single entity takes more than one form, it is known as polymorphism.
2. What do you call the languages that support classes but not polymorphism?
a) Class based language
b) Procedure Oriented language
c) Object-based language
d) If classes are supported, polymorphism will always be supported
Answer: c
Explanation: The languages which support classes but doesn’t support polymorphism, are known as object-based languages.
Polymorphism is such an important feature, that is a language doesn’t support this feature, it can’t be called as a OOP language.
3. Which among the following is the language which supports classes but not polymorphism?
a) SmallTalk
b) Java
c) C++
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.
4. If same message is passed to objects of several different classes and all of those can respond in a different way, what is this
feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
Answer: c
Explanation: The feature defined in question defines polymorphism features. Here the different objects are capable of
responding to the same message in different ways, hence polymorphism.
5. Which class/set of classes can illustrate polymorphism in the following code?
abstract class student
{
public : int marks;
calc_grade();
}
class topper:public student
{
public : calc_grade()
{
return 10;
}
};
class average:public student
{
public : calc_grade()
{
return 20;
}
};
class failed{ int marks; };
a) Only class student can show polymorphism
b) Only class student and topper together can show polymorphism
c) All class student, topper and average together can show polymorphism
d) Class failed should also inherit class student for this code to work for polymorphism
Answer: c
Explanation: Since Student class is abstract class and class topper and average are inheriting student, class topper and
average must define the function named calc_grade(); in abstract class. Since both the definition are different in those classes,
calc_grade() will work in different way for same input from different objects. Hence it shows polymorphism.
6. Which type of function among the following shows polymorphism?
a) Inline function
b) Virtual function
c) Undefined functions
d) Class member functions
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.
7. In case of using abstract class or function overloading, which function is supposed to be called first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time, because of low priority
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread scheduling in CPU, but it focuses on whether
the function in local scope is present or not, or if scope resolution is used in some way, or if the function matches the argument
signature. So all these things define which function has the highest priority to be called in runtime. Local function could be one of
the answer but we can’t say if someone have used pointer to another function or same function name.
8. Which among the following can’t be used for polymorphism?
a) Static member functions
b) Member functions overloading
c) Predefined operator overloading
d) Constructor overloading
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t be considered for overloading/overriding. For
polymorphism, function must be property of object, not only of class.
9. What is output of the following program?
class student
{
public : int marks;
void disp()
{
cout<<”its base class”
};
class topper:public student
{
public :
void disp()
{
cout<<”Its derived class”;
}
}
void main() { student s; topper t;
s.disp();
t.disp();
}
a) Its base classIts derived class
b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class
Answer: a
Explanation: You need to focus on how the output is going to be shown, no space will be given after first message from base
class. And then the message from derived class will be printed. Function disp() in base class overrides the function of base
class being derived.
10. Which among the following can show polymorphism?
a) Overloading ||
b) Overloading +=
c) Overloading <<
d) Overloading &&
Answer: c
Explanation: Only insertion operator can be overloaded among all the given options. And the polymorphism can be illustrated
here only if any of these is applicable of being overloaded. Overloading is type of polymorphism.
11. Find the output of the following program.
class education
{
char name[10];
public : disp()
{
cout<<”Its education system”;
}
class school:public education
{
public: void dsip()
{
cout<<”Its school education system”;
}
};
void main()
{
school s;
s.disp();
}
}
a) Its school education system
b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system
Answer: a
Explanation: Notice that the function name in derived class is different from the function name in base class. Hence when we call
the disp() function, base class function is executed. No polymorphism is used here.
12. Polymorphism is possible in C language.
a) True
b) False
Answer: a
Explanation: It is possible to implement polymorphism in C language, even though it doesn’t support class. We can use
structures and then declare pointers which in turn points to some function. In this way we simulate the functions like member
functions but not exactly member function. Now we can overload these functions, hence implementing polymorphism in C
language.
13. Which problem may arise if we use abstract class functions for polymorphism?
a) All classes are converted as abstract class
b) Derived class must be of abstract type
c) All the derived classes must implement the undefined functions
d) Derived classes can’t redefine the function
Answer: c
Explanation: The undefined functions must be defined is a problem, because one may need to implement few undefined
functions from abstract class, but he will have to define each of the functions declared in abstract class. Being useless task, it is
a problem sometimes.
14. Which among the following is not true for polymorphism?
a) It is feature of OOP
b) Ease in readability of program
c) Helps in redefining the same functionality
d) Increases overhead of function definition always
Answer: d
Explanation: It never increases function definition overhead, one way or another if you don’t use polymorphism, you will use the
definition in some other way, so it actually helps to write efficient codes.
15. If 2 classes derive one base class and redefine a function of base class, also overload some operators inside class body.
Among these two things of function and operator overloading, where is polymorphism used?
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can be applied only once in a program
Answer: d
Explanation: Both of them are using polymorphism. It is not necessary that polymorphism can be used only once in a program, it
can be used anywhere, any number of times in a single program.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Private
Access Specifier”.
1. If a function has to be called only by using other member functions of the class, what should be the access specifier used for
that function?
a) Private
b) Protected
c) Public
d) Default
Answer: a
Explanation: The function should be made private. In this way, the function will be available to be called only from the class
member functions. Hence the function will be secure from the outside world.
2. Which among the following is correct for the code given below?
class student
{
private: student()
{
}
public : student( int x)
{
marks =x;
}
};
a) The object can never be created
b) The object can be created without parameters
c) Only the object with only 1 parameter can be created
d) Only the object with some parameters can be created
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.
3. Which among the following is true for the code given below?
class A
{
private : int marks; char name[20];
public :
A(int x=100)
{
marks=x;
}
};
a) Objects can be created with one parameter or without parameter
b) Object can be created only with one parameter
c) Object can be created with more than one parameter
d) Objects can be create only without parameter
Answer: a
Explanation: The constructor here has a default argument constructor. Hence we can pass one parameter, but that is optional. If
an object is created without parameter, the default value will be used in the constructor definition.
4. Which among the following is correct to call a private member from outside the class?
a) object.memberfunction( parameters );
b) object->memberfunction( parameters );
c) object->memberfunction( parameteres); or object.memberfunction( parameters );
d) Not possible
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.
5. If private members have to be accessed directly from outside the class but the access specifier must not be changed, what
should be done?
a) Specifier must be changed
b) Friend function should be used
c) Other public members should be used
d) It is not possible
Answer: b
Explanation: For calling the function directly, we can’t use another function because that will be indirect call. Using friend function,
we can access the private members directly.
6. Which access specifier is/are most secure during inheritance?
a) Private
b) Default
c) Protected
d) Private and default
Answer: a
Explanation: The private members are most secure in inheritance. The default members can still be in inherited in special cases,
but the private members can’t be accessed in any case.
7. Choose the correct option for the code given below.
class A{ static int c=0; public: A(){ c++; } };
a) Constructor will make c=1 for each object created
b) Constructor will make c=0 for each object created
c) Constructor will keep number of objects created
d) Constructor will just initialize c=0 then increment by 1
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.
8. Which option is false for the following code?
class A
{
private : int sum(int x, int y)
{
return x+y;
}
public: A()
{
}
A(int x, int y)
{
cout<<sum(x,y);
}
};
a) Constructor can be created with zero argument
b) Constructor prints sum, if two parameters are passed with object creation
c) Constructor will give error if float values are passed
d) Constructor will take 0 as default value of parameters if not passed
Answer: d
Explanation: Constructor is not having any default arguments hence no default value will be given to any parameters. Only integer
values must be passed to the constructor if we need the sum as output, otherwise if float values are passed, type mismatch will
be shown as error.
9. Which member will never be used from the following class?
class A()
{
int marks; char name[20];
public : A()
{
marks=100;
}
void disp()
{
cout<<”Marks= ”<'<marks;
cout<<”Student”;
}
};
a) name variable will never be used
b) marks variable will never be used
c) constructor will never be used
d) disp() function will never be used
Answer: a
Explanation: Variable name will never be used. It is a private member. None other than class members can access name, also,
neither the constructor nor the disp() function are accessing the variable name. Hence it will never be accessible.
10. Private member functions can be overloaded.
a) True
b) False
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.
11. Which among the following is true?
a) Private member functions can’t be overloaded
b) Private member functions can be overridden
c) Private member functions can’t be overloaded with a public member
d) Private member function can’t be overridden
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.
12. Which data member in following code will be used whenever an object is created?
Class A
{
int x; int y; int z;
public : A()
{
y=100; x=100*y;
}
};
a) x will be used
b) y will be used
c) z will be used
d) All 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.
13. Which member can be considered most secure in the code below?
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.
14. Which among the following is correct for the code given below?
class A
{
private : A()
{
}
public : A(int x)
{
}
};
A a;
A b(100);
a) Program will give compile time error
b) Program will run fine
c) Program will give runtime error
d) Program will give logical error
Answer: a
Explanation: The program will be giving a compile time error as the default constructor is private in class. And, the logical errors
are usually runtime so we can’t say that the program will give logical error. The program will not run.
15. Which among the following is correct?
a) Private specifier must be used before public specifier
b) Private specifier must be used before protected specifier
c) Private specifier must be used first
d) Private specifier can be used anywhere in class
Answer: d
Explanation: The private specifier can be used anywhere in the class as required. It is not a rule to mention the private members
first and then others. It is just followed to write first for better readability.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Public
Access Specifier”.
1. Which among the following is true for the code given below?
class A
{
int marks;
public : disp()
{
cout<<marks;
}
}
class B: protected A
{
char name[20];
}
A a; a.disp();
B b; b.disp();
a) Only object of class A can access disp() function
b) Only object of class B can access disp() function
c) Both instances can access disp() function
d) Accessing disp() outside class is not possible
Answer: a
Explanation: The object of class A can access the disp() function. This is because the disp() function is public in definition of
class A. But it can’t be accessed from instance of class B because the disp() function is protected in class B, since it was
inherited as protected.
2. If the members have to be accessed from anywhere in the program and other packages also, which access specifier should
be used?
a) Public
b) Private
c) Protected
d) Default
Answer: a
Explanation: The access specifier must be public so as to access the members outside the class and anywhere within the
program without using inheritance. This is a rule, predefined for the public members.
3. Which among the following have least security according to the access permissions allowed?
a) Private
b) Default
c) Protected
d) Public
Answer: d
Explanation: The public members are available to the whole program. This makes the members most vulnerable to accidental
changes, which may result in unwanted modification and hence unstable programming.
4. Which among the following can be used for outermost class access specifier in java?
a) Private
b) Public
c) Default
d) Default or Public
Answer: d
Explanation: Either default or public access specifier must be used for outermost classes. Private can be used with inner
classes. This is done so that all the members can access and use the utmost class and that program execution can be done
from anywhere. Inner classes can be made private for security.
5. We can reduce the visibility of inherited methods.
a) True
b) False
Answer: b
Explanation: The statement given is false. This is because when we inherit the members they can either be made more secure
or be at same access. But the visibility reduction is not possible, for example, if a member is protected in parent class, then it
can only be made protected or private in subclass and not public in any case.
6. If members of a super class are public, then________
a) All those will be available in subclasses
b) None of those will be available in subclasses
c) Only data members will be available in subclass
d) Only member functions will be available in subclass
Answer: a
Explanation: All the members will be available in subclasses. Though it is not guaranteed whether the members will be available
in subsequent subclasses from the first subclass.
7. How many public class(s) (outermost) can be there in a java program?
a) 1
b) 2
c) 3
d) As required
Answer: a
Explanation: There can be only one public class in a java program. The public class name must match the name of file. And there
can’t be more than one class with same name in a single program in same scope. Hence it is not possible to have more than
one public class in java program.
8. What is the output of the following code?
package pack1;
class A
{
public A()
{
System.out.print(“object created”);
}
}
package pack2;
import pack1.*;
class B
{
A a=new A();
}
a) Output is: object created
b) Output is: object createdobject created
c) Compile time error
d) Run time error
Answer: c
Explanation: The program will give compile time error. Class A is defined with default access specifier. This directly means that
class A will be available within package only. Even if the constructor is public, the object will not be created.
9. Which among the following for public specifier is false?
a) The static members can’t be public
b) The public members are available in other packages too
c) The subclasses can inherit the public members privately
d) There can be only one public class in java program
Answer: a
Explanation: The static members are not property of any object of the class. Instead, those are treated as property of class. This
allows us to have public static members too.
10. A class has its default constructor defined as public. Class B inherits class A privately. The class ___________
a) B will not be able to have instances
b) Only A can have instances
c) Only B can have instances
d) Both classes can have instances
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.
11. 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.
12. Which among the following is true for public class?
a) There can be more than one public class in a single program
b) Public class members can be used without using instance of class
c) Public class is available only within the package
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.
13. If a class doesn’t have public members, then________
a) None of its members will be able to get inherited
b) None of its instance creation will be allowed
c) None of its member function can be called outside the class
d) None of its data members will be able to get initial value
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.
14. In multi-level inheritance(all public), the public members of parent/superclass will ________
a) Will continue to get inherited subsequently
b) Will not be inherited after one subclass inheritance
c) Will not be available to be called outside class
d) Will not be able to allocated with any memory space
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.
15. Which specifier allows to secure the public members of base class in inherited classes?
a) Private
b) Protected
c) Public
d) Private and Protected
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Public
Member Functions”.
1. What are public member functions?
a) Functions accessible outside the class but not in derived class
b) Functions accessible outside the class directly
c) Functions accessible everywhere using object of class
d) Functions that can’t be accessed outside the class
Answer: c
Explanation: The most suitable definition would be that public member functions are accessible everywhere using object of the
class. If derived classes are using those, derived class object can be used to call those functions.
2. Which among the following is true for public member functions?
a) Public member functions doesn’t have a return type
b) Public member functions doesn’t have any security
c) Public member functions are declared outside the class
d) Public member functions can be called using object of class
Answer: d
Explanation: The public member functions can be called using object of the class. The members can’t be declared outside the
class as those would become non-member functions of the class. The functions have security as those can be accessed using
the class object only.
3. Which type of member functions get inherited in the same specifier in which the inheritance is done? (If private inheritance is
used, those become private and if public used, those become public)
a) Private member functions
b) Protected member functions
c) Public member functions
d) All member functions
Answer: c
Explanation: The public member functions gets into the same specifier in which the inheritance is done. If protected members
are involved in public inheritance, still those remain protected in the derived class but public members become public on public
inheritance and protected in protected inheritance.
4. Which syntax among the following is correct for public member functions?
a) public::void functionName(parameters)
b) public void functionName(parameters)
c) public(void functionName(parameters))
d) public:-void functionName(Parameters)
Answer: b
Explanation: The public member functions declaration must be mentioned with the keyword public. The syntax given is used in
java. Keyword public is followed by the usual function declaration.
5. Which syntax is applicable to declare public member functions in C++?
a) public: <function declaration>
b) public(<function declaration>)
c) public void <function declaration>
d) public::<function declaration>
Answer: a
Explanation: The syntax in C++ must contain the public keyword followed by a colon. Thereafter, all the public members can be
declared. But in few other language, public have to be mentioned explicitly with each member.
6. In java, which rule among the following is applicable?
a) Keyword public can’t be preceded with all the public members
b) Keyword public must be preceded with all the public members
c) Keyword public must be post mentioned the function declaration
d) Keyword public is not mandatory
Answer: b
Explanation: The public members in java must be preceded with the keyword public. It must be mentioned with each public
member, unlike C++ where we mention it only once. In java, each member must have explicit declaration of specifier type.
7. How many public members are allowed in a class?
a) Only 1
b) At most 7
c) Exactly 3
d) As many as required
Answer: d
Explanation: The number of public members that can be defined in a class doesn’t have any limit. Though the programmer
should not use too many functions, instead should use another class for more specific functions to reduce the readability
complexity.
8. Which is not a proper way to access public members of a class?
a) Using object pointer with arrow operator
b) Using object of class in main function
c) Using object of class with arrow operator
d) Using object anywhere in the program
Answer: c
Explanation: The public members can be accessed anywhere in the program using the object of the class. And if object pointer
is used, then arrow operator is used to access class members. If normal object is used with arrow operator, an error will be
generated.
9. Which call is correct for public members of a nested class?
a) Can be called from object of enclosing class
b) Can be called within enclosing class only with direct names
c) Direct names should be used for the nested classes
d) Only with help of nested class object pointer
Answer: a
Explanation: The closest definition is that any public member function of the nested class can be accessed with the help of
enclosing class object. The nested class object pointer can be used only within the enclosing class. It’s not mandatory to use the
members of nested class only within the enclosing class.
10. Which public function call among the following is correct outside the class, if return type is void (C++)?
a) object.void functionName(parameters);
b) object.functionName(parameters);
c) object.functionName void (parameters)
d) object.void functionName();
Answer: b
Explanation: The condition given says that there is no return type hence we can call the function directly. The object name should
be mentioned with a dot operator to access its class members. Then the function name with parameters, if required, can be
given.
11. If public members are to be restricted from getting inherited from the subclass of the class containing that function, which
alternative is best?
a) Make the function private
b) Use private inheritance
c) Use public inheritance
d) Use protected inheritance
Answer: b
Explanation: If private inheritance is used then the class containing the function will be able to use the function with rules of
whichever specifier is used. Then the derived class makes those function the private members of itself. This restricts the public
members of parent class from further inheritance.
12. A derived class object can access the public members of the base class.
a) True
b) False
Answer: b
Explanation: The public members of the base class can be accessed from the derived class object only if public inheritance is
used. If protected or private inheritance is used then those members become public/protected in derived class and hence won’t
be able to be called from object of the derived class.
13. If a class have a public member function and is called directly in the main function then ___________________________
a) Undeclared function error will be produced
b) Out of memory error is given
c) Program gives warning only
d) Program shut down the computer
Answer: a
Explanation: If the function is called directly without using any object then the compiler doesn’t gets to know that the function have
to be called from a specific class. And if there are no global or in-scope function with same name then the compiler produces an
error stating that the called function is undeclared.
14. The function main() must always be public.
a) True
b) False
Answer: a
Explanation: The main() function must always be public. This is because the whole function and the operating system that is out
of the package have to access the main function throughout the program execution. Hence the main() function should be public
so as to be available everywhere in the program.
15. All the public member functions ___________________
a) Can’t access the private members of a class
b) Can’t access the protected members of a class
c) Can access only public members of a class
d) Can access all the member of its class
Answer: d
Explanation: The public member function can access any private, protected and public member of its class. Not only public
member function, any member function of a class can access each and every other member declared inside the class. Hence
are flexible to program.
This set of Object Oriented Programming using C++ Quiz focuses on “Constant Member Functions”.
1. What are the constant member functions?
a) Functions which doesn’t change value of calling object
b) Functions which doesn’t change value of any object inside definition
c) Functions which doesn’t allow modification of any object of class
d) Functions which doesn’t allow modification of argument objects
Answer: a
Explanation: The constant member functions are a special type of member functions. These are intended to restrict any
modification in to the values of object which is used to invoke that function. This is done to ensure that there are no accidental
modifications to the object.
2. Which keyword must be used to declare a member function as a constant member function?
a) Constant
b) Const
c) FunctionConst
d) Unchanged
Answer: b
Explanation: The keyword const is provided in most of the programming languages. This indicates that the member on which it is
specified remains constant with the respective values of members. The keyword must be mentioned so as to declare a member
function to be constant.
3. Which objects can call the const functions?
a) Only const objects
b) Only non-const objects
c) Both const and non-const objects
d) Neither const not non-const objects
Answer: c
Explanation: All the objects of a class can call const functions for its use. Const objects can call the const functions to since those
values are already constant. And the non- const objects can call the const functions to keep their values constant.
4. Non-const functions _______________________
a) Can be called only from non-const object
b) Can be called only from const object
c) Can be called both by const and non-const object
d) Can’t be called with object
Answer: a
Explanation: The non-const functions are able to modify the values of object which called the function. So only the non-const
functions can be called. If const object is used then the compiler produces an error as the const object is being given to a
function which can modify its values.
5. Which is the correct condition on const member functions?
a) Const member functions can’t call non-const member functions
b) Const member functions can’t call any other function
c) Const member functions can call only the functions which are neither const nor non-const
d) Const member functions can call only data members of call not member functions
Answer: a
Explanation: The const member functions are restricted to call any other non-const member functions. This is to ensure that the
const function doesn’t have any code that might modify the calling object.
6. If a const object calls a non-const member function then ____________________
a) Run time error may get produced
b) Compile time error may get produced
c) Either compile time or run time error is produced
d) The program can’t be compiled
Answer: b
Explanation: The program gets compiled but produces an error. The error is produced because a constant value is being
changed. Even if there is no code that can change any object value, but non-const member functions are assumed to change the
values.
7. Can a constructor function be constant?
a) Yes, always
b) Yes, only if permissions are given
c) No, because objects are not involved
d) No, never
Answer: d
Explanation: The constructors can’t be made const. This is to ensure that the constructor is capable of initializing the values to
the members of the object. If it is made constant then it won’t be able to initialize any data member values.
8. A function can have both the const and non-const version in the same program.
a) True
b) False
Answer: a
Explanation: The functions in a program can be made both const and non-const. This feature is made available to make
programming more flexible. This ensures the security too as we can call const function whenever required.
9. How is it possible to have both const and non-const version of a function?
a) Function overriding
b) Function prototyping
c) Function overloading
d) Function declaring
Answer: c
Explanation: The functions can be declared const and non-const in the same program. The technique used is function
overloading. We can define a const function and then a non-const version of same function using overloading.
10. When both the const and non-const version of functions are required?
a) Return value have to be different in const
b) Return value have to be same in const
c) Return values have to be ignored
d) Return values have to be suppressed
Answer: a
Explanation: The return values can help to overload the functions. Also, this will allow us to use a non-const function to be called
inside both the const and non-const version of functions.
11. If a function is to be made const, which is the correct syntax?
a) const functionName(parameters);
b) const returnType functionName(parameters);
c) const functionName(returnType)(Parameters);
d) const (functionName(parameters));
Answer: b
Explanation: The function declaration must contain the keyword const. The const keyword makes the function const type. The
usual function declaration can be given followed by the keyword. The keyword const can be given after the declaration of function
and before definition.
12. Functions which differ in const-ness are considered ______________________
a) To have same signature
b) To have different signature
c) To produce compile time error
d) To produce runtime error
Answer: b
Explanation: The functions are considered to have different signature. This is because the const-ness also defines the type of
function or the working of functions. And hence the functions can be considered different. This is the reason that we can use
function overloading for const and non-const version of same function.
13. If const version of a function when overloading is used, the function ___________________
a) Returns reference to object
b) Returns volatile reference
c) Returns mutable reference
d) Returns const reference
Answer: d
Explanation: The function returns a const reference. This is to ensure that the value of object calling the function is not modified.
This is a security feature.
14. Which among the following is recommended for const functions?
a) Const function use should be reduced in a program
b) Const function use should be more in a program
c) Const function use should not matter in a program
d) Const function use should be able to modify the values
Answer: b
Explanation: The const member functions should be used more in a program. The reason behind is to ensure there is no
accidental modification of data of object. Also to ensure any unintended modification which may result in unexpected termination
of program.
15. Use of const member function in a program _________________________
a) Is mandatory, always
b) Is optional, always
c) Is mandatory, if objects are used
d) Is optional, if const objects are used
Answer: b
Explanation: The use of const member functions is not mandatory. If const objects are involved then there is a high use of const
member functions too. But there is no mandatory condition.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on
“Returning Objects”.
1. In which of the following way(s) can the object be returned from a function?
a) Can only be returned by value
b) Can only be returned by reference
c) Can be returned either by value or reference
d) Can neither be returned by value nor by reference
Answer: c
Explanation: The objects can be returned either by value or reference. There is no mandatory condition for the way it should be
used. The way of returning object can be decided based on the requirement.
2. Whenever an object is returned by value ____________________
a) A temporary object is created
b) Temporary object is not created
c) Temporary object may or may not be created
d) New permanent object is created
Answer: a
Explanation: A temporary object is created when an object is returned by value. The temporary object is used to copy the values
to another object or to be used in some way. The object holds all the values of the data members of the object.
3. Where the temporary objects (created while return by value) are created?
a) Outside the function scope
b) Within the function
c) Inside the main function
d) Inside the calling function
Answer: b
Explanation: The temporary object are created within the function and are intended to return the value for specific use. Either the
object can be assigned to another object or be used directly if possible.
4. Which is the correct syntax for returning an object by value?
a) void functionName ( ){ }
b) object functionName( ) { }
c) class object functionName( ) { }
d) ClassName functionName ( ){ }
Answer: d
Explanation: The class name itself should be the return type of the function. This notifies that the function will return an object of
specified class type. Only the class name should be specified.
5. Which is the correct syntax for defining a function which passes an object by reference?
a) className& functionName ( )
b) className* functionName( )
c) className-> functionName( )
d) &className functionName()
Answer: a
Explanation: The function declaration must contain the class name as return type. But, a & symbol should be followed by the
class name. & indicates that the object being returned will be returned by reference.
6. If an object is declared inside the function then ____________________ outside the function.
a) It can be returned by reference
b) It can’t be returned by reference
c) It can be returned by address
d) It can’t be returned at all
Answer: b
Explanation: The object which is declared inside the class can never be returned by reference. This is because the object will be
destroyed as it goes out of scope when the function is returned. The local variables get destroyed when function is returned
hence the local objects can’t be returned by reference.
7. How many independent objects can be returned at same time from a function?
a) 1
b) 2
c) 3
d) 4
Answer: a
Explanation: Only one object can be returned at a time. This is because a function is only capable of returning a single value at a
time. Though array of objects can be returned from a function.
8. Which error will be produced if a local object is returned by reference outside a function?
a) Out of memory error
b) Run time error
c) Compile time error
d) No error
Answer: c
Explanation: If the local object is returned outside the function then the compile-time error arises. While the program is being
converted and the processes happening during compile time, the compiler won’t be able to resolve the statement.
9. If object is passed by reference ____________________
a) Temporary object is created
b) Temporary object is created inside the function
c) Temporary object is created for few seconds
d) Temporary object is not created
Answer: d
Explanation: The temporary object is not created. If object is returned by reference, a particular memory location will be denoted
with another name and hence same address values will be used.
10. Which among the following is correct?
a) Individual data members can’t be returned
b) Individual data members can be returned
c) Individual member functions can be returned from another function
d) Individual data members can only be passed by reference
Answer: b
Explanation: It is not mandatory to return the whole object. Instead we can return a specific data member value. But the return
type given must match with the data type of the data being returned.
11. Can we return an array of objects?
a) Yes, always
b) Ye, only if objects are having same values
c) No, because objects contain many other values
d) No, because objects are single entity
Answer: a
Explanation: The Object array can be returned from a function. This can be done by putting a className* as the return type of
the function. This makes the return type to accept an array of objects in return.
12. If an object is passed by reference to a function then it must be returned by reference.
a) True
b) False
Answer: b
Explanation: It is not compulsory to return the object in the same way as it was passed. If the object is passed by reference then
there is actually no need to return the object. Because the changes made inside the function will be visible on the original object
of caller function also.
13. Which among the following is true?
a) Two objects can point to the same memory location
b) Two objects can never point to the same memory location
c) Objects not allowed to point at a location already occupied
d) Objects can’t point to any address
Answer: a
Explanation: When an object is created and instead of calling a constructor, another object is assigned to it. Both the objects
point to the same memory location. This can be illustrated with help of return by reference.
14. If an object is being returned by value then __________________________
a) Its member values are made constant
b) Its member values have to be copied individually
c) Its member values are not used
d) Its member values are copied using copy constructor
Answer: d
Explanation: When an object is returned by value, it will be returned to another object or will be directly used. Still in all the
conditions the copy constructor will be used to copy all the values from the temporary object that gets created.
15. Why temporary object is not created in return by reference?
a) Because compiler can’t create temporary objects
b) Because the temporary object is created within the function
c) Because return by reference just make the objects points to values memory location
d) Because return by reference just make the object point to null
Answer: c
Explanation: A reference to the memory location where the returned object is stored is made. This allows the new object which
takes the return value, point to the memory location and hence access the same values.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Single
Level Inheritance”.
1. Which among the following defines single level inheritance?
a) One base class derives another class
b) One derived class inherits from one base class
c) One base class inherits from one derived class
d) One derived class derives from another derived class
Answer: b
Explanation: If only one base class is used to derive only one subclass, it is known as single level inheritance. The reason of this
name is that we inherit the base class to one more level and stop the inheritance any further.
2. If class A and class B are derived from class C and class D, then ________________
a) Those are 2 pairs of single inheritance
b) That is multilevel inheritance
c) Those is enclosing class
d) Those are all independent classes
Answer: a
Explanation: Since class A is derived from class C and then class B is derived from class D, there are two pairs of classes
which shows single inheritance. Those two pairs are independent of each other though.
3. If single inheritance is used, program will contain ________________
a) At least 2 classes
b) At most 2 classes
c) Exactly 2 classes
d) At most 4 classes
Answer: a
Explanation: The program will contain at least 2 classes in the sense of base and derived classes. At least one base class and
one derived class must be there. Types of inheritance remains the same though.
4. Single level inheritance supports _____________ inheritance.
a) Runtime
b) Compile time
c) Multiple inheritance
d) Language independency
Answer: a
Explanation: The runtime inheritance is done when object of a class is created to call a method. At runtime the function is
searched if it is in class of object. If not, it will search in its parent classes and hierarchy for that method.
5. Which method in the code below is single level inherited?
class A
{
protected int a, b;
public: void show()
{
cout<<a<<b;
}
};
class B: public A
{
public: void disp()
{
cout<<a++<<b++;
}
};
class C: private A, public 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.
6. If single level inheritance is used and an abstract class is created with some undefined functions, can its derived class also
skip some definitions?
a) Yes, always possible
b) Yes, possible if only one undefined function
c) No, at least 2 undefined functions must be there
d) No, the derived class must implement those methods
Answer: d
Explanation: The derived class must implement those methods. This is because the parent class is abstract and hence will have
some undefined functions which has to be defined in derived classes. Since we are using single level inheritance, if derived
class doesn’t implement those functions then one more class has to be there which will become multi-level inheritance.
7. Which among the following is false for single level inheritance?
a) There can be more than 2 classes in program to implement single inheritance
b) There can be exactly 2 classes to implement single inheritance in a program
c) There can be more than 2 independent classes involved in single inheritance
d) The derived class must implement all the abstract method if single inheritance is used
Answer: c
Explanation: If more than 2 independent classes are involved to implement the single level inheritance, it won’t be possible as
there must be only one child and one parent class and none other related class.
8. Which concept will result in derived class with more features (consider maximum 3 classes)?
a) Single inheritance
b) Multiple inheritance
c) Multilevel inheritance
d) Hierarchical inheritance
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.
9. Single level inheritance is safer than _____________
a) Multiple inheritance
b) Interfaces
c) Implementations
d) Extensions
Answer: a
Explanation: Interfaces also represent a way of inheritance but is a wide word to decide which inheritance we are talking about in
it, hence can’t be considered. Implementation and extensions also doesn’t match that level of specific idea. And multiple
inheritance not so safe as it might result in some ambiguity.
10. Which language doesn’t support single level inheritance?
a) Java
b) C++
c) Kotlin
d) All languages support it
Answer: d
Explanation: All the languages support single level inheritance. Since any class can inherit other classes as required, if single
level inheritance was not allowed it would result in failing a lot of features of OOP.
11. What is the output of the following program?
class A
{
protected: int a,b;
public: void disp()
{
cout<<a<<b;
}
};
class B:public A
{
int x,y;
};
a) Garbage value
b) Compile time error
c) Runtime error
d) Runs but gives random values as output
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.
12. What is the output of the following program?
class A
{
float sal=40000;
}
class B extends A
{
int salBonus=10000;
public static void main(String args[])
{
B p=new B();
System.out.println("B salary is:"+p.sal);
System.out.println("Bonus of B is:"+p.bonus);
}
}
a)
B salary is: 4000.0
Bonus of B is: 10000
b)
B salary is 10000
Bonus of B is: 4000.0
c) Compile time error
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.
13. Single level inheritance will be best for___________
a) Inheriting a class which performs all the calculations
b) Inheriting a class which can print all the calculation results
c) Inheriting a class which can perform and print all calculations
d) Inheriting all the classes for different calculations
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.
14. Which constructor will be called first from the classes involved in single inheritance from object of derived class?
a) Base class constructor
b) Derived class constructor
c) Both class constructors at a time
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.
15. If base class contains 2 nested classes, will it be possible to implement single level inheritance?
a) Yes, always
b) Yes, only if derived class also have nested classes
c) No, it will use more than 2 classes which is wrong
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Static
Data Members”.
1. Which among the following best defines static variables members?
a) Data which is allocated for each object separately
b) Data which is common to all the objects of a class
c) Data which is common to all the classes
d) Data which is common to a specific method
Answer: b
Explanation: The static data members are made common to all the object of a class. They doesn’t change from object to object.
Those are property of class rather than of any individual object.
2. Which keyword should be used to declare static variables?
a) static
b) stat
c) common
d) const
Answer: a
Explanation: The keyword used to declare static variables is static. This is must be used while declaring the static variables. The
compiler can make variables static if and only if they are mentioned with static keyword.
3. Any changes made to static data member from one member function _____________
a) Is reflected to only the corresponding object
b) Is reflected to all the variables in a program
c) Is reflected to all the objects of that class
d) Is constant to that function only
Answer: c
Explanation: The changes made from any function to static data member will be a common change for all the other objects also.
If the change is made with respect to one object and change is printed from another object, the result will be same.
4. Which is the correct syntax for declaring static data member?
a) static mamberName dataType;
b) dataType static memberName;
c) memberName static dataType;
d) static dataType memberName;
Answer: d
Explanation: The syntax must firstly be mentioned with the keyword static. Then the data type of the member followed by the
member name should be given. This is general form of declaring static data members.
5. The static data member ______________________
a) Must be defined inside the class
b) Must be defined outside the class
c) Must be defined in main function
d) Must be defined using constructor
Answer: b
Explanation: The static data members must be defined outside the class. Since these are common to all the objects and should
be created only once, they must not be defined in the constructor.
6. The syntax for defining the static data members is __________
a) dataType className :: memberName = value;
b) dataType className : memberName = value;
c) dataType className . memberName = value;
d) dataType className -> memberName =value;
Answer: a
Explanation: The syntax doesn’t contain the static keyword. Since it is already been declared as static inside the class. The data
type and the corresponding class name must be there to allocate the variable to a class. The value is assigned using scope
resolution operator for the member name.
7. If static data members have to be used inside a class, those member functions _______________
a) Must not be static member functions
b) Must not be member functions
c) Must be static member functions
d) Must not be member function of corresponding class
Answer: c
Explanation: Only the static member functions can access the static data members. The definition of static members is made
common and hence the member function should be capable of manipulating the static data members.
8. The static data member __________________________
a) Can be accessed directly
b) Can be accessed with any public class name
c) Can be accessed with dot operator
d) Can be accessed using class name if not using static member function
Answer: d
Explanation: The static data members can be accessed using the class name also. If the member functions is not used or is not
to be used then we can call the static data members directly by using its corresponding class name.
9. Which among the following is the correct syntax to access static data member without using member function?
a) className -> staticDataMember;
b) className :: staticDataMember;
c) className : staticDataMember;
d) className . staticDataMember;
Answer: b
Explanation: For accessing the static data members without using the static member functions, the class name can be used. The
class name followed by scope resolution, indicating that static data members is member of this class, and then the data member
name.
10. Which data members among the following are static by default?
a) extern
b) integer
c) const
d) void
Answer: c
Explanation: The const data members of any class are made static by default. This is an implicit meaning given by the compiler
to the member. Since const values won’t change from object to object, hence are made static instead.
11. What is the output of the following program?
class Test
{
private: static int x;
public: static void fun()
{
cout << ++x << “ ”;
}
};
int Test :: x =20;
void main()
{
Test x;
x.fun();
x.fun();
}
a) 20 22
b) 20 21
c) 21 22
d) 22 23
Answer: c
Explanation: The static member is initialized with 20. Then the function is called which used pre-increment and printed value of x.
The function is called twice. Hence we get 21 22 as output.
12. Whenever any static data member is declared in a class ______________________
a) Only one copy of the data is created
b) New copy for each object is created
c) New memory location is allocated with each object
d) Only one object uses the static data
Answer: a
Explanation: The static data is same for all the objects. Instead of creating the same data each time an object is created, the
compiler created only one data which is accessed by all the objects of the class. This saves memory and reduces redundancy.
13. If object of class are created, then the static data members can be accessed ____________
a) Using dot operator
b) Using arrow operator
c) Using colon
d) Using dot or arrow operator
Answer: d
Explanation: The static data members can be accessed in usual way as other members are accessed using the objects. The dot
operator is used generally. Arrow can be used with the pointers.
14. What will be the output of the following program?
class Test
{
public: Test()
{
cout << "Test's Constructor is Called " << endl;
}
};
class Result
{
static Test a;
public:
Result()
{
cout << "Result's Constructor is Called " << endl;
}
};
void main()
{
Result b;
}
a) Test’s Constructor is Called
b) Result’s Constructor is Called
c) Result’s Constructor Called Test’s Constructor is Called
d) Test’s Constructor Called Result’s Constructor is Called
Answer: b
Explanation: The output is the message printed from the constructor of class Result. There is no inheritance used hence only one
constructor is called. Since static members are declared once in class declaration and are not defined. The constructor of class
Test will not be called.
15. Which among the following is wrong syntax related to static data members?
a) className :: staticDataMember;
b) dataType className :: memberName =value;
c) static dataType memberName;
d) className : dataType -> memberName;
Answer: d
Explanation: The syntax given in option d doesn’t belong to any particular declaration or definition. First one is to access the
static members using the class name. Second is to define the static data outside the class. Third syntax id to declare a data
member as static in a class.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Static
Member Functions”.
1. Which among the following is correct definition for static member functions?
a) Functions created to allocate constant values to each object
b) Functions made to maintain single copy of member functions for all objects
c) Functions created to define the static members
d) Functions made to manipulate static programs
Answer: b
Explanation: The functions which are made common, with respect to definition and data usage, to all the objects. These functions
are able to access the static data members of a class.
2. The static member functions __________________
a) Have access to all the members of a class
b) Have access to only constant members of a class
c) Have access to only the static members of a class
d) Have direct access to all other class members also
Answer: c
Explanation: The static member functions are common for all the objects. These functions can use only the static members of a
class in which those are defined. This is because other members change with respect to each object created.
3. The static member functions ____________________
a) Can be called using class name
b) Can be called using program name
c) Can be called directly
d) Can’t be called outside the function
Answer: a
Explanation: The static members can be accessed using class name also. This is because the static members remain common
to all the objects. Hence objects are not required.
4. Which is correct syntax to access the static member functions with class name?
a) className . functionName;
b) className -> functionName;
c) className : functionName;
d) className :: functionName;
Answer: d
Explanation: The scope resolution operator must be used to access the static member functions with class name. This indicates
that the function belongs to the corresponding class.
5. Which among the following is not applicable for the static member functions?
a) Variable pointers
b) void pointers
c) this pointer
d) Function pointers
Answer: c
Explanation: Since the static members are not property of objects, they doesn’t have this pointer. Every time the same member
is referred from all the objects, hence use of this pointer is of no use.
6. Which among the following is true?
a) Static member functions can’t be virtual
b) Static member functions can be virtual
c) Static member functions can be declared virtual if it is pure virtual class
d) Static member functions can be used as virtual in Java
Answer: a
Explanation: The static member functions can’t be virtual. This is a restriction on static member functions, since the definition
should not change or should not be overridden by any other function of derived class. The static members must remain same for
all the objects.
7. The static members are ______________________
a) Created with each new object
b) Created twice in a program
c) Created as many times a class is used
d) Created and initialized only once
Answer: d
Explanation: The static members are created only once. Then those members are reused whenever called or invoked. Memory
is allocated only once.
8. Which among the following is true?
a) Static member functions can be overloaded
b) Static member functions can’t be overloaded
c) Static member functions can be overloaded using derived classes
d) Static member functions are implicitly overloaded
Answer: b
Explanation: The static member functions can’t be overloaded because the definition must be the same for all the instances of a
class. If an overloaded function have many definitions, none of them can be made static.
9. The static member functions _______________
a) Can’t be declared const
b) Can’t be declared volatile
c) Can’t be declared const or volatile
d) Can’t be declared const, volatile or const volatile
Answer: d
Explanation: The static member functions can’t be made const, since any object or class itself should be capable of making
changes to the function. And the function must retain all changes common to all the objects.
10. Which keyword should be used to declare the static member functions?
a) static
b) stat
c) const
d) common
Answer: a
Explanation: The member functions which are to be made static, must be preceded with the keyword static. This indicates the
compiler to make the functions common to all the objects. And a new copy is not created with each of the new object.
11. The keyword static is used _______________
a) With declaration inside class and with definition outside the class
b) With declaration inside class and not with definition outside the class
c) With declaration and definition wherever done
d) With each call to the member function
Answer: b
Explanation: The keyword is used only inside the class while declaring the static member. Outside the class, only definition with
proper syntax is given. There is no need of specifying the keyword static again.
12. Which among the following can’t be used to access the members in any way?
a) Scope resolution
b) Arrow operator
c) Single colon
d) Dot operator
Answer: c
Explanation: The single colon can’t be used in any way in order to access the static members of a class. Other symbols can be
used according to the code and need.
13. We can use the static member functions and static data member __________________
a) Even if class object is not created
b) Even if class is not defined
c) Even if class doesn’t contain any static member
d) Even if class doesn’t have complete definition
Answer: a
Explanation: The static members are property of class as a whole. There is no need of specific objects to call static members.
Those can be called directly or with class name.
14. The static data member _________________
a) Can be mutable
b) Can’t be mutable
c) Can’t be integer
d) Can’t be characters
Answer: b
Explanation: The static data members can never be mutable. There copies are not made. Since those are common and created
only once.
15. If static data member are made inline, ______________
a) Those should be initialized outside the class
b) Those can’t be initialized with the class
c) Those can be initialized within the class
d) Those can’t be used by class members
Answer: c
Explanation: Since the members are created once and are common for all the instances, those can be initialized inside the
class. Those doesn’t change with each object being created hence can be defined inside the class once for all.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “String
Class”.
1. Which is a true statement for object of String class?
a) Object are immutable
b) Object are mutable
c) Object are created only once
d) Object can’t be created
Answer: a
Explanation: The object of string class are mostly immutable. This means that the String objects are constant. These can’t be
changed once created.
2. How to declare an object of class String?
a) String object_Name = value;
b) String object_name = new;
c) String object_name= new value;
d) String object_name= value new;
Answer: a
Explanation: The class name String is given. And then the object name is mentioned. There are two ways to declare and
initialize the string. Either by giving direct string value or by using new keyword. But if new operator is used, constructor of String
class have to be called. From the given options, the direct string value declaration is correct.
3. What does function length do in String class?
a) Returns length of string including null character
b) Returns length of string excluding null character
c) Returns length of substring
d) Returns size of string in bytes
Answer: b
Explanation: The length function returns the length of string. The length is the number of characters in the string but the last null
character is not counted. The string length can be used to loop through each character in the string.
4. Which is the function to get the character present at a particular index in the string?
a) char charAt(index);
b) char charIn(StringName);
c) char charAt(StringName);
d) char charIn(index);
Answer: a
Explanation: The function can be called using dot operator with the string object. Char is the return type of the function to return
the character at specified position. The index must be an integer value, less than the length of string.
5. If only one parameter is passed to substring function then __________________
a) It returns the character at the specified position
b) It returns the string of length 1 from the specified index
c) It returns the string from specified index till the end
d) It returns the string from starting of string till the specified index
Answer: c
Explanation: The substring function returns a string value. The string is the substring starting from the specified index till the end.
The substring function have to be called with the object of string class.
6. If two index are given as argument to substring function then ___________________
a) String of length equal to sum of two arguments is returned
b) String starting from first index and of length equal to send argument
c) String starting from first index and of length equal to sum of two arguments
d) String starting from first index and ending at second index position
Answer: d
Explanation: A value of string type is returned from this function. The returned string is a substring that starts from the first
argument position, till the second index position. The indices must be less than the length of actual string.
7. String class have a concat() function that is used to _____________________
a) Replace old string by new string
b) Add two strings
c) Append one string at end of another string
d) Remove a string from end of one string
Answer: c
Explanation: The concat function is used to append string into another string. The new string is always appended at the end of
source string. The target string is appended as it is and the whole string is then ended by null character.
8. The function lastIndexOf() is used to ___________________
a) Get the index of last occurrence of specified character in argument
b) Get the index of first occurrence of specified character in argument
c) Get the index of last occurrence of first character in string
d) Get the index of last occurrence of last character of string
Answer: a
Explanation: The function is used to get the last occurrence index of a character present in a string. The return type is char.
Single character is returned. The function is used with a string object and the target character is passed as its argument.
9. Function equals() is _______________ and equalIgnoreCase() is _________________
a) Case Insensitive, case insensitive
b) Case sensitive, Case insensitive
c) Case sensitive, case sensitive
d) Case insensitive, case sensitive
Answer: b
Explanation: Both the functions return Boolean value. The function equal() is case sensitive and returns false even if a single
character is case different in two strings. The other function ignores the case sensitivity and only checks if the spellings are
same.
10. The compareTo() function is used to ________________
a) Compare strings value to string object
b) Compare string value to string value
c) Compare string object to another string object
d) Compare string object to another string value
Answer: c
Explanation: The source and target must be objects of the string class. The compare is always case sensitive. To compare two
string objects without case sensitivity then we can use compareToIgnoreCase() function.
11. String class provides function toUpper() to _____________________
a) Convert first character to uppercase
b) Convert last character to uppercase
c) Convert the whole string characters to uppercase
d) Convert uppercase to lower and lower to uppercases
Answer: c
Explanation: The function is used to convert each character of the string. If the character is already uppercase then it remains the
same. But if some character is in lowercase then it will be converted to uppercase.
12. String trim() function is used to _______________________
a) Remove all the white spaces from the string
b) Remove white space from start of string
c) Remove white space at end of string
d) Remove white space from both the ends of string
Answer: d
Explanation: The function is used to remove any white space from both the ends of a given string. The white space include
space, tab, next line etc. It will be removed both from the starting of string and from the end of string.
13. Function replace() accepts _____________ arguments.
a) 1
b) 2
c) 3
d) 4
Answer: b
Explanation: The first argument is the target character. This target character will be replaced by another character. The new
character is the second argument to the function. Only the characters can be passed as argument, not a string.
14. If two arguments are passed to the indexOf() function then ___________________
a) Second argument indicates the occurrence number of specified character from starting
b) Second argument indicates the occurrence number of specified character from end
c) Second argument indicates the index of the character in first argument
d) Second argument indicates the index of the character from the last of the string
Answer: a
Explanation: The string may have more than one occurrence of a character. We use this function to get the index at which the
specified number of times a specific character has occurred in a string. For example, we can get the index of 5th occurrence of
character “j” in a string.
15. The string class deals with string of only character type.
a) True
b) False
Answer: a
Explanation: The string class objects can be used for any string consisting of characters. The characters include numbers,
alphabets and few special characters. String class is not necessary to be used but provides a huge set of inbuilt functions to
make the string operations easier.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on
“Template Class”.
1. A template class can have _____________
a) More than one generic data type
b) Only one generic data type
c) At most two data types
d) Only generic type of integers and not characters
Answer: a
Explanation: The template class can support more than one data type. The only thing is to add all the data types required in a list
separated by comma within template specification.
2. Which among the following is the proper syntax for the template class?
a) template <typename T1, typename T2>;
b) Template <typename T1, typename T2>;
c) template <typename T> T named(T x, T y){ }
d) Template <typename T1, typename T2> T1 named(T1 x, T2 y){ }
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.
3. Can default arguments be used with the template class?
a) Yes, in some special cases
b) Yes, always
c) No, it must satisfy some specific conditions first
d) No, it can’t be done
Answer: b
Explanation: The template class can use default arguments. This is used to specify the data type to be considered if it is not
specified while passing to the generic class. The default type will be used.
4. What is the syntax to use explicit class specialization?
a) template <int> class myClass<>{ }
b) template <int> class myClass<int>{ }
c) template <> class myClass<>{ }
d) template <> class myClass<int>{ }
Answer: d
Explanation: The class specialization is creation of explicit specialization of a generic class. We have to use template<>
constructor for this to work. It works in the same way as with explicit function specialization.
5. Which is the most significant feature that arises by using template classes?
a) Code readability
b) Ease in coding
c) Code reusability
d) Modularity in code
Answer: c
Explanation: The code reusability is the feature that becomes more powerful with the use of template classes. You can generate
a single code that can be used in variety of programming situations.
6. A template class defines the form of a class _____________________ it will operate.
a) With full specification of the data on which
b) With full specification of the functions on which
c) Without full specification of the data on which
d) Without full specification of the functions on which
Answer: c
Explanation: The template classes can accept all types of data types. There is no need to specify the data on which the class
has to operate. Hence it gives us flexibility to code without worrying about the type of data that might be used in the code.
7. What are the two specializations of I/O template classes in C++?
a) 16-bit character and wide characters
b) 8-bit character and wide characters
c) 32-bit character and locale characters
d) 64-bit characters and locale characters
Answer: b
Explanation: The I/O specialization is made with wide character and 8-bit characters. Wide characters are used to store the
characters that might take more than 1 byte of space in memory or any size that is different from the one that the machine is
using.
8. Can typeid() function be used with the object of generic classes?
a) Yes, only if default type is given
b) Yes, always
c) No, generic data can’t be determined
d) No, never possible
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.
9. The _____________ class is a specialization of a more general template class.
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.
10. How is function overloading different from template class?
a) Overloading is multiple function doing same operation, Template is multiple function doing different operations
b) Overloading is single function doing different operations, Template is multiple function doing different operations
c) Overloading is multiple function doing similar operation, Template is multiple function doing identical operations
d) Overloading is multiple function doing same operation, Template is same function doing different operations
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.
11. What if static members are declared inside template classes?
a) All instances will share the static variable
b) All instances will have their own static variable
c) All the instances will ignore the static variable
d) Program gives compile time error
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.
12. What is the output of following program?
template <typename T>
void test(const T&x)
{
static int count = 0;
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.
13. If template class is defined, is it necessary to use different types of data for each call?
a) No, not necessary
b) No, but at least two types must be there
c) Yes, to make proper use of template
d) Yes, for code efficiency
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.
14. 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.
15. Template classes must have at least one static member.
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.
This set of Object Oriented Programming using C++ test focuses on “Memory Allocation of Object”.
1. What does memory allocation for objects mean?
a) Actual creation and memory allocation for object members
b) Creation of member functions
c) Creation of data members for a class
d) Actual creation and data declaration for object members
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.
2. Where is the memory allocated for the objects?
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.
3. When is the memory allocated for an object?
a) At declaration of object
b) At compile time
c) When object constructor is called
d) When object is initialized to another object
Answer: c
Explanation: The object memory allocation takes place when the object constructor is called. Declaration of an object doesn’t
mean that memory is allocated for its members. If object is initialized with another object, it may just get a reference to the
previously created object.
4. Using new is type safe as _______________________
a) It require to be specified with type of data
b) It doesn’t require to be specified with type of data
c) It requires the name of data
d) It allocated memory for the data
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.
5. Which of the following function can be used for dynamic memory allocation of objects?
a) malloc()
b) calloc()
c) create()
d) malloc() and calloc()
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.
6. How much memory will be allocated for an object of class given below?
class Test
{
int mark1;
int mark2;
float avg;
char name[10];
};
a) 22 Bytes
b) 24 Bytes
c) 20 Bytes
d) 18 Bytes
Answer: a
Explanation: The size of an object of the class given in question will be of size 22 bytes. This is because the size of an object is
always equal to the sum of sizes of the data members of the class, except static members.
7. Which keyword among the following can be used to declare an array of objects in java?
a) new
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.
8. When is the memory allocated for an object gets free?
a) At termination of program
b) When object goes out of scope
c) When main function ends
d) When system restarts
Answer: b
Explanation: Whenever an object goes out of scope, the deletion of allocation memory takes place. Actually the data is not
deleted, instead the memory space is flagged to be free for further use. Hence whenever an object goes out of scope the object
members become useless and hence memory is set free.
9. Which among the following keyword can be used to free the allocated memory for an object?
a) delete
b) free
c) either delete or free
d) only delete
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.
10. Which function is called whenever an object goes out of scope?
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.
11. Which operator can be used to check the size of an object?
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.
12. The memory allocated for an object ____________________
a) Can be only dynamic
b) Can be only static
c) Can be static or dynamic
d) Can’t be done using dynamic functions
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.
13. If an object is declared in a user defined function __________________
a) Its memory is allocated in stack
b) Its memory is allocated in heap
c) Its memory is allocated in HDD
d) Its memory is allocated in cache
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.
14. In java, ____________________ takes care of managing memory for objects dynamically.
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.
15. 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++.
To practice all areas of Object Oriented Programming using C++ for tests, .
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Types
of Inheritance”.
1. How many types of inheritance are possible in C++?
a) 2
b) 3
c) 4
d) 5
Answer: d
Explanation: There are five types of inheritance that are possible in C++. Single level, Multilevel, multiple, hierarchical and hybrid.
Here we count hybrid also because it sometimes can bring up a new form of inheritance, Like inheritance using multiple and
hierarchical, which sometimes results in diamond problem.
2. Which among the following is true?
a) Java supports all types of inheritance
b) Java supports multiple inheritance
c) Java doesn’t support multiple inheritance
d) Java doesn’t support inheritance
Answer: c
Explanation: Java doesn’t support multiple inheritance. This is done to avoid the diamond problem that sometimes arises with
inherited functions. Though, multiple inheritance can be implemented in java using interfaces.
3. Which type of inheritance is illustrated by the following code?
class student{ public: int marks; };
class topper: public student { public: char grade; };
class average{ public: int makrs_needed; };
class section: public average{ public: char name[10]; };
class overall: public average{ public: int students; };
a) Single level
b) Multilevel and single level
c) Hierarchical
d) Hierarchical and single level
Answer: d
Explanation: It is hierarchical inheritance and single level inheritance. Since class topper is inheriting class student, it is single
level inheritance. And then average is inherited by section and overall, so it is hierarchical inheritance. But both of them are
separate. Hence it is not hybrid inheritance.
4. Which among the following best describes multiple inheritance?
a) Two classes being parent of any other classes
b) Three classes being parent of other classes
c) More than one class being parent of other child classes
d) More than one class being parent of single child
Answer: d
Explanation: If a class inherits more than one class, it is known as multiple inheritance. This should not be referred with only two
or three classes being inherited. But there must be one class which inherits more than one class to be called as multiple
inheritance.
5. How many types of inheritance can be used at a time in a single program?
a) Any two types
b) Any three types
c) Any 4 types
d) Any type, any number of times
Answer: d
Explanation: Any type of inheritance can be used in any program. There is no rule to use only few types of inheritance. Only thing
that matters is how the classes are inherited and used.
6. Which type of inheritance results in the diamond problem?
a) Single level
b) Hybrid
c) Hierarchical
d) Multilevel
Answer: b
Explanation: In diamond problem, hierarchical inheritance is used first, where two different classes inherit the same class and
then in turn a 4th class inherits the two classes which had inherited the first class. Using more than one type of inheritance here, it
is known as hybrid inheritance.
7. If 6 classes uses single level inheritance with pair classes (3 pairs), which inheritance will this be called?
a) Single
b) Multiple
c) Hierarchical
d) Multilevel
Answer: a
Explanation: Here all the pairs are using single inheritance. And no different pairs are inheriting same classes. Hence it can’t be
called hybrid or multilevel inheritance. You can say the single inheritance is used 3 times in that program.
8. Which among the following is correct for the following code?
class A
{
public : class B
{
public : B(int i): data(i)
{
}
int data;
}
};
class C: public A
{
class D:public A::B{ };
};
a) Multi-level inheritance is used, with nested classes
b) Multiple inheritance is used, with nested classes
c) Single level inheritance is used, with enclosing classes
d) Single level inheritance is used, with both enclosing and nested classes
Answer: d
Explanation: Class C is inheriting Class A. Class D is inheriting class B, both are nested. Hence it is single inheritance. For
multiple inheritance, class C or D should have inherited both class A and class B.
9. Which among the following is false?
a) If one class inherits the inherited class in single level inheritance, it is multi-level inheritance
b) Hybrid inheritance always contains multiple inheritance
c) Hierarchical inheritance involves inheriting same class into more than one classes
d) Hybrid inheritance can involve any types of inheritance together
Answer: b
Explanation: It is not necessary to have multiple inheritance in hybrid type. It can have any type together. This doesn’t have to be
of specific type always.
10. If class A has two nested classes B and C. Class D has one nested class E, and have inherited class A. If E inherits B and
C, then ________________
a) It shows multiple inheritance
b) It shows hierarchical inheritance
c) It shows multiple inheritance
d) Multiple inheritance among nested classes, and single level for enclosing classes
Answer: d
Explanation: This involves the same concept of inheritance, where the nested classes also follow the inheritance rules. The
Enclosing classes are having single inheritance. Nested classes involves multiple.
11. In hierarchical inheritance, all the classes involve some kind of inheritance.
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.
12. Which type of inheritance cannot involve private inheritance?
a) Single level
b) Multiple
c) Hybrid
d) All types can have private inheritance
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.
13. How many classes can be inherited by a single class in multiple inheritance (C++)?
a) Only 2
b) Only 27
c) Only 1024
d) Any number of classes can be inherited
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.
14. How many classes can be inherited by a single class in java?
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.
15. 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
b) Only till class J
c) Only till class Z
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.
This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on
“Upcasting”.
1. What is upcasting?
a) Casting subtype to supertype
b) Casting super type to subtype
c) Casting subtype to super type and vice versa
d) Casting anytype to any other type
Answer: a
Explanation: The upcasting concept includes only the casting of subtypes to the super types. This casting is generally done
implicitly. Smaller size types can fit into larger size types implicitly.
2. Which among the following is true for upcasting in inheritance?
a) Downward to the inheritance tree
b) Upward to the inheritance tree
c) Either upward or downward
d) Doesn’t apply on inheritance
Answer: b
Explanation: The upcasting concept in inheritance is always applied upward the inheritance tree. The derived class objects can
be type casted to any of its parent class type. Since is a relationship applies in general inheritance.
3. Which among the following is safe?
a) Upcasting
b) Downcasting
c) Both upcasting and downcasting
d) If upcasting is safe then downcasting is not, and vice versa
Answer: a
Explanation: The upcasting is always safe since the derived type or the smaller type is converted into the base type or the larger
size. This results in allocating a smaller size data into bigger type data. No data is lost in casting, hence safe.
4. Which among the following is the best situation to use upcasting?
a) For general code dealing with only subtype
b) For general code dealing with only supertype
c) For general code dealing with both the supertype and subtype
d) For writing a rigid code with respect to subtype
Answer: b
Explanation: When a general code has to be written where we use only the supertype object or the data of bigger size, then
upcasting would be the best option. Since the whole code will require only the supertype name references.
5. Which property is shown most when upcasting is used?
a) Code reusability
b) Code efficiency
c) Complex code simple syntax
d) Encapsulation
Answer: c
Explanation: The code written using upcasting mostly shows complex code in simpler syntax features. This is because the
upcasting concept can be applied as polymorphism and to group the similar type of objects.
6. Upcasting and downcasting objects are the same as casting primitive types.
a) True
b) False
Answer: b
Explanation: It is a bit confusing concept since both casting concepts are different. Primitive casting depends on the type and
size of data being typecast. Whereas in objects casting, the classes and inheritance order plays a big role.
7. Which casting among the following is allowed for the code given below?
class A
{
public :int a;
}
class B:public A
{
int b;
}
main()
{
B b=new A(); //casting 1
A a=new B(); //casting 2
}
a) Casting 1
b) Casting 2
c) casting 1 and casting 2
d) casting 1 nor casting 2
Answer: b
Explanation: The casting 2 is correct. The objects casting must be done from derived class object to a parent class object. That
is, the object of the superclass can be made an object of subclass only. Vice versa is not possible.
8. If multiple inheritance is implemented, which upcasting will be correct?
a) Upcast to first base class listed in inheritance
b) Upcast to send base class listed in inheritance
c) Upcast to any base class
d) Upcast is not possible
Answer: c
Explanation: The upcasting of derived class object is possible to any base class. This is because the base class object can
represent any of its derived classes using upcasting.
9. If class C inherits class B and class B inherits class A ________________
a) Class C object can be upcasted to object of class B only
b) Class C object can be upcasted to object of class A only
c) Class C object can be upcasted to object of either class A or B
d) Class C object can’t be upcasted
Answer: c
Explanation: Both class A and B are parent classes of class C. Class C object hence can be upcasted to any of those class
objects. It is not compulsory to upcast to nearest parent.
10. Upcasting is _____________________ without an explicit type cast.
a) Always allowed for public inheritance
b) Always allowed for protected inheritance
c) Always allowed for private inheritance
d) Not allowed
Answer: a
Explanation: The public inheritance shows the most flexible is-a relationship. Hence explicit type casting is not required. Implicit
type casting is done by the compiler.
11. Which concept is needed because of implicit type casting use?
a) Static binding
b) Dynamic binding
c) Compile time binding
d) Source code binding
Answer: b
Explanation: Since the implicit type casting allows casting of a base class pointer to refer to its derived class object or even
base class object. We need dynamic type casting so that the references can be resolved during execution of program.
12. When are the pointer types known for upcasting the objects?
a) Compile time
b) Runtime
c) Source code build time
d) Doesn’t apply to pointer types
Answer: a
Explanation: The pointer or reference types are known at compile time for the upcasting of an object. This is because the
addresses must be known for casting the derived class to base class object.
13. When are the object type known for upcasting the objects?
a) Compile time
b) Runtime
c) Source code build time
d) Doesn’t apply to objects directly
Answer: b
Explanation: The upcasting with objects directly requires runtime resolving. The objects are fixed and address are allocated at
compile time. But the execution of a program requires runtime knowledge of object types, for implicit type cast.
14. If two classes are defined “Parent” and “Child” then which is the correct type upcast syntax in C++?
a) Parent *p=child;
b) Parent *p=*child;
c) Parent *p=&child;
d) Parent *p=Child();
Answer: c
Explanation: The syntax must contain the base class name first. So that the parent class object pointer can be declared. Then the
object is assigned with the derived class object with & symbol. & symbol is added to get the address of the derived class object.
15. Which among the following is true?
a) Upcasting is possible only for single level inheritance
b) Upcasting is possible only for multilevel inheritance
c) Upcasting is possible only for multiple inheritance
d) Upcasting is possible for any type of inheritance
Answer: d
Explanation: The type of inheritance doesn’t matter with the upcasting concept. Upcasting applies to all types of inheritance. Any
derived class object can be upcasted to any of its base class object.
This set of Object Oriented Programming using C++ Question Bank focuses on “Protected Access Specifier”.
1. Which among the following best describes the protected specifier?
a) Members are most secure and can’t be used outside class
b) Members are secure but can be used outside the class
c) Members are secure as private, but can be inherited
d) Members are secure like private, but can’t be inherited
Answer: c
Explanation: The members which are made protected, are most secure if inheritance is not used. But, this facility is provided to
keep those members private and with that, they can be inherited by other classes. This is done to make the code more flexible.
2. If a constructor is defined in protected access, then?
a) It’s instance can be created inside the subclasses
b) It’s instance can be created anywhere in the program
c) It’s instance can be created inside the subclasses and main() function
d) It’s instance can be created inside the parent class only
Answer: a
Explanation: The instances will be allowed to be created only inside the sub classes. This is because the protected members will
be inherited and hence the constructor too. This will allow the subclasses to call the constructor whenever an object is created.
3. For the following code, choose the correct option.
class A
{
int marks;
protected : A()
{
marks=100;
}
public : A( int x)
{
marks=x;
}
};
a) The instances can be created only in subclasses
b) The instances can be created only in main() function
c) The instances can be created only in parent class
d) The instances can be created anywhere in the program
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.
4. If the protected members are to be made accessible only to the nearest subclass and no further subclasses, which access
specifier should be used in inheritance?
a) The sub class should inherit the parent class privately
b) The sub class should inherit the parent class as protected
c) The sub class should inherit the parent class as public
d) The sub class can use any access modifier
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.
5. What will be the output of the following code (all header files and required things are included)?
class A
{
int marks;
protected : A(int x)
{
marks=x;
}
public : A()
{
marks=100;
}
}
class B
{
A a;
A b=100;
};
main()
{
A a, b=100;
B c;
}
a) Program runs fine
b) Program gives runtime error
c) Program gives compile time error
d) Program gives logical error
Answer: c
Explanation: The objects being created with assignment value are allowed, if the constructor accepts only 1 argument. But main()
function will not be able to create the object here with assignment, as the constructor which accepts one argument is in protected
mode in the class.
6. Which among the following is true for the given code below?
class A
{
protected : int marks;
public :
A()
{
marks=100;
}
disp()
{
cout<<”marks=”<<marks;
}
};
class B: protected A
{
};
B b;
b.disp();
a) Object b can’t access disp() function
b) Object b can access disp() function inside its body
c) Object b can’t access members of class A
d) Program runs fine
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.
7. Protected members differ from default members as _______
a) Protected members can be accessed outside package using inheritance, but default can’t
b) Default members can be accessed outside package using inheritance, but protected can’t
c) Protected members are allowed for inheritance but Default members are not allowed
d) Both are same
Answer: a
Explanation: The protected members are allowed in the same package but can also be accessed in other packages using
inheritance. But the default members can never be accessible in other packages.
8. If all the members are defined in protected specifier then? (Constructors not considered)
a) Instance of class can’t be created
b) Instance of class can be created anywhere
c) Instance of class can be created only in subclasses
d) Instance of class can be created only in main() function
Answer: b
Explanation: The instances can be created anywhere in the program. This is because the constructors are not considered
among the members defined in protected mode. Hence the default implicit constructor will be used whenever an object is
created.
9. Which among the following is correct for the code given?
class A
{
private: int marks;
A()
{
}
Public : disp()
{
cout<< marks;
}
};
class B: public A
{
}
B b;
a) Instance of B will not be created
b) Instance of B will be created
c) Program gives compile time error
d) Program gives runtime error
Answer: a
Explanation: Instance of B will not be created. When you try to create an instance of B, First the constructor of parent class will be
called, but the parent class constructor is private, hence it won’t be able to initialize and allocate memory for parent class
members. In turn, the object of B will not be created.
10. If protected inheritance is used then _____
a) Public members become public in subclass
b) Protected members become public in subclass
c) Protected members become protected in subclass
d) Protected and Public members become protected in subclass
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.
11. If protected members are to be accessed from outside the class then__________
a) Members must be inherited publicly in subclass
b) Members must accessed using class pointers
c) Members must be accessed as usual
d) Members must be made public
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.
12. Which among the following can use protected access specifier?
a) Members which may be used in other packages
b) Members which have to be secure and should be used by other packages/subclass
c) Members which have to be accessed from anywhere in the program
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.
13. 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.
14. If a class have default constructor defined in private access, and one parameter constructor in protected mode, how will it be
possible to create instance of object?
a) Define a constructor in public access with different signature
b) Directly create the object in the subclass
c) Directly create the object in main() function
d) Not possible
Answer: a
Explanation: If a new constructor is defined in public access. That will be available to the whole program. Only restriction will be
of the way to use it.
15. What will be the output of the program given below?
class A
{
Public : A(int a=0)
{
cout<<”new A”;
}
};
A a;
A b;
A c;
a) new A new A new A
b) newAnewAnewA
c) new Anew Anew A
d) new A new Anew A
Answer: c
Explanation: The constructor has a default argument. Whenever the object is created, the constructor will be called and print the
message in its definition. Since the argument is default valued, it is not mandatory to pass anything to the new object.
To practice Object Oriented Programming Question Bank using C++, .