Java Certification Exam Questions and Answers- Records

Records is a new topic added into the new Java 17 Certification exam. Here is an article to understand more about this topic. Practice questions (mock exam questions) and answers are listed below.

Q1. Given the following code.

record Student(String name, String address) {
}
public class Record1 {
    public static void main(String[] args) {
        Student student1 = new Student("Tom", "Harris");
        Student student2 = new Student("Tom", "Harris");
    }
}

Which of the following will print true?

A. System.out.println(student1.equals(student2));
B. System.out.println(student1.hashCode()==student2.hashCode());
C. System.out.println(student1.name.equals(student2.name));
D. System.out.println(student1==student2);

Answer

Options A and B are correct. When a Record is defined, the equals() and hashcode() methods are automatically declared which specify that two record classes are equal if they are of the same type and contain equal field values. Here the Student records have the same values for their components, hence invoking equals() on these records and comparing their hashcodes will always return true.

Option C is incorrect because the accessor method has to be invoked using parentheses, like student1.name() and not simply as student1.name. Option D is incorrect because the == operator compares the references of the two records and not their contents. Hence, this will not return true.

Q2. Given the following code.

record Student(String name, String address) {
}

Which of the following is legal?

Choices
A. Student student1 = new Student();
   System.out.println(student1.name);
B. Student student1 = new Student("Tina","Texas");
   System.out.println(student1.name());
C. Student student1 = new Student("Tom", "Harris");
   student1.name("Mary");
D. Student student1 = new Student();
   student1.name("Mary");
Answer

Option B is correct. To create a record object, we can use the new operator and pass values for it’s component types. In this case, the Student record has two String components, hence it can only be instantiated by passing two String values. There is no parameterless constructor, hence options A and D are incorrect. A record will have a public accessor method with the same name and type of each component defined in it. In this case, the accessor methods automatically created will be name() and address(). As the name() accessor method cannot be used to set a value, option C is incorrect.

Q3. What will be the result?

record Square(Integer side) {
	Square { // Line 1
		java.util.Objects.requireNonNull(side); // Line 2
		this.side = side; // Line 3
	}
}

public class Record2 {
	public static void main(String[] args) {
		Square s = new Square(0);
		System.out.println(s.side());
	}
}

A. Compiler error at line 1
B. Compiler error at line 2
C. Compiler error at line 3
D. Exception at runtime
E. Prints 0

Answer

Option C is correct. In the Square record, the constructor defined is a compact constructor. At the end of a compact constructor, its implicit parameters are automatically assigned to the corresponding fields of the record. The explicit assignment of record fields are illegal in a compact constructor. So a compiler error occurs in line 3.

References

Learn about Records here

Read about the Record type from the Oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *