Inheritance in Java MCQ

Answer all questions carefully. After submission, you will see a detailed result and answer review.

Question 1
Which of the following is true? <p>1. A class can extend more than one class.<br />2. A class can extend only one class but many interfaces.<br />3. An interface can extend many interfaces.<br />4. An interface can implement many interfaces.<br />5. A class can extend one class and implement many interfaces.</p>
Question 2
Determine output: <pre class="prettyprint"> class A{ public void printName(){ System.out.println("Name-A"); } } class B extends A{ public void printName(){ System.out.println("Name-B"); } } class C extends A{ public void printName(){ System.out.println("Name-C"); } } 1. public class Test{ 2. public static void main (String[] args){ 3. B b = new B(); 4. C c = new C(); 5. b = c; 6. newPrint(b); 7. } 8. public static void newPrint(A a){ 9. a.printName(); 10. } 11. } </pre>
Question 3
Determine output: <pre class="prettyprint"> class A{ public void printValue(){ System.out.println("Value-A"); } } class B extends A{ public void printNameB(){ System.out.println("Name-B"); } } class C extends A{ public void printNameC(){ System.out.println("Name-C"); } } 1. public class Test{ 2. public static void main (String[] args){ 3. B b = new B(); 4. C c = new C(); 5. newPrint(b); 6. newPrint(c); 7. } 8. public static void newPrint(A a){ 9. a.printValue(); 10. } 11. } </pre>
Question 4
What will be printed after executing following program code? <pre class="prettyprint"> class Base{ int value = 0; Base(){ addValue(); } void addValue(){ value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; } } public class Test{ public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } } </pre>
Question 5
A mechanism where one class acquires the properties of another class:
Question 6
A class member declared protected becomes member of subclass of which type?
Question 7
What is the result of compiling and running this program? <pre class="prettyprint"> class Mammal{ void eat(Mammal m){ System.out.println("Mammal eats food"); } } class Cattle extends Mammal{ void eat(Cattle c){ System.out.println("Cattle eats hay"); } } class Horse extends Cattle{ void eat(Horse h){ System.out.println("Horse eats hay"); } } public class Test{ public static void main(String[] args){ Mammal h = new Horse(); Cattle c = new Horse(); c.eat(h); } } </pre>
Question 8
What is the output of this program? <pre class="prettyprint"> class A { public int i; public int j; A() { i = 1; j = 2; } } class B extends A { int a; B() { super(); } } class super_use { public static void main(String args[]) { B obj = new B(); System.out.println(obj.i + " " + obj.j) } } </pre>
Question 9
What is the output of this program? <pre class="prettyprint">class A { public int i; protected int j; } class B extends A { int j; void display() { super.j = 3; System.out.println(i + " " + j); } } class Output { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } </pre>
Question 10
What will be the output? <pre class="prettyprint"> class A{ int i = 10; public void printValue(){ System.out.print("Value-A"); } } class B extends A{ int i = 12; public void printValue(){ System.out.print("Value-B"); } } public class Test{ public static void main(String args[]){ A a = new B(); a.printValue(); System.out.print(a.i); } } </pre>