0
0
Swiftprogramming~20 mins

Ternary conditional operator in Swift - 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 a simple ternary expression
What is the output of this Swift code?
Swift
let a = 10
let b = 20
let result = a > b ? "a is greater" : "b is greater"
print(result)
Aa is greater
Bb is greater
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Compare the values of a and b carefully.
Predict Output
intermediate
2:00remaining
Ternary operator with numeric result
What will be printed by this Swift code?
Swift
let x = 5
let y = 8
let max = x > y ? x : y
print(max)
A8
Bfalse
Ctrue
D5
Attempts:
2 left
💡 Hint
Check which number is bigger.
Predict Output
advanced
2:30remaining
Nested ternary conditional output
What is the output of this Swift code?
Swift
let score = 75
let grade = score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : "F"
print(grade)
AA
BB
CF
DC
Attempts:
2 left
💡 Hint
Evaluate conditions from left to right.
Predict Output
advanced
2:00remaining
Ternary operator with Boolean result
What does this Swift code print?
Swift
let isSunny = false
let activity = isSunny ? "Go outside" : "Stay inside"
print(activity)
AStay inside
BGo outside
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Check the Boolean value of isSunny.
Predict Output
expert
3:00remaining
Complex ternary with side effects
What is the output of this Swift code?
Swift
var count = 0
let result = count == 0 ? "Zero" : { count += 1; return "Not zero" }()
print(result)
print(count)
ACompilation error
BNot zero and then 1
CZero and then 0
DZero and then 1
Attempts:
2 left
💡 Hint
Notice when the closure is executed.