banner Test 11

Test 11 - Free Java SE 25 certification Mock test - Exam Code 1Z0-831-Oracle certified Java SE 25 Professional

Good Java design is all about control of visibility and mutability and that's precisely what this free mock test for the Oracle 1Z0-831 exam is all about. It covers variable scope, encapsulation, immutable objects, local variable type inference with 'var' and the underscore as the unnamed variable. All of those are OOP topics that you'll come across in the object oriented objective.

Scope questions come first. You decide where a local variable, parameter, loop counter or pattern variable is visible and whether a declaration shadows a field or conflicts with an existing local name. Snippets use nested blocks, lambda bodies and switch cases as Java doesn't allow a lambda parameter to re-use a local variable name from the enclosing method.

Encapsulation items look at access modifiers across packages and subclasses. You'll be assessing whether a private, package-private, protected or public member is accessible from a given class (including the rather subtle case of protected access via a subclass reference in another package). You'll also be looking at how getters and setters can be used to protect invariants and how returning an internal mutable array or list will in fact break that protection.

Immutability is given a very careful treatment. You'll be looking at the recipe for an immutable class: final class (or private constructor), private final fields, no setters and defensive copies of any mutable inputs and outputs. There are a number of near-miss designs that you'll be asked to spot (e.g. a class that just stores a caller's Date or List directly) and identify the problem. You'll also see the difference between List.of and Collections.unmodifiableList (one is a copy, one is a view).

Type inference is also tested. var for local variables only works where you have an initializer, it doesn't work for fields or parameters and won't work if you have null or an array initializer with no type specified. You'll be given a number of tricky expressions and asked to work out what type they'll infer. The underscore as an unnamed variable is used in catch blocks, lambdas, patterns and enhanced for loops and you'll be asked to decide when it's valid. Note static nested access as private members are accessible throughout their top level class which is a bit of a surprise to a lot of candidates who would expect a much more restricted scope.

Explanations refer to the specific rule that underpins each answer. Complete the test, go through every miss and then go on to try and rewrite a mutable class as an immutable one. That really drives the point home far more effectively than simply re-reading the notes.

Welcome to your Free Java SE 25 – Test 11