COURSE 7: WORKING WITH DATA IN ANDROID QUIZ ANSWERS

Week 4: SQL Data in Android

Meta Android Developer Professional Certificate

Complete Coursera Answers & Study Guide

Enroll in Coursera Meta Android Developer Professional Certification

Introduction to Databases & SQL INTRODUCTION

Learn how to work with SQLite in Android

Learning Objectives

  • Store simple data in Android using Shared Preferences
  • Set up and query a SQLite database in Android
  • Connect data to app state

SELF REVIEW: READ AND WRITE WITH SHAREDPREFERENCES

1. In this exercise, what was the name of the shared preferences key?

  •  “Tip” (CORRECT)
  •  “Leave Tip”
  •  “Add Tip”

Correct! The name of the key was “Tip”.

 2. In this exercise, what was the Composable used to change shared preference key value? 

  • Button
  • Text
  • Row
  • Switch (CORRECT)

Correct! The Switch composable with onCheckedChange callback was used to change the shared preference key value.

3. In this exercise, MODE_WORLD_READABLE was used to access shared preferences. True or false?

  • True
  • False (CORRECT)

Correct! The mode MODE_PRIVATE was used to access shared preferences.

4. What is the best way to describe SharedPreferences?

  • A database.
  • An interface to a persistence solution. (CORRECT)
  • A collection of files.

That’s correct. SharedPreferences is an interface for persisting data between sessions.

5. Which method would you call to obtain shared preferences in the Activity?  

  • getSharedPreferences(“LittleLemon”, MODE_PRIVATE)  (CORRECT)
  • getPreferences(“LittleLemon”, MODE_PRIVATE)  
  • getSharedPrefs(“LittleLemon”, MODE_PRIVATE)  

That’s correct! getSharedPreferences will return an instance of shared preferences.  

6. In this exercise, you bound persisted state to the UI. When did you need to update the UI?   

  • When the data changes
  • When the screen is first loaded
  • Never. The UI updates automatically
  • When the screen is first loaded and when the data changes (CORRECT)

That’s correct. You need to update the UI before presenting it to the user and then again every time the data changes. Because mutableStateOf and remember function were used, the recomposition happens automatically.

KNOWLEDGE CHECK: INTRODUCTION TO SHAREDPREFERENCES

1. Which sentence best describes shared preferences?

  • It is a permanent data storage (CORRECT)
  • Preferences cannot be removed
  • It is a temporary data storage

Correct! Values saved to the shared preferences are stored permanently. This means the ealues will survive app restart and device reboot.

2. Which shared preference methods can be used to put data in shared preferences? Select all that apply.

  • putString (CORRECT)
  • putFloat (CORRECT)
  • putObject
  • putInt (CORRECT)
  • putBoolean (CORRECT)

Correct! The putString method can be used to save a String value in the shared preferences.

Correct! The putFloat method can be used to save a Float value in the shared preferences.

Correct! The putInt method can be used to save an Int value in the shared preferences.

Correct! The putBoolean method can be used to save a Boolean value in the shared preferences.

3. When are shared preference values vulnerable to data loss? Select all that apply.

  • During a device reboot
  • During a device reset (CORRECT)
  • During an app restart
  • During an app uninstall (CORRECT)

Correct! The device reset will remove all values from shared preferences. 

Correct! The app uninstall will remove all values from shared preferences.

4. How can a complex data type be stored in shared preferences?

  • It can’t be stored in any form 
  • It must be serialized before storing it in shared preferences  (CORRECT)
  • Use putObject

Correct! Complex data types can be serialized to String and saved in the shared preferences. 

5. Shared preferences is a temporary storage mechanism. True or false?

  • True
  • False (CORRECT)

Correct! The shared references survive app restart and device reboot, so it is persistent storage mechanism.

SELF REVIEW: READ AND WRITE WITH ROOM

1. In the last exercise, in which file did you need to place the Room dependencies?

  • build.gradle (CORRECT)
  • MenuDatabase.kt
  • MenuDao.kt

Correct! This is where you update the dependencies block. 

2. Observe the following code:

  Button(  
    modifier = Modifier  
         .fillMaxWidth()  
         .padding(16.dp),  
     onClick = {  
         val newMenuItem = MenuItem(  
             id = UUID.randomUUID().toString(),  
             name = dishName,  
             price = priceInput.toDouble()  
        )  
        lifecycleScope.launch {  
           withContext(IO) {  
                  database.menuDao().saveMenuItem(newMenuItem)  
            }  
        }  
        dishName = ""  
        priceInput = ""  
    }  
) {  
    Text("Add dish")  
}  

What is Button doing here?

  • Retrieving a MenuItem
  • Adding a new menu item to the database (CORRECT)
  • Deleting a MenuItem

Correct! It adds a new restaurant dish. 

3. In this exercise the list of menu items was attached to the app state in the MainActivity.  True or false?

  • True (CORRECT)
  • False

Correct! With MainActivity open you should update menuItems to be read from the database.

4. What is SQLite useful for? Choose the most appropriate answer. 

  • Processing CSV and JSON files.
  • Storing and reading files. (CORRECT)
  • Storing and querying complex data (collections of entities).
  • Storing and querying simple data (strings, Booleans, integers).

Correct! SQLite is a SQL engine that allows you to store and query complex data in SQL tables.

5. In this video the @Entity annotation is used to annotate what?     

  • The Room Database (CORRECT)
  • Operations such as adding a player, deleting a player or getting the list of all players   
  • A table containing the details information for the players

