Java Questions & Answers – Inheritance

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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 {}
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: a
Explanation: If one is extending any class, then they should use extends keyword not implements.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
  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
View Answer

Answer: c
Explanation: 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:

advertisement
$ 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.