Chapter 6: Methods Flashcards
How should this expression (ternary operator) be read?
boolean-expression ? expression1 :expression2
Evaluate the boolean-expression. If it is true, then use expression1. Otherwise, use expression2.
What is a method conceptually?
A collection of statements grouped together to perform an operation.
What is a method that belongs to a class and not a specific instance called?
Static. The name of the class must be used in the scope of them.
Think Math.sqrt(…) and Math.sin(…)
Why might we want to use methods?
Improve program readability.
Facilitate modular program development by breaking down programs into smaller, manageable pieces promoting incremental development practices.
Use stub methods to outline functionality before implementation (i.e., method definitions without statements written yet).
Avoid redundant code by centralizing logic in reusable methods.
Simplify program maintenance by requiring changes only in a single method.
What are the advantages of modularity?
- Controlling complexity
- Maintain programmer sanity
- Readability
- Implementation details are hidden in the method
- Ease of debugging
- Easily disable parts of the program
What is unit testing?
The process of individually testing a small part or unit of a program, typically a method.
What are the parts of unit testing?
A testbench (a.k.a. test harness) is a separate program designed specifically to verify that a method produces the correct output for various input values. Each unique set of input values is called a test vector, and an effective test vector includes border cases.
What are some additional considerations for test harnesses/testbenches?
A good test harness only displays messages when output values are incorrect. Manually examining a program’s output is both cumbersome and error-prone.
How do you define a method?
With a method header within a class. The header specifies the modifiers, the return type (possibly void), the name of the method, and the method parameter list.
modifiers returnValue Type methodName (list parameters) {
method Body;
return; //terminates the method
}
What are method parameters?
Parameters are assigned with argument values by position: First parameter with first argument, second with second, etc. A method definition may have multiple parameters, separated by commas.
Can a method be defined with no parameters?
Yes, but a method definition with no parameters must still have the parentheses.
public static double printVolume( ) {
..
}
How does a method return work?
It basically returns a value using a return statement.
Ex: return numToSquare * numToSquare; A method can only return one item. A return statement may appear as any statement in the method, not just the last. Multiple return statements may exist in a method. Control goes back to the calling program as soon as the first return statement is reached.
How does the void return type work?
- If the return type is void, the return statement is optional.
- A return statement may appear as any statement in the method, not just the last.
What is a method signature?
The method name and the parameters list together.
What’s the program that calls the methods called?
Caller.
What are the requirements for a method?
- The name of the method
- Argument (actual parameters to pass values) list. Could be a list of expressions or possibly empty.
What are Hierarchical (Nested) Method Calls?
- A method’s statements may include method calls.
- For example: main() is a method, and it typically calls many other methods.
What happens when a program calls a method?
Program control transfers to the called method, which runs and then returns control to the caller.
When does a method return control to the caller?
When it reaches a return statement or the closing brace of the method.
What is an activation record?
A memory record that stores a method’s parameters and variables, placed in the call stack.
Where are activation records stored?
In a special memory area called the call stack (also known as execution stack, runtime stack, or machine stack).
What happens when a method finishes executing in relation to the activation record and the call stack?
Its activation record is removed from the call stack.
What order does the call stack follow?
Last-In, First-Out (LIFO)—the last method called is the first one to finish and be removed.
What are the five steps when a method is invoked?
- Memory is allocated for parameters and local variables.
- Argument expressions are evaluated.
- Argument values are assigned to parameters.
- The method executes.
- Upon return, parameter and local variable memory is released.
What is a stack frame?
A memory block in the call stack that holds data for an active method call.
What does a stack frame contain?
- Method parameters
- Return address (where to go back after the method finishes)
- Local variables
What happens when a method returns in relation to the stack frame?
Its stack frame is discarded, and the program resumes from where it left off.
What can a method’s block of statements include?
- Branches (if-else statements)
- Loops (for, while, do-while)
- Assignments (variable updates)
- Calls to other methods
- Any other statements
What is a good guideline for method size?
- A method should fit on one screen or window.
- Ideally no more than ~30 lines of code.
- This is a guideline, not a strict rule.
What is pass-by-value in Java?
a value that is passed onto the a method from the main
Where is the argument value stored in pass-by-value?
the arguments value is copied into local memory allocated for the parameter
What happens if a method modifies a parameter passed by value?
Only the copy is changed. The original argument in the caller remains unchanged.
What happens to a method’s local variables when it returns?
They are discarded and removed from memory.
What is method overloading/method name overloading?
Methods with the same name but differing in the number or types of parameters.
What’s a cute fact about return values in method overloading?
- A method’s return type does not influence overloading.
- Thus, having two same-named method definitions with the same parameter types but different return types will yield a compiler error.
True or false: The return type of the method is not part of the signature.
True. The return type of the method is not part of the signature. That is, overloaded methods cannot differ only by their return type.
What is the scope of a variable?
The part of the program where the variable can be accessed or referenced.
What is a local variable?
A variable declared inside a method. It can only be used within that method.
How long does a local variable’s scope last?
From its declaration until the end of the block {}
that contains it.
When must a local variable be assigned a value?
Before it is used. Otherwise, the program won’t compile.
What are method parameters, and what is their scope?
Method parameters are local variables whose scope lasts for the entire method.
What is the scope of a loop variable?
From the opening brace {
of the loop to the closing brace }
of the loop.
What is the scope of a method?
A method is accessible within the entire class from the class’s opening {
to its closing }
.
Can a method access another method in the same class?
Yes, regardless of the order the methods are defined.
What does the public access modifier do?
It allows methods and fields to be accessed from other classes.
What is method abstraction?
It means using a method without knowing its internal details (hiding implementation).
Why is method abstraction important?
It simplifies programming by allowing developers to use methods without worrying about how they work internally.
What is another name for method abstraction?
Information hiding or encapsulation.
How does method abstraction benefit software development?
It keeps code modular, organized, and easier to maintain by hiding complex details.
What happens if a method’s implementation changes but its signature stays the same?
The client program will not be affected, since it only relies on the method’s interface.
What is stepwise refinement (divide-and-conquer)?
A technique where a program is broken into smaller subproblems to solve it piece by piece.
How does stepwise refinement help in programming?
It makes complex problems easier to manage by solving small parts first and then combining the solutions.
What is a perfect size array?
An array where the number of elements is exactly equal to the memory allocated.
How is a perfect size array passed to a method?
Using only the array’s reference (no extra parameters needed).
Can a method modify the original perfect size array when passed as a parameter?
Yes, because arrays are passed by reference, allowing direct modification.
What is an oversize array?
An array where the number of elements used is less than or equal to the memory allocated (extra space may exist).
How do you keep track of the used elements in an oversize array?
By using a separate integer variable that stores the current number of elements.
How is an oversize array passed to a method?
By passing both the array reference and the current size as separate parameters.
What happens if a method changes the size of an oversize array?
The new size must be returned by the method so the calling program knows the updated count.
What does a method signature indicate about the expected array size?
A method signature can tell whether a perfect size or oversize array is expected as an argument.
What is the method signature for a method that expects a perfect size array?
It has one parameter for the array reference but no parameter for the array size.
What is the method signature for a method that expects an oversize array?
It has two parameters: one for the array reference and one for the array size.
How does the return type help determine if a method uses a perfect size or oversize array?
If the method returns an array reference, it constructs a perfect size array. If it returns an int, it may use an oversize array, with the int indicating the new array size.
What is the relationship between the method’s return type and the array size used?
A method returning an array reference uses a perfect size array, while a method returning an int may use an oversize array, indicating the array’s new size.
Where are primitive type variables stored?
Primitive type variables are stored directly in the stack frame.
How are primitive data types passed to a method?
Primitive data types are passed by value, meaning a separate copy of the variable value is passed to the method.
What happens when an array is passed to a method?
The array’s reference is copied to the method’s stack frame.
Where are array elements stored?
Array elements are stored on the heap, and only the reference to the array is stored in the stack frame.
How is an array passed to a method?
An array is passed by reference, meaning both the calling and called methods have access to the array’s elements, and any changes in the method affect the array.
What is an error that unintentionally modifies data known as?
It is known as a side effect.
What is a common error when working with arrays in methods?
A common error is trying to modify an array reference in a method.
What happens when an array reference is passed to a method?
The reference itself is passed by value, meaning a method cannot change the argument’s array reference since separate copies are stored in two different stack frames.
Where are references to arrays stored?
References are stored in the stack frame, and while references can be swapped within a stack frame, the original references remain unchanged when the method returns.
What does the fill() method do when passed an array reference?
It fills the array with a specified value. The reference to the array is passed by value, so the array contents are modified, but the reference itself cannot be changed.
What is the purpose of a method that calculates a value based on an array without changing the array?
Such a method calculates and returns a value based on the array, without modifying the array contents.
Fill in the blank: A method that changes an array’s contents but not its size typically has a _______ return type.
void
What is the purpose of a method that creates a new array or changes the array’s size?
This method returns a new array reference since array references cannot be changed inside a method.
What can be identified from a method signature when dealing with arrays?
The method’s purpose can often be identified by its signature, whether it calculates a value, modifies the array’s contents, or changes the array’s size.
True or False: A method can change the original array reference when passed as an argument.
False
What happens to array references when a method is called?
Separate copies are stored in two different stack frames.
What does the binarySearch() method do?
It returns the index of the target in the array or a negative number if the target is not found.
What is an example of a method that modifies an array’s contents?
void sort(int[] arrayReference)
What is an example of a method that creates a copy of an array?
double[] copyOf(double[] arrayReference, int newLength)
Fill in the blank: The contents of an array can be modified, but its _______ cannot be changed.
reference
What type of return value does a method that calculates a value based on an array typically have?
It typically returns a value, not an array.