Unit 9: Inheritance

Which keyword is used to inherit a class in Java?
a) inherits
b) extends
c) implements
d) super

Answer: b) extends

What is the correct way to call a parent class’s constructor in a subclass?
a) super();
b) parent();
c) base();
d) this();

Answer: a) super();

Which of the following statements about inheritance is true?
a) A subclass can inherit private members of a superclass.
b) A subclass cannot override methods of a superclass.
c) A subclass can only have one superclass.
d) A subclass must have the same name as the superclass.

Answer: c) A subclass can only have one superclass.

Unit 10: Recursion

Which of the following is an example of a recursive method?
a) A method that calls itself
b) A method that calls another method
c) A method that runs indefinitely
d) A method that returns a value

Answer: a) A method that calls itself

What is the base case in a recursive method?
a) The condition under which the method stops calling itself
b) The first call to the recursive method
c) The last call to the recursive method
d) The condition under which the method calls itself

Answer: a) The condition under which the method stops calling itself

What will be the output of the following recursive method when called with factorial(3)?

public int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

a) 6
b) 3
c) 0
d) 1

Answer: a) 6