Chapter 5 - Methods Flashcards
What are methods used for?
Methods make it easier to modularize and make code reusable.
What values can methods return?
Methods can return any value-type, or none at all. If nothing is returned, the returnvaluetype keyword must be “void”
What does it mean that a method can be overloaded?
It means that two methods can have the same name, as long as their parameter list differ
What is method abstraction?
The act of putting code into methods. The method can be used without knowing how it is implemented. The implementation details are hidden inside the method. This is known as information hiding
Why is method abstraction a good thing?
Because it modularizes programs in a neat, hierarchical manner. Programs written as collections of concise methods are easier to write, debug, maintain, and modify than would otherwise be the case
What is stepwise refinement?
The act of breaking large problems into smaller ones, and the smaller into even smaller ones. Then write methods to solve each of the smaller problems
The signature of a method consists of __
Method name and parameter(s)
Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as __, which stores elements in last-in first-out fashion
A stack
A variable inside a method is referred to as __
A local variable
The signature of a method consists of __
A. method name
B. method name and parameter list
C. return type, method name, and parameter list
D. parameter list
B. method name and parameter list
Does the return statement in the following method cause compile errors?
public static void main(String[] args) { int max = 0; if (max != 0) System.out.println(max); else return; }
No.
Analyze the following code:
class Test { public static void main(String[] args) { System.out.println(xmethod(5)); }
public static int xmethod(int n, long t) {
System.out.println(“int”);
return n;
}
public static long xmethod(long n) { System.out.println("long"); return n; } }
A. The program displays int followed by 5.
B. The program displays long followed by 5.
C. The program runs fine but displays things other than 5.
D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.
B. The program displays long followed by 5.
Analyze the following code.
public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); }
public static double max(int num1, double num2) {
System.out.println(“max(int, double) is invoked”);
if (num1 > num2) return num1; else return num2; }
public static double max(double num1, int num2) {
System.out.println(“max(double, int) is invoked”);
if (num1 > num2) return num1; else return num2; } }
A. The program cannot compile because you cannot have the print statement in a non-void method.
B. The program cannot compile because the compiler cannot determine which max method should be invoked.
C. The program runs and prints 2 followed by “max(int, double)” is invoked.
D. The program runs and prints 2 followed by “max(double, int)” is invoked.
E. The program runs and prints “max(int, double) is invoked” followed by 2.
B. The program cannot compile because the compiler cannot determine which max method should be invoked.