Create classes and records, and define and use instance and static fields and methods, constructors, and instance and static initializers.
Create and use enumerations with fields, methods and constructors.
1. The correct answer is C.
Explanation:
Employee record explicitly defines a public constructor that initializes its fields.
Employee does not explicitly define a public constructor. Records automatically generate a public constructor with the same parameters as the record’s declaration.name and age can be reassigned to new values after an Employee object is created.
final, which means they cannot be reassigned to new values after an Employee object has been created. This immutability is one of the key characteristics of records.Employee record implicitly creates a public constructor and private final fields for name and age.
private and final. This means you don’t have to manually write boilerplate code for constructor, getters, or to ensure immutability.name and age in the Employee record.
2. The correct answer is B.
Explanation:
balance field can be modified using a public setter method within the Account record.
final and cannot be modified after the object’s construction, which is a key aspect of their design to enforce immutability.Account object is created, its id and balance cannot be changed.
id and balance in this case) cannot be changed. This immutability is ensured by making the fields private and final, and by not providing setter methods.id and balance fields.
final and private, which means they cannot be modified directly or through setter methods. The design of records enforces this immutability to ensure that instances of records act as true carriers of immutable data.3. The correct answer is D.
Explanation:
Product p = new Product();
Product p = Product(101, "Coffee", 15.99);
new keyword followed by the record name and the parameters in parentheses.Product p = {101, "Coffee", 15.99};
new keyword and proper constructor.Product p = new Product(101, "Coffee", 15.99);
new keyword followed by the record’s constructor, which requires passing all the fields defined in the record. This syntax correctly creates a new Product record with the given id, name, and price.4. The correct answer is B.
Explanation:
final and immutable by design, which prevents any form of behavior customization.
Comparable interface, allowing Item objects to be sorted based on their price.
Comparable<Item> interface by overriding the compareTo method. This customization allows instances of the Item record to be sorted based on the price field, demonstrating that records can indeed implement interfaces and override their methods as needed.compareTo method cannot be overridden in records because method overriding is not supported in record types.
compareTo method from the Comparable interface in this example. Method overriding is a key aspect of implementing interfaces and is fully supported by record types in Java.5. The correct answers are A and D.
Explanation:
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public access modifier makes this enum accessible from any other class.enum Month {
private JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}
private access modifiers for their constants. Enum constants are implicitly public, static, and final and should be declared without access modifiers.protected enum Season {
WINTER, SPRING, SUMMER, FALL
}
protected or private access levels. Enums are implicitly public if they are defined outside of a class. If defined within a class, they can have any access level, but the protected keyword cannot be used at the enum level itself.enum Status {
ACTIVE, INACTIVE, DELETED;
public void printStatus() {
System.out.println("Current status: " + this);
}
}
Status with a method printStatus(). Enums in Java can contain methods, fields, constructors, and implement interfaces. This demonstrates the ability of enums to have methods, making this declaration valid.6. The correct answer is A.
Explanation:
1
ordinal() method returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Since GREEN is the second enum constant declared in the Color enum, its ordinal value is 1.2
BLUE would be 2, not GREEN, because BLUE is the third declared constant in the Color enum.0
RED is 0, as it is the first declared constant in the Color enum.Color.GREEN
ordinal() method returns an integer representing the position of the enum constant in the declaration, not the enum constant itself.7. The correct answer is D.
Explanation:
public enum Size {
SMALL, MEDIUM, LARGE;
public static void printSize() {
System.out.println("The size is " + this.name());
}
}
printSize() is defined as static, which means it cannot access the this reference. Static methods in enums can’t directly access the enum constants without specifying the constant explicitly or being passed a reference.enum Flavor {
CHOCOLATE, VANILLA, STRAWBERRY;
void printFlavor() {
System.out.println("Flavor: " + Flavor.name);
}
}
name property of an enum constant is private. You can only access it using the this reference and the name() method (this.name()).protected enum Direction {
NORTH, SOUTH, EAST, WEST;
private printDirection() {
System.out.println("Going " + this.toString());
}
}
protected is not a valid access modifier for a top-level enum, top-level enums can only be public or package-private (no modifier). Second, printDirection() method is missing a return type (e.g., void).public enum Season {
WINTER, SPRING, SUMMER, FALL;
public void printSeason() {
System.out.println("The season is " + this.name());
}
}
printSeason() method is properly defined: it’s public, non-static, and utilizes the this reference to access the name of the current enum constant. This method correctly provides custom behavior for each enum constant, allowing it to print a message indicating the current season.Do you like what you read? Would you consider?
Do you have a problem or something to say?