ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 16 - Associate Android Developer discussion

Report
Export

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"?

A.
modelClass.getConstructor().newInstance(mRepository)
Answers
A.
modelClass.getConstructor().newInstance(mRepository)
B.
modelClass.getConstructor(MyRepository::class.java) .newInstance()
Answers
B.
modelClass.getConstructor(MyRepository::class.java) .newInstance()
C.
modelClass.getConstructor(MyRepository::class.java).newInstance(mRepository)
Answers
C.
modelClass.getConstructor(MyRepository::class.java).newInstance(mRepository)
Suggested answer: C
asked 18/09/2024
Giulia Alberghi
43 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first