Google Associate Android Developer Exam Practice Questions (P. 4)
- Full Access (107 questions)
- Six months of Premium Access
- Access to one million comments
- Seamless ChatGPT Integration
- Ability to download PDF files
- Anki Flashcard files for revision
- No Captcha & No AdSense
- Advanced Exam Configuration
Question #16
An example. In our ViewModelFactory (that implements ViewModelProvider.Factory) we have an instance of our Repository, named mRepository. Our
ViewModel has such constructor:
class MyViewModel(private val mRepository: MyRepository) : ViewModel() ...
Next, in our ViewModelFactory create ViewModel method (overriden) looks like this: override fun <T : ViewModel?> create(modelClass: Class<T>): T { return try {
//MISSED RETURN VALUE HERE"
} catch (e: InstantiationException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: IllegalAccessException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: NoSuchMethodException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: InvocationTargetException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
}
}
What should we write instead of "//MISSED RETURN VALUE HERE"?
ViewModel has such constructor:
class MyViewModel(private val mRepository: MyRepository) : ViewModel() ...
Next, in our ViewModelFactory create ViewModel method (overriden) looks like this: override fun <T : ViewModel?> create(modelClass: Class<T>): T { return try {
//MISSED RETURN VALUE HERE"
} catch (e: InstantiationException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: IllegalAccessException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: NoSuchMethodException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: InvocationTargetException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
}
}
What should we write instead of "//MISSED RETURN VALUE HERE"?
- AmodelClass.getConstructor() .newInstance(mRepository)
- BmodelClass.getConstructor(MyRepository::class.java) .newInstance()
- CmodelClass.getConstructor(MyRepository::class.java) .newInstance(mRepository)
Correct Answer:
C
C
send
light_mode
delete
Question #17
What is demonstrated by the code below?
// RawDao.kt
@Dao
interface RawDao {
@RawQuery
fun getUserViaQuery(query: SupportSQLiteQuery?): User?
}
// Usage of RawDao
...
val query =
SimpleSQLiteQuery("SELECT * FROM User WHERE id = ? LIMIT 1",
arrayOf<Any>(sortBy))
val user = rawDao.getUserViaQuery(query)
...
// RawDao.kt
@Dao
interface RawDao {
@RawQuery
fun getUserViaQuery(query: SupportSQLiteQuery?): User?
}
// Usage of RawDao
...
val query =
SimpleSQLiteQuery("SELECT * FROM User WHERE id = ? LIMIT 1",
arrayOf<Any>(sortBy))
val user = rawDao.getUserViaQuery(query)
...
- AA method in a Dao annotated class as a raw query method where you can pass the query as a SupportSQLiteQuery.
- BA method in a Dao annotated class as a query method.
- CA method in a RoomDatabase class as a query method.
Correct Answer:
A
A
send
light_mode
delete
Question #18
What happens when you create a DAO method and annotate it with @Insert?
Example:
@Dao
interface MyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUsers(vararg users: User)
}
Example:
@Dao
interface MyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUsers(vararg users: User)
}
- ARoom generates an implementation that inserts all parameters into the database in a single transaction.
- BRoom modifies a set of entities, given as parameters, in the database. It uses a query that matches against the primary key of each entity.
- CRoom removes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.
Correct Answer:
A
A
send
light_mode
delete
Question #19
What do you want from Room when you create a DAO method and annotate it with @Update?
Example:
@Dao
interface MyDao {
@Update
fun updateUsers(vararg users: User)
}
Example:
@Dao
interface MyDao {
@Update
fun updateUsers(vararg users: User)
}
- ARoom generates an implementation that inserts all parameters into the database in a single transaction.
- BRoom modifies a set of entities, given as parameters, in the database. It uses a query that matches against the primary key of each entity.
- CRoom removes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.
Correct Answer:
B
B
send
light_mode
delete
Question #20
What do you want from Room when you create a DAO method and annotate it with @Delete?
Example:
@Dao
interface MyDao {
@Delete
fun deleteUsers(vararg users: User)
}
Example:
@Dao
interface MyDao {
@Delete
fun deleteUsers(vararg users: User)
}
- ARoom generates an implementation that inserts all parameters into the database in a single transaction.
- BRoom modifies a set of entities, given as parameters, in the database. It uses a query that matches against the primary key of each entity.
- CRoom removes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.
Correct Answer:
C
C
send
light_mode
delete
All Pages