Method Signatures and Calling Methods 🌶️🌶️
A few key ideas this worksheet checks:
- Procedural abstraction: you can use a method correctly just from its documentation, without ever needing to know how it is implemented internally.
- A method's signature is its name plus the ordered list of its parameter TYPES — not the parameter names, and not the return type.
- Whether a method is void or non-void changes how it can legally be used: a void method is called as its own statement, never as part of an expression.
- Arguments are passed by value: a method receives a COPY of whatever value you pass in.
- Two methods can share a name (be "overloaded") only if their parameter lists genuinely differ.
- Calling a method interrupts the normal top-to-bottom flow of a program until that method finishes running.
1. A programmer uses a method from a library correctly, based only on its documentation, without ever looking at its source code. What is this an example of?
2. Which of these correctly makes up a method's signature?
3. What is the method signature of public double convert(int amount, String unit)? Write it in the form methodName(Type1, Type2) — for example, a method public void log(String msg, int level) would have the signature log(String, int).
convert(int, String)
4. Given public void logMessage(String msg), which of these is legal Java?
5. Given public int getScore(), which of these correctly captures and uses its return value?
6. A variable x holding the value 5 is passed as an argument to a method. Inside the method, the corresponding parameter is changed to 100. What happens to x back in the caller after the method returns?
7. A method has the signature computeArea(int, int). Which of these calls is valid?
8. Which of these is a valid example of two overloaded methods?
9. Trace through the code below. What does it print, one line per println, in the order it actually executes?
public static void main(String[] args) { System.out.println("start"); greet(); System.out.println("end");}public static void greet() { System.out.println("hello");}
start hello end
10. Consider these four method headers, each proposed as part of the same class:
1. public int getTotal(int x)2. public double getTotal(int x)3. public int getTotal(int x, int y)4. public int getTotal(double x)
Which two of these, if both included in the same class, would cause a compile error?
11. Consider these four method headers, each proposed as part of the same class:
1. public void printLabel(String text)2. public void printLabel(String message)3. public void printLabel(String text, int count)4. public int printLabel(int count)
Which two of these, if both included in the same class, would cause a compile error?
Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.