Challenge - 5 Problems
Top-level C# Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of a simple top-level statement program
What is the output of this C# program using top-level statements?
C Sharp (C#)
using System; Console.WriteLine("Hello from top-level statements!");
Attempts:
2 left
💡 Hint
Top-level statements allow code directly in the file without a Main method.
✗ Incorrect
In modern C#, top-level statements let you write code directly without defining a Main method explicitly. The program runs and prints the message.
❓ Predict Output
intermediate1:30remaining
Variable scope in top-level statements
What will be the output of this program?
C Sharp (C#)
int x = 5; { int x = 10; Console.WriteLine(x); } Console.WriteLine(x);
Attempts:
2 left
💡 Hint
Variables declared inside a block can shadow outer variables.
✗ Incorrect
The inner block declares a new variable x = 10, which shadows the outer x = 5. The first WriteLine prints 10, the second prints 5.
🔧 Debug
advanced2:00remaining
Identify the error in top-level statements with async Main
This program uses top-level statements with async code. What error will it produce?
C Sharp (C#)
using System.Threading.Tasks; async Task<int> GetNumberAsync() { await Task.Delay(100); return 42; } int result = GetNumberAsync().Result; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Blocking on async code with .Result can cause deadlocks.
✗ Incorrect
Calling .Result on an async method in top-level statements can cause a deadlock because the synchronization context waits for the task to complete while blocking the thread.
❓ Predict Output
advanced1:30remaining
Using namespaces with top-level statements
What is the output of this program?
C Sharp (C#)
namespace MyApp {
Console.WriteLine("Inside namespace");
}
Console.WriteLine("Outside namespace");Attempts:
2 left
💡 Hint
Top-level statements cannot be placed directly inside a namespace block.
✗ Incorrect
In C#, top-level statements must be at the file root, not inside namespaces. This code causes a compilation error.
🧠 Conceptual
expert2:00remaining
Order of execution in multiple top-level statement files
If a C# project has two files with top-level statements, File1.cs and File2.cs, which statement is true about their execution order?
Attempts:
2 left
💡 Hint
Top-level statements from multiple files are combined but order is not guaranteed.
✗ Incorrect
The C# compiler merges top-level statements from all files into one Main method, but the order depends on compilation order which is not guaranteed. Relying on execution order across files is unsafe.