CH 5

8 September 2022
4.7 (114 reviews)
45 test answers

Unlock all answers in this set

Unlock answers (41)
question
Methods are commonly used to Select one: 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
The correct answer is: break a problem down into small manageable pieces
question
When an object, such as a String, is passed as an argument, it is Select one: 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
The correct answer is: actually a reference to the object that is passed
question
A special variable that holds a value being passed into a method is called what? Select one: a. Modifier b. Parameter c. Alias d. Argument
answer
The correct answer is: Parameter
question
When you pass an argument to a method, be sure that the argument's data type is compatible with: Select one: 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
The correct answer is: the parameter variable's data type
question
A parameter variable's scope is: Select one: 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
The correct answer is: the method in which the parameter is declared
question
The lifetime of a method's local variable is Select one: 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
The correct answer is: only while the method is executing
question
Local variables Select one: 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
The correct answer is: All of the above
question
Which of the following values can be passed to a method that has an int parameter variable? Select one: a. float b. double c. long d. All of the above e. None of the above
answer
The correct answer is: None of the above
question
The header of a value-returning method must specify this. Select one: 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
The correct answer is: The data type of the return value
question
What will be returned from the following method? public double methodA() { double a = 8.5 + 9.5; return a; } Select one: a. 18.0 b. 18 (as an integer) c. 8 d. This is an error
answer
The correct answer is: 18.0
question
The phrase divide and conquer is sometimes used to describe Select one: 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
The correct answer is: the process of breaking a problem down into smaller pieces
question
Which of the following is not a benefit derived from using methods in programming? Select one: a. problems are more easily solved b. simplifies programs c. code reuse d. all of the above are benefits
answer
The correct answer is: all of the above are benefits
question
In a general sense, a method is Select one: a. a plan b. a statement inside a loop c. a comment d. a collection of statements that performs a specific task
answer
The correct answer is: a collection of statements that performs a specific task
question
Breaking a program down into small manageable methods Select one: a. makes problems more easily solved b. allows for code reuse c. simplifies programs d. all of the above
answer
The correct answer is: all of the above
question
This type of method performs a task and then terminates. Select one: a. value-returning b. void c. local d. simple
answer
The correct answer is: void
question
In the following code, Integer.parseInt(str), is an example of ________. int num; string str = "555"; num = Integer.parseInt(str) + 5; Select one: a. a value-returning method b. a void method c. a local variable d. a complex method
answer
The correct answer is: a value-returning method
question
Which of the following is not a part of the method header? Select one: a. return type b. method name c. parentheses d. semicolon
answer
correct answer is: semicolon
question
Which of the following is included in a method call? Select one: a. return type b. method modifiers c. parentheses d. return variable
answer
answer is: parentheses
question
When an argument value is passed to a method, the receiving parameter variable is Select one: 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
The correct answer is: declared in the method header inside the parentheses
question
If you attempt to use a local variable before it has been given a value, Select one: 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
FeedbackThe correct answer is: a compiler error will occur
question
What will be the result of the following code? int num; string str = "555"; num = Integer.parseInt(string str) + 5; Select one: 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
The correct answer is: the last line of code will cause an error
question
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); } Select one: a. showProduct("5", "40"); b. showProduct(10.0, 4.6); c. showProduct(10, 4.5); d. showProduct(3.3, 55);
answer
The correct answer is: showProduct(3.3, 55);
question
This type of method performs a task and sends a value back to the code that called it. Select one: a. value-returning b. void c. complex d. local
answer
FeedbackThe correct answer is: value-returning
question
Values stored in local variables Select one: 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
The correct answer is: are lost between calls to the method in which they are declared
question
Local variables can be initialized with Select one: a. constants b. parameter values c. the results of an arithmetic operation d. any of the above
answer
The correct answer is: any of the above
question
A value-returning method must specify this as its return type in the method header. Select one: a. an int b. a double c. a boolean d. any valid data type
answer
The correct answer is: any valid data type
question
What will be returned from the following method? public int methodA() { double a = 8.5 + 9.5; return a; } Select one: a. 18.0 b. 18 (as an integer) c. 8.0 d. This is an error
answer
The correct answer is: This is an error
question
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? Select one: 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
The correct answer is: int x = 7; displayValue(x);
question
In the header, the method name is always followed by this: Select one: a. parentheses b. return type c. data type d. braces
answer
The correct answer is: parentheses
question
This part of a method is a collection of statements that are performed when the method is executed. Select one: a. method header b. return type c. method body d. method modifier
answer
The correct answer is: method body
question
Which of the following is not part of a method call? Select one: a. method name b. return type c. parentheses d. all of the above are part of a method call
answer
The correct answer is: return type
question
Question text Values that are sent into a method are called ____________. Select one: a. variables b. arguments c. literals d. types
answer
The correct answer is: arguments
question
What is wrong with the following method call? displayValue (double x); Select one: a. There is nothing wrong with the statement. b. displayValue will not accept a parameter. c. should not include the data type in the method call. d. x should be a String.
answer
The correct answer is: should not include the data type in the method call.
question
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); } Select one: a. showProduct(5.5, 4.0); b. showProduct(10.0, 4); c. showProduct(10, 4.5); d. showProduct(33.0, 55.0);
answer
The correct answer is: showProduct(10, 4.5);
question
Methods are commonly used to break a problem into small manageable pieces. Select one: True False
answer
The correct answer is 'True'.
question
Two general categories of methods are void methods and value returning methods. Select one: True False
answer
The correct answer is 'True'.
question
In the method header, the method modifier public means that the method belongs to the class, not a specific object. Select one: True False
answer
The correct answer is 'False'.
question
Constants, variables, and the values of expressions may be passed as arguments to a method. Select one: True False
answer
answer is 'True'.
question
A parameter variable's scope is the method in which the parameter is declared. Select one: True False
answer
FeedbackThe correct answer is 'True'.
question
You must have a return statement in a value-returning method. Select one: True False
answer
The correct answer is 'True'.
question
In the method header the static method modifier means the method is available to code outside the class. Select one: True False
answer
The correct answer is 'False'.
question
Only constants and variables may be passed as arguments to methods. Select one: True False
answer
The correct answer is 'False'.
question
No statement outside the method in which a parameter variable is declared can access the parameter by its name. Select one: True False
answer
FeedbackThe correct answer is 'True'.
question
The expression in a return statement can be any expression that has a value. Select one: True False
answer
FeedbackThe correct answer is 'True'.
question
A value-returning method can return a reference to a non-primitive type. Select one: True False
answer
FeedbackThe correct answer is 'True'.