📖 Scenario: Imagine you are checking a list of numbers to find if any number is even. Once you find the first even number, you want to stop checking and return that number immediately.
🎯 Goal: You will write a Java method that looks through an array of integers and returns the first even number it finds. If no even number is found, it returns -1.
📋 What You'll Learn
Create an integer array called numbers with the exact values: 3, 7, 10, 5, 8.
Create an integer variable called notFound and set it to -1.
Write a method called findFirstEven that takes an integer array parameter called nums and uses a for loop with variable i to check each number.
Inside the loop, use return to immediately return the first even number found.
If no even number is found after the loop, return the value of notFound.
In the main method, call findFirstEven(numbers) and print the returned value.
💡 Why This Matters
🌍 Real World
Finding the first item that meets a condition quickly is common in many programs, like searching for the first available seat or the first error in a list.
💼 Career
Understanding how to return early from loops helps write efficient code and is a common task in software development interviews and real projects.
Progress0 / 4 steps
1
Create the integer array
Create an integer array called numbers with these exact values: 3, 7, 10, 5, 8.
Java
Hint
Use int[] numbers = {3, 7, 10, 5, 8}; to create the array.
2
Create the notFound variable
Create an integer variable called notFound and set it to -1.
Java
Hint
Write int notFound = -1; to create the variable.
3
Write the findFirstEven method
Write a method called findFirstEven that takes an integer array parameter called nums. Use a for loop with variable i to check each number. Inside the loop, use return to immediately return the first even number found. After the loop, return the value of notFound.
Java
Hint
Use a for loop and check if each number is even using nums[i] % 2 == 0. Return the number immediately if even.
4
Call the method and print the result
In the main method, call findFirstEven(numbers) and print the returned value using System.out.println.
Java
Hint
Use System.out.println(findFirstEven(numbers)); inside main to print the first even number.
Practice
(1/5)
1. What happens when a return statement is executed inside a loop in a Java method?
easy
A. The method stops immediately and returns the specified value.
B. The loop skips the current iteration and continues.
C. The loop finishes all iterations before returning.
D. The method throws an error and stops.
Solution
Step 1: Understand the role of return in methods
The return statement immediately ends the method and sends back a value to the caller.
Step 2: Effect of return inside a loop
When return is inside a loop, the method stops as soon as return runs, so the loop does not continue.
Final Answer:
The method stops immediately and returns the specified value. -> Option A
Hint: Return must match method type and be valid expression [OK]
Common Mistakes:
Omitting return value in non-void method
Using invalid expressions after return
Syntax errors like missing semicolons or operators
3. What is the output of this Java method?
public static int findFirstEven(int[] nums) {
for (int n : nums) {
if (n % 2 == 0) {
return n;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(findFirstEven(new int[]{3, 7, 8, 10}));
}
medium
A. 3
B. 7
C. 8
D. -1
Solution
Step 1: Trace the loop over array elements
The loop checks each number: 3 (odd), 7 (odd), then 8 (even).
Step 2: Return on first even number
When 8 is found even, method returns 8 immediately, stopping the loop.
Final Answer:
8 -> Option C
Quick Check:
First even number returned = 8 [OK]
Hint: Return stops loop at first matching condition [OK]
Common Mistakes:
Assuming method returns last even number
Ignoring return stops loop early
Confusing output with array contents
4. Identify the error in this Java method:
public static int findPositive(int[] arr) {
for (int x : arr) {
if (x > 0) {
return x;
} else {
return 0;
}
}
return -1;
}
medium
A. The method should return a boolean, not int.
B. The method has a syntax error in the for loop.
C. The method never returns a value.
D. The method returns 0 too early, not checking all elements.
Solution
Step 1: Analyze the return statements inside the loop
Inside the loop, both if and else branches have return, so method returns on first element always.
Step 2: Understand the logic flaw
If first element is not positive, method returns 0 immediately without checking others.
Final Answer:
The method returns 0 too early, not checking all elements. -> Option D
Quick Check:
Return inside else stops loop too soon [OK]
Hint: Return in both if and else inside loop stops early [OK]
Common Mistakes:
Expecting loop to check all elements
Ignoring that return ends method immediately
Confusing return with continue or break
5. You want to write a method that returns the first negative number from an array, or 0 if none found. Which code correctly uses return inside a loop to do this efficiently?
hard
A. public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) return n; } return 0; }
B. public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) return n; else return 0; } }
C. public static int firstNegative(int[] arr) { int result = 0; for(int n : arr) { if(n < 0) result = n; } return result; }
D. public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) break; } return 0; }
Solution
Step 1: Check return usage inside loop
public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) return n; } return 0; } returns immediately when first negative found, stopping loop early.
Step 2: Verify correct fallback return
If no negative found, method returns 0 after loop finishes, correctly.
Step 3: Identify flaws in other options
public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) return n; else return 0; } } returns 0 too early inside loop. public static int firstNegative(int[] arr) { int result = 0; for(int n : arr) { if(n < 0) result = n; } return result; } updates result but does not return early. public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) break; } return 0; } breaks loop but always returns 0, ignoring negatives.
Final Answer:
public static int firstNegative(int[] arr) { for(int n : arr) { if(n < 0) return n; } return 0; } -> Option A
Quick Check:
Return inside loop for first match, fallback after loop [OK]
Hint: Return immediately on first match, fallback after loop ends [OK]
Common Mistakes:
Returning fallback inside loop too early
Not returning immediately on first match
Using break without return causing wrong output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.