Bird
Raised Fist0
Javaprogramming~20 mins

Method declaration in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a method with parameters
What is the output of the following Java program?
Java
public class Test {
    public static int multiply(int a, int b) {
        return a * b;
    }
    public static void main(String[] args) {
        System.out.println(multiply(3, 4));
    }
}
A7
B12
C34
DError: method multiply not found
Attempts:
2 left
💡 Hint
Look at what the multiply method returns when given 3 and 4.
❓ Predict Output
intermediate
2:00remaining
Return type and output
What will be printed when this Java code runs?
Java
public class Demo {
    public static String greet() {
        return "Hello!";
    }
    public static void main(String[] args) {
        System.out.println(greet());
    }
}
Anull
Bgreet
CHello!
DCompilation error
Attempts:
2 left
💡 Hint
The greet method returns a string. What does it return exactly?
❓ Predict Output
advanced
2:00remaining
Method overloading output
What is the output of this Java program with overloaded methods?
Java
public class Overload {
    public static int add(int a, int b) {
        return a + b;
    }
    public static double add(double a, double b) {
        return a + b;
    }
    public static void main(String[] args) {
        System.out.println(add(2, 3));
        System.out.println(add(2.0, 3.0));
    }
}
A
5.0
5
B
5
5
CCompilation error due to ambiguous method call
D
5
5.0
Attempts:
2 left
💡 Hint
Check which add method is called for int and double arguments.
❓ Predict Output
advanced
2:00remaining
Void method and output
What will be printed when this Java program runs?
Java
public class VoidTest {
    public static void printMessage() {
        System.out.println("Hi there!");
    }
    public static void main(String[] args) {
        printMessage();
        System.out.println("Done");
    }
}
A
Hi there!
Done
B
Done
Hi there!
CHi there!Done
DCompilation error: void method used in print statement
Attempts:
2 left
💡 Hint
Void methods do not return a value but can print inside.
❓ Predict Output
expert
2:00remaining
Method recursion output
What is the output of this Java program using recursion?
Java
public class Recursion {
    public static int factorial(int n) {
        if (n <= 1) return 1;
        else return n * factorial(n - 1);
    }
    public static void main(String[] args) {
        System.out.println(factorial(4));
    }
}
A24
B10
CError: StackOverflowError
D1
Attempts:
2 left
💡 Hint
Factorial of 4 is 4 * 3 * 2 * 1.

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

  1. Step 1: Check method signature format

    The correct method declaration includes access modifier, return type, method name, and parentheses for parameters.
  2. Step 2: Validate syntax correctness

    public int getNumber() { return 5; } correctly uses 'public int getNumber()' with parentheses and a return statement.
  3. Final Answer:

    public int getNumber() { return 5; } -> Option B
  4. 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

  1. Step 1: Identify correct method header and body syntax

    Method header must have parentheses around parameters and body enclosed in braces.
  2. 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.
  3. Final Answer:

    void printMessage(String message) { System.out.println(message); } -> Option A
  4. 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

  1. Step 1: Understand method call and parameters

    The method add takes two integers and returns their sum.
  2. Step 2: Calculate the return value

    Calling add(3, 4) returns 3 + 4 = 7, which is printed.
  3. Final Answer:

    7 -> Option C
  4. 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

  1. Step 1: Check method header and body braces

    The method header must be followed by an opening brace '{' to start the method body.
  2. Step 2: Identify missing brace

    The code lacks '{' after the method header, causing a syntax error.
  3. Final Answer:

    Missing opening brace '{' after method header -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    public int[] filterPositive(int[] numbers) { /* implementation */ } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard