Cloud Firestore is a flexible, scalable NoSQL cloud database to store and sync data for client- and server-side development. It allows real-time updates and offline support in Android apps.
Click to reveal answer
beginner
How do you initialize Cloud Firestore in an Android Kotlin app?
You initialize Firestore by calling FirebaseFirestore.getInstance() which returns a Firestore database reference to perform read/write operations.
Click to reveal answer
beginner
What is a Document in Cloud Firestore?
A Document is a lightweight record that contains fields mapping to values. Documents are stored in Collections and identified by unique IDs.
Click to reveal answer
intermediate
How do you add data to a Firestore collection in Kotlin?
Use firestore.collection("collectionName").add(dataMap).addOnSuccessListener { ... } to add a new document with data to a collection.
Click to reveal answer
intermediate
What is the benefit of using listeners with Firestore in Android?
Listeners allow your app to receive real-time updates when data changes in Firestore, keeping the UI in sync without manual refresh.
Click to reveal answer
Which method initializes Cloud Firestore in an Android Kotlin app?
AFirebaseDatabase.getInstance()
BFirestore.initialize()
CFirebaseFirestore.getInstance()
DFirestore.connect()
✗ Incorrect
FirebaseFirestore.getInstance() returns the Firestore database reference needed to perform operations.
In Firestore, data is stored in what structure?
ACollections and Documents
BBuckets and Objects
CTables and Rows
DFiles and Folders
✗ Incorrect
Firestore stores data in Collections which contain Documents, unlike traditional tables.
Which Firestore method adds a new document with auto-generated ID?
Acollection.add(data)
Bcollection.set(data)
Cdocument.update(data)
Ddocument.get()
✗ Incorrect
The add() method adds a new document with an auto-generated ID to the collection.
What feature allows Firestore to update your app UI automatically when data changes?
ABackground Services
BPolling
CManual Refresh
DListeners
✗ Incorrect
Listeners provide real-time updates to your app when Firestore data changes.
Which of these is NOT a Firestore data type?
AString
BXML
CBlob
DBoolean
✗ Incorrect
Firestore does not support XML as a data type; it supports String, Boolean, Blob, and others.
Explain how to set up and initialize Cloud Firestore in an Android Kotlin app.
Think about how you get the Firestore database object to start using it.
You got /3 concepts.
Describe how real-time data updates work in Firestore and how you can use them in your app UI.
Consider how your app can react instantly when data changes in the cloud.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of Cloud Firestore in an Android app?
easy
A. To compile Kotlin code
B. To design the app's user interface
C. To store and sync app data in the cloud
D. To manage app permissions
Solution
Step 1: Understand Cloud Firestore's role
Cloud Firestore is a cloud database service that stores and syncs app data.
Step 2: Differentiate from other app functions
UI design, code compilation, and permission management are handled by other tools, not Firestore.
Final Answer:
To store and sync app data in the cloud -> Option C
Quick Check:
Cloud Firestore = Data storage and sync [OK]
Hint: Remember Firestore is a cloud database, not UI or code tool [OK]
Common Mistakes:
Confusing Firestore with UI design tools
Thinking Firestore compiles code
Assuming Firestore manages app permissions
2. Which Kotlin code correctly initializes a Firestore instance?
easy
A. val db = FirebaseFirestore.getInstance()
B. val db = Firestore.getInstance()
C. val db = Firebase.getFirestore()
D. val db = FirestoreClient.get()
Solution
Step 1: Recall Firestore initialization syntax
The official way to get Firestore instance in Kotlin is FirebaseFirestore.getInstance().
Step 2: Check other options for correctness
Other options use incorrect class or method names not in Firestore API.
Final Answer:
val db = FirebaseFirestore.getInstance() -> Option A
Quick Check:
Correct Firestore init = val db = FirebaseFirestore.getInstance() [OK]
Hint: Use FirebaseFirestore.getInstance() to start Firestore [OK]
Common Mistakes:
Using Firestore.getInstance() which does not exist
Confusing FirebaseFirestore with Firebase class
Using FirestoreClient which is not part of Android SDK
3. Given this code snippet, what will be printed if the document exists with field "name" = "Alice"?
val db = FirebaseFirestore.getInstance()
db.collection("users").document("user1").get()
.addOnSuccessListener { document ->
if (document != null && document.exists()) {
println(document.getString("name"))
} else {
println("No such document")
}
}
medium
A. Alice
B. No such document
C. null
D. Error at runtime
Solution
Step 1: Understand the get() and success listener
The get() fetches the document asynchronously; if it exists, the listener runs the success block.
Step 2: Check document existence and print field
Since document exists and has "name" = "Alice", println(document.getString("name")) prints "Alice".
Final Answer:
Alice -> Option A
Quick Check:
Document exists with name = Alice [OK]
Hint: If document exists, getString returns the field value [OK]
Common Mistakes:
Assuming document is null when it exists
Expecting 'No such document' incorrectly
Confusing getString with get() returning document
4. Identify the error in this Firestore write operation:
val db = FirebaseFirestore.getInstance()
db.collection("users").document("user1").set("name", "Bob")
medium
A. document() must be called after set()
B. Collection name cannot be "users"
C. FirebaseFirestore.getInstance() is incorrect
D. set() requires a Map or an object, not two parameters
Solution
Step 1: Check set() method signature
set() expects a single argument: a Map or an object representing fields and values.
Step 2: Analyze the provided code
Passing two parameters "name", "Bob" is invalid and causes a compile error.
Final Answer:
set() requires a Map or an object, not two parameters -> Option D
Quick Check:
set() needs one argument (Map/object) [OK]
Hint: Use a Map for set(), not separate key-value parameters [OK]
Common Mistakes:
Passing multiple parameters to set() instead of a Map
Calling document() after set() instead of before
Thinking collection name restrictions cause error
5. You want to listen in real-time to changes in a Firestore collection "messages" and update your UI accordingly. Which code snippet correctly sets up this listener?
hard
A. db.collection("messages").get().addOnSuccessListener { snapshots -> for (doc in snapshots) { println(doc.getString("text")) } }
B. db.collection("messages").addSnapshotListener { snapshots, e -> if (e != null) { println("Listen failed") } else { for (doc in snapshots!!) { println(doc.getString("text")) } } }
C. db.collection("messages").document().addSnapshotListener { doc, e -> println(doc?.getString("text")) }
D. db.collection("messages").addListener { snapshots -> println(snapshots) }
Solution
Step 1: Understand real-time listeners in Firestore
addSnapshotListener listens for real-time updates on a collection or document.
Step 2: Analyze each option
db.collection("messages").addSnapshotListener { snapshots, e -> if (e != null) { println("Listen failed") } else { for (doc in snapshots!!) { println(doc.getString("text")) } } } correctly uses addSnapshotListener on collection with error check and iterates snapshots. db.collection("messages").get().addOnSuccessListener { snapshots -> for (doc in snapshots) { println(doc.getString("text")) } } uses get() which is one-time fetch. db.collection("messages").document().addSnapshotListener { doc, e -> println(doc?.getString("text")) } listens on a document without specifying ID. db.collection("messages").addListener { snapshots -> println(snapshots) } uses non-existent addListener method.
Final Answer:
db.collection("messages").addSnapshotListener { snapshots, e -> if (e != null) { println("Listen failed") } else { for (doc in snapshots!!) { println(doc.getString("text")) } } } -> Option B
Quick Check:
Real-time listener = addSnapshotListener on collection [OK]
Hint: Use addSnapshotListener on collection for real-time updates [OK]
Common Mistakes:
Using get() instead of addSnapshotListener for real-time
Calling addSnapshotListener on document without ID
Using non-existent addListener method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.