Introduction to Java Programming, Sixth Edition, Y. Daniel Liang
Chapter 5 Methods
Sections 5.2 Creating 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 5.3 Calling a Method
4
Arguments to methods always appear within __________.
A.
brackets
B.
parentheses
C.
curly braces
D.
quotation marks
5
Does the return statement in the following method cause syntax errors?
public static void main(String[] args) {
   int max = 0;
   if (max != 0)
     System.out.println(max);
   else
     return;
}
A.
Yes
B.
No
6
Does the method call in the following method cause syntax 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 last-in first-out fashion.
A.
a heap
B.
storage area
C.
a stack
D.
an array
Sections 5.5 Passing Parameters by Values
8
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
9
Suppose
static void nPrint(String message, int n) {
   while (n > 0) {
     System.out.print(message);
     n--;
   }
}
What is the printout of the call nPrint('a', 4)?
A.
aaaaa
B.
aaaa
C.
aaa
D.
invalid call
10
Suppose
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);
A.
0
B.
1
C.
2
D.
3
Section 5.6 Overloading Methods
11
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 given in a and b.
D.
The program does not compile because the compiler cannot distinguish which xmethod to invoke.
12
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 given in a and b.
D.
The program does not compile because the compiler cannot distinguish which xmethod to invoke.
13
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.
14
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 syntax error because the two methods m have the same signature.
B.
The program has a syntax 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 5.8 The Scope of Local Variables
15
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
16
What is k after the following block executed?
{
   int k = 2;
   nPrint("A message", k);
}
A.
0
B.
1
C.
2
D.
k is not defined outside the block.
Section 5.9 The Math Class
17
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
18
Which of the following is a possible output from invoking Math.random()?
A.
3.43
B.
0.5
C.
0.0
D.
1
19
What is Math.round(3.6)?
A.
3.0
B.
3
C.
4
D.
5.0
20
What is Math.rint(3.6)?
A.
3.0
B.
3
C.
4.0
D.
5.0
21
What is Math.rint(3.5)?
A.
3.0
B.
3
C.
4
D.
4.0
E.
5.0
22
What is Math.ceil(3.6)?
A.
3.0
B.
3
C.
4.0
D.
5.0
23
What is Math.floor(3.6)?
A.
3.0
B.
3
C.
4
D.
5.0
Section 5.10 Case Study: Generating Random Characters
24
(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
25
'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'
26
(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'
27
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 5.11 Method Abstraction and Stepwise Refinement
28
__________ is to implement one method in the structure chart at a time from the top to the bottom.
A.
Bottom-up approach
B.
Top-down approach
C.
Bottom-up and top-down approach
D.
Stepwise refinement
29
__________ 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 top-down approach
Section 5.12 (Optional) Packages
30
What are the benefits of using packages?
A.
To avoid naming conflicts.
B.
To distribute software conveniently.
C.
To protect classes.
D.
To organize classes.
31
Which of the following statements are true?
A.
By convention, package names are all in lowercase.
B.
The package statement is optional.
C.
The import statement is optional.
D.
You don't have to explicitly import a class in the java.lang package.
32
Suppose the output path is c:\csci1301, and the package statement in the source code is package homework1. Where should the .class file be stored?
A.
c:\csci1301\classes
B.
c:\csci1301
C.
c:\csci1301\homework1
D.
c:\homework1\csci1301
33
Suppose the source path is c:\csci1301, and the package statement in the source code is package homework1. Where should the .java file be stored for the Java IDE such as JBuilder and NetBeans to work properly?
A.
c:\csci1301\classes
B.
c:\csci1301
C.
c:\csci1301\homework1
D.
c:\homework1\csci1301