Object Characteristics - Quiz Explanation
The correct answers are indicated below, along with the text that explains the correct answers.
1.
In an earlier lesson, you learned that
StatementPrinter
is not a good name for a class. Which of the suggestions below would be better?
Please select the best answer.
A.
A general
Printer
class that prints passbooks, statements, and a variety of other reports
B.
A
Statement
class with a responsibility to print itself
C.
A
CheckingAccount
class with a responsibility to print a statement each month
D.
A global function that will print a statement for any
CheckingAccount
object
The correct answer is C.
The information that is on a monthly statement is all kept in the
CheckingAccount
class. A is incorrect because the proposed
Printer
class would mix up business rules and information for many different types of accounts rather than encapsulating each individually. B is incorrect because a
Statement
class wouldn't hold anything different than the
CheckingAccount
class. D is incorrect because global functions are not object oriented, and the code relating to
CheckingAccount
information belongs inside
CheckingAccount
.
2.
Which one of these characteristics defines a problem that can be best addressed with a structured programming approach?
Please select the best answer.
A.
The problem involves a simulation of a process.
B.
The problem involves user-interface-intensive operations.
C.
The problem involves events that do not occur at precisely defined points.
D.
The problem involves a well-defined flow of control.
The correct answer is D.
The existence of a well-defined flow of control is a defining factor in selecting a structured (or procedural) approach. A, B, and C all represent problems with nonlinear, widely varying inputs that can occur in an almost limitless number of random patterns, a defining characteristic or problem that needs an object-oriented approach.
3.
If you are working with a system that already has a
Date
class, which of the following statements best illustrates encapsulation for that class?
Please select the best answer.
A.
You can change only the whole date, and not just one part of it at a time.
B.
You do not know what the internal variables are, but you know what a
Date
class can do.
C.
You know the internal variables and you know they will not be changed in the future.
D.
You can view the source of the
Date
class to decide if you should reuse it.
The correct answer is B.
Encapsulation frees programmers from having to know the internals of every class. A is incorrect because it may be possible to set a month or day in isolation: That's the choice of the
Date
developer. C is incorrect because encapsulation frees programmers to change the internals of their class, because no other code is relying on those internals. D is incorrect because reuse decisions and access to source code are not related to encapsulation.
4.
Still thinking about a
Date
class someone else has written, which of these statements best illustrates encapsulation?
Please select the best answer.
A.
When you reuse the
Date
class, it is encapsulated into your program.
B.
The developer who uses the
Date
class will implement whatever date rules (days per month, leap years, etc.) are appropriate for the system.
C.
Because all the variables about a date are encapsulated into one object, the program will execute more quickly.
D.
All the rules about dates (days per month, leap years, etc.) are encapsulated in the
Date
class.
The correct answer is D.
Encapsulation refers to any kind of gathering together, such as gathering all the date logic into a single class. A is incorrect because the act of reuse is not referred to as encapsulation. B is incorrect because the developer will not provide date rules: They are part of the class. C is incorrect because encapsulation does not have a direct effect on execution speed.