banner Test 15

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

Enums are vastly more capable than a simple list of constants and this free Java SE 25 mock test for the 1Z0-831 exam is a perfect illustration of that. You'll be looking at declaring enum types with fields, constructors and methods, using them in switch statements and collections and of course you'll be looking at the various compile time errors that the exam likes to sneak in to short snippets.

The first lot of questions are on declaration rules. Enum constants must come first, comma separated and a semicolon is required if anything follows. Constructors are implicitly private so declaring one public will not compile and you can't call new on an enum. You'll be asked to predict the output when a constructor prints out a message and you'll learn that each constant is constructed exactly once when the enum class is first initialised.

Next are the built-in methods. You've got values to iterate over the constants, valueOf to convert a string, ordinal for position, name for the declared identifier and compareTo for ordering. The items check what valueOf does with an unknown string (it throws an IllegalArgumentException) and why name and toString can be different when toString is overridden.

Constant specific class bodies are another nice feature. A constant can provide its own implementation of an abstract method declared by the enum and questions will arise as to whether a particular design will compile, which constants will be required to implement the method and how polymorphism will work. Enums can also implement interfaces which you'll then combine with lambdas and maps to produce some fairly realistic questions.

Collections tailored to enums are also available. EnumMap and EnumSet iterate in the natural order of declaration and are very compact and fast. Plain old maps aren't guaranteed to iterate in any particular order. You'll also see enums in switch statements (the case labels are unqualified constant names) and in switch expressions (where you can cover all of the constants and thus have no need for a default). Be mindful of enum equality as the double equals operator is safe for constants and will do an identity comparison as you'd expect.

Right at the end we look at enums nested within classes, static imports of enum constants and the implicit final (or sealed) nature of enum types. Each of the explanations gives the rationale for the answer. Take the test, go back and look at the rules you missed and then go ahead and build a small enum with fields and behaviour from memory to really cement the pattern.

Welcome to your Free Java SE 25 – Test 15