Chp06 CheckPointAnswers
Chp06 CheckPointAnswers
1. At least three benefits: (1) Reuse code; (2) Reduce complexity; (3) Easy to maintain.
2. See the Sections 5.2 and 5.3 on how to define and invoke methods.
4. True: a call to a method with a void return type is always a statement itself.
5. void
6. A syntax error occurs if a return statement is not used to return a value in a value-returning
method. You can have a return statement in a void method, which simply exits the method. But a
return statement cannot return a value such as return x + y in a void method.
8. (a) Computing a sales commission given the sales amount and the commission rate
9. Line 2: method1 is not defined correctly. It does not have a return type or void.
Line 11: if (n<0) should be removed in method, otherwise a compile error is reported.
10.
while (i<j) {
j--;
return j;
11. You pass actual parameters by passing the right type of value in the right order. The actual
parameter can have the same name as its formal parameter.
12. Two errors: 1. the arguments are not passed in the right order. 2. n is a parameter, but is
redeclared in the nPrintln method in line 7.
(B)
24
248
2 4 8 16
2 4 8 16 32
2 4 8 16 32 64
(C)
n=3
Welcome to Java!
n=2
Welcome to Java!
n=1
Welcome to Java!
(D)
21
21
421
i is 5
14.
Just before max is Just entering max. Just before max is Right after max is
invoked. returned returned
15. Two methods with the same name, defined in the same class, is called method overloading. It is
fine to have same method name, but different parameter types. You cannot overload methods based on
return type, or modifiers.
16. Methods public static void method(int x) and public static int method(int y) have the same
signature method(int).
17. (a) invokes the second method. (b) invokes the second. (c) invokes the first method.
19. The scope of a local variable starts from its declaration and continues to the end of the block that
contains the variable.