← Unit 5: Methods
ICS3U · Unit 5 test review
Methods: Test Review
Everything for the Unit 5 test on one page: the key facts from all 3 lessons, the mistakes that cost the most marks, and a practice test that points you back to the lessons you need.
Key facts
Writing and Calling Methods
- A signature is: access modifier,
static, return type, name, then the parameter list in brackets. voidmeans the method hands nothing back; any other return type demands areturnon every path.returnsupplies the value and ends the method immediately — nothing after it runs.- A call pauses the current line, runs the method, and resumes at exactly the same point with the returned value.
- Method names are camelCase verbs starting with a lowercase letter:
calculateAverage, notCalculateAverageoravg. - Methods are written inside the class but outside every other method; they can never be nested.
Parameters, Arguments and Scope
- A parameter is in the definition and has a type; an argument is at the call and is a value.
- Java matches arguments to parameters by position — number, order and type must all agree.
- Java is pass by value: a primitive argument is copied, so reassigning the parameter never changes the caller's variable.
- To make a change stick, return the new value and assign it:
score = addTen(score); - A variable exists from its declaration to the closing brace of its block, and nowhere else.
- Overloading means same name, different parameter list. A different return type alone is not enough.
Decomposition and Program Design
- One method, one job — if you need the word "and" to describe it, split it.
- Method names are verbs or verb phrases in camelCase; boolean methods start with
isorhas. - Top-down design: split the job into steps, then split any step that is still too big, before writing any logic.
- A stub has the correct signature and a placeholder body so the program compiles and runs while it is unfinished.
- Test one method at a time as you replace each stub, so any new bug is in the piece you just wrote.
- Every method called directly from
mainmust bestatic, becausemainruns without an object.
Where marks get lost
- Declaring a method
voidand then trying to store its result —int x = printTotal();will not compile, because void produces no value. (Writing and Calling Methods) - Forgetting
returnin a non-void method, giving the compile error missing return statement at the method's closing brace. (Writing and Calling Methods) - Calling a returning method as a bare statement, so the value is computed and silently discarded. (Writing and Calling Methods)
- Printing inside a method that was supposed to return, then finding it cannot be reused anywhere the value is needed rather than displayed. (Writing and Calling Methods)
- Writing the parameter's type at the call site —
square(int 7)— when types belong only in the definition. (Writing and Calling Methods) - Passing arguments in the wrong order when they are the same type — the program compiles perfectly and quietly computes the wrong answer. (Parameters, Arguments and Scope)
- Expecting a method to change the caller's variable by reassigning a parameter; with primitives, it never can. (Parameters, Arguments and Scope)
- Calling
addTen(score);and forgetting thescore =part, so the returned value is computed and discarded. (Parameters, Arguments and Scope) - Using a loop counter after the loop has ended, when the header's declaration limited it to the loop. (Parameters, Arguments and Scope)
- Giving a local variable the same name as a class variable and then wondering why the class variable never changes. (Parameters, Arguments and Scope)
- Writing every method before running anything, so the first test produces a pile of errors with no way to tell which method caused which. (Decomposition and Program Design)
- Naming methods with nouns or with nothing in particular —
data(),stuff(),calc()— which forces a reader to open the body to learn what it does. (Decomposition and Program Design) - Forgetting
staticon a helper, then getting non-static method cannot be referenced from a static context and not recognizing the cause. (Decomposition and Program Design) - Making a method both calculate and print, so it cannot be reused anywhere the value is needed but should not be displayed. (Decomposition and Program Design)
- Leaving a stub in place after moving on, so the program quietly reports 0.0 for something that was never finished. (Decomposition and Program Design)
Practice test
18 questions from across the unit. Each one shows the lesson it comes from.
- Writing and Calling Methods1What does a return type of
voidtell you about a method? - Writing and Calling Methods2Why does
public static int biggest(int a, int b) { if (a > b) { return a; } }fail to compile? - Writing and Calling Methods3Order what Java does when it reaches the line
int result = square(7);- 1Store 49 in result and continue with the next line of main
- 2Hit return and send 49 back to the call site
- 3Run the body of square, computing 7 * 7
- 4Pause the current line in main
- 5Copy the argument 7 into the parameter n
- Writing and Calling Methods4Match each part of
public static double average(int total, int count)to its meaning.publicstaticdoubleaverage(int total, int count) - Writing and Calling Methods5Given
public static int triple(int n) { return n * 3; }, what is printed bySystem.out.println(triple(4) + triple(2));? - Writing and Calling Methods6A method prints the average instead of returning it. Why is that usually the weaker design?
- Parameters, Arguments and Scope7
int score = 5; addTen(score);whereaddTenisvoid addTen(int number) { number += 10; }. What isscoreafterwards? - Parameters, Arguments and Scope8With
public static int addTen(int n) { return n + 10; }, what isscoreafterint score = 5; score = addTen(score);? - Parameters, Arguments and Scope9
for (int i = 0; i < 5; i++) { ... } System.out.println(i);gives cannot find symbol. Why? - Parameters, Arguments and Scope10What is printed?
int total = 0; for (int i = 1; i <= 3; i++) { int d = i * 2; total += d; } System.out.println(total); - Parameters, Arguments and Scope11Match each term to its meaning.ParameterArgumentPass by valueScopeOverloading
- Parameters, Arguments and Scope12Give the term for two methods with the same name but different parameter lists.
- Decomposition and Program Design13Which is the clearest sign that a method is doing too much?
- Decomposition and Program Design14Order the steps of top-down design.
- 1Write stubs so the program compiles and runs
- 2Name each piece with a verb and write its signature
- 3Replace one stub at a time, testing after each
- 4State the whole job in one sentence
- 5Split it into a handful of major steps
- 6Split any step that is still too big to write directly
- Decomposition and Program Design15A helper method gives the error non-static method cannot be referenced from a static context when called from main. What is the fix in a single-class program?
- Decomposition and Program Design16Using the decomposed MarkReport, what average is printed for the marks 90, 80, 70, 60 and 50?
- Decomposition and Program Design17Match each job to the best method signature for it.Produce the largest value in an arrayTurn an average into an achievement levelDisplay the finished reportRead one mark, rejecting anything out of range
- Decomposition and Program Design18Which principle says that the same logic should not be written out in several places?
Endless drill
Fresh numbers every time, for the calculation questions in this unit.
Endless drillnew numbers every time
Generating a problem…