ExamGecko
Home Home / Google / Associate Android Developer

Google Associate Android Developer Practice Test - Questions Answers, Page 8

Question list
Search
Search

List of questions

Search

Related questions











LiveData.postValue() and LiveData.setValue() methods have some differences. So if you have a following code executed in the main thread:

liveData.postValue("a"); liveData.setValue("b");

What will be the correct statement?

A.
The value "b" would be set at first and later the main thread would override it with the value "a".
A.
The value "b" would be set at first and later the main thread would override it with the value "a".
Answers
B.
The value "a" would be set at first and later the main thread would override it with the value "b".
B.
The value "a" would be set at first and later the main thread would override it with the value "b".
Answers
C.
The value "b" would be set at first and would not be overridden with the value "a".
C.
The value "b" would be set at first and would not be overridden with the value "a".
Answers
D.
The value "a" would be set at first and would not be overridden with the value "b".
D.
The value "a" would be set at first and would not be overridden with the value "b".
Answers
Suggested answer: B

In our TeaViewModel class, that extends ViewModel, we have such method:

public LiveData<Tea> getTea() { return mTea; }

An observer in our Activity (type of mViewModel variable in example is TeaViewModel) is set in this way:

mViewModel.getTea().observe(this, this::displayTea);

What will be a correct displayTea method definition?

A.
private void displayTea()
A.
private void displayTea()
Answers
B.
private void displayTea(Tea tea)
B.
private void displayTea(Tea tea)
Answers
C.
private void displayTea(LiveData<Tea>)
C.
private void displayTea(LiveData<Tea>)
Answers
D.
private void displayTea(LiveData<T>)
D.
private void displayTea(LiveData<T>)
Answers
Suggested answer: B

For example, our preferences.xml file was added by addPreferencesFromResource(R.xml.preferences). Our preferences.xml file contains such item:

<SwitchPreference android:id="@+id/notification" android:key="@string/pref_notification_key" android:title="@string/pref_notification_title"

android:summary="@string/pref_notification_summary" android:defaultValue="@bool/pref_notification_default_value" app:iconSpaceReserved="false"/>

In our Fragment, we can dynamically get current notification preference value in this way:

A.
boolean isNotificationOn = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean( getContext().getString(R.string.pref_notification_key),getContext().getResources().getBoolean(R.bool.pref_notification_default_value) );
A.
boolean isNotificationOn = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean( getContext().getString(R.string.pref_notification_key),getContext().getResources().getBoolean(R.bool.pref_notification_default_value) );
Answers
B.
boolean isNotificationOn = PreferenceManager.getSharedPreferences(getContext()).getBoolean( getContext().getString(R.string.pref_notification_default_value), getContext().getString(R.string.pref_notification_key) );
B.
boolean isNotificationOn = PreferenceManager.getSharedPreferences(getContext()).getBoolean( getContext().getString(R.string.pref_notification_default_value), getContext().getString(R.string.pref_notification_key) );
Answers
C.
boolean isNotificationOn = PreferenceManager.getSharedPreferences(getContext()).getBoolean( getContext().getResources().getBoolean(R.bool.pref_notification_default_value), getContext().getString(R.string.pref_notification_key) );
C.
boolean isNotificationOn = PreferenceManager.getSharedPreferences(getContext()).getBoolean( getContext().getResources().getBoolean(R.bool.pref_notification_default_value), getContext().getString(R.string.pref_notification_key) );
Answers
Suggested answer: A

For example, our preferences.xml file was added by addPreferencesFromResource(R.xml.preferences). Our preferences.xml file contains such item:

<ListPreference android:id="@+id/order_by" android:key="@string/pref_sort_key" android:title="@string/pref_sort_title" android:summary="@string/pref_sort_summary" android:dialogTitle="@string/pref_sort_dialog_title" android:entries="@array/sort_oder" android:entryValues="@array/sort_oder_value"

android:defaultValue="@string/pref_default_sort_value" app:iconSpaceReserved="false" />

In our Fragment, we can dynamically get current notification preference value in this way:

A.
String sortBy = PreferenceManager.getDefaultSharedPreferences(getContext()).getString( getContext().getString(R.string.pref_sort_key),getContext().getResources().getBoolean(R.bool.pref_default_sort_value) );
A.
String sortBy = PreferenceManager.getDefaultSharedPreferences(getContext()).getString( getContext().getString(R.string.pref_sort_key),getContext().getResources().getBoolean(R.bool.pref_default_sort_value) );
Answers
B.
String sortBy = PreferenceManager.getSharedPreferences(getContext()).getString( getContext().getString(R.string.pref_default_sort_value),getContext().getString(R.string.pref_sort_key) );
B.
String sortBy = PreferenceManager.getSharedPreferences(getContext()).getString( getContext().getString(R.string.pref_default_sort_value),getContext().getString(R.string.pref_sort_key) );
Answers
C.
boolean sortBy = PreferenceManager.getSharedPreferences(getContext()).getBoolean( getContext().getResources().getBoolean(R.bool.pref_default_sort_value), getContext().getString(R.string.pref_sort_key) );
C.
boolean sortBy = PreferenceManager.getSharedPreferences(getContext()).getBoolean( getContext().getResources().getBoolean(R.bool.pref_default_sort_value), getContext().getString(R.string.pref_sort_key) );
Answers
D.
String sortBy = PreferenceManager.getDefaultSharedPreferences(getContext()).getString( getContext().getString(R.string.pref_sort_key), getContext().getString(R.string.pref_default_sort_value) )
D.
String sortBy = PreferenceManager.getDefaultSharedPreferences(getContext()).getString( getContext().getString(R.string.pref_sort_key), getContext().getString(R.string.pref_default_sort_value) )
Answers
Suggested answer: D

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:

