ANSWERS
In an accounting application, you discover several places where the total profit, a double
value, is calculated. Which of the following should be done to improve the program
design?
I. The next time the total profit is calculated, use copy and paste to avoid making coding
errors.
II. Provide the same comment every time you repeat the total profit calculation.
III. Consider writing a method that returns the total profit as a double value. - Answer- III
What can be used as an argument in a method call?
I. A variable
II. An expression
III. Another method call that returns a value
IV. Another method call that has no return value - Answer- I, II and III
A programmer notices that the following code snippet uses the same algorithm for
computing interest earned, but with different variables, in the two places shown below
and in several other places in the program. What could be done to improve the
program?
final double RATE1 = 10;
final double RATE2 = 5.5;
double interest = investment * RATE;
...
balance = balance + balance * RATE; - Answer- Define a method that computes
the interest earned from an amount and a rate of interest.
You need to write a method that calculates the volume for a shape, which depends on
the shape's length, width, and height. What should be the parameter variables and their
data types for this method? - Answer- double width, double length, double height
Which of the following is true about method return statements?
a) A method can hold multiple return statements, but only one return statement
executes in one method call.
b) A method can hold only one return statement.
, c) A method can hold multiple return statements, and multiple return statements can
execute in one method call.
d) A method can have maximum of two return statements. - Answer- a) A method can
hold multiple return statements, but only one return statement executes in one method
call.
A programmer notices that the following code snippet uses the same algorithm for
computing cost after taxes, but with different variables, in the two places as shown
below, and in several other places in the program. What could be done to improve the
program?
final double TAXRATE1 = 10;
final double TAXRATE2 = 5.5;
double subtotal = price * (1 + TAXRATE1) / 100;
double total = subtotal + shipping * (1 + TAXRATE2) / 100;
a) Declare the tax rates as variables, not constants.
b) Define a method that looks up tax rates for goods and shipping charges.
c) Define a method that prompts the user for an amount and a tax rate, then returns the
total amount including the tax.
d) Define a method that computes the cost after taxes from arguments for the cost
before taxes and the tax rate. - Answer- Define a method that computes the cost after
taxes from arguments for the cost before taxes and the tax rate.
What process helps with identifying the methods that make up a computer program? -
Answer- Stepwise refinement
The term "Black Box" is used with methods because - Answer- Only the specification
matters; the implementation is not important.
One advantage of designing methods as black boxes is that - Answer- many
programmers can work on the same project without knowing the internal implementation
details of methods.
After the keywords "public static", what are the names (in order) of the parts of this
method header? - Answer- Return type, method name, parameter variable type,
parameter variable name
Parameter variables should not be changed within the body of a method because -
Answer- It is confusing because it mixes the concept of a parameter with that of a
variable
What statement about the steps for implementing a method is true? - Answer- Unit
testing (testing in isolation) of the implemented method is an important final step