True or false: A Java method declared with return type void may use return; to end execution early, but it cannot return a value.
5 Methods and Modular Design Online Quiz Questions
Use this free practice quiz with 20 questions to review 5 Methods and Modular Design, test your knowledge, and prepare for your next test or exam.
A program must calculate purchase totals in several places and may later change how totals are displayed. Which design best follows modular design?
- A
Put input, tax calculation, printing, and receipt formatting into one long
mainmethod. - B
Create a method that calculates the purchase total and let the caller decide how to display it.
- C
Create a method that calculates the total, prints it, formats a receipt, and saves a file.
- D
Create a separate method for every individual arithmetic operator used in the calculation.
What Java term describes the situation in which a parameter named count hides a field also named count inside a method?
In a Java method declaration, the part that states what kind of value the method sends back is its .
Which practices support good method design? Select all correct choices.
- A
Give the method one main responsibility.
- B
Use a meaningful name that communicates the method's purpose.
- C
Make every method print its result immediately.
- D
Return information that the caller may need.
True or false: If a Java method receives an array and assigns a new value to one of its elements, the caller can observe that element change after the method returns.
- A
True
- B
False
In the following structure, where can blockVariable be referred to?
if (local > 0) { int blockVariable = 10; }
- A
The variable is available throughout the class.
- B
The variable is available throughout the containing method.
- C
The variable is available only inside the
ifblock where it is declared. - D
The variable is available only to the method's caller.
The method calculateTotal(40.00, 0.08) returns price + price * taxRate. What numeric value does the call return?
In a method call, the actual values supplied to the method's parameters are called .
Which items are part of a useful method contract? Select all correct choices.
- A
The purpose of the method.
- B
The meaning and requirements of its inputs.
- C
The exact names of every local variable used in the implementation.
- D
The meaning of its output and any side effects.
In the exam-score example, what is the primary responsibility of printReport(int[] scores)?
- A
It replaces
findAverageandfindLetterGradewith one large calculation. - B
It coordinates the specialized methods and presents their results.
- C
It modifies the scores array before each method call.
- D
It determines which overloaded method the compiler must choose.
A single method currently reads exam scores, calculates their total and average, assigns a letter grade, and prints a report. Explain how you would decompose this program into methods and what design considerations you would document.
According to the supplied findAverage implementation, what does the method return when it receives an empty score array?
- A
It throws an error because division by zero is unavoidable.
- B
It returns the first score in the array.
- C
It returns
0.0. - D
It returns
NaN.
Which statement best describes a Java method declared with the return type void?
- A
It must return an
int. - B
It performs an action without returning a value.
- C
It can return any value without declaring a return type.
- D
It can only be called from
main.
In the call square(6), what is 6?
- A
The method name
- B
The return value
- C
The argument
- D
The parameter declaration
Why must absoluteValue return a compatible value in both the negative and nonnegative branches?
- A
Only the branch containing the first return needs a value.
- B
A non-void method may omit a return if its caller ignores the result.
- C
Every possible execution path must return a compatible value.
- D
Only methods returning boolean require a return statement.
Why does calling changeFirstElement(values) change values[0] even though Java passes arguments by value?
- A
The method receives the original reference variable, so all assignments affect the caller's variable.
- B
The method receives a copied reference that still refers to the caller's array.
- C
Arrays are passed by reference rather than by value in Java.
- D
The method returns the changed array implicitly.
Inside setCount(int count), the parameter count shadows the field named count, so this.count refers to the object's field.
- A
True
- B
False
What design approach builds a program from small components that can be developed, tested, and reused independently?
What integer does countEven return for the array {3, 8, 10, 11}?