2. Which Firebase syntax correctly starts a transaction to update a value atomically?
easy
A. firebase.database().ref('count').transaction(current => current + 1)
B. firebase.database().ref('count').update(current + 1)
C. firebase.database().ref('count').set(current + 1)
D. firebase.database().ref('count').push(current + 1)
Solution
Step 1: Identify transaction syntax in Firebase
The transaction method takes a function to update the current value atomically.
Step 2: Compare options to transaction usage
Only firebase.database().ref('count').transaction(current => current + 1) uses .transaction() with a function to increment the value safely.
Final Answer:
firebase.database().ref('count').transaction(current => current + 1) -> Option A
Quick Check:
Use .transaction() with a function for atomic update [OK]
Hint: Use .transaction() with a function to update atomically [OK]
Common Mistakes:
Using .update() or .set() which are not atomic
Using .push() which adds new data, not update atomically
Passing a value instead of a function to .transaction()
3. What will be the final value of 'likes' after this Firebase transaction code runs if the initial value is 5?
firebase.database().ref('likes').transaction(current => {
if (current === null) return 1;
return current + 2;
});
medium
A. 5
B. 7
C. 1
D. null
Solution
Step 1: Check initial value and transaction logic
Initial 'likes' is 5, so current is 5, not null.
Step 2: Apply transaction function
Since current is 5, function returns 5 + 2 = 7.
Final Answer:
7 -> Option B
Quick Check:
5 + 2 = 7 after transaction [OK]
Hint: Add 2 to current if not null, else set 1 [OK]
Common Mistakes:
Assuming initial value is null and returning 1
Ignoring the +2 increment
Confusing return values with no update
4. A developer wrote this Firebase transaction code but it sometimes causes incorrect counts:
firebase.database().ref('count').transaction(current => current + 1);
What is the likely problem?
medium
A. The code should use .set() instead of .transaction()
B. The transaction method is not atomic
C. The transaction function does not handle null initial values
D. The database reference path is incorrect
Solution
Step 1: Analyze transaction function behavior
If 'current' is null (no value yet), current + 1 causes error or NaN.
Step 2: Identify missing null check
Function should check if current is null and return 1 to start counting correctly.
Final Answer:
The transaction function does not handle null initial values -> Option C
Quick Check:
Always check for null in transaction function [OK]
Hint: Check for null before adding in transaction function [OK]
Common Mistakes:
Thinking transaction is not atomic
Replacing transaction with .set() which is not atomic
Assuming path is wrong without evidence
5. You want to update a shared 'stock' count in Firebase safely when many users buy items simultaneously. Which approach best ensures the stock count stays accurate?
hard
A. Use .push() to add each purchase as a new entry without updating stock
B. Use .set() to overwrite the stock count with the new value directly
C. Use .update() with the new stock value without reading current stock
D. Use a transaction that reads the current stock, subtracts the purchase amount, and writes back atomically
Solution
Step 1: Understand the problem of concurrent updates
Multiple users buying at once can cause race conditions if stock is overwritten without checking current value.
Step 2: Choose atomic update method
Using a transaction reads current stock, applies subtraction, and writes back atomically, preventing conflicts.
Final Answer:
Use a transaction that reads the current stock, subtracts the purchase amount, and writes back atomically -> Option D
Quick Check:
Atomic transaction ensures accurate shared stock update [OK]
Hint: Use transaction to read-modify-write shared data safely [OK]
Common Mistakes:
Overwriting stock without reading current value
Adding purchases without updating stock count
Using .update() without atomicity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.