Android Data Storage: SharedPreferences, SQLite, and Files Quiz Quiz

Evaluate your understanding of Android’s core data storage options, including SharedPreferences, SQLite databases, and file storage techniques. Discover key principles, use cases, and differences between these storage solutions to enhance your app development skills.

  1. SharedPreferences Usage

    Which data type is best suited for storage using SharedPreferences in Android, such as saving a user's login token?

    1. Small key-value pairs
    2. Complex relational data
    3. Large video files
    4. Graphs and charts

    Explanation: SharedPreferences is designed for storing small key-value pairs, such as user settings or tokens. Large video files require file storage because SharedPreferences is inefficient and not designed for large data. Complex relational data is better suited for databases like SQLite. Graphs and charts are not data types but visual representations, so SharedPreferences is inappropriate for them.

  2. SQLite Database

    What is a main advantage of using SQLite for storing data in Android apps that manage customer records with multiple fields?

    1. Automatically syncs across devices
    2. Supports structured queries and relations
    3. Designed for app settings only
    4. Optimized for saving images only

    Explanation: SQLite is a lightweight database that supports structured queries, relations, and efficient management of complex data. It is not limited to app settings—that's a use case for SharedPreferences. SQLite does not automatically sync data across devices; such functionality requires additional mechanisms. While images can be stored as BLOBs, SQLite is not specifically optimized for image storage.

  3. File Storage Use

    If you want to save and retrieve a user-generated audio recording in an Android app, which storage method should you typically use?

    1. SQLite
    2. SharedViewModel
    3. File storage
    4. SharedPreferences

    Explanation: File storage is suitable for binary data like audio recordings, allowing users to save and access large files efficiently. SharedPreferences is best for small pieces of data, not multimedia files. SharedViewModel does not handle persistent storage; it is used for sharing data between components. SQLite can store binary data but is not intended for large audio files.

  4. Data Persistence

    Which Android storage mechanism persists data after the device restarts and is ideal for saving app configuration settings?

    1. Notification Channel
    2. RAM
    3. AsyncTask
    4. SharedPreferences

    Explanation: SharedPreferences provides persistent storage for simple key-value pairs, making it ideal for configuration settings even after a device restart. RAM is volatile and loses data on restart. Notification channels are not storage mechanisms but ways to categorize notifications. AsyncTask is used for background tasks, not data storage.

  5. Database Structure

    In an Android SQLite database, what are tables primarily used for?

    1. Organizing rows and columns of related data
    2. Storing app icons
    3. Rendering user interface elements
    4. Scheduling application alarms

    Explanation: SQLite tables are used to organize data as rows and columns, supporting structured storage and relation management. Alarm scheduling is unrelated to database tables and handled by other services. Rendering UI elements is not a function of databases. App icons are typically stored as files, not directly in database tables.

  6. Privacy and Storage Locations

    Which storage location should you choose in Android to ensure that app-private files are inaccessible to other apps?

    1. Internal storage
    2. Cache partition
    3. Bluetooth transfer
    4. External public folder

    Explanation: Internal storage in Android is private to the app, and files saved there cannot be accessed by other apps. External public folders can be accessed by other apps and users, making them less secure for sensitive data. Bluetooth transfer is not a storage location but a data transfer method. The cache partition is for short-term data and may be cleared by the system.

  7. Efficient Data Updates

    Imagine you need to frequently update user profile information consisting of several fields in Android. Which storage method offers the best support for these updates?

    1. Clipboard
    2. SharedPreferences
    3. Raw text files
    4. SQLite

    Explanation: SQLite is designed for efficiently updating, querying, and maintaining structured records with multiple fields. Raw text files lack structure, making updates cumbersome. The clipboard is for temporary data transfer, not storage. SharedPreferences is best for simple key-value pairs, not for complex, frequently updated records.

  8. SQLite Statement

    Which command would you typically use to add a new row to a table in Android’s SQLite database?

    1. UPDATE
    2. SELECT
    3. INSERT
    4. DELETE

    Explanation: The INSERT command adds new rows to a table in an SQLite database. UPDATE is used to modify existing records. SELECT retrieves records, while DELETE removes them. Only INSERT is correct for adding new data.

  9. SharedPreferences Scope

    In Android, by default, what is the accessibility level of data stored in SharedPreferences?

    1. Public on external storage
    2. Available to system processes only
    3. Shared with all apps
    4. Private to the app

    Explanation: By default, SharedPreferences data is private to the specific app and inaccessible to others. It is not saved to external storage or made public. Sharing with all apps is not permitted for security reasons. While system processes have high privileges, SharedPreferences is not directly shared with them.

  10. When to Avoid SharedPreferences

    Which scenario is NOT appropriate for storing data using SharedPreferences in Android?

    1. Storing theme selection
    2. Storing language preference
    3. Saving login status (true/false)
    4. Saving user browsing history

    Explanation: Saving user browsing history typically involves large or complex datasets, unsuitable for SharedPreferences, which is intended for small, simple key-value pairs. Theme selection, login status, and language preference fit SharedPreferences' purpose, as they are small and simple pieces of information.