A programmer wants a method to calculate a discounted price so that other code can store the result, compare it, or use it in another calculation. Which design best meets this requirement?
06 Methods and Modular Programming Online Quiz Questions
Use this free practice quiz with 20 questions to review 06 Methods and Modular Programming, test your knowledge, and prepare for your next test or exam.
True or false: In the code def greet(name): ... followed by greet("Maya"), name is an argument and "Maya" is a parameter.
- A
True
- B
False
What programming design process divides a complex problem into smaller subproblems, each with a focused responsibility?
Complete the statement: In a method definition, named inputs are called .
A method contains tax = price * tax_rate and total = price + tax. What is normally true about tax and total?
- A
They can be accessed directly from every method in the program.
- B
They automatically become parameters of the method that called them.
- C
They are normally accessible only within the method that created them.
- D
They remain available globally after the method finishes.
Select all characteristics that generally make a method easier to reuse.
- A
It has a descriptive name.
- B
It depends on a particular global variable whenever possible.
- C
It accepts necessary data through parameters.
- D
It returns a useful result when appropriate.
- E
It must contain only one line of code.
True or false: If a method executes a return statement, ordinary statements written later in that same method still execute.
- A
True
- B
False
A method is defined as def add_tax(amount, rate): return amount * (1 + rate). What number does add_tax(30, 0.1) return?
Complete the statement: Printing, writing to a file, or changing a global variable is an example of a .
A recursive method repeatedly calls itself while solving a problem. What must it include to terminate the repeated calls?
- A
A global variable that stores every previous result.
- B
A base case that stops the repeated calls.
- C
A print statement that displays each recursive call.
- D
A default parameter that changes the method name.
Select all benefits that the material associates with decomposing a grade-processing program into focused methods.
- A
Each method can have a narrower responsibility.
- B
Methods can be tested independently.
- C
Every method must handle input, calculation, display, and file storage together.
- D
Errors can be isolated to a smaller section of code.
- E
A change to one responsibility can often be made in its focused method.
What programming concept hides implementation details behind a meaningful operation such as calculate_total()?
You are designing a program that processes an order. It must validate the items, calculate a total, and create a receipt. Explain how you would decompose this program into reusable methods, including how the methods should communicate and why this design is preferable to one large procedure.
Given def create_label(text, prefix="Item"): followed by create_label("Notebook") and create_label("Notebook", "Product"), which result is produced by each call?
- A
Both calls return
Notebookbecause the prefix is always fixed. - B
The first call returns
Item: Notebook, and the second returnsProduct: Notebook. - C
The first call returns
Product: Notebook, and the second returnsItem: Notebook. - D
Both calls cause an error because default parameters cannot be used with strings.
Which statement correctly describes a variable created inside a method?
- A
A variable created inside a method can normally be accessed directly from any other method.
- B
A variable created inside a method is normally local to that method.
- C
A parameter belongs to the caller rather than to the method being called.
- D
A return value can be accessed only while the method is still running.
A program must validate grades, calculate an average, assign a letter grade, and display a report. Which design best applies decomposition?
- A
Put every operation in one large method so that the program has fewer method calls.
- B
Create a separate method for every individual line, regardless of what that line does.
- C
Divide the problem into focused methods such as validating input, calculating a result, and creating a report.
- D
Store all intermediate values in global variables so that every method can use them.
True or false: In the method that contains it, a return statement ends the current method, so statements after the return do not execute.
- A
True
- B
False
In the definition def greet(name):, what is the programming term for name?
Consider these methods:
def printed_square(number): print(number * number)
def returned_square(number): return number * number
What is the important difference between calling printed_square(5) and assigning result = returned_square(5)?
- A
Both methods make 25 available in
resultbecause printing and returning are interchangeable. - B
Only
returned_square(5)can provide 25 as a result that the caller can store. - C
Only
printed_square(5)can provide 25 to the caller because printed output is automatically returned. - D
Neither method produces 25 because a method must accept at least two parameters.
Given def add(first, second): return first + second, what does add(7, 4) return?
- A
The call returns 3 because the second argument is subtracted from the first.
- B
The call returns 28 because the arguments are multiplied.
- C
The call returns 74 because the arguments are joined as text.
- D
The call returns 11 because 7 is assigned to
firstand 4 tosecond.