1

For querying content provider which one to use, getWritabledatabase or getReadabledatabase?

  • can use either
  • use getReadableDatabase
  • preferred to use getWritableDatabase
  • both are not required.

Answer: B use getReadableDatabase
Description: Query() means reading the data. so getReadableDatabase() is sufficient.
If you are doing too many operations on database like querying, updating frequently in some random order, then it doesn't really make any sense in using getReadabledatabase as it can be used only to read the values.
Based on your app requirement you can judge the usage. If you are only reading the values constantly then you can go for getreadabledatabase.

2

When does onCreate() function of ContentProvider will be called?

  • First time when any client calls that content provider.
  • First time when the application containing that content provider is loaded into the memory, it will be called.
  • After phone boots up Android will call onCreate() of all content providers in the phone.
  • onCreate() will be called every time if some client calls any DML functions on this content provider.

Answer: B First time when the application containing that content provider is loaded into the memory, it will be called.
Description: onCreate() will be called when the application containing the content provider is loaded into the memory for the first time.

3

What is the purpose of the ContentProvider class?

  • To play rich media content files.
  • To share data between Android applications.
  • To access the global information about an application environment.
  • To maintain global application state.

Answer: B To share data between Android applications.
Description: If you want to share the data of one application with the application then use content provider.
Note: We can start an Activity, a service, and a broadcast receiver by using intents. But you can't start or communicate with a content provider by using intents. If you want to communicate with content provider then you have to use content resolver.
1.content provider, and resolver will handle IPC(Inter process communication) mechanism when sharing data between 2 applications.
2.content provider has the capability to handle multiple threads, when queries are coming from multiple resolvers.

4

If you want to share the data across the all applications,you should go for ?

  • Content Provider
  • Internal Storage
  • SQLite Database
  • Shared Preferences

Answer: A Content Provider
Description: If you want to share the data of one application with the application then use content provider.

5

What is cursor.moveToNext()?

  • It will move the cursor to point to next row if it is available, else it returns false.
  • It will move the cursor to point to next row if it is available, else it crashes.
  • It will move the cursor to point to next column if it is available, else it returns false.
  • It will move the cursor to point to next column if it is available, else it crashes.

Answer: A It will move the cursor to point to next row if it is available, else it returns false.
Description: It will move the cursor to point to next row if it is available, else it returns false.

6

After fetching cursor from database query, what will happen if I use cursor.moveToNext() immediately?

  • yes we can
  • First we have to check if cursor is valid or not, by comparing with null.
  • No, we have to pass cursor to cursorfactory first, then only we can move to next row.
  • none of the above

Answer: B First we have to check if cursor is valid or not, by comparing with null.
Description: First we have to check if cursor is valid or not by cross checking it with null.

7

What will happen if database is not closed properly after using it?

  • System will automatically close database, as java has garbage collector.
  • nothing will happen
  • It will leak the memory. Once you are done with database we have to close it, because it will be cached into memory when we open it and eat up memory space.
  • If database is not closed properly, then what ever the data that was inserted & what ever the modifications done in that session will be lost.

Answer: C It will leak the memory. Once you are done with database we have to close it, because it will be cached into memory when we open it and eat up memory space.
Description:

8

Is it possible to call getWritableDataBase() from onCreate() of ContentProvider?

  • Yes, we can call.
  • Yes, we can call but before calling we have to make sure that it is called on different worker thread.
  • getWritableDataBase() will be called automatically by system. we don't need to call explicitly.
  • getWritableDatabase() may take lot of time to create and update tables, so its better to differ this call to later point of time. because if we put here, then application loading time will be increased and may irritate user.

Answer: D getWritableDatabase() may take lot of time to create and update tables, so its better to differ this call to later point of time. because if we put here, then application loading time will be increased and may irritate user.
Description: getWritableDatabase() may take lot of time to create and update tables, so its better to differ this call to later point of time. because if we put this call in onCreate(), then application loading time will be increased and may irritate the user.

9

what are the mandatory functions to be implemented for a Content provider?

  • onCreate, insert, update, delete, query, getType.
  • onCreate() and getType
  • only insert, update, delete, and query.
  • none of the above

Answer: A onCreate, insert, update, delete, query, getType.
Description: onCreate, insert, update, delete, query, getType are the mandatory functions to be implemented in Content Provider.

10

What is android:authorities in content provider?

  • There is no significance for this authorities. But we have to make sure every content provider should have at least one authority.
  • To register every content provider with Android, it has to use authorities, other than this there is no significance of it.
  • This is main key or hint to Android on which content provider the query has to be redirected to. When clients pass URI, it will have authorities in it which should match with authorities part of provider tag.
  • none of the above

