You are on page 1of 8

1.

What code is missing after ​extends​ in the following code snippet:

public class​ TrainAdapter ​extends​ **************** {

a. RecyclerAdapter.ViewHolder<TrainAdapter.ViewHolder>
b. RecyclerAdapter<TrainAdapter.ViewHolder>
c. RecyclerView.Adapter<TrainAdapter.ViewHolder>
d. TrainAdapter.ViewHolder<RecyclerView.Adapter>
Only option (c) makes sense here. We are subclassing a RecyclerView.Adapter of
type TrainAdapter.ViewHolder. (d) is the wrong way around, (a) and (b) are
nonsense as there is no such class as RecyclerAdapter.

2. What happens if you don't override the onCreate() lifecycle method of an


activity?

a. You get a compile error


b. You get a runtime error
c. An activity shows with no content
You can build and run this code, but without overriding onCreate(), the superclass
onCreate() will be called (which you should do anyway in your implementation of
onCreate()). This will not construct your View, so you’ll get an empty activity.
3. How many mistakes are there in this Gradle file?

apply ​plugin​: '​com.android.application​'

android {
compileSdkVersion ​24
buildToolsVersion ​"24.0.1"

defaultConfig {
applicationId ​"com.mad.exercise5"
minSdkVersion ​15
targetSdkVersion ​24
versionCode ​1
versionName ​"1.0"
}
}
dependencies {
compile fileTree(dir: ​'libs'​, include: [​'*.jar'​])
testCompile ​'junit:junit:4.12'
compile ​'com.android.support:appcompat-v7:24.2.0'
compile ​'com.android.support:design:24.2.0'
}

a. 0
b. 1
c. 2
d. 3

This one is perfectly fine.


4. How many compile errors are there in the following code?

final​ String PHONE = ​"12345678"​;

Intent myIntent = ​new​ Intent(​this​, ActivityTwo.​class​);


myIntent.putExtra(PHONE, PhoneStr);
myIntent.putExtra(​12345678​, PhoneStr);
myIntent.putExtras(​"12345678"​, PhoneStr);

a. 0
b. 1
c. 2
d. 3
The last line is an obvious error as the Intent class has no putExtras method (it is
putExtra). The first argument should be a String, so the third one is also a compile
error.

5. This code simulates downloading a file. Is it correct?

@Override
protected​ Void doInBackground(Void... params)
{
// Simulate the download process by sleeping for some period of time
​ try​ {
Thread.sleep(​FILE_DOWNLOAD_TIME​);
} ​catch​ (InterruptedException e) {
e.printStackTrace();
}
​mProgressDialog​.dismiss();
​return​ null;
}

a. Yes
b. No
We shouldn’t modify the UI from a background thread. Instead use
onProgressUpdate and onPostExecute methods.
6. Is this correct?

@Override
protected​ ​void​ onProgressUpdate(Integer... values) {
​super​.onProgressUpdate(values);
​mProgressDialog​.setMessage(getFormattedString (R.string.​downloading​,
values[​0​]));
​mProgressDialog​.incrementProgressBy(​1​);
}

a. Yes
b. No
Yes. This is executed in the context of the UI thread; your background task uses
publishProgress to asynchronously provide updates which are used by this method
to update the UI.

7. True or false? You always need to override the onPreExecute method in an


AsyncTask.
a. True
b. False
You can, but you don’t need to.

8. Does this follow the Android coding conventions?


@Override
protected​ ​void​ onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.​layout​.​activity_main​);
Spinner mSpinner = (Spinner) findViewByID (R.id.​activity_main_spinner​);

a. Yes
b. No
The Spinner is a local variable, so it shouldn’t be prefixed with m. Everything else is
OK.

You might also like