LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Exercise 3 of 4

Records, Enums and Sealed Classes Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program misuses an enum, a record or a sealed type in a way javac rejects. Choose the option that names the real problem.

Question 1 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
record Product(String name, double price) {
    int stock;
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Product("Pen", 20));
    }
}
A price must be int
B Records cannot declare instance fields beyond their components; add stock as a component or make the field static
C Records cannot have bodies
D name must be final
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Entity {
    long id;
}

record User(String name) extends Entity { }

public class Main {
    public static void main(String[] args) {
        System.out.println(new User("Asha"));
    }
}
A Entity must be abstract
B A record implicitly extends java.lang.Record, so it cannot extend another class; use composition or an interface
C User needs a constructor
D id must be final
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
record Counter(int value) {
    void increment() {
        value++;
    }
}

public class Main {
    public static void main(String[] args) {
        new Counter(0).increment();
    }
}
A increment must be static
B Record fields are final, so value cannot be changed; return a new record instead, e.g. Counter next() { return new Counter(value + 1); }
C value must be long
D Records cannot have methods
Question page & discussion
Question 4 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
record Percentage(int value) {
    Percentage {
        if (value < 0) value = 0;
        this.value = Math.min(value, 100);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Percentage(150));
    }
}
A Math.min cannot be used
B In a compact constructor the fields are assigned automatically at the end; assign the parameter (value = Math.min(value, 100)) instead of this.value
C Compact constructors cannot contain if
D value must be Integer
Question page & discussion
Question 5 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
enum Status { ACTIVE, BLOCKED }

public class Main {
    public static void main(String[] args) {
        Status s = new Status();
        System.out.println(s);
    }
}
A Status needs a constructor
B Enum types cannot be instantiated; use one of the constants, e.g. Status.ACTIVE
C Enums must be public
D s must be final
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
enum Currency {
    INR("₹"), USD("$");
    final String symbol;
    public Currency(String symbol) { this.symbol = symbol; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Currency.INR.symbol);
    }
}
A symbol must be private
B Enum constructors are implicitly private; declaring them public (or protected) is not allowed
C Enums cannot have fields
D ₹ is not a valid character
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
sealed interface Payment permits Upi, Card { }
final class Upi implements Payment { }
final class Card implements Payment { }
final class Crypto implements Payment { }

public class Main {
    public static void main(String[] args) {
        System.out.println("payments");
    }
}
A Upi must be a record
B Crypto is not listed in Payment's permits clause, so it may not implement the sealed interface
C Sealed interfaces cannot have implementations
D Card must be non-sealed
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
sealed class Vehicle permits Car, Truck { }
final class Car extends Vehicle { }
class Truck extends Vehicle { }

public class Main {
    public static void main(String[] args) {
        System.out.println("vehicles");
    }
}
A Vehicle must be abstract
B Every permitted subclass must declare whether it is final, sealed or non-sealed; Truck declares none
C Car cannot be final
D Sealed classes need an interface
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
sealed interface Animal permits Dog, Cat, Cow { }
record Dog() implements Animal { }
record Cat() implements Animal { }
record Cow() implements Animal { }

public class Main {
    static String sound(Animal a) {
        return switch (a) {
            case Dog d -> "woof";
            case Cat c -> "meow";
        };
    }

    public static void main(String[] args) {
        System.out.println(sound(new Dog()));
    }
}
A records cannot be empty
B The switch expression does not cover Cow, so it is not exhaustive; add case Cow c -> "moo"
C switch cannot use patterns
D sound must be static
Question page & discussion
Question 10 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
record Student(String name, int roll) { }

public class Main {
    public static void main(String[] args) {
        Student s = new Student("Asha", 7);
        System.out.println(s.getName() + " " + s.roll());
    }
}
A roll() does not exist
B Record accessors are named after the components – use s.name(), not s.getName()
C Student needs setters
D records cannot be printed
Question page & discussion
Up next · Exercise 4Records, Enums and Sealed Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links