Working with Objects | Classes - Quiz Explanation


The answers you selected are indicated below, along with the text that explains the correct answers.
1. A class is identified as the child of a parent class by using:
Please select the best answer.
  A. The class keyword
  B. The extends keyword
  C. The child keyword
  The correct answer is B. A class is identified as the child of a parent class by using the extends keyword. The class keyword is used to define a class. There is no child keyword in Java.


2. From the perspective of the Java compiler, what distinguishes two overloaded methods from each other?
Please select the best answer.
  A. Their names
  B. Their bodies
  C. Their parameters
  The correct answer is C. From the perspective of the Java compiler, two overloaded methods are distinguished by their parameters. The names of overloaded methods are always the same. The Java compiler doesn't use method bodies as a means of distinguishing methods.


3. What is unique about the default constructor for a class compared to other constructors for the class?
Please select the best answer.
  A. It always returns a type void.
  B. It is named the same as the class in which it appears.
  C. It takes no parameters.
  The correct answer is C. What makes the default constructor for a class unique is the fact that it takes no parameters. The return type of the default constructor does not necessarily make it unique as compared to other constructors. All constructors are named the same as the class in which they appear.

4. Given the Planet class, identify the code to instantiate a Planet object with the name "Jupiter" and the color "Color.red".
import java.awt.Color;
public class Planet {
  String name;
  Color  color;
  public Planet(String n, Color c) {
    name  = n;
    color = c;
  }
}

Please select the best answer.
  A. Planet p = Planet("Jupiter", Color.red);
  B. Planet = new Planet("Jupiter", Color.red);
  C. Planet p = new Planet("Jupiter", Color.red);
  C is the correct answer. A is missing the "new" keyword. B is missing the variable. D is incorrect because there is no constructor matching Planet ().

5. What does a class that is derived from another class inherit from the parent class?
Please select the best answer.
  A. Properties only
  B. Methods only
  C. Properties and methods
  The correct answer is C. A class that is derived from another class inherits properties and methods from the parent class.

6. If a class doesn't explicitly declare a parent class:
Please select the best answer.
  A. It derives from the Default class
  B. It derives from the Object class
  C. It doesn't derive from any class
  The correct answer is B. If a class doesn't explicitly declare a parent class, they are implicitly derived from the Object class. All classes derive from the Object class if they don't explicitly derive from some other class. There is no class named Default in the Java API.

7. Replacing an inherited method with a newer version is known as:
Please select the best answer.
  A. Overloading the method
  B. Overriding the method
  C. Overwriting the method
  The correct answer is B. Replacing an inherited method with a newer version is known as overriding the method. Overloading a method involves creating multiple methods with the same name but different parameters. There is no such process in Java as overwriting a method.