What is the output of this C# code?
int x = 10; int y = 20; if (x > y) { Console.WriteLine("X is greater"); } else if (x == y) { Console.WriteLine("X equals Y"); } else { Console.WriteLine("Y is greater"); }
Compare the values of x and y carefully.
Since x (10) is less than y (20), the else block runs, printing "Y is greater".
What will this program print?
bool isRaining = false; bool haveUmbrella = true; if (isRaining && haveUmbrella) { Console.WriteLine("Go outside"); } else { Console.WriteLine("Stay inside"); }
Both conditions must be true for the first block to run.
isRaining is false, but haveUmbrella is true. The condition isRaining && haveUmbrella is false, so the else block runs, printing "Stay inside".
Which option contains a syntax error in the if-else structure?
int number = 5; if (number > 0) Console.WriteLine("Positive"); else Console.WriteLine("Non-positive");
Check the syntax for the if statement in C#.
Option D misses parentheses around the condition, which is required in C#. This causes a syntax error.
What does this program print?
int score = 85; if (score >= 90) { Console.WriteLine("Grade A"); } else if (score >= 80) { Console.WriteLine("Grade B"); } else if (score >= 70) { Console.WriteLine("Grade C"); } else { Console.WriteLine("Grade F"); }
Check which condition matches the score first.
The score 85 is not >= 90, so first if fails. The second condition (>= 80) is true, so it prints "Grade B" and skips the rest.
Consider this code snippet. What is the final value of result after execution?
int x = 5; int result = 0; if (x > 3) { result = 10; if (x < 10) { result += 5; } else { result += 20; } } else { result = -10; }
Trace the nested if-else carefully and update result step by step.
Since x is 5, the first if is true, so result becomes 10. Then the nested if checks if x is less than 10, which is true, so 5 is added. Final result is 15.