250+ TOP MCQs on Local Class and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Local Class”.

1. What are local classes?
a) Classes declared inside a package
b) Classes declared inside a function
c) Classes declared inside a class
d) Classes declared inside structure
Answer: b
Clarification: The classes declared inside a package are available to all the functions and classes, hence can’t be called local. This is somewhat similar concept that we use to denote variables of a function. The classes declared inside functions will be local to them.

2. All member functions of a local class must be ___________
a) Defined outside class body
b) Defined outside the function definition
c) Defined inside the class body
d) Defined at starting of program
Answer: c
Clarification: There is a restriction on where the member functions of the local class should be define. Those must be defined inside the class body only. This is to reduce the ambiguity and complexity of program.

3. Can local class members access/use the general local variables (except static, abstract etc.) of the function in which it is defined?
a) Yes, it can access with arrow operator
b) No, it can’t access with dot operator
c) Yes, it can access using dot operator
d) No, it can’t access In anyway
Answer: d
Clarification: The local variables of the functions are not available to the member functions of the class. This is done to reduce the ambiguity in variables and their access rules.

4. Which type of data can a local class access from the function in which it is defined?
a) Static and extern
b) Abstract and static
c) Void and extern
d) Const and static
Answer: a
Clarification: The local classes have this feature to access the static and extern variables of the function in which those are defined. This feature is available since these type of data are common to the program and is created only one time. Run time creation and destruction of these variables is not done. The only restriction that may apply is those members must be constants.

5. Local classes can access the type names and enumerators defined by the enclosing function.
a) True
b) False
Answer: a
Clarification: This is a little tricky part with local classes. Though the local class can’t access the general variables of the function but can access the types that are defined inside the function. This is because the whole definition of that type would be existing inside the class.

6. Can static variables be declared inside a local class?
a) Yes, with public access specifier
b) Yes, anywhere as required
c) No, not possible in private access specifier
d) No, not possible anyway
Answer: d
Clarification: No, the static variables can’t be declared inside a local class. This is because each time the function is called, all the variables get created again and are destroyed as soon as the function is returned. This would have been possible id the static variable was of function.

7. All the member functions of local classes are __________ by default.
a) Static
b) Inline
c) Abstract
d) Virtual
Answer: c
Clarification: All the members are defined inside the class body. And when the member functions are defined inside the class body, they are made inline by default. If the definition is too complex, those are made normal functions.

8. The enclosing function has no special access to the members of the local class.
a) True
b) False
Answer: a
Clarification: This is a rule that the enclosing function doesn’t have any special access to the members of the local class. This is done to maintain the security of class members. And to adhere to the rules of OOP.

9. Which language can use inheritance with local classes?
a) Kotlin
b) Java
c) SmallTalk
d) SAP ABAP
Answer: d
Clarification: Other language might support inheritance with local classes but those doesn’t provide all the proper features of inheritance. Language SAP ABAP provides a way to implement inheritance with local classes efficiently.

10. How many local classes can be defined inside a single function?
a) Only 1
b) Only 3
c) Only 5
d) As many as required
Answer: d
Clarification: The local classes can be defined as required. There is no restriction on the number of local classes that can be defined inside a function. But all those classes must follow the rules and restrictions.

11. All the data members of local class must be ___________
a) Defined with declaration
b) Defined in constructor
c) Declared and defined in constructor
d) Declared using a member function
Answer: b
Clarification: The data members follow the same rules as of simple classes. Hence the data members must be declared first. Then their definition must be given using the constructors.

12. Can two different functions have local class with same name?
a) Yes, since local
b) No, names must be different
c) No, scope doesn’t work here
d) No, ambiguity arises
Answer: a
Clarification: The local classes can have same name if they belong to different functions. The classes would be local to those specific functions and hence can have same name. This is same as that of local variables concept.

13. What is the scope of local class?
a) Within the class only
b) Within the function
c) Within the program
d) One time creation and live till end of program
Answer: b
Clarification: The scope of a local class is limited only within the function definition. The function can use the class as usual as local variables. The class gets destroyed as soon as the function is returned.

14. Can a function, other than the enclosing function of local class, access the class members?
a) Yes, using object
b) Yes, using direct call
c) Yes, using pointer
d) No, can’t access
Answer: d
Clarification: The local classes are local to the specific enclosing function. Other functions can’t access the class. Even if the pointers are used, the class must be alive when the pointer is used. But this will not happen if the enclosing function is returned.

