Which method does the compiler NOT generate for record Point(int x, int y) {}?
Record fields are final; there are no setters. Accessors are named after the components.
Records, sealed types, pattern matching, switch expressions, var, text blocks and sequenced collections.
Pick an answer to see instantly whether you're right, with an explanation. Questions are shuffled every attempt.
record Point(int x, int y) {}?Record fields are final; there are no setters. Accessors are named after the components.
record Point(int x, int y) {}
System.out.println(new Point(1, 2).equals(new Point(1, 2)));Records generate value-based equals and hashCode from all components.
Records may have static members, instance methods and implement interfaces — but no extra instance fields or superclass.
List<String> component. How do you make it effectively immutable?Records are only shallowly immutable — defensively copy mutable components.
Every permitted subtype must state how the hierarchy continues. Records are implicitly final.
default branch?sealed interface Shape permits Circle, Square {}
record Circle(double r) implements Shape {}
record Square(double s) implements Shape {}
double area(Shape sh) {
return switch (sh) {
case Circle c -> Math.PI * c.r() * c.r();
case Square s -> s.s() * s.s();
};
}Sealed hierarchies make pattern switches exhaustive; adding a new permitted type makes this stop compiling.
case null?Add case null -> (or case null, default ->) to handle it explicitly.
yield produces the value of a switch-expression block; return would return from the enclosing method.
var needs an initialiser with a known type and works only for local variables.
The line-continuation escape is handy for long SQL strings; \s keeps a trailing space.
Object o = 42;
if (o instanceof Integer i && i > 40) {
System.out.println(i + 1);
}Pattern matching for instanceof binds i where the match is definitely true, including the right side of &&.
LinkedHashSet directly?Sequenced collections add getFirst, getLast, addFirst, addLast, removeFirst, removeLast and reversed().
case CharSequence cs -> appearing before case String s ->?More specific patterns must come before more general ones (dominance).
Virtual threads previewed in 19 and 20 and became final in Java 21 (JEP 444).
You scored 0 out of 14.