Object Oriented Programming using Java Questions and Answers – Hierarchical Inheritance

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Hierarchical Inheritance”.

1. What will be the output of the following Java code?

class A
{
    public void functionA()
    {
        System.out.println("Class A");
    }
}
class B extends A
{
    public void functionB()
    {
        System.out.println("Class B");
    }
}
class C extends A
{
  public void functionC()
  {
     System.out.println("Class C");
  }
}
public class Hierarchical1
{
    public static void main(String args[])
    {
        B obj1 = new B();
        C obj2 = new C();
        obj1.functionA();
        obj2.functionA();
    }
}

a)

class B
class B

b)

advertisement
advertisement
class C
class C

c)

class A
class A 

d)

class B
class C
View Answer
Answer: c
Explanation: The objects ‘obj1’ and ‘obj2’ are objects of the classes B and C respectively, which inherit the properties of the class A. Both the objects ‘obj1’ and ‘obj2’ are able to access the method of class A [function A()] as they are the objects of derived classes B and C respectively.
Output:

$ javac Hierarchical1.java
$ java Hierarchical1
class A
class A

advertisement

2. What will be the output of the following Java code?

advertisement
class X 
{ 
    public void A() 
    { 
        System.out.println("123"); 
    } 
}   
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("456"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("789"); 
    } 
}  
public class Hierarchical2
{ 
    public static void main(String[] args) 
    { 
        Y ob1 = new Y();
        Z ob2 = new Z();
        ob1.A();
        ob2.C();
    } 
}

a)

123
789

b)

123
456

c)

456
789

d)

123
456
789
View Answer
Answer: a
Explanation: The objects ‘ob1’ and ‘ob2’ are objects of the classes Y and Z respectively, which inherit the properties of class X. The object ‘ob1’ calls a method of class X[A()] as ob1 is an object of the derived class Y. The object ‘ob2’ calls a method of class Z[C()].
Output:

$ javac Hierarchical2.java
$ java Hierarchical2
123
789

3. What will be the output of the following Java code?

class A 
{ 
    public void X() 
    { 
        System.out.print("Welcome"); 
    } 
}   
class B extends A
{ 
    public void Y() 
    { 
        System.out.print(" to "); 
    } 
} 
class C extends A
{
    public void Z() 
    { 
        System.out.print("Sanfoundry"); 
    } 
}  
public class Display
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        obj1.X();
        obj1.Y();
        obj2.Z();
    } 
}

a) Welcome
b) Welcome to Sanfoundry
c) Sanfoundry
d) to
View Answer

Answer: b
Explanation: Classes B and C are derived classes, i.e., they inherit the properties of class A. Hence, objects ‘obj1’ and ‘obj2’ are able to access the method of class A[X()] along with the methods of their own class[Y() and Z() respectively].
Output:

$ javac Display.java
$ java Display
Welcome to Sanfoundry

4. What will be the output of the following Java code?

class A 
{ 
    public void X() 
    { 
        System.out.print("Rank-");
    } 
} 
class B extends A
{ 
    public void Y() 
    { 
        System.out.print("1"); 
    } 
} 
class C extends A
{
    public void Z() 
    { 
        System.out.print("2"); 
    } 
} 
class D extends A
{
    public void Z() 
    { 
        System.out.print("3"); 
    } 
}    
public class Numbers
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        D obj3 = new D();
        obj1.X();
        obj3.Z();
 
    } 
}

a) Rank-1
b) Rank-2
c) Rank
d) Rank-3
View Answer

Answer: d
Explanation: Classes B, C and D are derived classes of class A. Hence, when objects of these classes are created, they can access the methods of class A. Objects ‘obj1’, ‘obj2’ and ‘obj3’ are objects of classes B, C and D respectively.
Output:

$ javac Numbers.java
$ java Numbers 
Rank-3

5. What will be the output of the following Java code?

class X 
{ 
    public void A() 
    { 
        System.out.print("#"); 
    } 
}  
class Y extends X
{ 
    public void B() 
    { 
        System.out.print("@"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
         System.out.print("&"); 
    } 
}   
public class Symbols
{ 
    public static void main(String[] args) 
    { 
        Y ob1 = new Y();
        Z ob2 = new Z();
        ob1.B();
        ob2.C();
        ob2.A();
    } 
}

a) @&#
b) #@&
c) @#&
d) &#@
View Answer

