static void Main(string[] args) {
    try {
        Console.WriteLine("Level 1");
        try {
            Console.WriteLine("Level 2");
            goto exit;
        } finally {
            Console.WriteLine("Level 2 Finished");
        }
    } finally {
        Console.WriteLine("Level 1 Finished");
    }
    exit: ;
}

What is the output?

  1. a. Leve1,Level 2,Level 1 Finished

  2. b. Level,Leve2,Leve 2 Finished

  3. c. Level 1,Level 2,Leve 2 Finished,Level 1 Finished

  4. d.Error


Correct Option: C
Explanation:

To understand the output of this code, we need to examine the nested try-finally blocks and the use of the goto statement.

The code first prints "Level 1", then enters a try block. Within that try block, it prints "Level 2", then jumps to the "exit" label using a goto statement. This causes the finally block within the inner try block to execute, which prints "Level 2 Finished".

After the inner finally block completes, the outer finally block executes, which prints "Level 1 Finished". Thus, the final output of the code will be:

Level 1 Level 2 Level 2 Finished Level 1 Finished

Therefore, the correct answer is:

The Answer is: C. Level 1, Level 2, Level 2 Finished, Level 1 Finished

Find more quizzes: