Module 1: Getting started with Java

Knowledge check: Concrete and abstract

Practice Assignment

1. Which of the following declarations of an abstract method is correct?

  • public abstract void myMethod();

  • abstract public void myMethod();
  • abstract myMethod();
  • public void abstract myMethod();

2. Which two of the following statements about the abstract method are true?

  • You can have an abstract class with all the methods as non-abstract.

  • All the methods in an abstract class must be abstract.
  • A subclass of an abstract class can override concrete methods.

  • The abstract methods must be declared as public.

3. Which of the following statements is true of an abstract class?

  • An abstract class can extend another abstract class.
  • You can overload an abstract method in a concrete class.
  • An abstract class can not implement an interface.
  • An interface can extend any class, including an abstract class.

4. A class with at least one abstract method must be declared an abstract class.

  • True

  • False

5. You’re developing an app for a safari park that teaches users about animals. Your Java code contains an abstract class and an interface as follows:

You need to extend the Animal class to include a Lion as an object. Which of the following statements is a correct declaration of the Lion class?:

  • class Lion implements Animal extends Movable
  • class Lion implements Movable extends Animal
  • class Lion extends Animal implements Movable

  • class Lion extends Movable implements Animal

Knowledge check: Class hierarchy

Practice Assignment

6. Which one of the following elements do you require when designing a class hierarchy?

  • You need either an interface or an abstract class.
  • You need an abstract class.
  • You need an interface as well as an abstract class.
  • You do not need an interface or an abstract class.

7. What does an IS-A type of relationship between two classes indicate?

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Composition

8. Which three of the following techniques does Java support?

  • Method overriding

  • Multiple inheritance
  • Method overloading
  • Dynamic polymorphism

9. True or False: A class hierarchy can contain abstract and concrete classes.

  • True

  • False

10. What is the following code block an example of?

  • Abstraction
  • Two unrelated classes.
  • IS-A relationship
  • Composition

Knowledge check: Multiple inheritance

Practice Assignment

11. You are helping to develop an application to handle the collection of student fees and salaries of the college employees. While doing this, you learned that Java doesn't support subclassing from multiple classes and that it uses interfaces to achieve multiple inheritance. In this context, consider the following statements:

1. Interfaces define a class hierarchy.

2. One or more interfaces can be implemented by a class.

3. Interfaces help in achieving multiple inheritance.

Choose the correct option from the following:

  • Statements 1 and 2 are true.
  • Statements 2 and 3 are true.

  • Only statement 3 is true.
  • All three statements are true

12. True or false: An interface includes both abstract and concrete methods.

  • True
  • False

13. To establish multiple inheritance in Java, you should:

  • Extend multiple superclasses.
  • Implement multiple interfaces.
  • Extend a single superclass and implement one or more interfaces.

  • All of the above statements are true.

14. Consider the following four statements. Which of them is true?

  • An interface can only have abstract methods.

  • An interface cannot have instance variables.
  • Methods in an interface cannot have a return type.
  • An Interface can have concrete methods.

15. Which of the following is not a common feature of an abstract class and an interface?

  • Both may be extended by a subclass.
  • Both may contain concrete methods.
  • You cannot instantiate either.

  • Both may be implemented by a subclass.

Module quiz: Advanced OOP concepts

Graded Assignment

16. Which of these phrases can complete the sentence: “In Java, the interface - - - .” Select all that apply.

  • can extend another interface.

  • can implement another interface.
  • should be declared with abstract keyword.
  • can not be instantiated.

17. Consider the following code. Based on this code, what relationship exists between the Student and Teacher classes? Select all that apply.

  • IS-A relationship.
  • Inheritance relationship.
  • Composition relationship.
  • HAS-A relationship.

18. Complete the following sentence. “As in Java 8, an interface can have — .” Select all that apply.

  • private methods.
  • default methods.

  • abstract methods.
  • static methods.

19. Why can an interface extend another interface, but it cannot implement another interface?

  • Because interfaces are contracts to be fulfilled by a class.

  • Because interfaces cannot be instantiated.
  • Because interfaces are abstract.
  • Because interfaces don’t have attributes.

20. Which of the following statements is true about an abstract class?

  • An abstract class can implement an interface.
  • Concrete methods in an abstract class must be overridden by its subclass.
  • All the methods in it must be abstract.
  • An abstract class may have instance variables and abstract as well as concrete methods.

21. Which of the following are the correct signatures of an abstract method? Select all that apply.

  • void method();
  • public static abstract void method();
  • abstract void method();

  • abstract void method();

22. When a class extends an abstract class, which of the following statements is correct?

  • It must not override the concrete methods.
  • It need not override any method.
  • It must override the abstract methods.

  • It must override all the methods.

23. Which of the following is the main difference between an abstract method and a concrete method?

  • A concrete method has an implementation, while an abstract method only has a declaration.

  • Concrete methods always have a return value, while abstract methods are always void.
  • An abstract method can only be defined in an interface, while a concrete method can only be in a regular class.
  • Concrete methods are always final, while abstract methods must be overridden.

24. What are the similarities between abstract class and interface? Select all that apply

  • Both promote code reusability.

  • Both support multiple inheritance.
  • Both cannot be instantiated.

  • Both can extend and implement one or more interfaces.

25. True or False: Class hierarchies do not have interfaces.

  • True
  • False

Leave a Reply