Java Records MCQ

1. What are records in Java?

a) Data structures to store key-value pairs
b) A feature to log application data
c) A special type of class that holds immutable data
d) A new collection type

Answer:

c) A special type of class that holds immutable data

Explanation:

Records in Java are a special type of class introduced to hold immutable data in a concise manner.

2. In which version of Java were records introduced?

a) Java 8
b) Java 11
c) Java 14
d) Java 16

Answer:

c) Java 14

Explanation:

Records were introduced as a preview feature in Java 14 and became standard in Java 16.

3. How do you define a record in Java?

a) record MyRecord() {}
b) class MyRecord {}
c) record MyRecord {}
d) record MyRecord(var1, var2) {}

Answer:

d) record MyRecord(var1, var2) {}

Explanation:

A record is defined using the record keyword followed by the record name and a list of components.

4. Are record components final in Java?

a) Yes
b) No
c) Only if explicitly declared as final
d) They can be made final

Answer:

a) Yes

Explanation:

Record components are implicitly final, meaning their values cannot be changed once set.

5. Can records in Java have additional methods?

a) Yes
b) No
c) Only static methods
d) Only private methods

Answer:

a) Yes

Explanation:

Records can have additional methods besides the implicitly declared ones.

6. How are equals() and hashCode() methods handled in Java records?

a) Must be overridden by the developer
b) Automatically provided by the compiler
c) Not supported in records
d) Use default Object implementations

Answer:

b) Automatically provided by the compiler

Explanation:

The equals() and hashCode() methods are automatically and appropriately implemented by the compiler for records.

7. Can records implement interfaces in Java?

a) Yes
b) No
c) Only functional interfaces
d) Only if the interface is also a record

Answer:

a) Yes

Explanation:

Records can implement interfaces, just like regular classes.

8. Can a record in Java extend another class?

a) Yes
b) No
c) Only other records
d) Only abstract classes

Answer:

b) No

Explanation:

Records cannot extend any other class, although they implicitly extend java.lang.Record.

9. Which access level are record components in Java?

a) Private
b) Protected
c) Public
d) Package-private

Answer:

c) Public

Explanation:

Record components are implicitly public, and they also implicitly create corresponding public accessor methods.

10. What is the purpose of the canonical constructor in a record?

a) To initialize the record components
b) To override the default constructor
c) To create multiple instances of the record
d) To add additional functionality to the record

Answer:

a) To initialize the record components

Explanation:

The canonical constructor in a record is used to initialize its components.

11. Can records in Java be mutable?

a) Yes
b) No
c) Only with special annotations
d) Only if all components are mutable

Answer:

b) No

Explanation:

Records are designed to be immutable data carriers, so their components cannot be changed after construction.

12. How are record components accessed in Java?

a) Through getter methods
b) Directly by their names
c) Using reflection
d) Both a and b

Answer:

d) Both a and b

Explanation:

Record components can be accessed directly by their names or through automatically generated public accessor methods.

13. Can records be serialized in Java?

a) Yes
b) No
c) Only if they implement Serializable
d) Only with custom serialization methods

Answer:

c) Only if they implement Serializable

Explanation:

Records can be serialized like regular classes if they implement the Serializable interface.

14. What is the primary use case for records in Java?

a) To replace all classes
b) To handle database operations
c) To serve as simple data carriers
d) To create complex business logic

Answer:

c) To serve as simple data carriers

Explanation:

The primary use case for records is to act as simple, concise data carriers with minimal boilerplate.

15. How do you create a custom toString() method for a record in Java?

a) By overriding the toString() method
b) It's not possible; toString() is final
c) Using annotations
d) By defining it in the record components

Answer:

a) By overriding the toString() method

Explanation:

The toString() method can be overridden in a record to provide a custom string representation.

16. Are constructors mandatory in record declarations?

a) Yes
b) No
c) Only the default constructor is mandatory
d) Only the canonical constructor is mandatory

Answer:

b) No

Explanation:

Constructors are not mandatory in records. If not provided, a public canonical constructor is automatically generated.

17. Can records be generic in Java?

a) Yes
b) No
c) Only with restrictions
d) Only if they don't implement interfaces

Answer:

a) Yes

Explanation:

Records can be generic, allowing type parameters to be used in their declaration.

18. How do you destructure a record in Java?

a) Using the destructuring keyword
b) Through pattern matching
c) By accessing individual components
d) Destructuring is not supported for records

Answer:

c) By accessing individual components

Explanation:

While Java does not have a built-in destructuring feature, individual components of a record can be accessed directly.

19. Can records be annotated in Java?

a) Yes
b) No
c) Only with built-in annotations
d) Only with custom annotations

Answer:

a) Yes

Explanation:

Records can be annotated just like classes and methods.

20. How does the record's equals() method compare objects?

a) By reference
b) By comparing all record components
c) Based on hash codes
d) Using custom comparison logic

Answer:

b) By comparing all record components

Explanation:

The automatically generated equals() method in a record compares objects by comparing all the record's components for equality.

Leave a Comment

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

Scroll to Top