javaCh6

Ace your homework & exams now with Quizwiz!

# 10. Which of the following is not an advantage of using methods. a. Using methods makes programs run faster. b. Using methods makes reusing code easier. c. Using methods makes programs easier to read. d. Using methods hides detailed implementation from the clients.

Key:a

# 5. A call for the method with a void return type is always a statement itself, but a call for the method with a non-void return type can be treated as either a statement or an expression. a. true b. false

Key:a

12. 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.

Key:a

20. Variables defined in the method header are called _____. a. parameters b. arguments c. local variables d. global variables Key:

Key:a

24. Methods can be declared in any order in a class. a. true b. false

Key:a

4. The modifiers and return value type do not contribute toward distinguishing one method from another, only the parameter list. a. true b. false

Key:a

5. You may have a return statement in a void method. a. true b. false

Key:a

6. You must have a return statement in a non-void method. a. true b. false

Key:a

8. You may have a return statement in a void method. a. true b. false

Key:a

9. The actual parameters of a method must match the formal parameters in type, order, and number. a. true b. false

Key:a

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.

Key:a You cannot override the methods based on the type returned.

13. 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.

Key:b

2. Each Java class must contain a main method. a. true b. false

Key:b

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

Key:b

21. A variable or a value listed in a call to a method is called a. a parameter b. an argument c. a local variable d. a global variable

Key:b

3. A method can be defined inside a method in Java. a. true b. false

Key:b

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

Key: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.

1. You can redeclare the variable if it is declared in the method signature. a. true b. false

Key:b If a variable is declared as the formal parameter in the method, it's scope is the entire method. Therefore, it cannot be redeclared inside the method.

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

Key:b It is rare, but sometimes useful to have a return statement for circumventing the normal flow of control in a void method.

12. The signature of a method consists of the method name, parameter list and return type. a. true b. false

Key:b The signature of a method consists of the method name, parameter list, but not the return type. A class cannot have the methods with identical signature.

14. 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.

Key:b This is known as ambiguous method invocation.

11. 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

Key:c

22. Java allows you to declare methods with the same name in a class. This is called ________. a. method duplication b. method overriding c. method overloading d. method redeclaration

Key:c

4. Analyze the following code: class Test { public static void main(String[] args) { System.out.println(xMethod((double)5)); } public static int xMethod(int n) { 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.

Key:d

10. Given the following method 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

Key:d Invalid call because char 'a' cannot be passed to string message

17. 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

Key:d k is defined inside the block. Outside the block, k is not defined.

23. A return statement without any value can be used in _______. a. a non-void method b. void method

b

27. (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'

key:a

Sections 6.4 void Method Example 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 current second is an integer from 1 to 100. d. Write a method that converts an uppercase letter to lowercase.

key:a

26. (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'

key:b

9. 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

key:b

28. 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)

key:c

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

key:c

Section 6.10 Case Study: Generating Random Characters 25. (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

key:c


Related study sets

E-business Management- Ch.7 Social, Mobile, and Local Marketing

View Set

effectual entrepreneurship Ch 1-9

View Set

CM L15 - Constipation, IBS, Diarrhea

View Set

20 Heart Failure/Circulatory Shock

View Set