Object Oriented Programming using Java Questions and Answers – Hybrid Inheritance

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

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

class A
{
   public void disp()
   {
	System.out.println("Class A");
   }
}
class B extends A
{
   public void disp()
   {
	System.out.println("Class B");
   }
}
class C extends A
{
   public void disp()
   {
	System.out.println("Class C");
   }
}
public class Display extends C
{
   public void disp()
   {
	System.out.println("Class D");
   }
   public static void main(String args[])
   {
	Display obj = new Display();
	obj.disp();
   }
}

a) Class A
b) Class B
c) Class C
d) Class D
View Answer

Answer: d
Explanation: The inheritance whose the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. In the above block of code, there are four classes – A, B, C and Display which are involved in inheritance but as the object is created of class-type Display, the disp() function inside that class is executed.
Output:

$ javac Display.java
$ java Display
Class D

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

advertisement
advertisement
class X 
{ 
    public void A() 
    { 
        System.out.println("1"); 
    } 
}   
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("2"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("3"); 
    } 
} 
class V extends Y
{
    public void D()
    { 
        System.out.println("4"); 
    }    
}  
public class Numbers
{ 
    public static void main(String[] args) 
    { 
        Z ob1=new Z();
        V ob2=new V();
        ob1.A();
        ob2.B();
    } 
}

a)

1
2

b)

advertisement
2
3

c)

3
4

d)

advertisement
1
4
View Answer
Answer: a
Explanation: Hybrid inheritance is a combination of various types if inheritances like multiple inheritance and multilevel inheritance. A class is derived from two classes as in multiple inheritance (classes X and Y in this case). As methods A() and B() are called, 1 and 2 are printed.
Output:

$ javac Numbers.java
$ java Numbers
1
2

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

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"); 
    } 
} 
class V extends Y
{
    public void D()
    { 
        System.out.println("10"); 
    }    
} 
public class Hybrid1 
{ 
    public static void main(String[] args) 
    { 
        Y obj=new Y();
        V obj2=new V();
        obj.A();
        obj2.B();
    } 
}

a)

123
456

b)

789
10

c)

456
123

d)

123
10
View Answer
Answer: a
Explanation: A hybrid inheritance is a combination of more than one types of inheritance. There are four classes involved in inheritance in the above block of code. As ‘obj’ and ‘obj2’ are of classtype Y and V respectively, methods A() and B() are called and output is obtained.
Output:

$ javac Hybrid1.java
$ java Hybrid1 
123
456

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

class X 
{ 
    public void A() 
    { 
        System.out.println("Add"); 
    } 
}  
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("Subtract"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("Divide"); 
    } 
} 
class V extends Y
{
    public void D()
    { 
        System.out.println("Multiply"); 
    } 
} 
public class Hybrid2
{ 
    public static void main(String[] args) 
    { 
        V obj=new V();
        obj.A();
    } 
}

a) Add
b) Subtract
c) Divide
d) Multiply
View Answer

Answer: a
Explanation: The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. In the above block of code, it is a combination of single and multiple inheritance. An object ‘obj’ is created of classtype V which in turn is used to call a method A() which inturn prints “Add”.
Output:

$ javac Hybrid2.java
$ java Hybrid2
Add

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

class A
{
    public void disp()
    {
	    System.out.println("Rank 1");
    }
}
class B extends A
{
    public void disp()
    {
	    System.out.println("Rank 2");
    }
}
class C extends A
{
    public void disp()
    {
	    System.out.println("Rank 3");
    }	
}
public class Hybrid3 extends C
{
    public void disp()
    {
	    System.out.println("Rank 4");
    }
    public static void main(String args[])
    {
	    Hybrid3 obj = new Hybrid3();
	    obj.disp();
    }
}

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

Answer: d
Explanation: Hybrid inheritance is combination of two or more inheritances such as single, multiple, multilevel or hierarchical inheritances. There are multiple methods of the same function-name of disp(), but as the object ‘obj’ is of type Hybrid3, “Rank 4” is given as the output on execution.
Output:

$ javac Hybrid3.java
$ java Hybrid3
Rank 4

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

class X 
{ 
    public void A() 
    { 
        System.out.println("%"); 
    } 
}   
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("#"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("@"); 
    } 
} 
class V extends Y
{
    public void D()
    { 
        System.out.println("&"); 
    }  
} 
public class Symbol
{ 
    public static void main(String[] args) 
    { 
        V obj=new V();
        Z obj2=new Z();
        obj.A();
        obj2.C();
    } 
}

a)

#
@

b)

&
@

c)

&
%

d)

