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

Ternary conditional operator in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ternary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
Ax is greater
Bx equals y
Cx is smaller
DCompilation error
Attempts:
2 left
💡 Hint
Remember how nested ternary operators evaluate conditions from left to right.
Predict Output
intermediate
2: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);
AGo outside
BTake umbrella
CStay inside
DRuntime error
Attempts:
2 left
💡 Hint
Check the value of isSunny and then haveUmbrella.
Predict Output
advanced
2: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);
ACompilation error
BEven
C50
D100
Attempts:
2 left
💡 Hint
Check the types of the values returned by the ternary operator.
Predict Output
advanced
2: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}");
Aresult = 5, a = 3, b = 5
Bresult = 3, a = 4, b = 4
Cresult = 3, a = 3, b = 5
Dresult = 4, a = 3, b = 4
Attempts:
2 left
💡 Hint
Remember how post-increment and pre-increment operators work.
Predict Output
expert
2: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));
AEven
BCompilation error
COther
DDivisible by 3
Attempts:
2 left
💡 Hint
Check the order of conditions in the nested ternary.