0
0
Kotlinprogramming~20 mins

Finally block behavior in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Finally Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of finally block with return in try
What is the output of this Kotlin code snippet?
Kotlin
fun test(): Int {
    try {
        return 1
    } finally {
        println("Finally block executed")
    }
}

fun main() {
    println(test())
}
ACompilation error
B
Finally block executed
1
C
Finally block executed
0
D
1
Finally block executed
Attempts:
2 left
💡 Hint
Remember that finally block runs even if try returns.
Predict Output
intermediate
2:00remaining
Effect of exception and finally block
What will be printed when this Kotlin code runs?
Kotlin
fun test(): Int {
    try {
        throw Exception("Error")
    } finally {
        println("Finally block runs")
    }
}

fun main() {
    try {
        test()
    } catch (e: Exception) {
        println(e.message)
    }
}
A
Finally block runs
Error
BFinally block runs
C
Error
Finally block runs
DNo output, program crashes
Attempts:
2 left
💡 Hint
Finally block runs even if exception is thrown.
🔧 Debug
advanced
2:00remaining
Why does this finally block not execute?
Consider this Kotlin code. Why does the finally block not print anything?
Kotlin
fun test(): Int {
    try {
        System.exit(0)
    } finally {
        println("Finally block executed")
    }
}

fun main() {
    test()
}
ABecause println is inside try, not finally.
BBecause finally block only runs if exception is thrown.
CBecause System.exit terminates the JVM immediately, skipping finally.
DBecause the code has a syntax error and does not compile.
Attempts:
2 left
💡 Hint
System.exit stops the program immediately.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in finally usage
Which option contains a syntax error related to the finally block in Kotlin?
Atry { println("Try") } catch (e: Exception) { println("Catch") } finally { println("Finally") }
Btry { println("Try") } finally println("Finally")
C} )"yllaniF"(nltnirp { yllanif } )"hctaC"(nltnirp { )noitpecxE :e( hctac } )"yrT"(nltnirp { yrt
Dtry { println("Try") } catch (e: Exception) { println("Catch") } finally println("Finally")
Attempts:
2 left
💡 Hint
Finally block must be a block, not a single statement without braces if catch is present.
🧠 Conceptual
expert
2:00remaining
Finally block and variable modification with return
What is the output of this Kotlin code?
Kotlin
fun test(): Int {
    var x = 1
    try {
        return x
    } finally {
        x = 2
        println("Finally block: x = $x")
    }
}

fun main() {
    println("Returned value: ${test()}")
}
A
Finally block: x = 2
Returned value: 1
B
Finally block: x = 2
Returned value: 2
C
Returned value: 1
Finally block: x = 2
DCompilation error
Attempts:
2 left
💡 Hint
Return value is determined before finally block runs.