%
@
View Answer
Answer: d
Explanation: The main class Symbol consists of a main() method which involves creation of two objects ‘obj’ and ‘obj2’ of class types V and Z respectively. Using the concept of hybrid inheritance, methods A() and C() are accessible using the two objects ‘obj’ and ‘obj2’ as mentioned in the main() method.
Output:

$ javac Symbol.java
$ java Symbol
%
@

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

class X 
{ 
    public void A() 
    { 
        System.out.println("Bangalore"); 
    } 
}  
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("Delhi"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("Kolkata"); 
    } 
} 
class V extends Y
{
    public void D()
    { 
        System.out.println("Chennai"); 
    }    
} 
public class Cities
{ 
    public static void main(String[] args) 
    { 
        V obj=new V();
        Z obj2=new Z();
        obj.A();
        obj2.A();
    } 
}

a)

Bangalore
Bangalore

b)

Bangalore
Delhi

c)

Delhi
Chennai

d)

Kolkata
Chennai
View Answer
Answer: a
Explanation: In object oriented programming, the combination of any two or more types of inheritance is known as hybrid inheritance. Each method displays a city name when called using an object. As the method A() is called twice using different objects in the main method, the string “Bangalore” is printed twice.
Output:

$ javac Cities.java
$ java Cities
Bangalore
Bangalore

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

class X 
{ 
    public void A() 
    { 
        System.out.println("India-New Delhi"); 
    } 
}  
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("USA-Washington, D.C."); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("England-London"); 
    } 
} 
class V extends Y
{
    public void D()
    { 
        System.out.println("Australia-Canberra"); 
    }   
}
public class Capitals
{ 
    public static void main(String[] args) 
    { 
        V obj=new V();
        Z obj2=new Z();
        obj.A();
        obj.D();
        obj2.C();
    } 
}

a)

India-New Delhi
Australia-Canberra
England-London    

b)

India-New Delhi
USA-Washington, D.C.
England-London 

c)

USA-Washington, D.C.
Australia-Canberra
India-New Delhi 

d)

Australia-Canberra
India-New Delhi
USA-Washington, D.C.
View Answer
Answer: a
Explanation: The above program is used to print particular strings in each function. As the four classes – X, Y, Z and V are linked to each other by inheritance, the functions can be called by using the object of the parent class. The methods are called using ‘obj’ and ‘obj2’ of classes V and Z and hence the strings are printed.
Output:

$ javac Capitals.java
$ java Capitals
India-New Delhi
Australia-Canberra
England-London

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

class X 
{ 
    public void A() 
    { 
        System.out.println("Living"); 
    } 
}  
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("Humans"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("Animals"); 
    } 
} 
class V extends Z
{
    public void D()
    { 
        System.out.println("Dog"); 
    }     
}
public class Pets
{ 
    public static void main(String[] args) 
    { 
        V obj=new V();
        Z obj2=new Z();
        obj.A();
        obj2.C();
        obj.D();
    } 
}

a)

Living
Animals
Dog

b)

Living
Humans
Dog

c)

Animals
Living
Dog

d)

Dog
Humans
Animals
View Answer
Answer: a
Explanation:
Output: Hybrid inheritance is performed when there is a valid combination of different types of inheritances. In the above block of code, the objects ‘obj’ and ‘obj2’ are used to call functions A(), C() and D() and as these objects are of parent classes V and Z, they can be used to call the methods.

$ javac Pets.java
$ java Pets
Living
Animals
Dog

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

class X 
{ 
    public void A() 
    { 
        System.out.println("Hybrid Inheritance"); 
    } 
}  
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("Single Inheritance"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("Hierarchical Inheritance"); 
    } 
} 
class V extends Z
{
    public void D()
    { 
        System.out.println("Multilevel Inheritance"); 
    }   
} 
public class Inheritance1
{ 
    public static void main(String[] args) 
    { 
        V obj=new V();
        obj.A();
    } 
}

a) Multilevel Inheritance
b) Hybrid Inheritance
c) Single Inheritance
d) Hierarchical Inheritance
View Answer

Answer: b
Explanation: When more than one type of inheritance is combined in a particular program, it is called as hybrid inheritance. The method A() belongs to class X, but as class X is indirectly inherited to class V, the object of class V (‘obj’ in this case) can be used to call it in the main() method.
Output:

$ javac Inheritance1.java
$ java Inheritance1
Hybrid Inheritance

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

class X 
{ 
    public void A() 
    { 
        System.out.println("C"); 
    } 
} 
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("Python"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("C++"); 
    } 
} 
class V extends Y, Z
{
    public void D() 
    { 
        System.out.println("Java"); 
    } 
}
public class Programming
{
    public static void main(String[] args) 
    { 
        V obj=new V();
        obj.A();
        obj.B();
        obj.C();
        obj.D();
    } 
}

