0
0
Javaprogramming~20 mins

Return inside loops in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Return Loop 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 code with return inside a loop?

Consider the following Java method. What will be printed when testReturn() is called?

Java
public class Test {
    public static int testReturn() {
        for (int i = 0; i < 5; i++) {
            if (i == 2) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(testReturn());
    }
}
A2
B5
CCompilation error
D-1
Attempts:
2 left
💡 Hint

Remember that return exits the method immediately.

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

What value does the method findFirstEven return when called?

Java
public class Test {
    public static int findFirstEven(int[] numbers) {
        for (int num : numbers) {
            if (num % 2 == 0) {
                return num;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(findFirstEven(new int[]{1, 3, 5, 6, 8}));
    }
}
A5
B1
C-1
D6
Attempts:
2 left
💡 Hint

The method returns the first even number it finds.

🔧 Debug
advanced
2:00remaining
What error occurs in this code with return inside a loop?

What error does this Java code produce when compiled?

Java
public class Test {
    public static int test() {
        for (int i = 0; i < 3; i++) {
            if (i == 1) {
                return i;
            }
        }
        // Missing return statement here
        return -1;
    }
}
ACompilation error: missing return statement
BRuntime exception: NullPointerException
CReturns 1
DCompilation error: unreachable code
Attempts:
2 left
💡 Hint

Check if all code paths return a value.

Predict Output
advanced
2:00remaining
What is printed by this method with return inside nested loops?

What does the following Java method print when called?

Java
public class Test {
    public static int nestedReturn() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i * j > 2) {
                    return i + j;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(nestedReturn());
    }
}
A-1
B3
C4
D5
Attempts:
2 left
💡 Hint

Look for the first time i * j > 2 is true.

Predict Output
expert
2:00remaining
What is the output of this method with return inside a loop and try-catch?

What does this Java method print when called?

Java
public class Test {
    public static int trickyReturn() {
        for (int i = 0; i < 3; i++) {
            try {
                if (i == 1) {
                    return i;
                }
            } catch (Exception e) {
                return -1;
            }
        }
        return -2;
    }

    public static void main(String[] args) {
        System.out.println(trickyReturn());
    }
}
A-1
B1
C-2
DCompilation error
Attempts:
2 left
💡 Hint

Consider if any exception is thrown inside the try block.