15. Which among the following is the main advantage of using local classes?
a) Make program more efficient
b) Makes program execution faster
c) Helps to add extra functionality to a function
d) Helps to add more members to a function
Answer: c
Clarification: The closest answer is to add more functionalities to a function or to make some specific functions to be generic. Adding more members to a function can be done directly but to add some special functionality that are encapsulated, can be done using local classes.

250+ TOP MCQs on Multiple Inheritance and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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.

250+ TOP MCQs on Passing Object to Functions and Answers

Object Oriented Programming Interview Questions Experienced people on “Passing Object to Functions”.

1. Passing object to a function _______________
a) Can be done only in one way
b) Can be done in more than one ways
c) Is not possible
d) Is not possible in OOP
Answer: b
Clarification: The objects can be passed to the functions and this requires OOP concept because objects are main part of OOP. The objects can be passed in more than one way to a function. The passing depends on how the object have to be used.

2. The object ________________
a) Can be passed by reference
b) Can be passed by value
c) Can be passed by reference or value
d) Can be passed with reference
Answer: c
Clarification: The objects can be passed by reference if required to use the same object. The values can be passed so that the main object remains same and no changes are made to it if the function makes any changes to the values being passed.

3. Which symbol should be used to pass the object by reference in C++?
a) &
b) @
c) $
d) $ or &
Answer: a
Clarification: The object to be passed by reference to the function should be preceded by & symbol in the argument list syntax of the function. This indicates the compiler not to use new object. The same object which is being passed have to be used.

4. If object is passed by value ______________
a) Copy constructor is used to copy the values into another object in the function
b) Copy constructor is used to copy the values into temporary object
c) Reference to the object is used to access the values of the object
d) Reference to the object is used to created new object in its place
Answer: a
Clarification: The copy constructor is used. This constructor is used to copy the values into a new object which will contain all the values same as that of the object being passed but any changes made to the newly created object will not affect the original object.

5. Pass by reference of an object to a function _______________
a) Affects the object in called function only
b) Affects the object in prototype only
c) Affects the object in caller function
d) Affects the object only if mentioned with & symbol with every call
Answer: c
Clarification: The original object in the caller function will get affected. The changes made in the called function will be same in the caller function object also.

6. Copy constructor definition requires __________________
a) Object to be passed by value
b) Object not to be passed to it
c) Object to be passed by reference
d) Object to be passed with each data member value
Answer: c
Clarification: The object must be passed by reference to a copy constructor. This is to avoid the out of memory error. The constructors keeps calling itself, if not passed by reference, and goes out of memory.

7. What is the type of object that should be specified in the argument list?
a) Function name
b) Object name itself
c) Caller function name
d) Class name of object
Answer: d
Clarification: The type of object is the class itself. The class name have to be specified in order to pass the objects to a function. This allows the program to create another object of same class or to use the same object that was passed.

8. If an object is passed by value, _________________
a) Temporary object is used in the function
b) Local object in the function is used
c) Only the data member values are used
d) The values are accessible from the original object
Answer: b
Clarification: When an object is called by values, copy constructor is called and object is copied to the local object of the function which is mentioned in the argument list. The values gets copied and are used from the local object. There is no need to access the original object again.

9. Can data members be passed to a function using the object?
a) Yes, it can be passed only inside class functions
b) Yes, only if the data members are public and are being passed to a function outside the class
c) No, can’t be passed outside the class
d) No, can’t be done
Answer: b
Clarification: The data members can be passed with help of object but only if the member is public. The object will obviously be used outside the class. The object must have access to the data member so that its value or reference is used outside the class which is possible only if the member is public.

10. What exactly is passed when an object is passed by reference?
a) The original object name
b) The original object class name
c) The exact address of the object in memory
d) The exact address of data members
Answer: c
Clarification: The location of the object, that is, the exact memory location is passed, when the object is passed by reference. The pass by reference is actually a reference to the object that the function uses with another name to the same memory location as the original object uses.

11. If the object is not to be passed to any function but the values of the object have to be used then?
a) The data members should be passed separately
b) The data members and member functions have to be passed separately
c) The values should be present in other variables
d) The object must be passed
Answer: a
Clarification: The data members can be passed separately. There is no need to pass whole object, instead we can use the object to pass only the required values.

