0
0
Android Kotlinmobile~20 mins

Error handling patterns in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Handling Null Pointer Exception in Kotlin Android
What will be the output when this Kotlin Android code runs and the nullable variable is null?
Android Kotlin
val userName: String? = null
val displayName = userName!!.length
println("Length: $displayName")
AThrows KotlinNullPointerException at runtime
BPrints: Length: 0
CPrints: Length: null
DCompilation error due to null safety
Attempts:
2 left
💡 Hint
The double exclamation mark (!!) forces a non-null assertion.
🧠 Conceptual
intermediate
2:00remaining
Try-Catch Block Behavior in Kotlin Android
What is the output of this Kotlin code snippet in Android when an exception occurs inside the try block?
Android Kotlin
try {
  val result = 10 / 0
  println("Result: $result")
} catch (e: ArithmeticException) {
  println("Caught division by zero")
} finally {
  println("Finally block executed")
}
AResult: 0\nFinally block executed
BOnly Finally block executed
CCaught division by zero\nFinally block executed
DRuntime crash without any output
Attempts:
2 left
💡 Hint
Division by zero throws ArithmeticException.
lifecycle
advanced
2:00remaining
Exception Handling in Android Activity Lifecycle
If an exception is thrown inside onCreate() of an Android Activity and not caught, what happens?
AException is logged but app continues normally
BException is silently ignored and activity continues
CActivity restarts automatically without user notice
DApp crashes and shows a crash dialog
Attempts:
2 left
💡 Hint
Uncaught exceptions in lifecycle methods cause app crashes.
📝 Syntax
advanced
2:00remaining
Correct Kotlin Syntax for Catching Multiple Exceptions
Which Kotlin code snippet correctly catches both IOException and NumberFormatException in Android?
Android Kotlin
fun parseData() {
  try {
    // code that may throw exceptions
  } catch (e: IOException, e: NumberFormatException) {
    println("Exception caught")
  }
}
Acatch (e: IOException) { println("Exception caught") } catch (e: NumberFormatException) { println("Exception caught") }
Bcatch (e: IOException | NumberFormatException) { println("Exception caught") }
Ccatch (e: Exception) { if (e is IOException || e is NumberFormatException) println("Exception caught") }
Dcatch (e: IOException, e: NumberFormatException) { println("Exception caught") }
Attempts:
2 left
💡 Hint
Kotlin does not support multiple exception types in one catch clause.
🔧 Debug
expert
2:00remaining
Diagnosing Silent Failure in Coroutine Exception Handling
In this Kotlin coroutine code, why does the exception not crash the app or print the error message?
Android Kotlin
GlobalScope.launch {
  try {
    throw IllegalStateException("Error in coroutine")
  } catch (e: Exception) {
    // empty catch block
  }
}
println("Coroutine launched")
AGlobalScope.launch requires explicit supervisor to catch exceptions
BException is caught but ignored due to empty catch block
CCoroutine does not support exceptions so it fails silently
DException is thrown on main thread causing crash before catch
Attempts:
2 left
💡 Hint
Empty catch blocks swallow exceptions silently.