A.
InputStream input = context.openRawResource(R.raw.sample_teas);
A.
InputStream input = context.openRawResource(R.raw.sample_teas);
Answers
B.
InputStream input = context.getRawResource(R.raw.sample_teas);
B.
InputStream input = context.getRawResource(R.raw.sample_teas);
Answers
C.
InputStream input = context.getResources().openRawResource(R.raw.sample_teas);
C.
InputStream input = context.getResources().openRawResource(R.raw.sample_teas);
Answers
Suggested answer: C

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

A.
String line; try {while ((line = reader.readLine()) != null) { builder.append(line);}JSONObject json = new JSONObject(builder.toString()); return json;} catch (IOException | JSONException exception) { exception.printStackTrace();}
A.
String line; try {while ((line = reader.readLine()) != null) { builder.append(line);}JSONObject json = new JSONObject(builder.toString()); return json;} catch (IOException | JSONException exception) { exception.printStackTrace();}
Answers
B.
JSONObject line; try {while ((line = reader.readJSONObject ()) != null) { builder.append(line);}JSONObject json = new JSONObject(builder.toString()); return json;} catch (IOException | JSONException exception) { exception.printStackTrace();}
B.
JSONObject line; try {while ((line = reader.readJSONObject ()) != null) { builder.append(line);}JSONObject json = new JSONObject(builder.toString()); return json;} catch (IOException | JSONException exception) { exception.printStackTrace();}
Answers
C.
String line; try {while ((line = reader.readLine()) != null) { builder.append(line);}JSONObject json = new JSONObject(builder.toString()); return json;} catch (RuntimeException|ArrayIndexOutOfBoundsException exception) { exception.printStackTrace();}
C.
String line; try {while ((line = reader.readLine()) != null) { builder.append(line);}JSONObject json = new JSONObject(builder.toString()); return json;} catch (RuntimeException|ArrayIndexOutOfBoundsException exception) { exception.printStackTrace();}
Answers
Suggested answer: 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 doing this:

A.
InputStream input = context.getResources().openRawResource(R.raw.sample_teas);
A.
InputStream input = context.getResources().openRawResource(R.raw.sample_teas);
Answers
B.
InputStream input = context.getAssets().open("sample_teas.json");
B.
InputStream input = context.getAssets().open("sample_teas.json");
Answers
C.
InputStream input = context.getResources().getAssets().open("sample_teas.json");
C.
InputStream input = context.getResources().getAssets().open("sample_teas.json");
Answers
Suggested answer: B

An example. In our ViewModelFactory (that implements ViewModelProvider.Factory) we have an instance of our Repository, named mRepository. Our ViewModel has such constructor:

public MyViewModel(MyRepository myRepository)...

Next, in our ViewModelFactory create ViewModel method (overriden) looks like this: @NonNull @Override

public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { try {

//MISSED RETURN VALUE HERE

} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {

throw new RuntimeException("Cannot create an instance of " + modelClass, e);

}}

What should we write instead of "//MISSED RETURN VALUE HERE"?

A.
return modelClass.getConstructor().newInstance(mRepository);
A.
return modelClass.getConstructor().newInstance(mRepository);
Answers
B.
return modelClass.getConstructor(MyRepository.class).newInstance();
B.
return modelClass.getConstructor(MyRepository.class).newInstance();
Answers
C.
return modelClass.getConstructor(MyRepository.class).newInstance(mRepository);
C.
return modelClass.getConstructor(MyRepository.class).newInstance(mRepository);
Answers
Suggested answer: C

What is demonstrated by the code below?

// RawDao.java @Dao

interface RawDao {

@RawQuery

User getUserViaQuery(SupportSQLiteQuery query); }

// Usage of RawDao ...

SimpleSQLiteQuery query =

new SimpleSQLiteQuery("SELECT * FROM User WHERE id = ? LIMIT 1", new Object[]{userId});

User user = rawDao.getUserViaQuery(query); ...

A.
A method in a Dao annotated class as a raw query method where you can pass the query as a SupportSQLiteQuery.
A.
A method in a Dao annotated class as a raw query method where you can pass the query as a SupportSQLiteQuery.
Answers
B.
A method in a Dao annotated class as a query method.
B.
A method in a Dao annotated class as a query method.
Answers
C.
A method in a RoomDatabase class as a query method.
C.
A method in a RoomDatabase class as a query method.
Answers
Suggested answer: A

What happens when you create a DAO method and annotate it with @Insert?

Example:

@Dao

public interface MyDao {

@Insert(onConflict = OnConflictStrategy.REPLACE) public void insertUsers(User... users); }

A.
Room generates an implementation that inserts all parameters into the database in a single transaction.
A.
Room generates an implementation that inserts all parameters into the database in a single transaction.
Answers
B.
Room modifies a set of entities, given as parameters, in the database. It uses a query that matches against the primary key of each entity.
B.
Room modifies a set of entities, given as parameters, in the database. It uses a query that matches against the primary key of each entity.
Answers
C.
Room removes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.
C.
Room removes a set of entities, given as parameters, from the database. It uses the primary keys to find the entities to delete.
Answers
Suggested answer: A
Total 128 questions
Go to page: of 13