Java Questions & Answers – Inheritance – Abstract Class and Super

This section of our 1000+ Java MCQs focuses on Abstract class in Java Programming Language.

1. Which of these keywords are used to define an abstract class?
a) abst
b) abstract
c) Abstract
d) abstract class
View Answer

Answer: b
Explanation: None.

2. Which of these is not abstract?
a) Thread
b) AbstractList
c) List
d) None of the Mentioned
View Answer

Answer: a
Explanation: Thread is not an abstract class.

3. If a class inheriting an abstract class does not define all of its function then it will be known as?
a) Abstract
b) A simple class
c) Static class
d) None of the mentioned
View Answer

Answer: a
Explanation: Any subclass of an abstract class must either implement all of the abstract method in the superclass or be itself declared abstract.
advertisement
advertisement

4. Which of these is not a correct statement?
a) Every class containing abstract method must be declared abstract
b) Abstract class defines only the structure of the class not its implementation
c) Abstract class can be initiated by new operator
d) Abstract class can be inherited
View Answer

Answer: c
Explanation: Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition of implementation it is not possible to create an abstract object.

5. Which of these packages contains abstract keyword?
a) java.lang
b) java.util
c) java.io
d) java.system
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

  1.     class A 
  2.     {
  3.         public int i;
  4.         private int j;
  5.     }    
  6.     class B extends A 
  7.     {
  8.         void display() 
  9.         {
  10.             super.j = super.i + 1;
  11.             System.out.println(super.i + " " + super.j);
  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) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
output:

advertisement
$ javac inheritance.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The field A.j is not visible

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

advertisement
  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

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

  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 method_overriding 
  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:

$ javac method_overriding.java
$ java method_overriding
2

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

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

a) 1 2
b) 2 1
c) 1 3
d) 3 1
View Answer

Answer: a
Explanation: Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:

$ javac Output.java
$ java Output
1 2

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.