This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Types of Inheritance”.
1. What will be the output of the following Java code?
public class Inheritance { public static void main(String[] args) { Super i = new Super(); i.show(); } } class Super { public void show() { System.out.println("Base"); } } class Sub extends Super { public void show() { System.out.println("Derived"); } }
a) Base
b) Compilation Error
c) Derived
d) Runtime Error
View Answer
Explanation: The object ‘i’ is an object of the class Sub, which inherits the properties of the class Super. When the show() function is called, it refers to the show() function of the derived class as mentioned in the main() method in the above block of code.
Output:
$ javac Inheritance.java
$ java Inheritance
Derived
2. What will be the output of the following Java code?
class Base { public void show() { System.out.println("Base"); } } class Derived extends Base { public void show() { System.out.println("Derived"); } } public class Inheritance1 { public static void main(String[] args) { Base i = new Base(); i.show(); } }
a) Base
b) Compilation Error
c) Derived
d) Runtime Error
View Answer
Explanation: The object ‘i’ is an object of the class Base. Although the class Derived inherits the properties of Base, in order to call the show function of the Derived class, it is necessary to create an object of the derived class.
Output:
$ javac Inheritance1.java
$ java Inheritance1
Base
3. What will be the output of the following Java code?
class Base { int a = 30; public void display() { System.out.println(a); System.out.println("Base"); } } class Derived extends Base { public void display() { System.out.println(a); System.out.println("Derived"); } } public class InheritCall { public static void main(String[] args) { Base b = new Derived(); b.display(); } }
a)
30 Base
b)
30 Derived
c)
0 Base
d)
0 DerivedView Answer
Explanation: The object ‘b’ is an object of the class Derived, which in turn inherits the properties of Base. The display() function of the Derived class is called using the object and the value of ‘a’ is accessed from the Base class.
Output:
$ javac InheritCall.java $ java InheritCall 30 Derived
4. What will be the output of the following Java code?
public class Inherit1 { public static void main(String[] args) { Base b = new Derived(); b.print(); } } class Base { public static void print() { System.out.println("Base"); } } class Derived extends Base { public static void print() { System.out.println("Derived class"); } }
a) Base
b) Compilation Error
c) Derived class
d) Runtime Error
View Answer
Explanation: The function print() is static in both the Base and Derived classes. As it is known that static functions do not allow runtime polymorphism, only the Base class print function is called and the string “Base” is printed.
Output:
$ javac Inherit1.java
$ java Inherit1
Base
5. What will be the output of the following Java code?
class Base { public static void display() { System.out.println("Base"); } } class Derived extends Base { public static void display() { System.out.println("Derived"); } } class Inheritance2 { public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); x.display(); y.display(); z.display(); } }
a)
Base Derived Derived
b)
Derived Derived Base
c)
Base Base Derived
d) Compilation Error
View Answer
Explanation: Although the object ‘y’ is an object of the derived class, it is of the Base class type. As all the display functions are all static, runtime polymorphism doesn’t take place, hence the object ‘y’ calls the display() function of the base class.
Output:
$ javac Inheritance2.java
$ java Inheritance2
Base
Base
Derived
6. 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() { super.super.Print(); System.out.println("C"); } } public class ExtendsCall { public static void main(String[] args) { C c = new C(); c.Print(); } }
a)
A B C
b)
C C C
c)
A A A
d) Compilation Error
View Answer
Explanation: 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 as this is not possible in Java.
Output:
$ javac ExtendsCall.java $ java ExtendsCall error: not a statement super.super.Print();
7. 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 ExtendsCall1 { public static void main(String[] args) { C c = new C(); c.Print(); } }
a)
A B C
b)
A B B
c)
A A A
d)
C C CView Answer
Explanation: The super keyword is used to refer to the immediate parent of the class it is called in. Here the super keyword in the class C refers to class B, and that in class B refers to class A. The super keyword is followed by the print() function, hence it calls the respective print functions of their parent classes.
Output:
$ javac ExtendsCall1.java
$ java ExtendsCall1
A
B
C
8. 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; } class D extends C { public void calculate() { System.out.println(a*b*c); } } public class Multilevel { public static void main(String[] args) { D d = new D(); d.calculate(); } }
a) 20
b) 30
c) 2000
d) 6000
View Answer
Explanation: This code is an example of multilevel inheritance where class B inherits class A, class C inherits class B and class D inherits class C. The values ‘a’, ‘b’ and ‘c’ are inherited to D through this multilevel inheritance and hence when calculate is called, it uses all the values of the three classes.
Output:
$ javac Multilevel.java $ java Multilevel 6000
9. 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; } class D extends C { int c; public void calculate() { System.out.println(a*b*c); } } public class Default { public static void main(String[] args) { D d = new D(); d.calculate(); } }
a) 200
b) 6000
c) 0
d) 600
View Answer
Explanation: When the object ‘d’ is created, the value of the variable ‘c’ of the class D is set to 0 by default. Although all the values are achieved by inheritance, the value of ‘c’ is still 0 because it presents in class D.
Output:
$ javac Default.java $ java Default 0
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 A { int b = 30; } class D extends B,C { void disp() { System.out.println(b); } } public class Display { public static void main(String[] args) { D d = new D(); d.calculate(); } }
a) 20
b) 30
c) Compilation Error
d) Runtime Error
View Answer
Explanation: Java doesn’t support the concept of multiple inheritance in object oriented programming as it leads to confusion as to which value of ‘b’ to refer to in this program. Hence it results in a compilation error.
Output:
$ javac Display.java $ java Display error: '{' expected class D extends B, C
11. What will be the output of the following Java code?
class A { String s = "Sanfoundry"; } class B extends A { void position(int a) { System.out.println(s.charAt(a)); } } public class Position { public static void main(String[] args) { B b = new B(); b.position(3); } }
a) Compilation Error
b) n
c) a
d) f
View Answer
Explanation: The object b is an object of the class B, which extends class A, thus inheriting it’s string variable ‘s’. The charAt function returns the character at the specified index in the string calling it. As indexing starts at 0, the nth position is the (n-1)th index.
Output:
$ javac Position.java $ java Position f
12. What will be the output of the following Java code?
class A { int a = 69; } class B extends A { void ascii() { System.out.println((char)a); } } public class Ascii { public static void main(String[] args) { B b = new B(); b.ascii(); } }
a) e
b) Compilation Error
c) E
d) Runtime Error
View Answer
Explanation: The object ‘b’ is an object of the class B, which extends class A, hence inheriting the value of the integer variable ‘a’. When an integer is converted to a character, it is converted based on it’s ascii value. The ascii value of ‘e’ is 101, and that of ‘E’ is 69.
Output:
$ javac Ascii.java
$ java Ascii
E
13. What will be the output of the following Java code?
class A { char ch = 'f'; } class B extends A { void ascii() { System.out.println((int)ch); } } public class AsciiValue { public static void main(String[] args) { B b = new B(); b.ascii(); } }
a) f
b) Compilation Error
c) F
d) Runtime Error
View Answer
Explanation: The object ‘b’ is an object of the class B, which extends class A, hence inheriting the value of the character variable ch. When a character is converted to an integer, it is converted based on it’s ascii value. The ascii value of ‘f’ is 102, and that of ‘F’ is 70.
Output:
$ javac AsciiValue.java $ java AsciiValue 102
14. What will be the output of the following Java code?
class A { int a = 50; } class B extends A { int a = 100; } public class PrintObject { public static void main(String[] args) { A b = new B(); System.out.println(b); } }
a) 50, 100
b) 100, 50
c) B@2a139a55
d) A$B@2a139a55
View Answer
Explanation: The object ‘b’ is an object of the class B, which extends class A. When an object is printed, it is printed in the format ClassName@HashCode. Although class B extends class A, it is not a subclass of it, hence it is an object of only class B.
Output:
$ javac PrintObject.java
$ java PrintObject
B@2a139a55
15. What will be the output of the following Java code?
class A { String s = "Sanfoundry"; } class B extends A { void index(char ch) { System.out.println(s.indexOf(ch)); } } public class Index { public static void main(String[] args) { B b = new B(); b.index('n'); } }
a) 3
b) 2
c) 6
d) 7
View Answer
Explanation: The object ‘b’ is an object of the class B, which extends class A, hence inheriting the value of the String variable ‘s’. The indexOf function returns the index of the first instance of a character in the string. As indexing starts from 0, the nth position refers to the (n-1)th index.
Output:
$ javac Index.java $ java Index 2
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.