Challenge - 5 Problems
If–else Mastery in Go
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if–else in Go
What is the output of this Go program?
Go
package main import "fmt" func main() { x := 10 if x > 5 { if x < 15 { fmt.Println("A") } else { fmt.Println("B") } } else { fmt.Println("C") } }
Attempts:
2 left
💡 Hint
Check the conditions step by step from outer to inner if.
✗ Incorrect
x is 10, which is greater than 5 and less than 15, so the inner if prints "A".
❓ Predict Output
intermediate2:00remaining
If–else with else if in Go
What will this Go program print?
Go
package main import "fmt" func main() { score := 75 if score >= 90 { fmt.Println("Excellent") } else if score >= 60 { fmt.Println("Pass") } else { fmt.Println("Fail") } }
Attempts:
2 left
💡 Hint
Check which condition matches the score 75.
✗ Incorrect
75 is not >= 90 but is >= 60, so it prints "Pass".
❓ Predict Output
advanced2:00remaining
If–else with short statement and variable scope
What is the output of this Go code?
Go
package main import "fmt" func main() { if x := 5; x > 3 { fmt.Println(x) } else { fmt.Println("No") } // fmt.Println(x) // What if uncommented? }
Attempts:
2 left
💡 Hint
The variable x is declared in the if statement header.
✗ Incorrect
x is 5 and greater than 3, so it prints 5. Outside the if block, x is not defined.
❓ Predict Output
advanced2:00remaining
If–else with multiple conditions
What does this Go program print?
Go
package main import "fmt" func main() { a, b := 3, 7 if a > 5 && b < 10 { fmt.Println("X") } else if a > 2 || b > 10 { fmt.Println("Y") } else { fmt.Println("Z") } }
Attempts:
2 left
💡 Hint
Evaluate each condition carefully with given values.
✗ Incorrect
a=3 > 2 is true, so the else if condition is true and prints "Y".
🧠 Conceptual
expert2:00remaining
Understanding if–else statement behavior in Go
Which statement about if–else statements in Go is TRUE?
Attempts:
2 left
💡 Hint
Think about Go syntax rules for if–else formatting.
✗ Incorrect
In Go, else must be on the same line as the closing brace of the if block; variables declared in if header are scoped to the if block; braces are mandatory; conditions are not enclosed in parentheses.