1Z0-811 Arrays and Methods Practice Question
A method 'public static int findMax(int[] numbers)' returns the maximum value in the array. Which implementation correctly handles an empty array by returning 0?
⚠ Common exam trap
Many exam-takers choose Option C because they see the empty check but overlook that initializing `max` to 0 instead of the first element causes incorrect results for arrays with all negative numbers, which the exam frequently uses to test understanding of edge cases and initialization logic.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
if(numbers.length == 0) return 0; int max = numbers[0]; for(int i=1; i<numbers.length; i++) if(numbers[i] > max) max = numbers[i]; return max;
Ly handles an empty array by checking `numbers.length == 0` and returning 0 before attempting to access `numbers[0]`, which would throw an `ArrayIndexOutOfBoundsException` on an empty array. It then initializes `max` to the first element and iterates from index 1, ensuring all elements are compared correctly even if all numbers are negative.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
int max = 0; for(int n: numbers) if(n > max) max = n; return max;
Why it's wrong here
No empty check; initializes max to 0; fails for all negative numbers.
- ✗
int max = numbers[0]; for(int i=1; i<numbers.length; i++) if(numbers[i] > max) max = numbers[i]; return max;
Why it's wrong here
No check for empty array; throws exception.
- ✗
if(numbers.length == 0) return 0; int max = 0; for(int n: numbers) if(n > max) max = n; return max;
Why it's wrong here
Initializes max to 0; fails if all numbers negative.
- ✓
if(numbers.length == 0) return 0; int max = numbers[0]; for(int i=1; i<numbers.length; i++) if(numbers[i] > max) max = numbers[i]; return max;
Why this is correct
Correctly handles empty and non-empty arrays.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.