Free Associate Android Developer Exam Braindumps (page: 5)

Page 4 of 33

For example, we have a file in our raw folder app/src/main/res/raw/sample_teas.json. To get an
InputStream for reading it, from out Context context, we can do this:

  1. val input = context!!.openRawResource(R.raw.sample_teas)
  2. val input = context!!.getRawResource(R.raw.sample_teas)
  3. val input = context!!.resources.openRawResource(R.raw.sample_teas)

Answer(s): C



For example, we have a BufferedReader reader, associated with the json file through
InputStreamReader. To get a file data we can do this:

  1. var line: String?
    try {
    while (reader.readLine().also { line = it } != null) {
    builder.append(line)
    }
    val json = JSONObject(builder.toString())
    return json
    } catch (exception: IOException) {
    exception.printStackTrace()
    } catch (exception: JSONException) {
    exception.printStackTrace()
    }
  2. var line: JSONObject ?
    try {
    while (reader.readJSONObject ().also { line = it } != null) {
    builder.append(line)
    }
    val json = JSONObject(builder.toString())
    return json
    } catch (exception: IOException) {
    exception.printStackTrace()
    } catch (exception: JSONException) {
    exception.printStackTrace()
    }
  3. var line: String?
    try {
    while (reader.readLine().also { line = it } != null) {
    builder.append(line)
    }
    val json = JSONObject(builder.toString())
    return json
    } catch (exception: RuntimeException) {
    exception.printStackTrace()
    } catch (exception: ArrayIndexOutOfBoundsException) {
    exception.printStackTrace()
    }

Answer(s): A



For example, we have a file in our assets folder app/src/main/assets/sample_teas.json. To get an InputStream for reading it, from out Context context, we can try do this:

  1. val input = context!!.resources.openRawResource(R.raw.sample_teas)
  2. val input = context!!.assets.open("sample_teas.json")
  3. val input = context!!.resources.assets.open("sample_teas.json")

Answer(s): B



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

  1. modelClass.getConstructor()
    .newInstance(mRepository)
  2. modelClass.getConstructor(MyRepository::class.java)
    .newInstance()
  3. modelClass.getConstructor(MyRepository::class.java)
    .newInstance(mRepository)

Answer(s): C






Post your Comments and Discuss Google Associate Android Developer exam with other Community members:

Associate Android Developer Discussions & Posts