Chapter 6 Method
(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'
a
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
a
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.
a
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.
a
__________ 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
a
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.
a You cannot override the methods based on the type returned.
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
ab
(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'
b
26. __________ 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
b
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.
b
Arguments to methods always appear within __________. a. brackets b. parentheses c. curly braces d.quotation marks
b
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
b
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
b A value-returning method can also be invoked as a statement in Java. In this case, the caller simply ignores the return value. This is rare, but permissible if the caller is not interested in the return value.
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
b By definition, a method signature consists of method name and parameter list.
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
b It is rare, but sometimes useful to have a return statement for circumventing the normal flow of control in a void method.
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.
b This is known as ambiguous method invocation.
. 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)
c
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
c
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
c
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); a. 0 b. 1 c. 2 d. 3
c
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)
c Java application's starting method is the main method.
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
d
Consider the following incomplete code: public class Test { public static void main(String[] args) { System.out.println(f(5)); } public static int f(int number) { // Missing body } } The missing method body should be ________. a. return "number"; b. System.out.println(number); c. System.out.println("number"); d. return number;
d
static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is the output of the call nPrint('a', 4)? a. aaaaa b. aaaa c. aaa d. invalid call
d Invalid call because char 'a' cannot be passed to string message
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 e. void
d char should be placed here because the method returns a character.
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
d k is defined inside the block. Outside the block, k is not defined.
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
e void should here because the method does not return any value.