Java Chapter 5

7 September 2022
4.7 (114 reviews)
60 test answers

Unlock all answers in this set

Unlock answers (56)
question
1) Methods are commonly used to: A) speed up the compilation of a program B) break a problem down into small manageable pieces C) emphasize certain parts of the logic D) document the program
answer
Answer: B
question
2) Which of the following is NOT a benefit derived from using methods in programming? A) Pproblems are more easily solved. B) simplifies programs C) code reuse D) All of the above are benefits.
answer
Answer: D
question
3) This type of method performs a task and sends a value back to the code that called it. A) value-returning B) void C) complex D) local
answer
Answer: A
question
4) In the following code, System.out.println(num) is an example of: double num = 5.4; System.out.println(num); num = 0.0; A) a value-returning method B) a void method C) a complex method D) a local variable
answer
Answer: B
question
5) To create a method you must write its: A) header B) return type C) body D) definition
answer
Answer: D
question
6) In the header, the method name is always followed by this: A) parentheses B) return type C) data type D) braces
answer
Answer: A
question
7) This part of a method is a collection of statements that are performed when the method is executed. A) method header B) return type C) method body D) method modifier
answer
Answer: C
question
8) Which of the following is NOT part of a method call? A) method name B) return type C) parentheses D) all of the above are part of a method call
answer
Answer: B
question
9) If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens? A) Control is returned to method A. B) Control is returned to method B. C) Control is returned to method C. D) The program terminates.
answer
Answer: C
question
10) Values that are sent into a method are called: A) variables B) arguments C) literals D) types
answer
Answer: B
question
11) When an argument is passed to a method: A) its value is copied into the method's parameter variable B) its value may be changed within the called method C) values may not be passed to methods D) the method must not assign another value to the parameter that receives the argument
answer
Answer: A
question
12) What is wrong with the following method call? displayValue (double x); A) There is nothing wrong with the statement. B) displayValue will not accept a parameter. C) Do not include the data type in the method call. D) x should be a String.
answer
Answer: C
question
13) Given the following method header, which of the method calls would be an error? public void displayValues(int x, int y) A) displayValue(a,b); // where a is a short and b is a byte B) displayValue(a,b); // where a is an int and b is a byte C) displayValue(a,b); // where a is a short and b is a long D) They would all give an error.
answer
Answer: C
question
14) Which of the following would be a valid method call for the following method? public static void showProduct (int num1, double num2) { int product; product = num1 * (int)num2; System.out.println("The product is " + product); } A) showProduct(5.5, 4.0); B) showProduct(10.0, 4); C) showProduct(10, 4.5); D) showProduct(33.0, 55.0);
answer
Answer: C
question
15) When an object, such as a String, is passed as an argument, it is: A) actually a reference to the object that is passed B) passed by value like any other parameter value C) encrypted D) necessary to know exactly how long the string is when writing the program
answer
Answer: A
question
16) All @param tags in a method's documentation comment must: A) end with a */ B) appear after the general description of the method C) appear before the method header D) span several lines
answer
Answer: B
question
17) A special variable that holds a value being passed into a method is called what? A) Modifier B) Parameter C) Alias D) Argument
answer
Answer: B
question
18) When you pass an argument to a method, be sure that the argument's data type is compatible with: A) the parameter variable's data type B) the method's return type C) the version of Java currently being used D) IEEE standards
answer
Answer: A
question
19) A parameter variable's scope is: A) the method in which the parameter is declared B) the class to which the method belongs C) the main method D) All of the above
answer
Answer: A
question
20) The lifetime of a method's local variable is: A) the duration of the program B) the duration of the class to which the method belongs C) the duration of the method that called the local variable's method D) only while the method is executing
answer
Answer: D
question
21) Local variables: A) are hidden from other methods B) may have the same name as local variables in other methods C) lose the values stored in them between calls to the method in which the variable is declared D) All of the above
answer
Answer: D
question
22) Which of the following values can be passed to a method that has an int parameter variable? A) float B) double C) long D) All of the above E) None of the above
answer
Answer: E
question
23) The header of a value-returning method must specify this. A) The method's local variable names B) The name of the variable in the calling program that will receive the returned value C) The data type of the return value D) All of the above
answer
Answer: C
question
24) What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; } A) 18.0 B) 18 (as an integer) C) 8 D) This is an error.
answer
Answer: A
question
25) In a @return tag statement the description: A) cannot be longer than one line B) describes the return value C) must be longer than one line D) describes the parameter values
answer
Answer: B
question
26) When a method tests an argument and returns a true or false value, it should return: A) a zero for true and a one for false B) a boolean value C) a zero for false and a non-zero for true D) a method should not be used for this type test
answer
Answer: B
question
27) The phrase divide and conquer is sometimes used to describe: A) the backbone of the scientific method B) the process of dividing functions C) the process of breaking a problem down into smaller pieces D) the process of using division to solve a mathematical problem
answer
Answer: C
question
28) In a general sense, a method is: A) a plan B) a statement inside a loop C) a comment D) a collection of statements that performs a specific task
answer
Answer: D
question
29) Breaking a program down into small manageable methods: A) makes problems more easily solved B) allows for code reuse C) simplifies programs D) all of the above
answer
Answer: D
question
30) This type of method performs a task and then terminates. A) value-returning B) void C) local D) simple
answer
Answer: B
question
31) In the following code, Integer.parseInt(str), is an example of: int num; string str = "555"; num = Integer.parseInt(str) + 5; A) a value-returning method B) a void method C) a local variable D) a complex method
answer
Answer: A
question
32) Which of the following is NOT a part of the method header? A) return type B) method name C) parentheses D) semicolon
answer
Answer: D
question
33) Which of the following is included in a method call? A) return type B) method modifiers C) parentheses D) return variable
answer
Answer: C
question
34) You should always document a method by writing comments that appear: A) just before the method's definition B) just after the method's definition C) at the end of the file D) only if the method is more than five lines long
answer
Answer: A
question
35) When an argument value is passed to a method, the receiving parameter variable is: A) declared within the body of the method B) declared in the method header inside the parentheses C) declared in the calling method D) uses the declaration of the argument
answer
Answer: B
question
36) If you attempt to use a local variable before it has been given a value: A) a compiler error will occur B) the local variable will always contain the value 0 C) the results will be unpredictable D) the local variable will be ignored
answer
Answer: A
question
37) What will be the result of the following code? int num; string str = "555"; num = Integer.parseInt(string str) + 5; A) num will be set to 560. B) str will have a value of "560". C) The last line of code will cause an error. D) Neither num or str will be changed.
answer
Answer: C
question
38) Given the following method header, which of the method calls would be an error? public void displayValues(double x, int y) A) displayValue(a,b); // where a is a long and b is a byte B) displayValue(a,b); // where a is an int and b is a byte C) displayValue(a,b); // where a is a short and b is a long D) They would all give an error.
answer
Answer: C
question
39) Which of the following would be a valid method call for the following method? public static void showProduct(double num1, int num2) { double product; product = num1 * num2; System.out.println("The product is " + product); } A) showProduct("5", "40"); B) showProduct(10.0, 4.6); C) showProduct(10, 4.5); D) showProduct(3.3, 55);
answer
Answer: D
question
40) When writing the documentation comments for a method, you can provide a description of each parameter by using a: A) @comment tag B) @doc tag C) @param tag D) @return tag
answer
Answer: C
question
41) Values stored in local variables: A) are lost between calls to the method in which they are declared B) retain their values from the last call to the method in which they are declared C) may be referenced by the calling method D) may be referenced by any other method, if the method in which they are declared is a public method
answer
Answer: A
question
42) Local variables can be initialized with: A) constants B) parameter values C) the results of an arithmetic operation D) any of the above
answer
Answer: D
question
43) A value-returning method must specify this as its return type in the method header. A) an int B) a double C) a boolean D) any valid data type
answer
Answer: D
question
44) What will be returned from the following method? public static int methodA() { double a = 8.5 + 9.5; return a; } A) 18.0 B) 18 (as an integer) C) 8.0 D) This is an error.
answer
Answer: D
question
45) To document the return value of a method, use this in a documentation comment. A) The @param tag B) The @comment tag C) The @return tag D) The @returnValue tag
answer
Answer: C
question
46) The process of breaking a problem down into smaller pieces is sometimes called: A) divide and conquer B) scientific method C) top-down programming D) whole-into-part
answer
Answer: A
question
47) Any method that calls a method with a throws clause in its header must: A) handle the potential exception B) have the same throws clause C) both of the above D) do nothing, the called program will take care of the throws clause
answer
Answer: C
question
48) Assume that the following method header is for a method in class A. public void displayValue(int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method? A) int x = 7; void displayValue(x); B) int x = 7; displayValue(x); C) int x = 7; displayValue(int x); D) int x = 7; displayValue(x)
answer
Answer: B
question
T/F: Methods are commonly used to break a problem into small manageable pieces.
answer
Answer: TRUE
question
T/F: Two general categories of methods are void methods and value returning methods.
answer
Answer: TRUE
question
T/F: In the method header, the method modifier public means that the method belongs to the class, not a specific object.
answer
Answer: FALSE
question
T/F: Constants, variables, and the values of expressions may be passed as arguments to a method.
answer
Answer: TRUE
question
T/F: A parameter variable's scope is the method in which the parameter is declared.
answer
Answer: TRUE
question
T/F: You must have a return statement in a value-returning method.
answer
Answer: TRUE
question
T/F: Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause.
answer
Answer: TRUE
question
T/F: In the method header the static method modifier means the method is available to code outside the class.
answer
Answer: FALSE
question
T/F: Only constants and variables may be passed as arguments to methods.
answer
Answer: FALSE
question
10) No statement outside the method in which a parameter variable is declared can access the parameter by its name.
answer
Answer: TRUE
question
T/F: The expression in a return statement can be any expression that has a value.
answer
Answer: TRUE
question
T/F: A value-returning method can return a reference to a non-primitive type.
answer
Answer: TRUE