AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

10 Enums & records / Checkpoint

Checkpoint — Enums & records

22 min · +25 XP · 3 exercises · 12 questions

Exercises0/3
Quiz · 70% to clear12 q

This checkpoint covers plain enums, enums with state and behaviour, EnumSet/EnumMap, records, and the practical record patterns.

How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.

Before you start, make sure you can answer:

  • Why == is right for enums, and what valueOf("friday") does.
  • Why an enum constructor cannot read a static field, and how a fromCode lookup is built.
  • What EnumSet and EnumMap are made of and why they beat hash collections.
  • Everything a record generates, and the two things it forbids.
  • What a compact constructor can do, and why a List component should be copied.

The programs are an enum with per-constant behaviour driving a small calculator, and a record-based model with validation and grouping into an EnumMap.

Common questions

What does Day.valueOf("friday") do when the constant is FRIDAY?

It throws IllegalArgumentException, because valueOf matches the exact, case-sensitive constant name. Normalise the input first with trim().toUpperCase(Locale.ROOT) and catch the exception, or write a lenient lookup method.

What does a Java record generate, and what does it forbid?

From its header a record generates a private final field per component, a canonical constructor, accessors named after the components, and equals, hashCode and toString. It forbids instance fields beyond the components and an extends clause; its fields are final, so it has no setters.

Why should a record copy its List components?

A record's fields are final, but a List it holds can still be changed by whoever passed it in. Copying it in the compact constructor with members = List.copyOf(members) stores an immutable copy, so the record stays immutable all the way down.

Java Enums and Records Quiz: EnumSet and EnumMap Practice

Test your Java enum and record skills with 12 questions and two programs on enum methods and behaviour, EnumSet, EnumMap, records and compact constructors.

  • Course: Java study plan
  • Module: Enums & records
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 22 min
  • Runtime: OpenJDK 18

Checkpoint — Enums & records is the checkpoint that closes the Enums & records module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers plain enums, enums with state and behaviour, EnumSet/EnumMap, records, and the practical record patterns.

How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.

Before you start, make sure you can answer:

  • Why == is right for enums, and what valueOf("friday") does.
  • Why an enum constructor cannot read a static field, and how a fromCode lookup is built.
  • What EnumSet and EnumMap are made of and why they beat hash collections.
  • Everything a record generates, and the two things it forbids.
  • What a compact constructor can do, and why a List component should be copied.

The programs are an enum with per-constant behaviour driving a small calculator, and a record-based model with validation and grouping into an EnumMap.

Common questions

What does Day.valueOf("friday") do when the constant is FRIDAY?

It throws IllegalArgumentException, because valueOf matches the exact, case-sensitive constant name. Normalise the input first with trim().toUpperCase(Locale.ROOT) and catch the exception, or write a lenient lookup method.

What does a Java record generate, and what does it forbid?

From its header a record generates a private final field per component, a canonical constructor, accessors named after the components, and equals, hashCode and toString. It forbids instance fields beyond the components and an extends clause; its fields are final, so it has no setters.

Why should a record copy its List components?

A record's fields are final, but a List it holds can still be changed by whoever passed it in. Copying it in the compact constructor with members = List.copyOf(members) stores an immutable copy, so the record stays immutable all the way down.

Exercises

Planets

Declare enum Planet with MERCURY(3.303e23, 2.4397e6), EARTH(5.976e24, 6.37814e6) and JUPITER(1.9e27, 7.1492e7) (mass in kg, radius in m), a method surfaceGravity() = G * mass / (radius²) with G = 6.67300e-11, and surfaceWeight(double otherMass) = otherMass * surfaceGravity(). Read a weight on Earth in newtons; compute the mass as weight / EARTH.surfaceGravity() and print, for every planet in declaration order, <NAME> <weight with 2 decimals>.

Example: 175 →

MERCURY 66.11
EARTH 175.00
JUPITER 442.85

Inventory by category

Declare enum Category { FOOD, TOOLS, TOYS } and record Item(String name, Category category, int qty, long priceCents) whose compact constructor rejects a negative quantity or price with IllegalArgumentException, plus long valueCents() = qty × price. Read an integer n and n lines <name> <CATEGORY> <qty> <priceCents>. Skip invalid items (count them). Total the value per category in an EnumMap and print, in declaration order, <CATEGORY> <total> for every category (0 if none), then invalid=<count>.

Example: 3 then apple FOOD 10 50, saw TOOLS -1 900, ball TOYS 2 300 →

FOOD 500
TOOLS 0
TOYS 600
invalid=1

A deck of cards

Declare enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }, enum Rank { TWO, THREE, …, TEN, JACK, QUEEN, KING, ACE } and record Card(Rank rank, Suit suit). Read n cards as <RANK> <SUIT>. Print them sorted by rank then suit (both by ordinal) as RANK of SUIT, then highest=<the highest card>, flush=<all the same suit>, and pairs=<how many ranks appear at least twice> using an EnumMap<Rank, Integer>.

Example: 3 then ACE SPADES, TWO HEARTS, TWO CLUBS →

TWO of CLUBS
TWO of HEARTS
ACE of SPADES
highest=ACE of SPADES
flush=false
pairs=1

In this module: Enums & records

← Records in practice — validation, wither methods and modelling · Static nested and inner classes →