Chapter 6 Methods
Sections 6.2 Defining a Method
1. Suppose your method does not return any value, which of the following keywords can be used as a return type?
a. void
b. int
c. double
d. public
e. None of the above
#
2. The signature of a method consists of .
a. method name
b. method name and parameter list
c. return type, method name, and parameter list
d. parameter list
#
3. All Java applications must have a method .
a. public static Main(String[] args)
b. public static Main(String args[])
c. public static void main(String[] args)
d. public void main(String[] args)
e. public static main(String[] args)
#
Sections 6.3 Calling a Method
4. Arguments to methods always appear within .
a. brackets
b. parentheses
c. curly braces
d. quotation marks
#
6. Does the method call in the following method cause compile errors?
public static void main(String[] args) {
Math.pow(2, 4);
}
a. Yes
b. No
#
7. Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as
, which stores elements in lastin first-out fashion.
a. a heap
b. storage area
c. a stack
d. an array
#
Sections 6.4 void vs. ValueReturning Methods
8. Which of the following should be defined as a void method?
a. Write a method that prints integers from 1 to 100.
b. Write a method that returns a random integer from 1 to 100.
c. Write a method that checks whether a number is from 1 to 100.
d. Write a method that converts an uppercase letter to lowercase.
#
9. You should fill in the blank in the following code with .
public class Test {
public static void main(String[] args) {
System.out.print(“The grade is “);
printGrade(78.5);
System.out.print(“The grade is “);
printGrade(59.5);
}
public static printGrade(double score) {
if (score >= 90.0) {
System.out.println(‘A’);
}
else if (score >= 80.0) {
System.out.println(‘B’);
}
else if (score >= 70.0) {
System.out.println(‘C’);
}
else if (score >= 60.0) {
System.out.println(‘D’);
}
else {
System.out.println(‘F’);
}
}
}
a. int
b. double
c. boolean
d. char
e. void
#
10. You should fill in the blank in the following code with .
public class Test {
public static void main(String[] args) {
System.out.print(“The grade is + getGrade(78.5));
System.out.print(“\nThe grade is + getGrade(59.5));
}
public static getGrade(double score) {
if (score >= 90.0)
return ‘A’;
else if (score >= 80.0)
return ‘B‘;
else if (score >= 70.0)
return ‘C‘;
else if (score >= 60.0)
return ‘D’;
else
return ‘F’;
}
}
a. int
b. double
c. boolean
d. char
#
5. Does the return statement in the following method cause compile errors?
public static void main(String[] args) {
int max = 0;
if (max != 0)
System.out.println(max);
else
return;
}
a. Yes
b. No
#
11.
Consider the following incomplete code. The missing method body should be .
public class Test {
public static void main(String[] args) {
System.out.println(f(5));
}
public static int f(int number) {
// Missing body
}
}
a.
0
b.
1
c.
2
d.
3
a. return “number”;
b. System.out.println(number);
c. System.out.println(“number”);
d. return number;
#
Sections 6.5 Passing Parameters by Values
12. When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred
to as .
a. method invocation
b. pass by value
c. pass by reference
d. pass by name
#
13. Given the following method, what is the output of the call nPrint(‘a’, 4)?
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n;
}
}
a. aaaaa
b. aaaa
c. aaa
d. invalid call
#
14. Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n;
}
}
What is k after invoking nPrint(“A message”, k)?
int k = 2;
nPrint(“A message”, k);
#
Section 6.8 Overloading Methods
15. Analyze the following code:
public class Test {
public static void main(String[] args) {
System.out.println(xMethod(5, 500L));
}
public static int xMethod(int n, long l) {
System.out.println(“int, long”);
return n;
}
public static long xMethod(long n, long l) {
System.out.println(“long, long”);
return n;
}
}
a. The program displays int, long followed by 5.
b. The program displays long, long followed by 5.
c. The program runs fine but displays things other than 5.
d. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
#
16. Analyze the following code:
class Test {
public static void main(String[] args) {
System.out.println(xmethod(5));
}
public static int xmethod(int n, long t) {
System.out.println(“int”);
return n;
}
public static long xmethod(long n) {
System.out.println(“long”);
return n;
}
}
a. The program displays int followed by 5.
b. The program displays long followed by 5.
c. The program runs fine but displays things other than 5.
d. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
#
17. Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2) {
System.out.println(“max(int, double) is invoked”);
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
System.out.println(“max(double, int) is invoked”);
if (num1 > num2)
return num1;
else
return num2;
}
}
a. The program cannot compile because you cannot have the print statement in a non-void method.
b. The program cannot compile because the compiler cannot determine which max method should be invoked.
c. The program runs and prints 2 followed by “max(int, double)” is invoked.
d. The program runs and prints 2 followed by “max(double, int)” is invoked.
e. The program runs and prints “max(int, double) is invoked” followed by 2.
#
18. Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(m(2));
}
public static int m(int num) {
return num;
}
public static void m(int num) {
System.out.println(num);
}
}
a. The program has a compile error because the two methods m have the same signature.
b. The program has a compile error because the second m method is defined, but not invoked in the main method.
c. The program runs and prints 2 once.
d. The program runs and prints 2 twice.
#
Section 6.9 The Scope of Variables
19. A variable defined inside a method is referred to as .
a. a global variable
b. a method variable
c. a block variable
d. a local variable
#
20. What is k after the following block executes?
{
int k = 2;
nPrint(“A message”, k);
}
System.out.println(k);
a. 0
b. 1
c. 2
d. k is not defined outside the block. So, the program has a compile error
#
Section 6.10 Case Study: Generating Random Characters
21. (int)(Math.random() * (65535 + 1)) returns a random number .
a. between 1 and 65536
b. between 1 and 65535
c. between 0 and 65535
d. between 0 and 65536
#
22. (int)(‘a’ + Math.random() * (‘z’ ‘a’ + 1)) returns a random number .
a. between 0 and (int)’z’
b. between (int)’a’ and (int)’z’
c. between ‘a’ and ‘z’
d. between ‘a’ and ‘y’
#
23. (char)(‘a’ + Math.random() * (‘z’ ‘a’ + 1)) returns a random character .
a. between ‘a’ and ‘z’
b. between ‘a’ and ‘y’
c. between ‘b’ and ‘z’
d. between ‘b’ and ‘y’
#
24. Which of the following is the best for generating random integer 0 or 1?
a. (int)Math.random()
b. (int)Math.random() + 1
c. (int)(Math.random() + 0.5)
d. (int)(Math.random() + 0.2)
e. (int)(Math.random() + 0.8)
#
Section 6.11 Method Abstraction and Stepwise Refinement
25. The client can use a method without knowing how it is implemented. The details of the implementation are
encapsulated in the method and hidden from the client who invokes the method. This is known as .
a. information hiding
b. encapsulation
c. method hiding
d. simplifying method
#
26. is to implement one method in the structure chart at a time from the top to the bottom.
a. Bottomup approach
b. Top-down approach
c. Bottomup and topdown approach
d. Stepwise refinement
#
27. is a simple but incomplete version of a method.
a. A stub
b. A main method
c. A non-main method
d. A method developed using topdown approach