Google Developers Certification - Associate Android Developer (Kotlin and Java Exam) Associate-Android-Developer Question # 7 Topic 1 Discussion

Google Developers Certification - Associate Android Developer (Kotlin and Java Exam) Associate-Android-Developer Question # 7 Topic 1 Discussion

Associate-Android-Developer Exam Topic 1 Question 7 Discussion:
Question #: 7
Topic #: 1

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 create(modelClass: Class): 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)


B.

modelClass.getConstructor(MyRepository::class.java)

.newInstance()


C.

modelClass.getConstructor(MyRepository::class.java)

.newInstance(mRepository)


Get Premium Associate-Android-Developer Questions

Contribute your Thoughts:


Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.