Records & Enums Questions with Answers

21 practice questions on Records & Enums (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 21 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

enum Day { MON, TUE, SAT, SUN }

public class Main {
    public static void main(String[] args) {
        Day d = Day.SAT;
        switch (d) {
            case SAT:
            case SUN:
                System.out.println("weekend");
                break;
            default:
                System.out.println("weekday");
        }
    }
}
  1. weekend
  2. Compilation error
  3. weekday
  4. weekend weekday

Answer: A. weekend

Explanation

Inside a switch on an enum, case labels use the bare constant names; SAT falls through to the SUN group and prints weekend.

Step-by-step solution

  1. D is SAT, so the switch jumps to case SAT.
  2. SAT has no break, so it falls through to case SUN.
  3. The println prints weekend, and the break exits the switch so default is skipped.
  4. Output: weekend, which is option A.

Why the other options are wrong

  • B. Bare enum names are valid case labels in a switch on an enum, so the code compiles.
  • C. Default runs only when no case matches, and SAT does match.
  • D. Only one println runs before the break, so weekday is never printed.

Tip: Without a break, a switch case falls through into the next case.

Question 2Easy

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

record Item(String name, int price) {}

public class Main {
    public static void main(String[] args) {
        Item it = new Item("pen", 10);
        System.out.println(it.name() + " " + it.price());
    }
}
  1. pen 10
  2. null 0
  3. Item[name=pen, price=10]
  4. Compilation error

Answer: A. pen 10

Explanation

For each component a record generates a public accessor method with the same name (name(), price()), not getName().

Step-by-step solution

  1. A record generates accessors named after components.
  2. name() gives pen and price() gives 10.
  3. Output: pen 10.

Why the other options are wrong

  • B. The values come from the constructor.
  • C. That would be the toString of the record, not the printed expression.
  • D. The code compiles; accessors are generated.

Tip: Record accessor = name(), not getName().

Question 3Easy

Why is it safe to compare enum values with == in Java?

  1. Enums are primitive types, so == compares values
  2. The compiler replaces enum constants with their ordinal integers
  3. Each enum constant is a single instance created once per class loader
  4. == on enums is overloaded to call equals()

Answer: C. Each enum constant is a single instance created once per class loader

Explanation

The JVM guarantees exactly one instance per enum constant (even through serialization), so reference equality is the same as logical equality.

Step-by-step solution

  1. Each enum constant is a single object, created once when the enum class is initialised.
  2. So two references to the same constant point to the same object, and == compares them correctly.
  3. The answer is option C: one instance per constant.

Why the other options are wrong

  • A. Enums are objects (reference types), not primitives, so == compares references.
  • B. The compiler does not turn enum constants into integers; they stay object references.
  • D. == is never overloaded in Java; it always compares references.

Tip: Enum constants are singletons, so == is safe for enums and is the usual way to compare them.

Question 4Easy

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

enum Level { LOW, MEDIUM, HIGH }

public class Main {
    public static void main(String[] args) {
        Level l = Level.MEDIUM;
        System.out.println(l + " " + l.ordinal() + " " + Level.values().length);
    }
}
  1. MEDIUM 2 3
  2. 1 1 3
  3. Level.MEDIUM 1 3
  4. MEDIUM 1 3

Answer: D. MEDIUM 1 3

Explanation

An enum constant prints its name, ordinal() is zero-based (LOW=0, MEDIUM=1), and values() returns all 3 constants.

Step-by-step solution

  1. L is Level.MEDIUM so l prints as MEDIUM.
  2. l.ordinal() returns 1 because ordinal is zero-based starting from LOW=0.
  3. Level.values().length gives 3 since there are three enum constants.
  4. So the output is MEDIUM 1 3.

Why the other options are wrong

  • A. ordinal() of MEDIUM is 1, since LOW is 0, not 2.
  • B. Enum toString prints the constant name MEDIUM, not its ordinal 1.
  • C. println uses the constant's name MEDIUM, without the Level. prefix.

Tip: Remember ordinal starts at 0 and values() gives total count.

Question 5Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

class Base {}
record Child(int a) extends Base {}

public class Main {
    public static void main(String[] args) {
        System.out.println("x");
    }
}
  1. x followed by a warning
  2. Runtime error
  3. x
  4. Compilation error

Answer: D. Compilation error

Explanation

A record implicitly extends java.lang.Record and cannot have an extends clause; it may only implement interfaces.

Step-by-step solution

  1. A record implicitly extends java.lang.Record.
  2. Declaring record Child(int a) extends Base adds a second superclass, and records cannot have an extends clause.
  3. The compiler rejects this, so the program does not compile and the answer is a compilation error, option D.

Why the other options are wrong

  • A. This is a hard compile error, not a warning, so nothing runs.
  • B. The problem is caught at compile time, so the program never runs and gives no runtime error.
  • C. The program would print x only if it compiled, and it does not compile.

Tip: Records can implement interfaces but can never extend a class.

Question 6Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

record Range(int lo, int hi) {
    Range {
        if (lo > hi) throw new IllegalArgumentException(lo + ">" + hi);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            new Range(5, 1);
        } catch (IllegalArgumentException e) {
            System.out.print("bad " + e.getMessage() + " ");
        }
        System.out.println(new Range(1, 5));
    }
}
  1. Compilation error
  2. Range[lo=5, hi=1] Range[lo=1, hi=5]
  3. bad 5>1
  4. bad 5>1 Range[lo=1, hi=5]

Answer: D. bad 5>1 Range[lo=1, hi=5]

Explanation

