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

Top-level statements in modern C# - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Top-level C# Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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!");
ANo output, program does nothing
BRuntime error: entry point not found
CHello from top-level statements!
DCompilation error: missing Main method
Attempts:
2 left
💡 Hint
Top-level statements allow code directly in the file without a Main method.
Predict Output
intermediate
1: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);
A10\n5
B5\n10
CCompilation error: variable 'x' already declared
D10\n10
Attempts:
2 left
💡 Hint
Variables declared inside a block can shadow outer variables.
🔧 Debug
advanced
2: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);
ACompilation error: async method not awaited
BDeadlock or runtime hang
COutput: 42
DRuntime exception: NullReferenceException
Attempts:
2 left
💡 Hint
Blocking on async code with .Result can cause deadlocks.
Predict Output
advanced
1: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");
ACompilation error: statements not allowed directly inside namespace
BInside namespace\nOutside namespace
COnly Outside namespace
DRuntime error: invalid namespace usage
Attempts:
2 left
💡 Hint
Top-level statements cannot be placed directly inside a namespace block.
🧠 Conceptual
expert
2: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?
AFiles execute in alphabetical order by filename
BThe file listed first in the project file executes first
CThe compiler merges all top-level statements into a single Main method in source order
DExecution order is undefined and should not be relied upon
Attempts:
2 left
💡 Hint
Top-level statements from multiple files are combined but order is not guaranteed.