Answer: a
Explanation: The objects ‘ob1’ and ‘ob2’ are objects of the derived classes Y and Z respectively. This means that objects ob1 and ob2 can access the methods of the base class X also and hence the methods in the main() method are called successfully.
Output:

$ javac Symbols.java
$ java Symbols
@&#

6. What will be the output of the following Java code?

class A
{
    static int a =5;
}
class B extends A
{
    B()
    {
        A.a = 10;
    }
}
class C extends A
{
    C()
    {
         A.a = 15;
    }
}
class D extends A
{
    D()
    {
        A.a = 20;
    }
}
public class DotOperator
{
    public static void main(String[] args) 
    {
        B obj1 = new B();
        C obj2 = new C();
        D obj3 = new D();
 
        System.out.println(A.a);
    }
}

a) 5
b) 10
c) 15
d) 20
View Answer

Answer: d
Explanation: Static variables are those, whose only one copy exists in memory. In this program, the variable a of class A is static. Hence, it is modified by the constructors of the class that inherit from class A. The final that is called, is of class D which initialises (variable ‘a’)’s value to 20.
Output :

$ javac DotOperator.java
$ java DotOperator
20

7. What will be the output of the following Java code?

final class A
{
    int a;
    A()
    {
        a=5;
    }
}
class B extends A
{
    int a;
    B()
    {
        A obj = new A();
        a = obj.a;
    }
}
class C extends A
{
    int a;
    C()
    {
        A obj = new A();
        a = obj.a;
    }
}
class D extends A
{
    int a;
    D()
    {
        A obj = new A();
        a = obj.a;
    }
}
public class Hierarchy
{
    public static void main(String[] args) {
        B obj1 = new B();
        C obj2 = new C();
        D obj3 = new D();
        A obj = new B();
        System.out.println(obj.a);
    }
}

a) 5
b) 10
c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: The final keyword in java is used to restrict users. It can be applied to variables, classes and methods. If you declare a class as a final class, you cannot inherit it. A final variable is one whose value cannot be changed throughout a program and a final method is one that cannot be overridden.
Output :

$ javac Hierarchy.java
$ java Hierarchy
error: cannot inherit from final A : class B extends A
error: cannot inherit from final A : class C extends A 
error: cannot inherit from final A :  class D extends A

8. What will be the output of the following Java code?

class A
{
    protected void func()
    {
        System.out.println("Class A");
    }
}
class B extends A
{
    public void func()
    {
        System.out.println("Class B");
    }
}
class C extends A
{
    public void func()
    {
        System.out.println("Class C");
    }
}
public class Display1
{
    public static void main(String[] args) 
    {
        B obj1 = new B();
        A obj2 = new C();
        obj1.func();
        obj2.func();
    }
}

a)

Class B
Class A  

b)

Class B
Class C

c)

Class C
Class A

d) Runtime Error
View Answer

Answer: b
Explanation: Function overriding takes place when a subclass has a function with the same name as the parent class. In this program, the class B overrides the function func() present in class A to print “Class B” as the output. For obj2, though we create an object of type class A, we do it by calling the constructor of class C. Therefore, the overriding function present in class C is called, printing “Class C” as the output.
Output :

$ javac Display1.java
$ java Display1
Class B                                                                  
Class C

9. What will be the output of the following Java code?

class A
{
    int a = 10;
}
class B extends A
{
    int a = super.a;
}
class C extends A
{
    int c = a+10;
}
public class Add
{
    public static void main(String[] args) 
    {
        B obj1 = new B();
        C obj2 = new C();
        System.out.println(obj1.a + obj2.c);
    }
}

a) 10
b) 20
c) 30
d) Runtime Error
View Answer

Answer: c
Explanation: The super keyword is similar to “this” keyword. It can be used to access any data member of the parent class. In this program, the value of the variable a of class B is initialised to the value of the variable a of the parent class A. Therefore, the value of a is equal to 10 in class B. Since class C extends class A, it can directly access the variable of the parent class. Hence, the value of variable c in class C is 20. On adding, the sum is 30.
Output :

$ javac Add.java
$ java Add
30

10. What will be the output of the following Java code?

class A
{
    A()
    {
        System.out.println("Class A");
    }
}
class B extends A
{
    B()
    {
        System.out.println("Class B");
    }
}
class C extends A
{
    C()
    {
        System.out.println("Class C");
    }
}
public class MultipleObjects
{
    public static void main(String[] args) 
    {
        B obj1 = new B();
        C obj2 = new C();
    }
}

a)

Class A               
Class B              
Class A            
Class C 

