Inheritence(Java Program ) Questions and Answers

Question 1. Which is true?
  1.    "X extends Y" is correct if and only if X is a class and Y is an interface
  2.    "X extends Y" is correct if and only if X is an interface and Y is a class
  3.    "X extends Y" is correct if X and Y are either both classes or both interfaces
  4.    "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces
Explanation:-
Answer: Option C. -> "X extends Y" is correct if X and Y are either both classes or both interfaces
A is incorrect because classes implement interfaces, they don't extend them. B is incorrect because interfaces only "inherit from" other interfaces. D is incorrect based on the preceding rules.

Question 2. What is the result of compiling and running the following code?
class Base{
public Base(){
System.out.print("Base");
}
}
public class Derived extends Base{
public Derived(){
this("Examveda");
System.out.print("Derived");
}
public Derived(String s){
System.out.print(s);
}
public static void main(String[] args){
new Derived();
}
}
  1.    ExamvedaDerived
  2.    ExamvedaBaseDerived
  3.    BaseExamvedaDerived
  4.    ExamvedaDerivedBase
  5.    Compilation Error
Explanation:-
Answer: Option C. -> BaseExamvedaDerived
1. new Derived(); statement executes and invoke the non-parametrized constructor of derived class i.e.
public Derived();
2. As Derived class is a subclass of class Base so super(); executes and calls the super class constructor and prints "Base".
3. After that
this("Examveda"); executes and call the parametrized constructor
public Derived(String s); of Derived class as this always refer to the current object. So, it prints "Examveda".
4. Lastly the print statement executes and prints "Derived"
Hence output is BaseExamvedaDerived.

Question 3. What is the output of the following program code?
abstract class C1{
public C1(){
System.out.print(1);
}
}
class C2 extends C1{
public C2(){
System.out.print(2);
}
}
class C3 extends C2{
public C3(){
System.out.println(3);
}
}
public class Test{
public static void main(String[] a){
new C3();
}
}
  1.    12
  2.    23
  3.    123
  4.    321
Explanation:-
Answer: Option C. -> 123

Question 4. The concept of multiple inheritance is implemented in Java by
I.   Extending two or more classes.
II.  Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
  1.    Only (II)
  2.    (I) and (II)
  3.    (II) and (III)
  4.    Only (I)
  5.    Only (III)
Explanation:-
Answer: Option C. -> (II) and (III)

Question 5. Which of the following is true?
1. A class can extend more than one class.
2. A class can extend only one class but many interfaces.
3. An interface can extend many interfaces.
4. An interface can implement many interfaces.
5. A class can extend one class and implement many interfaces.
  1.    1 and 2
  2.    2 and 4
  3.    3 and 5
  4.    3 and 4
  5.    2 and 5
Explanation:-
Answer: Option C. -> 3 and 5

Question 6. Determine output:
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. }
  1.    Name B
  2.    Name C
  3.    Compilation fails due to an error on lines 5
  4.    Compilation fails due to an error on lines 9
  5.    None of these
Explanation:-
Answer: Option C. -> Compilation fails due to an error on lines 5
Reference variable can refer to any object of the same type as the declared reference OR can refer to any subtype of the declared type. Reference variable "b" is type of class B and reference variable "c" is a type of class C. So Compilation fails.

Question 7. What will be the result of compiling and executing the following program code?
class Vehicle{
public void printSound(){
System.out.print("vehicle");
}
}
class Car extends Vehicle{
public void printSound(){
System.out.print("car");
}
}
class Bike extends Vehicle{
public void printSound(){
System.out.print("bike");
}
}
public class Test{
public static void main(String[] args){
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
  1.    Compilation fails.
  2.    ClassCastException exception is thrown at runtime.
  3.    "vehiclecar" is printed.
  4.    "vehiclebike" is printed.
  5.    "carcar" is printed.
Explanation:-
Answer: Option B. -> ClassCastException exception is thrown at runtime.

Question 8. Determine output:
class Small{
public Small(){
System.out.print("a ");
}
}
class Small2 extends Small{
public Small2(){
System.out.print("b ");
}
}
class Small3 extends Small2{
public Small3(){
System.out.print("c ");
}
}
public class Test{
public static void main(String args[]){
new Small3();
}
}
  1.    a
  2.    c
  3.    a b c
  4.    c b a
  5.    The code runs without output..
Explanation:-
Answer: Option C. -> a b c

Question 9. What is the output for the below code ?
class A{
private void printName(){
System.out.println("Value-A");
}
}
class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args){
B b = new B();
b.printName();
}
}
  1.    Value-A
  2.    Name-B
  3.    Value-A Name-B
  4.    Compilation fails - private methods can't be override
  5.    None of these
Explanation:-
Answer: Option B. -> Name-B
You can not override private method , private method is not availabe in subclass . In this case printName() method a class A is not overriding by the printName() method of class B. printName() method of class B is a different method. So you can call printName() method of class B.

Question 10. What will be the result of compiling and running the given code?
class A{
int b=10;
private A(){
this.b=7;
}
int f(){
return b;
}
}
class B extends A{
int b;
}
public class Test{
public static void main(String[] args){
A a = new B();
System.out.println(a.f());
}
}
  1.    Compilation Fails
  2.    Prints 0
  3.    Prints 10
  4.    Prints 7
  5.    None of these
Explanation:-
Answer: Option A. -> Compilation Fails
Choice A is the correct answer.
The code does not compile because the constructor of class A is declared as private. This creates a problem when the subclass constructor makes an implicit super() call to the parent class constructor at the time B is instantiated.
Since the code does not compile, all the other choices are incorrect. If the constructor of A had not been private, the output would have been 7.