The following code snippet shows an example of an Espresso test:
Answer : B
As an example. In an Activity we have our TimerViewModel object (extended ViewModel), named mTimerViewModel. mTimerViewModel.getTimer
() method returns a LiveData<Long> value. What can be a correct way to set an observer to change UI in case if data was changed?
Answer : B
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?
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?
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:
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:
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:
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:
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:
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"?
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);
...
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);
}
Answer : A
What do you want from Room when you create a DAO method and annotate it with @Update?
Example:
@Dao
public interface MyDao {
@Update
public void updateUsers(User... users);
}
Answer : B
What do you want from Room when you create a DAO method and annotate it with @Delete?
Example:
@Dao
public interface MyDao {
@Delete
public void deleteUsers(User... users);
}
Answer : C
In Android 8.0, API level 26, some APIs regarding notification behaviors were moved from Notification to NotificationChannel. For example, what should we use instead of NotificationCompat.Builder.setPriority() for Android 8.0 and higher?
Answer : B
Reference:
https://developer.android.com/training/notify-user/build-notification