Oracle Java SE 21 Developer Professional Exam Practice Test
The Oracle Java SE 21 Developer Professional Exam practice covers Java code comprehension: evaluating expressions, tracing control flow, understanding references and inheritance, and interpreting core language features. The available questions include loops, arrays, strings, local variable inference with `var`, static method hiding, and resource cleanup.
Exam overview
About the Oracle Java SE 21 Developer Professional Exam
Java developer work often requires predicting how code behaves before changing or debugging it. Practice with expression precedence and compound assignment, pre- and post-increment, and the difference between changing an enhanced-for loop variable and changing an array element. Distinguish reference types and object state from primitive values, and determine how static methods are resolved from a reference type rather than dispatched polymorphically. Exception-handling questions call for understanding when `finally` executes and how try-with-resources closes resources. These skills support reviewing unfamiliar Java code, spotting logic errors, and making safe edits. The question bank also samples declarations and basic control flow, so trace each statement in order and account for the exact scope and type of every variable.
Sample Practice Questions
Review a selection from this exam's published US Practice Tests practice bank.
What does this code print? `int count = 0; while (count < 3) { count++; } System.out.println(count);`
Show answer
Correct answer: BThe loop increments `count` while it is less than 3. After the third increment, `count` is 3, the condition is false, and that value is printed.
What is the value of `n` after this statement completes? `int n = 2; n *= 3 + 1;`
Show answer
Correct answer: BThe addition on the right-hand side is evaluated first, producing 4. The compound assignment then multiplies `n` by 4, giving `2 * 4`, or 8.
What does the following code print? `int total = 0; for (int n : new int[] {2, 3, 4}) { if (n == 3) continue; total += n; } System.out.println(total);`
Show answer
Correct answer: BWhen `n` is 3, `continue` skips the addition for that iteration. The remaining values, 2 and 4, are added, giving 6.
Which statement about a Java `String` is correct?
Show answer
Correct answer: BJava strings are immutable. Methods such as `toUpperCase()` return a result string and do not change the original string object.
Given `class Parent { static String tag() { return "P"; } }` and `class Child extends Parent { static String tag() { return "C"; } }`, what is printed by `Parent item = new Child(); System.out.println(item.tag());`?
Show answer
Correct answer: AStatic methods are hidden rather than dynamically overridden. The method selected is based on the reference's compile-time type, which is `Parent`.
Which statement about `var` in a local variable declaration is correct?
Show answer
Correct answer: BFor a local variable declared with `var`, the compiler infers a static type from the initializer. `var` is not permitted for fields or method return types.
What is the result of this code? `int[] scores = {4, 7, 9}; for (int score : scores) { score *= 2; } System.out.println(scores[1]);`
Show answer
Correct answer: AFor an array of primitive values, the enhanced `for` loop variable receives a copy of each element. Changing `score` does not update the corresponding array element, so `scores[1]` remains 7.
How to use this practice test
Access is free. A session presents up to 20 random questions from the 50 published items in this exam’s bank. Answer by tracing code and applying Java language rules; review the results after the session to identify which concepts need another pass.
Preparation tips
- For expression questions, write intermediate values for precedence, compound assignment, and pre- versus post-increment before selecting an output.
- Trace loops one iteration at a time, noting when `continue` skips statements and whether an enhanced-for variable is a copy of an array element’s value.
- Compare the declared reference type with the runtime object when reasoning about inheritance; check whether a method is static before assuming dynamic dispatch.
- Review local `var` rules by identifying the initializer’s inferred type and checking whether the declaration context permits inference.
- For exception questions, distinguish normal completion from abrupt completion and account for automatic resource closing in try-with-resources.