12. Which among the following is true?
a) More than one object can’t be passed to a function
b) Any number of objects can be passed to a function
c) Objects can’t be passed, only data member values can be passed
d) Objects should be passed only if those are public in class
Answer: b
Clarification: There is no restriction on passing the number of objects to a function. The operating system or the compiler or environment may limit the number of arguments. But there is no limit on number of objects till that limit.

13. What will be the output if all necessary code is included (Header files and main function)?

void test (Object &y)
{
    y = "It is a string";
}
void main()
{
	Object x = null;
	test (x);
	System.out.println (x);
}

a) Run time error
b) Compile time error
c) Null
d) It is a string
Answer: d
Clarification: This is because the x object is passed by reference. The changes made inside the function will be applicable to original function too.

14. In which type is new memory location will be allocated?
a) Only in pass by reference
b) Only in pass by value
c) Both in pass by reference and value
d) Depends on the code
Answer: b
Clarification: The new memory location will be allocated only if the object is passed by value. Reference uses the same memory address and is denoted by another name also. But in pass by value, another object is created and new memory space is allocated for it.

15. Pass by reference and pass by value can’t be done simultaneously in a single function argument list.
a) True
b) False
Answer: b
Clarification: There is no condition which specifies that only the reference pass or values pass is allowed. The argument list can contain one reference pass and another value pass. This helps to manipulate the objects with functions more easily.

Object Oriented Programming for Interviews,

250+ TOP MCQs on Polymorphism and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: Function with highest priority is called. Here, it’s not about the thread scheduling in CPU, but it 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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.

250+ TOP MCQs on Nested Class and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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

View Answer

Answer: c
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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.

250+ TOP MCQs on Hierarchical Inheritance and Answers

Object Oriented Programming (OOPs) Multiple Choice Questions & Answers (MCQs) on “Hierarchical Inheritance”.

1. Which among the following is best to define hierarchical inheritance?
a) More than one classes being derived from one class
b) More than 2 classes being derived from single base class
c) At most 2 classes being derived from single base class
d) At most 1 class derived from another class
Answer: a
Clarification: When two or more classes get derived from a single base class, it is known as hierarchical inheritance. This gives us freedom to use same code with different scopes and flexibility into different classes.

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

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

4. How many classes must be there to implement hierarchical inheritance?
a) Exactly 3
b) At least 3
c) At most 3
d) At least 1
Answer: b
Clarification: At least 3 classes must be there. Two derived classes and one base class. This lets us implement two classes that have common characteristics from base class.

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

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

7. Which class uses hierarchical inheritance in following code?

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

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

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

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

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

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

10. If one class have derived the base class privately then another class can’t derive the base class publically.
a) True
b) False
Answer: b
Clarification: The classes are independent and can access the base class and inherit it in whichever way it is required. The classes can use the base base class members privately or publically maintaining the security of data and methods.

11. Which among the following is true?
a) Hierarchical inheritance is subset of multiple inheritances
b) Hierarchical inheritance is strongest inheritance type
c) Hierarchical inheritance uses only 2 classes for implementation
d) Hierarchical inheritance allows inheritance of common features to more than one class
Answer: d
Clarification: Hierarchical inheritance is used to make all the inherited classes have some common features obtained from a single base class. This allows all the classes to maintain a group or to be classified under one class.

12. Hierarchical inheritance can be a subset of _________________
a) Hybrid inheritance
b) Multiple inheritance
c) Single level inheritance
d) Multilevel inheritance
Answer: a
Clarification: When we use hybrid inheritance, it can contain any type of inheritance or combination or more than two types. Hence it may contain Hierarchical inheritance too, hence it can be subset of hybrid inheritance.

13. Which type of inheritance is most suitable for inheriting Same syllabus into different colleges with different streams?
a) Multiple
b) Single
c) Hierarchical
d) Multilevel
Answer: c
Clarification: When hierarchical inheritance is used, the common syllabus can be adopted into different college classes where the same syllabus is applicable. For changing the syllabus only the details of base class will have to changed.

14. Which class constructor is called first when an object of derived class is created?
a) Base class constructor
b) Derived class constructor
c) Firstly created derived class constructor
d) Last created derived class constructor
Answer: a
Clarification: The base class must be initialised first hence the constructor of base class is called first. This makes everything ready for the new object being created.

15. All the derived classes can access only a few members of base class that other derived classes can’t access at same time, in hierarchical inheritance.
a) True
b) False
Answer: b
Clarification: The derived classes have full access to all the non private member’s of base class. Every derived class have equal access, none of the class can have special access to specific members of base class in general cases.