Object Oriented Programming using Java Questions and Answers – Multilevel Inheritance

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

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

class A 
{
    public void Print() 
    {
        System.out.println("A");
    }
}
class B extends A 
{
    public void Print() 
    {
        System.out.println("B");
    }
}
class C extends B 
{
    public void Print() 
    {
        System.out.println("C");
    }
}
public class Multilevel
{
    public static void main(String[] args) 
    {
        B c = new C();
        c.Print();
    }
}

a) A
b) B
c) C
d) Compilation Error
View Answer

Answer: c
Explanation: The object ‘c’ is an object of the class C. Class C inherits the properties of class B, which inherits the properties of class A, through the concept of multilevel inheritance. Although class C inherits the properties of class B and class A, as it is an object of the class C, the print function of class C is called.
Output:

$ javac Multilevel.java
$ java Multilevel
C

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

advertisement
advertisement
class A 
{
    public void Print() 
    {
        System.out.println("A");
    }
}
class B extends A 
{
    public void Print() 
    {
        System.out.println("B");
    }
}
class C extends B 
{
    public void Print() 
    {
        super.super.Print();
        System.out.println("C");
    }
}
public class Multi1
{
    public static void main(String[] args) 
    {
        B c = new C();
        c.Print();
    }
}

a)

A
B
C 

b)

advertisement
C
C
C

c)

A
A
A

d) Compilation Error
View Answer

Answer: d
Explanation: Class C inherits the properties of classes A and B through multilevel inheritance. The super keyword is used to refer to the parent class of the derived class, but it cannot be used more than once in the same statement in order to call the parent of the parent class. Multiple super or nested super statements are not valid in Java.
Output:

advertisement
$ javac Multi1.java
$ java Multi1
error: not a statement super.super.Print();

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

class A 
{
    public void Print() 
    {
        System.out.println("A");
    }
}
class B extends A 
{
    public void Print() 
    {
        super.Print();
        System.out.println("B");
    }
}
class C extends B 
{
    public void Print() 
    {
        super.Print();
        System.out.println("C");
    }
}
public class Multilevel2
{
    public static void main(String[] args) 
    {
        B c = new C();
        c.Print();
    }
}

a)

A
B
C 

b)

C
C
C

c)

A
A
A

d) Compilation Error
View Answer

Answer: a
Explanation: The object ‘c’ is an object of class C, which inherits class B and class A through multilevel inheritance. The super keyword is used to refer to the parent class of the class it is called from. The super keyword in class C refers to class B, and that in class B refers to class A.
Output:

$ javac Multilevel2.java
$ java Multilevel2
A
B
C

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

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

a) 120
b) 40
c) 50
d) Compilation Error
View Answer

Answer: a
Explanation: The object c is an object of the class C, which inherits the properties of classes A and B through multilevel inheritance. As class C has inherited both the classes,A and B, it can use the values of the variables of those respective classes in it’s own class functions.
Output:

$ javac Sum.java
$ java Sum
120

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

class A 
{
    String s = "Sanfoundry";
}
class B extends A 
{
    int b = 5;
}
class C extends B 
{
    public void print() 
    {
        System.out.println(s.charAt(b));
    }
}
public class CharAt
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.print();
    }
}

a) o
b) Compilation Error
c) u
d) Runtime Error
View Answer

Answer: c
Explanation: Class C inherits the properties of class B, which inherits the properties of class A, through multilevel inheritance. Therefore an object of class C can access the values of the variables of classes A and B. The charAt() function returns the character at the index passed to the function in the string calling it.
Output:

$ javac CharAt.java
$ java CharAt
u

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

class A 
{
    String s = "Sanfoundry";
}
class B extends A 
{
    char ch = 'n';
}
class C extends B 
{
    int a = 3;
    public void display() 
    {
        System.out.println(s.indexOf(ch,a));
    }
}
public class IndexOf
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.display();
    }
}

a) 2
b) 3
c) 7
d) 6
View Answer

Answer: d
Explanation: The object ‘c’ is an object of the class C, which extends classes A and B through multilevel inheritance, and hence can access the variables of those classes. The indexOf function returns the first index of the character passed to it in the string calling it. If an integer is passed along with the character, it starts the search from that index.
Output:

$ javac IndexOf.java
$ java IndexOf
6

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

class A 
{
    int a = 67;
}
class B extends A 
{
    char ch = (char)a;
}
class C extends B 
{
    public void ascii() 
    {
        System.out.println(ch);
    }
}
public class Ascii
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.ascii();
    }
}

a) C
b) Compilation Error
c) c
d) Runtime Error
View Answer

Answer: a
Explanation: The object ‘c’ is an object of the class C, which extends classes A and B through multilevel inheritance, and hence can access the variables of those classes. When an integer is converted to a character, it is converted on the basis of it’s ascii value. The ascii value of ‘c’ is 99, while that of ‘C’ is 67.
Output:

$ javac Ascii.java
$ java Ascii
c

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

import java.util.*;
class A 
{
    LinkedList l = new LinkedList();
}
class B extends A 
{
    void add(int a)
    {
        l.add(a);
    }
}
class C extends B 
{
    public void sum() 
    {
        int sum=0;
        Iterator i = l.iterator();
        while(i.hasNext())
        sum+=(int)i.next();
        System.out.println(sum);
    }
}
public class Sum
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.add(53);
        c.add(27);
        c.add(80);
        c.sum();
    }
}

a) 160
b) Compilation Error
c) 0
d) 120
View Answer