A compact canonical constructor runs before the fields are assigned and can validate arguments; the first call throws, the second succeeds and prints the generated toString.

Step-by-step solution

  1. New Range(5, 1) runs the compact constructor; 5 > 1 is true, so it throws IllegalArgumentException with message 5>1.
  2. The catch block prints bad 5>1 followed by a space, with no newline.
  3. New Range(1, 5) passes the check and is printed with the generated toString.
  4. Output: bad 5>1 Range[lo=1, hi=5], which is option D.

Why the other options are wrong

  • A. The code compiles; a compact constructor is allowed to throw.
  • B. The first record is never created, and the exception is caught before anything is printed for it.
  • C. The second record is also printed, as Range[lo=1, hi=5], on the same line.

Tip: A compact constructor runs before fields are set, so it is the place to validate arguments and throw.

Question 7Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

enum Day { MON, TUE, SAT, SUN }

public class Main {
    public static void main(String[] args) {
        Day d = Day.TUE;
        String kind = switch (d) {
            case SAT, SUN -> "weekend";
            case MON, TUE -> "weekday";
        };
        System.out.println(kind);
    }
}
  1. Compilation error: missing default
  2. weekend
  3. weekday
  4. null

Answer: C. weekday

Explanation

A switch expression over an enum needs no default when every constant is covered; TUE matches the MON, TUE arm.

Step-by-step solution

  1. The switch is a switch expression over an enum.
  2. All constants are covered, so no default needed.
  3. TUE -> weekday.

Why the other options are wrong

  • A. Exhaustive enum switch needs no default.
  • B. TUE is not a weekend day.
  • D. The switch returns a value.

Tip: Cover all constants.

Question 8Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

enum Planet {
    MOON(2), EARTH(10), JUPITER(25);
    private final int g;
    Planet(int g) { this.g = g; }
    int weight(int mass) { return mass * g; }
}

public class Main {
    public static void main(String[] args) {
        for (Planet p : Planet.values()) System.out.print(p.weight(10) + " ");
    }
}
  1. 2 10 25
  2. 20 100 250
  3. Compilation error
  4. 10 10 10

Answer: B. 20 100 250

Explanation

Each constant passes its argument to the private enum constructor; weight multiplies the mass 10 by g: 20, 100, 250.

Step-by-step solution

  1. Each constant is created with its g value: MOON 2, EARTH 10, JUPITER 25.
  2. weight(10) returns 10 times g for each planet.
  3. MOON gives 20, EARTH gives 100 and JUPITER gives 250, each followed by a space.
  4. Output: 20 100 250, which is option B.

Why the other options are wrong

  • A. 2 10 25 are the g values, not the weights; the mass 10 must be multiplied by g.
  • C. The code compiles; the private enum constructor is called from inside the enum.
  • D. The planets have different g values, so the results differ.

Tip: Enum constructors are called once per constant; method calls use each constant's own fields.

Question 9Hard

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

enum Op {
    PLUS { int apply(int a, int b) { return a + b; } },
    TIMES { int apply(int a, int b) { return a * b; } };
    abstract int apply(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Op.PLUS.apply(6, 3) + " " + Op.TIMES.apply(6, 3) + " " + Op.TIMES.getClass().getSuperclass().getSimpleName());
    }
}
  1. 9 18 Op
  2. Compilation error
  3. 9 18 Enum
  4. 9 18 Object

Answer: A. 9 18 Op

Explanation

Constants with bodies are compiled as anonymous subclasses of the enum, each overriding the abstract apply; so the superclass of TIMES's class is Op.

Step-by-step solution

  1. PLUS.apply(6, 3) returns 6 + 3 = 9.
  2. TIMES.apply(6, 3) returns 6 × 3 = 18.
  3. TIMES has a constant body, so its class is an anonymous subclass of Op; getSuperclass() gives Op and getSimpleName() gives Op.
  4. Output: 9 18 Op, which is option A.

Why the other options are wrong

  • B. Abstract methods are fine when every constant has a body that implements them, so the code compiles.
  • C. The superclass of a constant with a body is the enum type Op, not Enum.
  • D. The superclass is Op, not Object.

Tip: A constant with a body is a subclass of its enum; getSuperclass() on it returns the enum type.

Question 10Hard

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

record Box(int size) {
    static Box unit() { return new Box(1); }
    Box doubled() { return new Box(size * 2); }
    public String toString() { return "Box" + size; }
}

public class Main {
    public static void main(String[] args) {
        Box b = new Box(3);
        System.out.println(b + " " + Box.unit() + " " + b.doubled());
    }
}
  1. Compilation error
  2. Box3 Box1 Box6
  3. Box3 Box1 Box3
  4. Box[size=3] Box[size=1] Box[size=6]

Answer: B. Box3 Box1 Box6

Explanation

Records may declare static factory methods and instance methods, and may override the generated toString; doubled returns a new record rather than changing b.

Step-by-step solution

  1. B is Box(3), and its overridden toString returns Box followed by size, so it prints Box3.
  2. Box.unit() returns new Box(1), which prints Box1.
  3. b.doubled() returns a new Box(6), which prints Box6; b itself is unchanged.
  4. Output: Box3 Box1 Box6, which is option B.

Why the other options are wrong

  • A. The class compiles; records can have static factory methods and instance methods.
  • C. doubled() creates a new record, so the result is Box6, not Box3.
  • D. The explicit toString replaces the generated one, so the Box[size=...] form is not used.

Tip: If a record declares toString, that method is used instead of the generated one.

Practise all 21 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics