CJCoding With Joseph
15per day
← Back to Question List
Java Code Walkthrough #33

Return Stops a Loop Early

mediumFunctions

📄 Code

Read carefully — what does this print?
1public class Main {
2 
3 static int firstEven(int[] nums) {
4 for (int i = 0; i < nums.length; i++) {
5 if (nums[i] % 2 == 0) {
6 return nums[i];
7 }
8 }
9 return -1;
10 }
11 
12 public static void main(String[] args) {
13 int[] data = {3, 5, 7, 8, 10};
14 System.out.println(firstEven(data));
15 }
16}

🎯 Your Answer