Chapter 10: Programming Quiz

25 July 2022
4.7 (114 reviews)
29 test answers

Unlock all answers in this set

Unlock answers (25)
question
When a class member is declared as protected, what code may access it?
answer
Protected members of class may be accessed by methods in a subclass, and by methods in the same package as the class.
question
What is the difference between private members and protected members?
answer
Private members may be accessed only by methods in the same class. A protected member of a class may be directly accessed by methods of the same class or methods of a subclass. In addition, protected members may be accessed by methods of any class that are in the same package as the protected member's class.
question
Why should you avoid making class members protected when possible?
answer
Because any other class that inherits from the class, or is in the same package, has unrestricted access to the protected member.
question
What is the difference between private access and package access?
answer
Private members may be accessed only by methods in the same class. Any method in the same package as the class may directly access the class's members that have package access.
question
Why is it easy to give package access to a class member by accident?
answer
When you accidentally leave out the access specifier, the member will have package access.
question
What is a class hierarchy?
answer
A graphical representation of inheritance relationships between classes.
question
Describe the placement of more general and more specialized classes in a class hierarchy.
answer
The more general classes are drawn toward the top of the tree and the more specialized classes are drawn toward the bottom of the tree.
question
In an inheritance relationship, this is the general class.
answer
The superclass
question
In an inheritance relationship, this is the specialized class.
answer
The subclass
question
This key word indicates that a class inherits from another class.
answer
extends
question
A subclass does not have access to these superclass members.
answer
private
question
This key word refers to an object's superclass
answer
super
question
In a subclass constructor, a call to the superclass constructor must __________.
answer
Appear as the very first statement
question
The following is an explicit call to the superclass's default constructor.
answer
super();
question
A method in a subclass that has the same signature as a method in the superclass is an example of _________.
answer
overriding
question
What is "Inheritance"?
answer
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends.
question
Describe the "Is a" relationship.
answer
When one object is a specialized version of another object, there is an "is a" relationship between them.
question
What are the "superclass" and "subclass"?
answer
The superclass is the general class and the subclass is the specialized class. The subclass inherits fields and methods form the superclass without any of them having to be rewritten. New fields and methods may be added to the subclass to make it a specialized version of the superclass.
question
Can a subclass access members of a superclass?
answer
Yes
question
Can a superclass access members of a subclass?
answer
No
question
Why would a superclass need a no-arg constructor?
answer
So that the constructor can be called automatically whenever a subclass is created.
question
To what does the "super" key word refer?
answer
It refers to the object's superclass
question
Does the "super" key word work only with the superclass constructors? Please explain.
answer
The "super" keyword can be used to call the superclass constructors and can also be used to access superclass members.
question
Summarize the three guidelines for calling a superclass constructor.
answer
1. The super constructor may only be called in a subclass constructor. 2. The super statement that calls the superclass constructor must be called on the first line. 3. If the subclass constructor does not explicitly call the superclass constructor, Java will automatically call the default or no-arg constructor before the sub-class constructor.
question
What is "overriding" ?
answer
The term overriding is when the subclass replaces inadequate super class methods with more suitable once.
question
What is a method signature?
answer
The method's signature consists of the method's name and the data type of the methods parameters in the order that they appear.
question
What is the @Override annotation and why is it a good idea to use it?
answer
It tells the compiler that a method is intended to override a superclass method. It is a good idea to use it so that any errors in properly overriding the method are displayed.
question
What is the difference between "Overriding" and "Overloading" a method?
answer
Overloaded methods have the same name but different signatures. Overridden methods both have the same signature.
question
How can you prevent a method from being overridden?
answer
Add the "final" modifier to the method header.