Challenge - 5 Problems
Ternary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested ternary operator
What is the output of this C# code snippet?
C Sharp (C#)
int x = 5; int y = 10; string result = x > y ? "x is greater" : x == y ? "x equals y" : "x is smaller"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember how nested ternary operators evaluate conditions from left to right.
✗ Incorrect
Since x (5) is less than y (10), the first condition (x > y) is false, so it evaluates the second ternary: x == y? which is also false, so it returns "x is smaller".
❓ Predict Output
intermediate2:00remaining
Ternary operator with boolean expressions
What will this code print?
C Sharp (C#)
bool isSunny = false; bool haveUmbrella = true; string message = isSunny ? "Go outside" : haveUmbrella ? "Take umbrella" : "Stay inside"; Console.WriteLine(message);
Attempts:
2 left
💡 Hint
Check the value of isSunny and then haveUmbrella.
✗ Incorrect
isSunny is false, so it evaluates the second ternary. haveUmbrella is true, so it returns "Take umbrella".
❓ Predict Output
advanced2:00remaining
Ternary operator with different data types
What is the output of this code?
C Sharp (C#)
var value = 7; var output = value % 2 == 0 ? "Even" : value > 5 ? 100 : 50; Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Check the types of the values returned by the ternary operator.
✗ Incorrect
The ternary operator must return the same type for both outcomes. Here, one branch returns a string and the other returns an int, causing a compilation error.
❓ Predict Output
advanced2:00remaining
Ternary operator with side effects
What will be printed by this code?
C Sharp (C#)
int a = 3; int b = 4; int result = a > b ? a++ : ++b; Console.WriteLine($"result = {result}, a = {a}, b = {b}");
Attempts:
2 left
💡 Hint
Remember how post-increment and pre-increment operators work.
✗ Incorrect
Since a (3) is not greater than b (4), the false branch ++b is executed. ++b increments b to 5 and returns the new value 5, which is assigned to result. a remains 3. Output: result = 5, a = 3, b = 5.
❓ Predict Output
expert2:00remaining
Complex nested ternary with method calls
What is the output of this code?
C Sharp (C#)
static string Check(int n) => n % 2 == 0 ? "Even" : n % 3 == 0 ? "Divisible by 3" : "Other"; Console.WriteLine(Check(9));
Attempts:
2 left
💡 Hint
Check the order of conditions in the nested ternary.
✗ Incorrect
9 is not even, so first condition is false. Then it checks if 9 is divisible by 3, which is true, so it returns "Divisible by 3".