Skip to content
Study Hall
← 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.
  • void means the method hands nothing back; any other return type demands a return on every path.
  • return supplies 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, not CalculateAverage or avg.
  • 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 is or has.
  • 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 main must be static, because main runs without an object.

Where marks get lost

  • Declaring a method void and then trying to store its result — int x = printTotal(); will not compile, because void produces no value. (Writing and Calling Methods)
  • Forgetting return in 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 the score = 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 static on 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.

  1. Writing and Calling Methods
    1
    What does a return type of void tell you about a method?
  2. Writing and Calling Methods
    2
    Why does public static int biggest(int a, int b) { if (a > b) { return a; } } fail to compile?
  3. Writing and Calling Methods
    3
    Order what Java does when it reaches the line int result = square(7);
    1. 1Store 49 in result and continue with the next line of main
    2. 2Hit return and send 49 back to the call site
    3. 3Run the body of square, computing 7 * 7
    4. 4Pause the current line in main
    5. 5Copy the argument 7 into the parameter n
  4. Writing and Calling Methods
    4
    Match each part of public static double average(int total, int count) to its meaning.
    public
    static
    double
    average
    (int total, int count)
  5. Writing and Calling Methods
    5
    Given public static int triple(int n) { return n * 3; }, what is printed by System.out.println(triple(4) + triple(2));?
  6. Writing and Calling Methods
    6
    A method prints the average instead of returning it. Why is that usually the weaker design?
  7. Parameters, Arguments and Scope
    7
    int score = 5; addTen(score); where addTen is void addTen(int number) { number += 10; }. What is score afterwards?
  8. Parameters, Arguments and Scope
    8
    With public static int addTen(int n) { return n + 10; }, what is score after int score = 5; score = addTen(score);?
  9. Parameters, Arguments and Scope
    9
    for (int i = 0; i < 5; i++) { ... } System.out.println(i); gives cannot find symbol. Why?
  10. Parameters, Arguments and Scope
    10
    What is printed? int total = 0; for (int i = 1; i <= 3; i++) { int d = i * 2; total += d; } System.out.println(total);
  11. Parameters, Arguments and Scope
    11
    Match each term to its meaning.
    Parameter
    Argument
    Pass by value
    Scope
    Overloading
  12. Parameters, Arguments and Scope
    12
    Give the term for two methods with the same name but different parameter lists.
  13. Decomposition and Program Design
    13
    Which is the clearest sign that a method is doing too much?
  14. Decomposition and Program Design
    14
    Order the steps of top-down design.
    1. 1Write stubs so the program compiles and runs
    2. 2Name each piece with a verb and write its signature
    3. 3Replace one stub at a time, testing after each
    4. 4State the whole job in one sentence
    5. 5Split it into a handful of major steps
    6. 6Split any step that is still too big to write directly
  15. Decomposition and Program Design
    15
    A 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?
  16. Decomposition and Program Design
    16
    Using the decomposed MarkReport, what average is printed for the marks 90, 80, 70, 60 and 50?
  17. Decomposition and Program Design
    17
    Match each job to the best method signature for it.
    Produce the largest value in an array
    Turn an average into an achievement level
    Display the finished report
    Read one mark, rejecting anything out of range
  18. Decomposition and Program Design
    18
    Which 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…

Keep going