Explore fundamental concepts of Room Database in Android development with these questions focused on database structure, entity creation, DAO methods, and common annotations. Ideal for beginners seeking to strengthen their understanding of Room's basic components and operations.
Which annotation is used to define a data class as a table entity in Room Database?
Explanation: @Entity is the correct annotation used to indicate that a class represents a table in Room Database. @Table and @DatabaseTable are not valid in this context; they might be confused with terms from other frameworks. @Data is used for data classes but does not provide any information to Room about database structure.
To uniquely identify each record in a Room Entity, which property should be used?
Explanation: The property 'primaryKey' is specified using the @PrimaryKey annotation in Room to denote unique identifiers for each row. 'identity', 'autoField', and 'uniqueField' are not recognized by Room and might mislead developers into thinking they provide unique identification.
Why is the version number important when defining a RoomDatabase abstract class?
Explanation: The version number helps Room identify when the schema changes, allowing it to trigger migration logic as needed. Maximum connections and encryption strength are unrelated to versioning. Cursor type is also not determined by the database version.
What annotation should be used on a method in a DAO interface to retrieve all rows from a table?
Explanation: @Query is the correct annotation used to execute SQL queries and fetch data from Room tables. @GetAll, @Retrieve, and @Fetch are not valid annotations in this context and will not function as expected with Room.
Which Room annotation automatically generates SQL to insert one or more rows into a table?
Explanation: @Insert tells Room to generate the necessary SQL statement for data insertion. @Add, @Create, and @Put might sound correct, but they are not recognized by Room for data insertion purposes.
When initializing a Room database, which parameter is mandatory in the builder method?
Explanation: Context is required so Room knows where and how to store the database. UserAgent and Theme have no relevance to database creation. FilePath is not directly used in the standard Room database builder.
Which annotation would you use to enforce non-null values for an entity property in Room Database?
Explanation: @NonNull is the correct annotation to ensure a property cannot have null values in the database. @NotEmpty, @Required, and @Mandatory are not valid in this context and won't be recognized by Room for nullability checks.
What is the main responsibility of a Data Access Object (DAO) in the Room architecture?
Explanation: DAOs define how the app interacts with the data layer, offering abstract methods for queries, inserts, updates, and deletes. Input validation and UI rendering are unrelated to DAOs. Encryption is also not handled directly by the DAO.
Which return type allows a DAO method in Room to observe data changes and update automatically?
Explanation: LiveData allows Room to observe changes and notify observers about data updates in real-time. Optional is a utility for null safety but does not enable observation. JsonObject and RecordSet are unrelated to Room's data change mechanism.
If you change the schema of an existing Room database, what must you provide to prevent data loss?
Explanation: Migration object tells Room how to handle schema changes without losing existing data. MigrationFlag, SchemaPatch, and DataSync are incorrect as they are not recognized elements in Room's migration process.