Bird
Raised Fist0
Javaprogramming~20 mins

Method parameters in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Method Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method with primitive parameter modification
What is the output of the following Java code?
Java
public class Test {
    public static void modify(int x) {
        x = x + 10;
    }
    public static void main(String[] args) {
        int a = 5;
        modify(a);
        System.out.println(a);
    }
}
A5
B15
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Think about how Java passes primitive types to methods.
❓ Predict Output
intermediate
2:00remaining
Output of method with object parameter modification
What is the output of this Java program?
Java
class Box {
    int value;
    Box(int v) { value = v; }
}
public class Test {
    public static void modify(Box b) {
        b.value = 10;
    }
    public static void main(String[] args) {
        Box box = new Box(5);
        modify(box);
        System.out.println(box.value);
    }
}
A10
BRuntime error
CCompilation error
D5
Attempts:
2 left
💡 Hint
Consider how Java passes object references to methods.
❓ Predict Output
advanced
2:00remaining
Output of method with varargs parameter
What will be printed when this Java code runs?
Java
public class Test {
    public static int sum(int... numbers) {
        int total = 0;
        for (int n : numbers) {
            total += n;
        }
        return total;
    }
    public static void main(String[] args) {
        System.out.println(sum(1, 2, 3));
    }
}
ARuntime error
B123
CCompilation error
D6
Attempts:
2 left
💡 Hint
Varargs allow passing multiple arguments as an array.
❓ Predict Output
advanced
2:00remaining
Output of method with overloaded parameters
What is the output of this Java program?
Java
public class Test {
    public static void print(int x) {
        System.out.println("int: " + x);
    }
    public static void print(String x) {
        System.out.println("String: " + x);
    }
    public static void main(String[] args) {
        print(5);
        print(null);
    }
}
ACompilation error
Bint: 5\nString: null
Cint: 5\nRuntime error
Dint: 5\nCompilation error
Attempts:
2 left
💡 Hint
Consider how Java resolves overloaded methods with null argument.
❓ Predict Output
expert
2:00remaining
Output of method with final parameter and reassignment attempt
What happens when this Java code runs?
Java
public class Test {
    public static void modify(final int x) {
        // x = x + 1; // Uncommenting this line causes what?
        System.out.println(x);
    }
    public static void main(String[] args) {
        modify(5);
    }
}
ARuntime error
BCompilation error due to reassignment of final parameter
CPrints 5
DPrints 6
Attempts:
2 left
💡 Hint
Final parameters cannot be reassigned inside the method.

Practice

(1/5)
1. What is the purpose of method parameters in Java?
public void greet(String name) { ... }
easy
A. To declare variables inside the method
B. To define the method's return type
C. To send data into methods so they can use it inside
D. To specify the method's access level

Solution

  1. Step 1: Understand method parameters

    Method parameters are variables listed in the method definition that accept values when the method is called.
  2. Step 2: Identify their purpose

    They allow sending data into the method so it can perform actions using that data.
  3. Final Answer:

    To send data into methods so they can use it inside -> Option C
  4. Quick Check:

    Parameters pass data into methods [OK]
Hint: Parameters pass info into methods for flexible use [OK]
Common Mistakes:
  • Confusing parameters with return types
  • Thinking parameters declare variables inside method body
  • Mixing parameters with access modifiers
2. Which of the following is the correct syntax to declare a method with two parameters in Java?
easy
A. public void add(int a; int b) { }
B. public void add(int a, int b) { }
C. public void add(int a int b) { }
D. public void add(int a & int b) { }

Solution

  1. Step 1: Recall parameter syntax

    Parameters are listed inside parentheses separated by commas, each with a type and name.
  2. Step 2: Check each option

    public void add(int a, int b) { } uses commas correctly; others use semicolon, missing comma, or invalid symbol.
  3. Final Answer:

    public void add(int a, int b) { } -> Option B
  4. Quick Check:

    Parameters separated by commas [OK]
Hint: Parameters must be comma-separated with type and name [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Omitting commas between parameters
  • Using invalid symbols like &
3. What will be the output of this Java code?
public class Test {
  public static void printSum(int x, int y) {
    System.out.println(x + y);
  }
  public static void main(String[] args) {
    printSum(5, 3);
  }
}
medium
A. 8
B. 53
C. x + y
D. Compile error

Solution

  1. Step 1: Understand method call with parameters

    The method printSum is called with 5 and 3 as arguments, assigned to x and y.
  2. Step 2: Calculate the sum inside the method

    Inside printSum, it prints x + y which is 5 + 3 = 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Sum parameters values to get output [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting variable names printed
  • Thinking code won't compile
4. Identify the error in this method declaration:
public void displayMessage(String message, int) {
  System.out.println(message);
}
medium
A. Parameter list missing variable name for int type
B. Method must return a value
C. Cannot use String as parameter type
D. System.out.println is invalid here

Solution

  1. Step 1: Check parameter list syntax

    Each parameter must have a type and a variable name. Here, int has no name.
  2. Step 2: Identify the error

    Missing variable name after int causes a syntax error.
  3. Final Answer:

    Parameter list missing variable name for int type -> Option A
  4. Quick Check:

    Parameters need type and name [OK]
Hint: Every parameter needs a name after its type [OK]
Common Mistakes:
  • Omitting parameter names
  • Thinking return type is missing
  • Misunderstanding print statement usage
5. You want to create a method that calculates the area of a rectangle. Which method signature correctly uses parameters to accept the rectangle's width and height as doubles?
hard
A. public void area(double width; double height)
B. public double area(width, height)
C. public double area(int width, int height)
D. public double area(double width, double height)

Solution

  1. Step 1: Identify correct parameter types and syntax

    The method should accept two double parameters named width and height, separated by commas.
  2. Step 2: Evaluate each option

    public double area(double width, double height) correctly declares two double parameters with names and commas; public double area(width, height) misses types; public double area(int width, int height) uses int instead of double; public void area(double width; double height) uses semicolon instead of comma and void return type.
  3. Final Answer:

    public double area(double width, double height) -> Option D
  4. Quick Check:

    Parameters need type, name, comma-separated [OK]
Hint: Use type and name with commas for parameters [OK]
Common Mistakes:
  • Omitting parameter types
  • Using wrong separators like semicolons
  • Using wrong data types for precision
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