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 1Medium
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));
}
}
Aprice must be int
BRecords cannot declare instance fields beyond their components; add stock as a component or make the field static
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"));
}
}
AEntity must be abstract
BA record implicitly extends java.lang.Record, so it cannot extend another class; use composition or an interface
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();
}
}
Aincrement must be static
BRecord fields are final, so value cannot be changed; return a new record instead, e.g. Counter next() { return new Counter(value + 1); }
Cvalue must be long
DRecords cannot have methods
Correct Answer: Option B
javac: "cannot assign a value to final variable value".
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));
}
}
AMath.min cannot be used
BIn a compact constructor the fields are assigned automatically at the end; assign the parameter (value = Math.min(value, 100)) instead of this.value
CCompact constructors cannot contain if
Dvalue must be Integer
Correct Answer: Option B
javac: "cannot assign a value to final variable value".
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);
}
}
Asymbol must be private
BEnum constructors are implicitly private; declaring them public (or protected) is not allowed
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");
}
}
AUpi must be a record
BCrypto is not listed in Payment's permits clause, so it may not implement the sealed interface
CSealed interfaces cannot have implementations
DCard must be non-sealed
Correct Answer: Option B
javac: "class is not allowed to extend sealed class: Payment".
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");
}
}
AVehicle must be abstract
BEvery permitted subclass must declare whether it is final, sealed or non-sealed; Truck declares none
CCar cannot be final
DSealed classes need an interface
Correct Answer: Option B
javac: "sealed, non-sealed or final modifiers expected".
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()));
}
}
Arecords cannot be empty
BThe switch expression does not cover Cow, so it is not exhaustive; add case Cow c -> "moo"
Cswitch cannot use patterns
Dsound must be static
Correct Answer: Option B
javac: "the switch expression does not cover all possible input values" – exactly the safety sealed types are for.
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());
}
}
Aroll() does not exist
BRecord accessors are named after the components – use s.name(), not s.getName()
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·