Using non-existent methods like createUser or registerUser
Confusing method names
3. Given this Kotlin code snippet, what will be printed if the user is signed in?
val user = firebaseAuth.currentUser
if (user != null) {
println("User is signed in")
} else {
println("No user signed in")
}
medium
A. User is signed in
B. NullPointerException error
C. No user signed in
D. Compilation error
Solution
Step 1: Understand currentUser property
firebaseAuth.currentUser returns the current signed-in user or null if none.
Step 2: Analyze the if condition
If user is not null, it prints "User is signed in"; otherwise, "No user signed in".
Final Answer:
User is signed in -> Option A
Quick Check:
currentUser != null means user signed in [OK]
Hint: currentUser null means no user; else signed in [OK]
Common Mistakes:
Assuming currentUser throws error if null
Confusing sign-in check logic
Expecting compilation errors incorrectly
4. Identify the error in this Kotlin code for signing out a user:
firebaseAuth.signOut
println("User signed out")
medium
A. signOut is not a FirebaseAuth method
B. Missing parentheses after signOut method
C. println should be inside a callback
D. No error, code is correct
Solution
Step 1: Check signOut method usage
signOut is a method and requires parentheses: signOut()
Step 2: Verify other options
signOut exists in FirebaseAuth; println can be outside callback; so only missing parentheses is error.
Final Answer:
Missing parentheses after signOut method -> Option B
Quick Check:
signOut() needs parentheses [OK]
Hint: Methods need () even if no parameters [OK]
Common Mistakes:
Forgetting parentheses on method calls
Thinking signOut is a property
Assuming println must be in callback
5. You want to create a function in Kotlin that registers a user and then immediately signs them in using Firebase Authentication. Which code snippet correctly does this?
hard
A. firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { println("User registered and signed in") } }
B. firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { println("User registered and signed in") } }
C. firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { firebaseAuth.signInWithEmailAndPassword(email, password) } }
D. firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { firebaseAuth.signOut() } }
This method registers and automatically signs in the user if successful.
Step 2: Analyze options for correct flow
createUserWithEmailAndPassword(...).addOnCompleteListener { task -> if (task.isSuccessful) { println("User registered and signed in") } } registers and confirms success. signInWithEmailAndPassword only signs in existing users. Calling signIn after createUser is redundant. Calling signOut after is incorrect.
Final Answer:
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { println("User registered and signed in") } } -> Option A
Quick Check:
Registration auto signs in [OK]
Hint: Register auto signs in; no need to call signIn after [OK]
Common Mistakes:
Calling signIn after createUserWithEmailAndPassword unnecessarily
Using signIn without registering first
Signing out immediately after registration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.