This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Single Level Inheritance”.
1. What will be the output of the following Java code?
class A { int a = 10; } class B extends A { void print() { System.out.println(a); } } public class Inheritance { public static void main(String[] args) { B b = new B(); b.print(); } }
a) 10
b) Compilation Error
c) 0
d) Runtime Error
View Answer
Explanation: The object ‘b’ is an object of the class B, which inherits the properties of class A through single level inheritance. The value of variable ‘a’ is given to the print function in class B by class A through the concept of single level inheritance.
Output:
$ javac Inheritance.java $ java Inheritance 10
2. What will be the output of the following Java code?
class A { void print() { System.out.println("A"); } } class B extends A { void print() { System.out.println("B"); } } public class Single { public static void main(String[] args) { A b = new B(); b.print(); } }
a) A
b) Compilation Error
c) B
d) Runtime Error
View Answer
Explanation: The object ‘b’ is an object of the class B, which extends class A. As it is an object of the class B, the print function of the class B is executed. This program is an example of single inheritance as class B inherits the properties of class A.
Output:
$ javac Single.java
$ java Single
B
3. What will be the output of the following Java code?
class A { void print() { System.out.println("A"); } } class B extends A { void print() { System.out.println("B"); } } public class Single1 { public static void main(String[] args) { A a = new A(); a.print(); } }
a) B
b) Runtime Error
c) A
d) Compilation Error
View Answer
Explanation: The object ‘a’ is an object of the class A. Although the class B extends class A, in order to call its print function, it requires the creation of an object of class B. Hence, only the print() function in class A is executed successfully.
Output:
$ javac Single1.java
$ java Single1
A
4. What will be the output of the following Java code?
class Base { int a = 10; void display() { System.out.println(a); System.out.println("Base"); } } class Derived extends Base { int a = 20; void display() { System.out.println(a); System.out.println("Derived"); } } public class Polymorphism { public static void main(String[] args) { Base a = new Derived(); a.display(); } }
a)
10 Base
b)
20 Base
c)
10 Derived
d)
20 DerivedView Answer
Explanation: The object ‘a’ is an object of the class Derived, which extends class Base by the concept of single inheritance. As the object is an object of the Derived class, it calls the print() function which is a part of the Derived class.
Output:
$ javac Polymorphism.java $ java Polymorphism 20 Derived
5. What will be the output of the following Java code?
class Base { String s = "Sanfoundry"; } class Derived extends Base { void index() { System.out.println(s.indexOf('N')); } } public class Index { public static void main(String[] args) { Derived a = new Derived(); a.index(); } }
a) 3
b) 7
c) 2
d) -1
View Answer
Output:
$ javac Index.java $ java Index -1
6. What will be the output of the following Java code?
class Base { String s = "Single Inheritance"; } class Derived extends Base { void charAt() { System.out.println(s.charAt(8)); } } public class Index1 { public static void main(String[] args) { Derived a = new Derived(); a.charAt(); } }
a) I
b) e
c) n
d) h
View Answer
Explanation: The object ‘a’ is an object of the class Derived, which inherits the properties of the class Base through single inheritance. The charAt() function is used to return the character in the string calling it at the index passed to the function. As indexing starts at 0, the nth position corresponds to the (n-1)th index.
Output:
$ javac Index1.java
$ java Index1
n
7. What will be the output of the following Java code?
class Base { char ch = 'D'; } class Derived extends Base { void ascii() { System.out.println((int)ch); } } public class Ascii { public static void main(String[] args) { Derived a = new Derived(); a.ascii(); } }
a) 68
b) Compilation Error
c) 100
d) Runtime Error
View Answer
Explanation: The object ‘a’ is an object of the Derived class, which inherits class Base through single inheritance. When a character is converted to an integer, it is converted based on the ascii value of that character. The ascii value of ‘d’ is 100, and that of ‘D’ is 68.
Output:
$ javac Ascii.java $ java Ascii 68
8. What will be the output of the following Java code?
class Base { int a = 76; } class Derived extends Base { void ascii() { System.out.println((char)a); } } public class Ascii1 { public static void main(String[] args) { Derived i = new Derived(); i.ascii(); } }
a) L
b) Compilation Error
c) l
d) Runtime Error
View Answer
Explanation: The object ‘i’ is an object of the Derived class, which inherits class Base through single inheritance. When an integer is converted to a character, it is converted on the basis of it’s ascii value. The ascii value of ‘L’ is 76, while that of ‘l’ is 108.
Output:
$ javac Ascii1.java
$ java Ascii1
L
9. What will be the output of the following Java code?
class Base { int a[] = {1,2,3,4,5,6}; } class Derived extends Base { void even() { for(int i = 0; i < a.length; i++) { if(i%2==0) { System.out.print(a[i]+" "); } } } } public class Array { public static void main(String[] args) { Derived a = new Derived(); a.even(); } }
a) 2 4 6
b) Compilation Error
c) 1 3 5
d) Runtime Error
View Answer
Explanation: The object ‘a’ is an object of the class Derived, which inherits the properties of the class Base through single inheritance. The for loop checks for even values of ‘i’, and prints the elements at that index. As indexing starts from 0, the nth position corresponds to the (n-1)th index, hence odd numbers are printed.
Output:
$ javac Array.java $ java Array 1 3 5
10. What will be the output of the following Java code?
class Base { int x = 30; } class Derived extends Base { int y = 45; void display() { System.out.println(this); } } public class ClassName { public static void main(String[] args) { Derived a = new Derived(); a.display(); } }
a) 30, 45
b) x = 30, y = 45
c) Derived@2a139a55
d) Base$Derived@2a139a55
View Answer
Explanation: When an object is printed, it is in the format ClassName@HashCode. The object a is an object of the class Derived. Although the class Derived inherits the class Base, it is not a subclass of Base, hence only Derived appears in the ClassName.
Output:
$ javac ClassName.java
$ java ClassName
Derived@2a139a55
11. What will be the output of the following Java code?
class Base { String s = "Sanfoundry"; } class Derived extends Base { Integer y = new Integer(47); void display() { System.out.println(s.getClass().getSuperclass()); System.out.println(y.getClass().getSuperclass()); } } public class ClassName1 { public static void main(String[] args) { Derived a = new Derived(); a.display(); } }
a)
class java.lang.Object class java.lang.Number
b)
class java.lang.String class java.lang.Integer
c)
class java.io.Object class java.io.Number
d)
class java.util.Object class java.util.NumberView Answer
Explanation: String is a subclass of the Object class of the lang package of java, and Integer is a subclass of Number class of the lang package in java. The object a is an object of the Derived class, which extends the class Base, thereby inheriting its properties.
Output:
$ javac ClassName1.java $ java ClassName1 class java.lang.Object class java.lang.Number
12. What will be the output of the following Java code?
class Base { void print() { System.out.println("Base"); } } class Derived extends Base { void print() { super.print(); System.out.println("Derived"); } } public class Super { public static void main(String[] args) { Derived a = new Derived(); a.print(); } }
a)
Derived Base
b) Derived
c) Base
d)
Base DerivedView Answer
Explanation: The super keyword is used to refer to the parent class of the class where it is called. Here, the class Derived extends class Base, hence making it a child class of the Base class. Therefore, the super keyword refers to the Base class, and calls the print function of the base class before printing “Derived”.
Output:
$ javac Super.java $ java Super Base Derived
13. What will be the output of the following Java code?
class Base { int a = 37; } class Derived extends Base { int b = 45; void calculate() { System.out.println(a*b); } } public class Multiply { public static void main(String[] args) { Derived a = new Derived(); a.calculate(); } }
a) 0
b) Compilation Error
c) 1665
d) Runtime Error
View Answer
Explanation: The object ‘a’ is an object of the class Derived, which inherits the properties of class Base, hence obtaining the value of integer variable ‘a’ which results in 1665(37*45) as mentioned in the calculate() function.
Output:
$ javac Multiply.java $ java Multiply 1665
14. What will be the output of the following Java code?
import java.util.*; class Base { LinkedList l = new LinkedList(); void sum() { int sum=0; Iterator i = l.iterator(); while(i.hasNext()) sum+=(int)i.next(); System.out.println(sum); } } class Derived extends Base { void add(int a) { l.add(a); } } public class Sum { public static void main(String[] args) { Derived a = new Derived(); a.add(31); a.add(36); a.add(28); a.add(51); a.sum(); } }
a) Compilation Error
b) 146
c) 129
d) Runtime Error
View Answer
Explanation: The object ‘a’ is an object of the class Derived, which inherits the variables and functions of the parent class, Base. The add() function adds an element to the end of the linked list, and the iterator is used to iterate through the linked list.
Output:
$ javac Sum.java $ java Sum 146
15. What will be the output of the following Java code?
class Base { String s = "San"; } class Derived extends Base { String s = "foundry"; void print() { System.out.println(super.s+s); } } public class Sanfoundry { public static void main(String[] args) { Derived a = new Derived(); a.print(); } }
a) SanSan
b) foundryfoundry
c) Sanfoundry
d) Compilation Error
View Answer
Explanation: The class Derived inherits the properties of the class Base through single inheritance. The super keyword is used to refer to the parent class, and hence super. The variable ‘s’ corresponds to the string variable ‘s’ of the parent class Base. Hence the string “Sanfoundry” is printed.
Output:
$ javac Sanfoundry.java
$ java Sanfoundry
Sanfoundry
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.