📖 Scenario: You are creating a simple Java program to greet users by name. This program will help you understand how to declare and use methods in Java.
🎯 Goal: Build a Java program that declares a method called greetUser which takes a String parameter called name and prints a greeting message including that name.
📋 What You'll Learn
Declare a method named greetUser that takes one String parameter called name
The method should print the message: Hello, <name>! where <name> is the parameter value
Call the greetUser method from the main method with the argument "Alice"
Print the greeting message to the console
💡 Why This Matters
🌍 Real World
Methods are used in all Java programs to organize code into small, reusable pieces that perform specific tasks.
💼 Career
Understanding method declaration is essential for any Java developer, as it is the foundation for writing clean, maintainable, and reusable code.
Progress0 / 4 steps
1
Create the main class and main method
Create a public class named Greeting and inside it, write the main method with the exact signature public static void main(String[] args).
Java
Hint
The main method is the entry point of a Java program. Use the exact signature public static void main(String[] args).
2
Declare the greetUser method
Inside the Greeting class but outside the main method, declare a public static method named greetUser that takes a String parameter called name and returns nothing (void).
Java
Hint
Method declaration syntax: public static void greetUser(String name) means the method is public, static, returns nothing, and takes one String parameter.
3
Add print statement inside greetUser method
Inside the greetUser method, write a System.out.println statement that prints the message Hello, <name>! using the parameter name with string concatenation.
Java
Hint
Use System.out.println("Hello, " + name + "!") to print the greeting message.
4
Call greetUser method from main
Inside the main method, call the greetUser method with the argument "Alice" to display the greeting message.
Java
Hint
Call the method by writing greetUser("Alice"); inside the main method.
Practice
(1/5)
1. What is the correct way to declare a method in Java that returns an integer and takes no parameters?
easy
A. int getNumber { return 5; }
B. public int getNumber() { return 5; }
C. public getNumber() int { return 5; }
D. public int getNumber[] { return 5; }
Solution
Step 1: Check method signature format
The correct method declaration includes access modifier, return type, method name, and parentheses for parameters.
Step 2: Validate syntax correctness
public int getNumber() { return 5; } correctly uses 'public int getNumber()' with parentheses and a return statement.
Final Answer:
public int getNumber() { return 5; } -> Option B
Quick Check:
Method declaration = public int getNumber() { ... } [OK]
Hint: Method declaration needs return type, name, and parentheses [OK]
Common Mistakes:
Omitting parentheses after method name
Missing return type before method name
Using brackets [] instead of parentheses for parameters
2. Which of the following is a correct method declaration syntax in Java?
easy
A. void printMessage(String message) { System.out.println(message); }
B. void printMessage(String message); { System.out.println(message); }
C. void printMessage String message { System.out.println(message); }
D. void printMessage(String message) System.out.println(message);
Solution
Step 1: Identify correct method header and body syntax
Method header must have parentheses around parameters and body enclosed in braces.
Step 2: Check for syntax errors
void printMessage(String message) { System.out.println(message); } correctly declares method with parameters in parentheses and body in braces.
Final Answer:
void printMessage(String message) { System.out.println(message); } -> Option A
Quick Check:
Method syntax = returnType name(params) { body } [OK]
Hint: Method parameters must be inside parentheses and body inside braces [OK]
Common Mistakes:
Adding semicolon after method header
Missing parentheses around parameters
Omitting braces for method body
3. What will be the output of the following code?
public class Test {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(3, 4));
}
}
medium
A. 0
B. 34
C. 7
D. Error
Solution
Step 1: Understand method call and parameters
The method add takes two integers and returns their sum.
Step 2: Calculate the return value
Calling add(3, 4) returns 3 + 4 = 7, which is printed.
Final Answer:
7 -> Option C
Quick Check:
3 + 4 = 7 [OK]
Hint: Method add returns sum; 3 + 4 = 7 [OK]
Common Mistakes:
Confusing concatenation with addition
Expecting compile error due to static method
Ignoring return value and expecting no output
4. Identify the error in the following method declaration:
public int multiply(int x, int y)
return x * y;
}
medium
A. Extra semicolon after return statement
B. Missing return type
C. Incorrect parameter types
D. Missing opening brace '{' after method header
Solution
Step 1: Check method header and body braces
The method header must be followed by an opening brace '{' to start the method body.
Step 2: Identify missing brace
The code lacks '{' after the method header, causing a syntax error.
Final Answer:
Missing opening brace '{' after method header -> Option D
Quick Check:
Method body must start with '{' [OK]
Hint: Method body always starts with '{' after header [OK]
Common Mistakes:
Forgetting opening brace '{'
Placing return statement outside method body
Confusing semicolon placement
5. You want to declare a method named filterPositive that takes an array of integers and returns a new array containing only the positive numbers. Which method declaration is correct?
hard
A. public int[] filterPositive(int[] numbers) { /* implementation */ }
B. public int filterPositive(int numbers[]) { /* implementation */ }
C. public void filterPositive(int[] numbers) { /* implementation */ }
D. public int filterPositive(int[] numbers[]) { /* implementation */ }
Solution
Step 1: Determine return type and parameter type
The method returns an array of integers, so return type must be int[]. The parameter is an int array.
Step 2: Check syntax correctness
public int[] filterPositive(int[] numbers) { /* implementation */ } correctly declares return type as int[] and parameter as int[]. Other options have wrong return types or parameter syntax.
Final Answer:
public int[] filterPositive(int[] numbers) { /* implementation */ } -> Option A
Quick Check:
Return type matches output array type [OK]
Hint: Return type must match method output type (int[] for array) [OK]
Common Mistakes:
Using int instead of int[] as return type
Incorrect parameter array syntax
Using void when method returns data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.