Bird
Raised Fist0
Kotlinprogramming~20 mins

Finally block behavior in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of the finally block in Kotlin?

easy
A. To execute code that must run regardless of exceptions
B. To catch exceptions thrown in the try block
C. To declare variables used in try block
D. To stop program execution immediately

Solution

  1. Step 1: Understand the role of finally

    The finally block runs after try and catch, no matter what happens.
  2. Step 2: Identify its main use

    It is used to run cleanup code like closing files or releasing resources, always executing even if exceptions occur.
  3. Final Answer:

    To execute code that must run regardless of exceptions -> Option A
  4. Quick Check:

    finally always runs = A [OK]
Hint: Remember: finally always runs after try/catch [OK]
Common Mistakes:
  • Confusing finally with catch block
  • Thinking finally only runs if no exception
  • Believing finally stops program execution
2.

Which of the following is the correct syntax for a try-catch-finally block in Kotlin?

?
easy
A. catch (e: Exception) { /* handle */ } try { /* code */ } finally { /* cleanup */ }
B. try { /* code */ } finally { /* cleanup */ } catch (e: Exception) { /* handle */ }
C. try { /* code */ } catch { /* handle */ } finally { /* cleanup */ }
D. try { /* code */ } catch (e: Exception) { /* handle */ } finally { /* cleanup */ }

Solution

  1. Step 1: Recall Kotlin try-catch-finally order

    The correct order is try, then catch, then finally.
  2. Step 2: Check syntax correctness

    try { /* code */ } catch (e: Exception) { /* handle */ } finally { /* cleanup */ } follows the correct order and syntax with exception type in catch.
  3. Final Answer:

    try { /* code */ } catch (e: Exception) { /* handle */ } finally { /* cleanup */ } -> Option D
  4. Quick Check:

    try-catch-finally order = C [OK]
Hint: Remember order: try, catch, then finally [OK]
Common Mistakes:
  • Placing finally before catch
  • Omitting exception type in catch
  • Starting with catch block
3.

What will be the output of the following Kotlin code?

fun test(): Int {
    try {
        println("Try block")
        return 1
    } catch (e: Exception) {
        println("Catch block")
        return 2
    } finally {
        println("Finally block")
    }
}

fun main() {
    println("Result: ${test()}")
}
medium
A. Try block Finally block Result: 1
B. Try block Result: 1
C. Try block Catch block Finally block Result: 2
D. Finally block Result: 1

Solution

  1. Step 1: Trace the try block execution

    The try block prints "Try block" and returns 1, but before returning, finally runs.
  2. Step 2: Understand finally block behavior with return

    The finally block prints "Finally block" before the function returns the value 1.
  3. Final Answer:

    Try block Finally block Result: 1 -> Option A
  4. Quick Check:

    finally runs before return = A [OK]
Hint: finally runs even if return happens in try [OK]
Common Mistakes:
  • Ignoring finally output when return is in try
  • Assuming catch runs without exception
  • Thinking finally runs after return prints
4.

Identify the error in this Kotlin code snippet:

fun example() {
    try {
        println("Inside try")
    } finally {
        println("Inside finally")
    } catch (e: Exception) {
        println("Inside catch")
    }
}
medium
A. "catch" block must come before "finally" block
B. "finally" block must come after "catch" block
C. "catch" block cannot be used without "try"
D. No error, code is correct

Solution

  1. Step 1: Check order of try-catch-finally

    The correct order is try, then catch, then finally. Here finally comes before catch, which is invalid.
  2. Step 2: Identify correct placement

    The finally block must always come after all catch blocks.
  3. Final Answer:

    "finally" block must come after "catch" block -> Option B
  4. Quick Check:

    finally after catch = D [OK]
Hint: Catch comes before finally in try-catch-finally [OK]
Common Mistakes:
  • Placing finally before catch
  • Thinking catch can be after finally
  • Ignoring syntax order rules
5.

Consider this Kotlin function:

fun compute(): Int {
    try {
        return 10
    } finally {
        return 20
    }
}

What will compute() return and why?

hard
A. 10, because the first return in try is used
B. Compilation error due to multiple returns
C. 20, because finally's return overrides try's return
D. 0, because finally resets the return value

Solution

  1. Step 1: Analyze return in try block

    The try block returns 10, but before returning, finally executes.
  2. Step 2: Understand return in finally block

    The finally block also has a return statement, which overrides the previous return from try.
  3. Final Answer:

    20, because finally's return overrides try's return -> Option C
  4. Quick Check:

    Return in finally overrides try return = B [OK]
Hint: Return in finally overrides any previous return [OK]
Common Mistakes:
  • Assuming first return always wins
  • Thinking multiple returns cause error
  • Ignoring finally's effect on return
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard