Bird
Raised Fist0
Javaprogramming~20 mins

Return values in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java method call?

Consider the following Java method:

public static int multiplyByTwo(int x) {
    return x * 2;
}

public static void main(String[] args) {
    System.out.println(multiplyByTwo(5));
}

What will be printed when main runs?

Java
public static int multiplyByTwo(int x) {
    return x * 2;
}

public static void main(String[] args) {
    System.out.println(multiplyByTwo(5));
}
A25
B5
CCompilation error
D10
Attempts:
2 left
💡 Hint

Think about what the method returns when given 5.

❓ Predict Output
intermediate
2:00remaining
What does this method return?

Look at this Java method:

public static String greet(String name) {
    if (name == null) {
        return "Hello, Guest!";
    }
    return "Hello, " + name + "!";
}

public static void main(String[] args) {
    System.out.println(greet(null));
}

What will be printed when main runs?

Java
public static String greet(String name) {
    if (name == null) {
        return "Hello, Guest!";
    }
    return "Hello, " + name + "!";
}

public static void main(String[] args) {
    System.out.println(greet(null));
}
AHello, null!
BHello, Guest!
CHello, !
DNullPointerException
Attempts:
2 left
💡 Hint

Check what happens when the input is null.

❓ Predict Output
advanced
2:00remaining
What is the output of this recursive method?

Consider this Java method:

public static int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

public static void main(String[] args) {
    System.out.println(factorial(4));
}

What will be printed when main runs?

Java
public static int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

public static void main(String[] args) {
    System.out.println(factorial(4));
}
A10
B1
C24
DStackOverflowError
Attempts:
2 left
💡 Hint

Recall how factorial is calculated recursively.

❓ Predict Output
advanced
2:00remaining
What is the value of x after this method call?

Given this Java code:

public static int addFive(int num) {
    num = num + 5;
    return num;
}

public static void main(String[] args) {
    int x = 10;
    addFive(x);
    System.out.println(x);
}

What will be printed when main runs?

Java
public static int addFive(int num) {
    num = num + 5;
    return num;
}

public static void main(String[] args) {
    int x = 10;
    addFive(x);
    System.out.println(x);
}
A10
B15
C5
DCompilation error
Attempts:
2 left
💡 Hint

Remember Java passes primitives by value.

🧠 Conceptual
expert
2:00remaining
Which option causes a compilation error due to return type mismatch?

Which of the following Java methods will cause a compilation error because the return type does not match the returned value?

Apublic static int getNumber() { return 3.14; }
Bpublic static double getDouble() { return 3.14; }
Cpublic static String getText() { return "Hello"; }
Dpublic static boolean isTrue() { return true; }
Attempts:
2 left
💡 Hint

Check if the returned value type matches the declared return type exactly.

Practice

(1/5)
1. What does the return keyword do in a Java method?
easy
A. It stops the program completely.
B. It sends a value back from the method to where it was called.
C. It prints a value to the screen.
D. It declares a new variable inside the method.

Solution

  1. Step 1: Understand the purpose of return

    The return keyword is used inside a method to send a value back to the caller.
  2. Step 2: Differentiate from other actions

    Stopping the program, printing, or declaring variables are different actions and not what return does.
  3. Final Answer:

    It sends a value back from the method to where it was called. -> Option B
  4. Quick Check:

    Return sends value back [OK]
Hint: Return sends data back from method to caller [OK]
Common Mistakes:
  • Thinking return stops the whole program
  • Confusing return with print statements
  • Assuming return declares variables
2. Which of the following method signatures correctly declares a method that returns an integer?
easy
A. public String getNumber() { return 5; }
B. public void getNumber() { return 5; }
C. public int getNumber() { return 5; }
D. public int getNumber() { System.out.println(5); }

Solution

  1. Step 1: Check method return type and return statement

    The method must declare int as return type to return an integer value.
  2. Step 2: Match return type with returned value

    public int getNumber() { return 5; } declares int and returns 5, which is an integer, so it is correct.
  3. Final Answer:

    public int getNumber() { return 5; } -> Option C
  4. Quick Check:

    Return type matches returned value [OK]
Hint: Return type must match returned value type [OK]
Common Mistakes:
  • Using void but returning a value
  • Returning wrong type value
  • Missing return statement in non-void method
3. What is 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. 7
B. 34
C. Error
D. 0

Solution

  1. Step 1: Understand the add method

    The method adds two integers and returns their sum.
  2. Step 2: Calculate add(3, 4)

    3 + 4 equals 7, so the method returns 7.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Add numbers, return sum, print result [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting error due to return
  • Confusing return with print
4. Identify the error in this method:
public int multiply(int x, int y) {
  int result = x * y;
}
medium
A. Method name is invalid.
B. Method return type should be void.
C. Variables x and y are not declared.
D. Method is missing a return statement.

Solution

  1. Step 1: Check method return type

    The method declares it returns an int, so it must return an int value.
  2. Step 2: Verify return statement presence

    The method calculates result but does not return it, so it misses a return statement.
  3. Final Answer:

    Method is missing a return statement. -> Option D
  4. Quick Check:

    Non-void methods must return a value [OK]
Hint: Non-void methods must have return statement [OK]
Common Mistakes:
  • Forgetting to return value in non-void method
  • Changing return type to void without reason
  • Assuming variable declaration is error
5. Given this method:
public static String checkNumber(int n) {
  if (n > 0) return "Positive";
  else if (n < 0) return "Negative";
  else return "Zero";
}

What will checkNumber(0) return?
hard
A. "Zero"
B. "Negative"
C. "Positive"
D. null

Solution

  1. Step 1: Analyze the if-else conditions

    If n > 0 returns "Positive", if n < 0 returns "Negative".
  2. Step 2: Check the else case for n = 0

    Since 0 is neither greater nor less than 0, else returns "Zero".
  3. Final Answer:

    "Zero" -> Option A
  4. Quick Check:

    0 triggers else return "Zero" [OK]
Hint: Check all conditions including else for exact return [OK]
Common Mistakes:
  • Assuming 0 is positive or negative
  • Missing else return case
  • Expecting null when no return
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