Java Inheritance
Multiple Choice Questions (MCQs)
   1. Which of the following is true about inheritance in Java?
A) A class can inherit from multiple classes
B) A class can inherit from only one class
C) Interfaces cannot be inherited
D) Inheritance is optional in Java
Answer: B
   2. What is the correct syntax for making a class inherit from another class?
A) class B inherits A
B) class B implements A
C) class B extends A
D) class B extends implements A
Answer: C
   3. In Java, what is the base class of all classes?
A) Base
B) Parent
C) Object
D) Class
Answer: C
   4. Which keyword is used to call the parent class constructor?
A) super()
B) this()
C) parent()
D) base()
Answer: A
   5. Which of the following statements is NOT true about inheritance in Java?
A) A subclass can override a method of its parent class
B) A subclass can access private members of its superclass
C) A subclass can have additional fields and methods
D) The subclass constructor can invoke the superclass constructor
Answer: B
    6. Which of the following is true about an interface in Java?
A) An interface can contain method bodies.
B) An interface can extend multiple classes.
C) An interface can extend multiple interfaces.
D) An interface cannot extend another interface.
        Answer: C
    7. Which of the following statements is true about methods in an interface?
A) All methods in an interface are private by default.
B) All methods in an interface are public and abstract by default.
C) An interface cannot have any methods.
D) Methods in an interface must always be static.
        Answer: B
    8. Can an interface in Java contain fields? If yes, what is the default type of these fields?
A) Yes, fields in an interface are private by default.
B) Yes, fields in an interface are protected by default.
C) Yes, fields in an interface are public, static, and final by default.
D) No, interfaces cannot contain fields.
        Answer: C
    9. What happens if a class does not provide implementations for all methods of an
       interface it implements?
A) The program will run with a warning.
B) The class must be declared abstract.
C) The program will throw a runtime exception.
D) The class will inherit the methods from Object.
        Answer: B
    10. What will happen if two interfaces provide the same default method, and a class
        implements both interfaces without overriding the method?
A) The code will compile without errors.
B) The code will not compile due to ambiguity.
C) The class will inherit the method from the first interface only.
D) The class will inherit the method from the second interface only.
        Answer: B
Assertion-Reasoning Questions
Question 11.
Assertion (A): A subclass inherits both the members and methods of the superclass.
Reason (R): Inheritance in Java allows a class to be derived from only one class.
    A)   A is true, and R is true, and R is the correct explanation of A
    B)   A is true, and R is true, and R is not the correct explanation of A
    C)   A is true, but R is false
    D)   A is false, but R is true
Answer: A is true, and R is true, and R is the correct explanation of A
Question 12.
Assertion (A): In Java, a subclass can override a static method of the superclass.
Reason (R): Static methods are not part of the instance of the class and belong to the class
itself.
    A)   A is true, and R is true, and R is the correct explanation of A
    B)   A is true, and R is true, and R is not the correct explanation of A
    C)   A is true, but R is false
    D)   A is false, but R is true
Answer: A is false, but R is true
Question 13.
Assertion (A): Inheritance helps in code reusability.
Reason (R): When a subclass inherits from a superclass, it gains access to the superclass’s
members and methods.
    A)   A is true, and R is true, and R is the correct explanation of A
    B)   A is true, and R is true, and R is not the correct explanation of A
    C)   A is true, but R is false
    D)   A is false, but R is true
Answer: A is true, and R is true, and R is the correct explanation of A
Question 14.
Assertion (A): Private members of a class are inherited by the subclass.
Reason (R): Private members cannot be directly accessed by the subclass.
    A)    A is true, and R is true, and R is the correct explanation of A
    B)    A is true, and R is true, and R is not the correct explanation of A
    C)    A is true, but R is false
    D)    A is false, but R is true
Answer: A is false, but R is true
Question 15.
Assertion (A): A subclass can override the non-final methods of its superclass in Java.
Reason (R): Final methods cannot be overridden in Java to ensure they remain unchanged.
    A)    A is true, and R is true, and R is the correct explanation of A
    B)    A is true, and R is true, and R is not the correct explanation of A
    C)    A is true, but R is false
    D)    A is false, but R is true
Answer: A is true, and R is true, and R is the correct explanation of A
Complete the Code
class A
{
          int m;
          A(int mm)
          {
                  m=mm;
          }
          void display()
          {
          System.out.print(“M=”+m);
          }
}
class B ?....1…..? A
{
        double x;
        int m;
          B(?.....2……?)
          {
                   super(p);
                   x=q;
                   m=r;
          }
          void display()
        {
                ?......3…..?.display();
                System.out.print(“X=”+x+”M=”+?.......4…..?); // statement is to print the value
                                                             // of instance variable of sub-
                                                             // class.
        }
}
Question 16.
?....1….?
      A) super
      B) implements
      C) extends
      D) inherit
Question 17.
?....2….?
      A) No parameters
      B) int p, int r, double q
      C) double p, int r, int q
      D) double p, double r, int q
Question 18.
?....3….?
      A) super
      B) implements
      C) extends
      D) this
Question 19.
?....4….?
      A) m
      B) super.m
      C) can not print m
      D) super().m
Question 20.
abstract class Animal {
  abstract void sound();
}
class Cat extends Animal {
  ______ void sound() {
    System.out.println("Cat meows");
  }
}
// Main method
public class Main {
  public static void main(String[] args) {
    Cat c = new Cat();
    c.sound();
  }
}
   A)   super
   B)   abstract
   C)   public
   D)   void
Question 21.
Define a class Faculty with the following specifications:
   ● Data members:
        o name – to store the name of the faculty
        o basic – to store the basic salary of the faculty
   ● Member functions:
        o Faculty() – Default constructor
        o Faculty(String n, double b) – Parameterized constructor to assign values
           to the data members
        o void display() – To display the name and basic salary
Design another class Teacher which inherits the class Faculty with the following
specifications:
   ●    Data members:
           o   subject – to store the subject taught by the teacher
           o   allowance – to store the allowance (15% of basic)
           o   totalSalary – to store the total salary (basic + allowance)
   ●    Member functions:
          o Teacher() – Default constructor
          o Teacher(String n, double b, String s) – Parameterized constructor to
             assign values to data members and calculate the total salary
          o void display() – To display all the details of the teacher
Using the concept of inheritance, implement the class Teacher.