a)

C
Java
C++
Python

b)

Java
Python
C++
C

c)

C++
Python
Java
C

d) Compilation Error
View Answer

Answer: d
Explanation: Hybrid inheritance is considered a valid combination of one or more types of inheritance in a program. In the above block of code, multiple inheritance is being implemented for class V as it extends two classes. Since Java doesn’t support multiple inheritance, the compiler throws an error on execution.
Output:

$ javac Programming.java
$ java Programming
Uncompilable source code

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

class X 
{ 
    public void A() 
    { 
        System.out.println("Superclass"); 
    } 
} 
class Y extends X
{ 
    public void B() 
    { 
        System.out.println("Subclass of X"); 
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println("Subclass of X"); 
    } 
} 
class V extends Y
{
    public void D() 
    { 
        System.out.println("Subclass of Y"); 
    } 
}
public class Superclass
{
    public static void main(String[] args) 
    { 
        V obj=new V();
        obj.A();
        obj.D();
    } 
}

a)

Superclass
Subclass of Y

b)

Superclass
Subclass of X

c)

Subclass of X
Subclass of Y

d)

Superclass
Superclass
View Answer
Answer: a
Explanation: The following block of code is implemented using the concept of hybrid inheritance. Sub Class is the class that inherits properties from another class whereas
super class is the class whose properties are inherited by the sub class. In the above block of code class A is the superclass and class B is the subclass.
Output:

$ javac Superclass.java
$ java Superclass
Superclass
Subclass of Y

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

class X 
{ 
        int a;
} 
class Y extends X
{ 
    public void B() 
    { 
        a=10;
        System.out.println(a);
    } 
} 
class Z extends X
{
    public void C() 
    { 
        a=5;
        System.out.println(a); 
    } 
} 
class V extends Y
{
    public void D() 
    { 
        a=15;
        System.out.println(a);
    } 
}
public class Value
{
    public static void main(String[] args) 
    { 
        V obj=new V();
        obj.B();
        obj.D();
    } 
}

a)

5
10

b)

10
15

c)

15
10

d)

5
15
View Answer
Answer: b
Explanation: Hybrid inheritance is the process of deriving a class using more than one level or more than one mode of inheritance. The object of class V is given an identifier called ‘obj’ and this is used to call the methods B() and D() and therefore prints the value present in those functions.
Output:

$ javac Value.java
$ java Value
10
15

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

class X 
{ 
    int a=2,b=5;
} 
class Y extends X
{ 
    public void B() 
    { 
        System.out.println(a+b);
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println(a-b); 
    } 
} 
class V extends Y
{
    public void D() 
    { 
        System.out.println(a*b);
    } 
}
public class Calculation
{
    public static void main(String[] args) 
    { 
        V obj=new V();
        Z obj2=new Z();
        obj.B();
        obj.D();
        obj2.C();
    } 
}

a)

7
10
-3

b)

10
-3
7

c)

7
-3
10

d)

-3
10
7
View Answer
Answer: a
Explanation: Hybrid inheritance is a type of inheritance which comprises more than one form of inheritance. In the above block of code, there are three functions B(), D() and C() which are called using the objects ‘obj’ and ‘obj2’ of classes V and Z respectively. Hence, the compilation is successful and output is given.
Output:

$ javac Calculation.java
$ java Calculation
7
10
-3

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

class X 
{ 
        int a=4,b=8;
} 
class Y extends X
{ 
    public void B() 
    { 
        System.out.println(Math.pow(a,b));
    } 
} 
class Z extends X
{
    public void C() 
    { 
        System.out.println(Math.sqrt(a)); 
    } 
} 
class V extends Y
{
    public void D() 
    { 
        System.out.println(Math.cbrt(b));
    } 
}
public class Operations
{
    public static void main(String[] args) 
    { 
        V obj=new V();
        Z obj2=new Z();
        obj.B();
        obj.D();
        obj2.C();
    } 
}

a)

2.0
2.0
2.0

b)

1024.0
2.0
2.0

c)

2.0	
4.0 
8.0

d)

65536.0
2.0
2.0
View Answer
Answer: d
Explanation: The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. The classes V and Z are used to create objects ‘obj’ and ‘obj2’, which are used inturn to call the methods B(), C() and D() whose properties are inherited by classes V and Z using the ‘extends’ keyword.
Output:

$ javac Operations.java
$ java Operations
65536.0
2.0
2.0

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.

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.