Answer: a
Explanation: Class C inherits the properties and variables of class A and class B through multilevel inheritance, and hence can access the add function and the linked list ‘l’. The iterator is used to iterate through the elements of the linked list and the sum of those elements is then printed.
Output:

$ javac Sum.java
$ java Sum
160

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

class A 
{
    String s = "San";
}
class B extends A 
{
    String s = super.s+"foun";
}
class C extends B 
{
    public void print() 
    {
        System.out.println(s+"dry");
    }
}
public class Print
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.print();
    }
}

a) Sanfoundry
b) Sandry
c) foundry
d) Compilation Error
View Answer

Answer: a
Explanation: The object ‘c’ is an object of the class C, which inherits both classes A and B through multilevel inheritance, hence can access their string variable ‘s’. The super keyword is used to call the parent class of the class calling it. When ‘s’ is called in print() function, it refers to the string ‘s’ of it’s parent class, which is class B.
Output:

$ javac Print.java
$ java Print
Sanfoundry

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

class A 
{
    int a = 10;
}
class B extends A 
{
    int b = 20;
}
class C extends B 
{
    int c = 30;
}
public class ClassName
{
    public static void main(String[] args) 
    {
        C c = new C();
        System.out.println(c);
    }
}

a) 10, 20, 30
b) a = 10, b = 20, c = 30
c) C@2a139a55
d) A$B$C@2a139a55
View Answer

Answer: c
Explanation: The object ‘c’ is an object of the class C, which inherits the properties of classes A and B through multilevel inheritance. Although class C inherits classes A and B, it is not a subclass of either of them, hence it’s ClassName remains C. When an object is printed, it is in the format ClassName@HashCode.
Output:

$ javac ClassName.java
$ java ClassName
C@2a139a55

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

class A 
{
    int x = 5;
}
class B extends A 
{    
    int fact = 1;
    void fact()
    {
        for(int i = 1; i<=x; i++)
        {
            fact*=i;
        }
    }
}
class C extends B 
{
    void print()
    {
        System.out.println(fact);
    }
}
public class Factorial
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.fact();
        c.print();
    }
}

a) 5
b) 120
c) Compilation Error
d) 24
View Answer

Answer: b
Explanation: Class C extends classes A and B through multilevel inheritance, thus gaining access to the function fact and the variables ‘x’ and ‘fact’. The function fact() is used to calculate the factorial of the variable ‘x’, and the print() function is used to print it.
Output:

$ javac Factorial.java
$ java Factorial
120

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

import java.util.*;
class A 
{
    int a = 10;
}  
class B extends A 
{
    int b = 20;
}  
class C extends B 
{
    int c = 30;
}
class D extends C
{
    int c;
    public void multiply() 
    {
        System.out.println(a*b*c);
    }
}
public class Multiply
{
    public static void main(String[] args) 
    {
        D d = new D();
        d.multiply();
    }
}

a) 300
b) 6000
c) 0
d) 200
View Answer

Answer: c
Explanation: The class D extends classes A, B and C through multilevel inheritance, therefore gaining access to the variables ‘a’, ‘b’ and ‘c’. Although it inherits class C, class D also has declared the variable ‘c’ with no initial value, hence at the time of object creation, it is given the initial value of 0 and thus the multiplication product is 0.
Output:

$ javac Multiply.java
$ java Multiply
0

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

import java.util.*;
class A 
{
    LinkedList l = new LinkedList();
}
class B extends A 
{
    void add(int a)
    {
        l.add(a);
    }
}
class C extends B 
{
    public void remove(int a) 
    {
        l.remove(a);
    }
}
public class list
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.add(1);
        c.add(2);
        c.add(3);
        c.add(4);
        c.remove(3);
        System.out.println(c.l);
    }
}

a) [1, 2, 3]
b) [1, 2, 4]
c) {1, 2, 3}
d) {1, 2, 4}
View Answer

Answer: a
Explanation: THe object ‘c’ is an object of the class C, which inherits the properties and members of the classes A and B through multilevel inheritance. The add() function adds elements to the linked list ‘l’, while the remove function removes elements. The remove function removes elements at the index specified to it, and as indexing starts at 0, the nth position refers to the (n-1)th index.
Output:

$ javac list.java
$ java list
[1, 2, 3]

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

class A 
{
    int a = 30;
}
class B extends A 
{
    int a = 40;
}
class C extends B 
{
    public void display() 
    {
        System.out.println(a);
    }
}
public class Parent
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.display();
    }
}

a) 30
b) Compilation Error
c) 40
d) Runtime Error
View Answer

Answer: c
Explanation: Class C inherits the properties of classes A and B through multilevel inheritance, thus gaining access to their variables and functions. Although class C inherits both classes A and B, it’s immediate parent class is B, hence it uses that value in the display() function.
Output:

$ javac Parent.java
$ java Parent
40

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

class A 
{
    char ch = 'Y';
}
class B extends A 
{
    int a = (int)ch;
}
class C extends B 
{
    public void print() 
    {
        System.out.println(a);
    }
}
public class Ascii1
{
    public static void main(String[] args) 
    {
        C c = new C();
        c.print();
    }
}

a) 89
b) Compilation Error
c) 121
d) Runtime Error
View Answer

Answer: a
Explanation: The object ‘c’ is an obejct of the class C, which inherits the properties of the classes A and B through multilevel inheritance, thus can use their variables and functions. When a character is converted to an integer, it is converted based on it’s ascii value. The ascii value of ‘y’ is 121, while that of ‘Y’ is 89.
Output:

$ javac Ascii1.java
$ java Ascii1
121

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.