Challenge - 5 Problems
Go Else-If Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of else-if ladder with multiple conditions
What is the output of the following Go program?
Go
package main import "fmt" func main() { x := 15 if x < 10 { fmt.Println("Less than 10") } else if x < 20 { fmt.Println("Between 10 and 19") } else if x < 30 { fmt.Println("Between 20 and 29") } else { fmt.Println("30 or more") } }
Attempts:
2 left
💡 Hint
Check which condition matches the value of x first.
✗ Incorrect
The variable x is 15, which is not less than 10, but it is less than 20. So the second condition is true and prints "Between 10 and 19".
❓ Predict Output
intermediate2:00remaining
Output when all else-if conditions fail
What will this Go program print?
Go
package main import "fmt" func main() { score := 85 if score > 90 { fmt.Println("Grade A") } else if score > 80 { fmt.Println("Grade B") } else if score > 70 { fmt.Println("Grade C") } else { fmt.Println("Grade F") } }
Attempts:
2 left
💡 Hint
Check the first condition that is true for score 85.
✗ Incorrect
85 is not greater than 90, but it is greater than 80, so it prints "Grade B".
🔧 Debug
advanced2:00remaining
Identify the error in else-if ladder syntax
Which option contains a syntax error in the else-if ladder?
Go
package main import "fmt" func main() { n := 5 if n > 10 { fmt.Println("Greater than 10") } else if n > 5 { fmt.Println("Greater than 5") } else if n > 0 { fmt.Println("Greater than 0") } else { fmt.Println("Zero or negative") } }
Attempts:
2 left
💡 Hint
Check the placement of braces {} in else-if blocks.
✗ Incorrect
Option B is missing the opening brace '{' after else if n > 5, causing a syntax error.
🧠 Conceptual
advanced2:00remaining
Understanding else-if ladder execution flow
Given the following Go code, what will be the value of variable result after execution?
Go
package main func main() { var result string x := 7 if x > 10 { result = "A" } else if x > 5 { result = "B" } else if x > 0 { result = "C" } else { result = "D" } }
Attempts:
2 left
💡 Hint
Check which condition matches x = 7 first.
✗ Incorrect
x is 7, which is not greater than 10 but is greater than 5, so result is set to "B".
🚀 Application
expert3:00remaining
Determine output with nested else-if ladder
What will this Go program print when run?
Go
package main import "fmt" func main() { a := 3 b := 7 if a > b { fmt.Println("a is greater") } else if a == b { fmt.Println("a equals b") } else { if b > 10 { fmt.Println("b is large") } else if b > 5 { fmt.Println("b is medium") } else { fmt.Println("b is small") } } }
Attempts:
2 left
💡 Hint
Check the outer else block and then the inner else-if ladder for b.
✗ Incorrect
a is 3 and b is 7, so a > b is false and a == b is false. The else block runs. Inside it, b is 7 which is greater than 5 but not greater than 10, so it prints "b is medium".