Correct. The body function is used to convert a response string to a Kotlin object. 

6. In this exercise, the count on screen updated when you tapped the ‘quantity’ plus and minus buttons. Why did this happen? 

  • The app observed the Room database, which was updated on taps (CORRECT)   
  • The app updated the inventory on screen on tap 
  • The Item instance was updated on tap   
  • The screen kept refreshing until something changed    

That’s correct. The screen was never updated directly. When the user taps a button, the database is updated. In turn, this updates the UI. 

KNOWLEDGE CHECK: INTRODUCTION TO ROOM

1.  SQLite stores data in SQL tables using SQL statements. True or false?

  • True   (CORRECT)  
  • False   

Correct! SQLite stores and queries data in SQL tables using SQL statements.

2. Which of these does SQLite allow you to do? Select all that apply.

  • Read data (CORRECT)
  • Update data (CORRECT)
  • Persist data (CORRECT)
  • Delete data (CORRECT)

Correct! SQLite allows to update the data.
Correct! SQLite allows to read the data.
Correct! SQLite allows to persist the data.
Correct! SQLite allows to update the data.

3. Which of these are major Room annotations? Select all that apply.

  • @Dao (CORRECT)
  • @Entity (CORRECT)
  • @Data
  • @Database (CORRECT)

Correct! The Dao annotation allows to interact with the stored data.
Correct! The Entity annotation allows to define data structure.
Correct! The Database annotation facilitates database access.

4. Which sentences correctly describe Room? Select all that apply.

  • It provides an abstraction layer over SQLite. (CORRECT)
  • It allows you to easily create and manage SQL databases. (CORRECT)
  • It’s a powerful library for working with data on an Android app. (CORRECT)
  • It simplifies database creation and management. (CORRECT)

Correct! Room creates a database abstraction layer.
Correct! Room allows you to easily create and manage SQL databases.
Correct! Room allows to easily work with databases.
Correct! The Room deals with database table creation.

5. What does the class do in the following code?

@Entity(tableName = "user_table") 
data class User ( 
    @PrimaryKey(autoGenerate = true) 
    var id: Int, 
    var name: String 
)	
  • Serves as the entry point to the Room library.
  • Provides methods for accessing and modifying the data in the database.
  • Defines the structure of the data that will be stored in the database. (CORRECT)

Correct! The @Entity Annotation is used to define database data structure.

MODULE QUIZ: SQL DATA IN ANDROID

1. Which shared preference methods should be used to save numeric data types? Select all that apply.

  • putString
  • putFloat (CORRECT)
  • putBoolean
  • putInt (CORRECT)

Correct! The putFloat method can be used to save the numeric Float value in the shared preferences.
That’s right! The putInt method can be used to save the numeric Int value in the shared preferences.

2. When are shared preference values persisted? Select all that apply.

  • During an app uninstall
  • During an app restart (CORRECT)
  • During a device reset
  • During a device reboot (CORRECT)

Correct! The shared preference value will survive the app restart.
Correct! The shared preference value will survive the device reboot.

3. Shared preferences is a persistent storage mechanism. True or false?

  • True (CORRECT)
  • False

Correct! The shared references survive app restart and device reboot, so it is a persistent storage mechanism.

4. Which one of these are major Room annotations which are used to define Room data structure?

  • @Database
  • @Dao
  • @Entity (CORRECT)

Correct! The Entity annotation allows to define data structure.

5. What does the DAO abbreviation stand for?

  • Domain Access Object
  • Device Access Object
  • Data Access Object (CORRECT)

Correct! The Data Access Object is the class that will provide methods for accessing and modifying the data in the database.

6. Which method is used to create a Room database?

  • Room.getDatabase
  • Room.createDatabase
  • Room.databaseBuilder (CORRECT)

Correct! The Room.databaseBuilder class is used to create a Room database builder that can build the database using build method.

7. Observe the following code snippet:

interface MenuDao {
@Query("SELECT * FROM MenuItem")
fun getAllMenuItems(): LiveData<List<MenuItem>>
}

Which language was used to query the data in the MenuDao interface?

  • SQL (CORRECT)
  • JSON
  • XML

Correct! SQL was used to query the database.

8. Observe the following code snippet:

@Database(entities = [Player::class], version = 1)
abstract class AppDatabase: RoomDatabase() {
abstract fun playerDao(): PlayerDao
}

What does this class represent?

  • Access player data
  • Define app database
  • Define app database and access player data (CORRECT)

That’s right. @Database annotation together with the RoomDatabase parent class is the way to define app database and access Player data.

9. What is the right syntax for saving shared preferences value?

  • sharedPreferences.edit(commit = true) { putBoolean(“Tip”, true) } (CORRECT)
  • sharedPreferences.put (commit = true) { putBoolean(“Tip”, true) }
  • sharedPreferences.putBoolean(“Tip”, true)
  • sharedPreferences.saveBoolean(true)

That’s right. This is the syntax for saving a Boolean value to shared preferences.

10. What is the right syntax for retrieving shared preferences instance?

  • getSharedPreferences(“LittleLemon”, MODE_PRIVATE) (CORRECT)
  • getSharedPreferences (MODE_PRIVATE)
  • getPreferences(“LittleLemon”)
  • getPreferences(“LittleLemon”, MODE_PRIVATE)

That’s right. Calling getSharedPreferences with a custom String key and MODE_PRIVATE is one way to get the shared preferences instance.

CONCLUSION

TBW

Subscribe to our site

Get new content delivered directly to your inbox.

Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!