0
0
C Sharp (C#)programming~20 mins

Finally block behavior in C Sharp (C#) - 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 try-finally without catch
What is the output of this C# code snippet?
C Sharp (C#)
using System;
class Program {
  static void Main() {
    try {
      Console.WriteLine("Start");
    } finally {
      Console.WriteLine("Finally block executed");
    }
  }
}
AFinally block executed\nStart
BNo output
CStart\nFinally block executed
DStart
Attempts:
2 left
💡 Hint
Remember that the finally block always runs after the try block, even if no exception occurs.
Predict Output
intermediate
2:00remaining
Finally block with exception thrown
What will be printed when this code runs?
C Sharp (C#)
using System;
class Program {
  static void Main() {
    try {
      Console.WriteLine("Before exception");
      throw new Exception("Error");
    } finally {
      Console.WriteLine("In finally");
    }
  }
}
ABefore exception\nIn finally\nUnhandled Exception: Error
BIn finally\nBefore exception\nUnhandled Exception: Error
CBefore exception\nUnhandled Exception: Error
DNo output
Attempts:
2 left
💡 Hint
The finally block runs even if an exception is thrown, before the exception propagates.
Predict Output
advanced
2:00remaining
Return value with finally modifying variable
What is the output of this program?
C Sharp (C#)
using System;
class Program {
  static int Test() {
    int x = 1;
    try {
      return x;
    } finally {
      x = 2;
      Console.WriteLine("Finally executed");
    }
  }
  static void Main() {
    Console.WriteLine(Test());
  }
}
AFinally executed\n1
BFinally executed\n2
C1\nFinally executed
D2\nFinally executed
Attempts:
2 left
💡 Hint
The return value is determined before finally runs, even if finally changes the variable.
Predict Output
advanced
2:00remaining
Finally block with return inside it
What will this program print?
C Sharp (C#)
using System;
class Program {
  static int Test() {
    try {
      return 1;
    } finally {
      return 2;
    }
  }
  static void Main() {
    Console.WriteLine(Test());
  }
}
ARuntime exception
B1
CCompilation error
D2
Attempts:
2 left
💡 Hint
A return in finally overrides any previous return from try.
🧠 Conceptual
expert
2:00remaining
Behavior of finally with exception and return
Consider this code snippet. What happens when it runs?
C Sharp (C#)
using System;
class Program {
  static int Test() {
    try {
      throw new Exception("Error");
    } finally {
      return 42;
    }
  }
  static void Main() {
    Console.WriteLine(Test());
  }
}
APrints nothing and program crashes
BPrints 42 and suppresses the exception
CThrows the exception and does not print anything
DCompilation error due to return in finally
Attempts:
2 left
💡 Hint
A return in finally block suppresses any exception thrown in try.