Font size
WorksheetsC# Debugging Practice Quiz
Total questions: 22
Worksheet time: 11mins
What is the syntax error in the following C# code snippet?
int number = 10
Console.WriteLine(number);
Missing semicolon after `int number = 10`
Incorrect variable type
Missing parentheses in `Console.WriteLine`
Incorrect method name
Which of the following is a common debugging tool in Visual Studio?
Code Formatter
Breakpoints
Code Refactor
Code Snippets
Identify the syntax error in this C# code:
string name = "John";
if (name = "John") { Console.WriteLine("Hello, John!"); }
Incorrect use of `Console.WriteLine`
Missing semicolon after `Console.WriteLine("Hello, John!")`
Assignment operator `=` should be `==` in the `if` statement
Incorrect string declaration
Which of the following is NOT a feature of the Visual Studio Debugger?
Step Into
Step Over
Step Back
Step Out
What is the syntax error in the following C# code?
void SampleFunction() {
Console.WriteLine("The result is: " + result);
}
Missing semicolon
`result` is not defined
Incorrect use of `Console.WriteLine`
Incorrect string concatenation
Which of the following is a benefit of using a debugger?
It automatically fixes syntax errors
It helps identify logical errors in code
It optimises code for better performance
It formats code according to style guidelines
What is the syntax error in this C# code snippet?
for (int i = 0; i < 10; i++) {
Console.WriteLine(i)
}
Incorrect loop condition
Missing semicolon after `Console.WriteLine(i)`
Incorrect variable declaration
Missing curly braces
What is the purpose of the `try-catch` block in C#?
To declare variables
To handle exceptions
To define classes
To create loops
Which of the following is NOT a syntax error in C#?
Missing semicolon
Incorrect variable type
Using `=` instead of `==` in a condition
Using a reserved keyword as a variable name
What is the syntax error in this C# code?
int[] numbers = {1, 2, 3, 4, 5};
Console.WriteLine(numbers[5]);
Incorrect array declaration
Index out of range
Missing semicolon
Incorrect use of `Console.WriteLine`
Which of the following is a feature of the Visual Studio Debugger?
Code Refactoring
Code Snippets
Watch Window
Code Formatting
What is the syntax error in this C# code snippet?
void public DisplayMessage() {
Console.WriteLine("Hello, World!") ;
}
Missing semicolon after `Console.WriteLine("Hello, World!")`
Incorrect method declaration
Missing return type
Incorrect use of `Console.WriteLine`
A program is supposed to print all elements in an array. However, it misses the last element.
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length - 1; i++) // Bug here
{
Console.WriteLine(numbers[i]);
}
What’s the issue?
Incorrect array initialisation
Loop ends too early
Array index out of bounds
A program hangs indefinitely due to a loop. What's the issue?
int i = 0;
while (i < 100)
{
Console.WriteLine(i);
}
What’s the fix?
Add i++; inside the loop
Use a for 10 loop instead
No issues to fix
A program is supposed to check if a word is "Hello", but it fails for "hello".
string input = "hello";
if (input == "Hello") // Bug here
{
Console.WriteLine("Matched!");
}
Use ToLower() or ToUpper() for comparison
Change the condition to if (input.Equals("Hello", true))
Use a regular expression
What will happen if you run the following code?
int a = 10, b = 0;
Console.WriteLine(a / b);
It will throw a DivideByZeroException
It will print 0
It will output a long floating number
The program should print "Even" for even numbers, but it always prints "Odd".
int num = 4;
if (num % 2 == 1) // Bug here
{ Console.WriteLine("Odd"); }
else
{ Console.WriteLine("Even"); }
What’s the issue?
Incorrect modulus operation
Incorrect condition for even numbers
The else block is missing
The program throws an exception when accessing an array element. What's the problem?
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[3]);
Accessing an element out of bounds
Index starts from 1
Array is empty
The program crashes with a NullReferenceException.
string name = null;
Console.WriteLine(name.ToUpper());
How to fix this?
Add a null check before accessing name
Assign a default value to name
Both A and B
What will happen if you run this code?
int number = "123";
It will print 123
It will throw a compilation error
It will assign 0 to number
The program is supposed to calculate the sum of all even numbers from 1 to 10, but the result is wrong.
int sum = 0;
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 1) { sum += i; }
}
Console.WriteLine(sum);
What’s the issue?
The loop condition is correct
The condition should check for even numbers
Missing initialisation of sum
The method below doesn't compile. Why?
public static int GetSquare(int number)
{
if (number > 0)
{
return number * number;
}
}
The method doesn't return a value for all paths
The condition is incorrect
The method should be void