b)

Class B
Class C

c)

Class A
Class B
Class C

d) Runtime Error
View Answer

Answer: a
Explanation: In this program, class B extends class A. Therefore, when an object of class B is created, it calls the constructor of parent class A first and then calls the constuctor of derived class B. The same occurs for class C since class C also extends class A.
Output :

$ javac MultipleObjects.java
$ java MultipleObjects
Class A              
Class B        
Class A        
Class C

11. What will be the output of the following Java code?

class A 
{ 
    public void X() 
    { 
        System.out.print("Bangalore-"); 
    } 
}   
class B extends A
{ 
    public void Y() 
    { 
        System.out.print("560102"); 
    } 
} 
class C extends A
{
    public void Z() 
    { 
        System.out.print("560108"); 
    } 
} 
public class Details
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        obj2.X();
        obj2.Z();
    } 
}

a) 560102
b) Bangalore-560102
c) Bangalore-560108
d) 560108
View Answer

Answer: c
Explanation: Classes B and C inherit properties of class A. Therefore, objects ‘obj1’ and ‘obj2’ of derived classes B and C respectively can access the method of class A[X()] and print the values inside the method without any compilation error using the concept of hierarchical inheritance.
Output :

$ javac Details.java
$ java Details
Bangalore-560108

12. What will be the output of the following Java code?

class A 
{  
    int a=10, b=5;
}  
class B extends A
{ 
    public void Y() 
    { 
        System.out.println(a+b); 
    } 
} 
class C extends A
{
    public void Z() 
    { 
        System.out.print(a*b); 
    } 
} 
public class SumandProduct
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        obj1.Y();
        obj2.Z();
    } 
}

a)

15
50

b) 15 50
c)

50
15

d) 50 15
View Answer

Answer: a
Explanation: As classes B and C are derived classes of base class A, they can access the variables a and b which are initialized in class A. Class B performs addition of the two variables while class C performs multiplication and prints the output.
Output :

$ javac SumandProduct.java
$ java SumandProduct
15
50

13. What will be the output of the following Java code?

import java.lang.Math; 
class A 
{  
    int a=9;
}  
class B extends A
{ 
    public void Y() 
    { 
        System.out.println(Math.sqrt(a)); 
    } 
} 
class C extends A
{
    public void Z() 
    { 
        System.out.println(Math.pow(a,2)); 
    } 
} 
public class Power
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        obj1.Y();
        obj2.Z();
    } 
}

a)

3.0
81.0

b)

3.0
3.0

c)

81.0
3.0

d)

3.0
9.0
View Answer
Answer: a
Explanation: As classes B and C are derived classes of base class A, they can access the variable a which is initialized in class A. Class B finds the square root of variable a while class C finds the square value of variable ‘a’ and it is executed successfully in the main() method.
Output :

$ javac Power.java
$ java Power
3.0
81.0

14. What will be the output of the following Java code?

class A 
{ 
    private int a = 10;
} 
class B extends A
{ 
    int b = a;
} 
class C extends A
{
    int c = a;
} 
public class PrivateVariable
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        System.out.println(obj1.b+obj2.c);
    } 
}

a) 20
b) 30
c) 40
d) Compile time Error
View Answer

Answer: d
Explanation: The “private” keyword in java is an access modifier which can be assigned to variables, methods or inner classes. In this program, the variable a of class A is declared as a private variable. Therefore, its scope is limited to the class A and cannot be inherited by classes B and C. Therefore, it throws an error.
Output :

$ javac PrivateVariable.java
$ java PrivateVariable
error: a has private access in A

15. What will be the output of the following Java code?

class A 
{ 
    int a;
    A()
    {
        a=50;
    }
}  
class B extends A
{ 
    int b= a;
} 
class C extends A
{
    int c = a;
} 
public class Hierarchical15
{ 
    public static void main(String[] args) 
    { 
        B obj1 = new B();
        C obj2 = new C();
        System.out.println(obj1.b+obj2.c);
    } 
}

a) 50
b) 100
c) Runtime Error
d) Compile time Error
View Answer

Answer: b
Explanation: As classes B and C are derived classes of base class A, they can access the variable a which is initialized in class A. Since the two classes extend class A, the constructor of class A is called first during the creation of their objects. Therefore, the value of ‘a’ is initialised to 50 before the values of variable ‘b’ and ‘c’ are assigned.
Output :

$ javac Hierarchical15.java
$ java Hierarchical15
100

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.