Answer: C This is main key or hint to Android on which content provider the query has to be redirected to. When clients pass URI, it will have authorities in it which should match with authorities part of provider tag.
Description: Authorities is main key or hint to Android on which content provider the query has to be redirected to. When clients pass URI, it will have authorities in it which should match with authorities part of provider tag.

11

what is contentprovider and contentresolver? which of the below statements are true about them?

  • i. content provider, and resolver will handle IPC mechanism when query comes from outside apps to access the data.
  • ii. content provider has the capability to handle multiple threads, when queries are coming from multiple resolvers.
  • iii. sharedpreferences are supported with content providers.
  • i
  • i & ii
  • i, ii, & iii
  • none

Answer: B i & ii
Description: If you want to share the data of one application with the application then use content provider.
Only content resolver can communicate with content provider, from client application.
Note: We can start an Activity, a service, and a broadcast receiver by using intents. But you can't start or communicate with a content provider by using intents. If you want to communicate with content provider then you have to use content resolver.
1.content provider, and resolver will handle IPC(Inter process communication) mechanism when sharing data between 2 applications.
2.content provider has the capability to handle multiple threads, when queries are coming from multiple resolvers.

12

what is UriMatcher in android content provider?

  • i. It is used for conversion logic from Uris to tables.
  • ii. It can be comparable to DNS, which converts URLs to IP addresses.
  • iii. it is used to store tables in databases.
  • i
  • i & ii
  • i, ii, & iii
  • none

Answer: B i & ii
Description: It is used for conversion logic or mapping logic from Uris to tables.It is similar to DNS, which converts URLs to IP addresses.
Note : Just like how we use URLs to connect to servers in the internet (eg: http://google.com for google server) similarly, in android if any application wants to access the data hold by other applications which is shared via content provider, the client application has to access it via URI (Uniform resource identifier). URI's looks exactly like an URL.
When client application access our database tables using URI, UriMatcher will map that URI to appropriate table.

13

When implementing a content provider, how to expose table URIs?

  • i. create a separate interface file, which is accessible to out side world and put table URIs in that file.
  • ii. Along with URIs we have to mention column names of all the tables, which we are exposing with URIs.
  • iii. we have to document in interface file about what is the data type of each column.
  • i
  • i & ii
  • i, ii, & iii
  • ii & iii

Answer: C i, ii, & iii
Description: 1.Create a separate interface file, which is accessible to out side world and put table URIs in that file.
2.Along with URIs we have to mention column names of all the tables, which we are exposing with URIs.
3.Then we have to document in interface file about what is the data type of each column.

14

What is the difference between Content provider and SQLite database? which one to use?

  • i. Use content provider if you want to share the db created by one activity of application to other activity of the same application.
  • ii. Use database if we want to store DB private to application, and for storing structured heavy data.
  • iii. Use content provider if you want to share the data created by one application to other application.
  • i & ii
  • ii
  • ii & iii
  • none

Answer: C ii & iii
Description: DB is used to create tables private to application. You can access SQLite Database with in the application directly. Other applications can't directly touch the SQLite Database of a given application.If you want to share your database with other applications then use Content provider.
Content provider is used to share the data with other applications.

15

Which of the options are supported to be used with a content provider

  • i. sharedpreferences
  • ii. files
  • iii. databases
  • i
  • i & ii
  • ii & iii
  • none

Answer: C ii & iii
Description: To share data with other applications, data can be stored internally by using files, or databases, or through some network servers. Right now there is no support for SharedPreferences.

16

Where does the context is available in a Content Provider?

  • it is available in constructor of the content provider.
  • it will be available only from onCreate() of content provider.
  • content providers doesn't have contexts.
  • none are true.

Answer: B it will be available only from onCreate() of content provider.
Description: context will be available only from onCreate() of content provider.

17

How to access context in content provider?

  • use getContext() in onCreate().
  • use getApplicationContext() any where.
  • Context will not have both its own and applications context, it has to depend on others context.
  • both option 1 and 2 are right.

Answer: D both option 1 and 2 are right.
Description: use getcontext() in oncreate() or use getApplicationContext().

18

Android fetching contact details into application:
What does below code do?

Cursor c1 = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(c1 != null && c1.getCount() > 0)
{
c1.moveToNext();
String n = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
...
}
  • It is getting all rows of contacts table, and fetching all the names.
  • It is getting all rows of contacts table, and fetching contact name from first row.
  • It is getting all rows of contacts table, and fetching contact name from last row.
  • It is getting all rows of contacts table, and fetching contact number from first row.

Answer: B It is getting all rows of contacts table, and fetching contact name from first row.
Description: we are getting all rows of contacts table into a cursor, and fetching contact name from first row.