250+ TOP MCQs on Inheritance – 1 and Answers

Java MCQs on Inheritance of Java Programming Language.

1. Which of this keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends

Answer: d
Clarification: None.

2. A class member declared protected becomes a member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member

Answer: b
Clarification: A class member declared protected becomes a private member of subclass.

3. Which of these is correct way of inheriting class A by class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}

Answer: c
Clarification: None.

4. Which two classes use the Shape class correctly?

A. public class Circle implements Shape 
   {
    private int radius;
   }
B. public abstract class Circle extends Shape 
   {
    private int radius;
   }
C. public class Circle extends Shape 
   {
   private int radius;
   public void draw();
   }
D. public abstract class Circle implements Shape 
   {
    private int radius;
    public void draw();
   }
E. public class Circle extends Shape 
   {
    private int radius;
    public void draw()
    {
     /* code here */
    }
   }
F. public abstract class Circle implements Shape 
   {
     private int radius;
     public void draw() 
     { 
      /* code here */ 
     }
   }

a) B,E
b) A,C
c) C,E
d) T,H

Answer: a
Clarification: If one is extending any class, then they should use extends keyword not implements.

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

  1.     class A 
  2.     {
  3.         int i;
  4.         void display() 
  5.         {
  6.             System.out.println(i);
  7.         }
  8.     }    
  9.     class B extends A 
  10.     {
  11.         int j;
  12.         void display() 
  13.         {
  14.             System.out.println(j);
  15.         }
  16.     }    
  17.     class inheritance_demo 
  18.     {
  19.         public static void main(String args[])
  20.         {
  21.             B obj = new B();
  22.             obj.i=1;
  23.             obj.j=2;   
  24.             obj.display();     
  25.         }
  26.    }

a) 0
b) 1
c) 2
d) Compilation Error

Answer: c
Clarification: Class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
output:

$ javac inheritance_demo.java 
$ java inheritance_demo 
2

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

  1.     class A 
  2.     {
  3.         int i;
  4.     }    
  5.     class B extends A 
  6.     {
  7.         int j;
  8.         void display() 
  9.         {
  10.             super.i = j + 1;
  11.             System.out.println(j + " " + i);
  12.         }
  13.     }    
  14.     class inheritance 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.i=1;
  20.             obj.j=2;   
  21.             obj.display();     
  22.         }
  23.    }

a) 2 2
b) 3 3
c) 2 3
d) 3 2

Answer: c
Clarification: None
output:

$ javac inheritance.java
$ java inheritance
2 3

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

  1.     class A 
  2.     {
  3.         public int i;
  4.         public int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9. 	}
  10.     }    
  11.     class B extends A 
  12.     {
  13.         int a;
  14.         B() 
  15.         {
  16.             super();
  17.         } 
  18.     }    
  19.     class super_use 
  20.     {
  21.         public static void main(String args[])
  22.         {
  23.             B obj = new B();
  24.             System.out.println(obj.i + " " + obj.j)     
  25.         }
  26.    }

a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

Answer: a
Clarification: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.
output:

$ javac super_use.java
$ java super_use
1 2

Leave a Reply

Your email address will not be published. Required fields are marked *