Module 3: Object-oriented programming basics

Knowledge check: Classes and objects: Making code reusable

Practice Assignment

1. Imagine you're building a game with different characters. You want to create a blueprint for each character that defines their properties (like name, health) and actions (like attack). Which of the following concepts enables you to do this?

  • Objects
  • Loops
  • Variables
  • Classes

2. Let's say you created a Character class with a method to display the character's health. How can you create multiple characters (such as a knight or an archer) with different health values but reuse the same method to display their health?

  • Copy and paste the display health method into separate classes for each character.
  • There’s no way to reuse the method with different health values.
  • Create a single health variable outside the Character class and track which character it belongs to when displaying health.
  • Create objects from the Character class and assign different health values to each object.

3. You designed a Calculator class with a method to add two numbers. How can you easily reuse this class to also perform subtractions?

  • The Calculator class is specifically designed for addition and cannot be reused for subtraction.
  • Use the addition method in a different way to achieve subtraction. For example, to calculate 10 minus 5, you might use the addition method: 10 + (a negative number representing 5).
  • Write a separate method for subtraction within the Calculator class.

  • Duplicate the entire Calculator class and rename it to Subtractor.

4. You're designing a Point class in Java to represent a location in 2D space with x and y coordinates. The class currently has a no-argument constructor (meaning it takes no arguments). You haven't explicitly assigned values within the constructor for the x and y coordinates (type int).

What will happen to the x and y coordinates of a new Point object created using this no-argument constructor?

  • The new Point object will only be created if values are provided during creation.
  • The new Point object will be created with random values for x and y.
  • The new Point object will not be created due to an incomplete constructor.
  • The object will be created with its x and y coordinates set to their default values (0 for integers in Java).

5. True or False: In Java, all methods must return a value.

  • True
  • False

Knowledge check: Inheritance and polymorphism: Super and sub classes

Practice Assignment

6. Which of the following statements about inheritance in Java is accurate?

  • Java allows a subclass to inherit from multiple superclasses.
  • A subclass in Java is not allowed to override methods from its superclass.
  • A superclass in Java can inherit methods from its subclass.
  • A subclass in Java can only inherit from a single superclass.

7. Which of the following statements correctly describes polymorphism in Java?

  • Polymorphism in Java prevents a class from defining multiple methods with the same name.
  • Polymorphism allows a superclass reference to refer to an object of a subclass.

  • Polymorphism allows objects to be treated as instances of their subclass instead of their superclass.
  • Polymorphism in Java applies solely to methods, not to variables.

8. Considering the Java code snippet below, what will be the output?

  • “Some generic animal sound” will be printed.
  • The code will produce runtime errors.
  • “Bark” will be printed.

  • The code will fail to compile.

9. Suppose you are developing a vehicle rental system. You have a Vehicle superclass with a calculateRentalCost() method. Subclasses like Car, Truck, and Motorcycle each implement this method based on different rental rate calculations.

Which of the following best describes how polymorphism is applied in this scenario?

  • Polymorphism requires that all subclasses implement calculateRentalCost() identically.
  • Polymorphism restricts subclasses from adding new methods.
  • Polymorphism mandates that each subclass include unique methods unrelated to the Vehicle class.
  • Polymorphism allows a Vehicle reference to invoke calculateRentalCost() on any subclass, executing the subclass-specific implementation.

10. What is the main purpose of the super keyword in Java?

  • To access superclass methods and variables from within a subclass.

  • To mark a method for overriding in a subclass.
  • To create a new subclass from an existing class.
  • To create multiple instances of a class.

Knowledge check: Encapsulation and abstraction: Enhancing code modularity

Practice Assignment

11. What key characteristics are exhibited by the abstract class Robot in the provided code?

  • Method overloading
  • Interfaces
  • Inheritance
  • Encapsulation

12. Consider the following code, where the Robot class extends the Machine class and implements the Jumping interface:

Given this code, what must the Robot class do to ensure it correctly implements the Jumping interface and fully adheres to Java’s object-oriented principles?

  • The Robot class must implement the Machine class.
  • The Robot class must implement the jumpAround() method.

  • The Robot class can choose not to implement the jumpAround() method if it does not use it.
  • The Robot class must make the method drive() public.

13. Consider the following code, which attempts to create an AmphibianAnimal class that inherits properties from both LandAnimal and WaterAnimal:

Identify the issue with this code.

  • The AmphibianAnimal class cannot extend from more than one parent class.

  • The class AmphibianAnimal must implement both classes.
  • The class AmphibianAnimal cannot have the property transferSpeedFromLandToWater as private.
  • WaterAnimal must extend the class LandAnimal before implementing AmphibianAnimal.

14. Which properties of the Parent class can be directly accessed within the Child class? Consider the provided code and select all correct statements.

class Parent { private String privateField; protected String protectedField; public String publicString; }class Child extends Parent { // code of Child class }

  • The field privateField can be accessed directly in Child class.
  • The field privateField can be accessed directly in Parent class.

  • The field publicField can be accessed directly only in Parent class.
  • The field protectedField can be accessed directly in Child class.

15. True or False: The following code will display “Hi humans” and “Bow wow!” because the dogObject is both an Animal and a Dog.

  • True
  • False

Module quiz: Object-oriented programming basics

Graded Assignment

16. True or False: A class is represented by a real-world object like Toni, and an object is used as a blueprint for creating classes.

  • False
  • True

17. True or False: The following two methods have the same signature:

  • False
  • True

18. Given the following classes:

The class Trainee is a specialized version of the Student class that also includes a stipend. What keyword should replace ------ to establish an inheritance relationship between the Trainee class and the Student class?

  • derives
  • extends
  • inherits
  • implements

19. Given the following code:

What should replace ------ to correctly display "A car is a vehicle" using the typeOfVehicle property from the Vehicle class?

  • this.typeOfVehicle
  • typeOfVehicle
  • super.typeOfVehicle

  • “Vehicle”

20. Given the following code:

What are the properties that the class Child can access directly?

  • privateField and publicField.
  • only publicField
  • Protected field and publicField.
  • protectedField and privateField.

21. Which of the following statements about method overriding are true? Select all that apply.

  • The overridden methods must use the @Override annotation.
  • The return type of the overridden method must also be the same as that of the method in the superclass.
  • The access level of the access modifier can be equal to or less restrictive than that of the parent class.
  • The overridden method in the subclass must have the same name and signature as that of the superclass.

22. What is displayed when you run the following code:

  • It displays both “I am an Animal.”and “Animal is sleeping.”
  • It displays only “Animal is sleeping.”
  • It gives a compile time error.
  • It displays both “I am a Duck.”and “Animal is sleeping.”

23. What is displayed when you run the following code:

  • Error during program execution.
  • No output.
  • Bike is moving.

  • Compilation error.

24. Which of the following statements about interfaces and multiple inheritance are correct? Select all that apply.

  • Interfaces can extend other interfaces.

  • A class can inherit from multiple classes and also multiple interfaces.
  • A class can extend multiple interfaces.
  • A class can implement multiple interfaces in Java.

25. Which of the following statements are true? Select all that apply.

  • A subclass can inherit methods from an abstract class, and an object of the subclass can access all the methods defined in the abstract class and implemented interfaces.

  • A class that extends an abstract class must implement all abstract methods of the abstract class and must use the annotation @Overwritten.
  • An abstract class can implement multiple interfaces, provide concrete methods, and can be instantiated directly using the keyword new.
  • Interfaces must provide constructors.

Leave a Reply