Challenge - 5 Problems
Nested Conditionals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of nested if-else with integers
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 starting from the outer if.
โ Incorrect
The outer if checks if x > 5, which is true for x=10. Then the inner if checks if x < 15, also true, so it prints "A".
โ Predict Output
intermediate2:00remaining
Nested if-else with strings
What will this Go program print?
Go
package main import "fmt" func main() { status := "active" if status == "active" { if len(status) >= 5 { fmt.Println("Long active") } else { fmt.Println("Active") } } else { fmt.Println("Inactive") } }
Attempts:
2 left
๐ก Hint
Check the length of the string "active".
โ Incorrect
The status is "active" which matches the first if. The length of "active" is 6, which is greater than 5, so it prints "Long active".
โ Predict Output
advanced2:00remaining
Nested if with multiple conditions
What is the output of this Go code?
Go
package main import "fmt" func main() { a, b := 3, 7 if a < 5 { if b > 10 { fmt.Println("X") } else if b > 5 { fmt.Println("Y") } else { fmt.Println("Z") } } else { fmt.Println("W") } }
Attempts:
2 left
๐ก Hint
Check the values of a and b and follow the nested conditions carefully.
โ Incorrect
a is 3 which is less than 5, so the outer if is true. Then b is 7, which is not greater than 10 but greater than 5, so it prints "Y".
โ Predict Output
advanced2:00remaining
Nested if with boolean variables
What will this Go program output?
Go
package main import "fmt" func main() { isRaining := false hasUmbrella := true if isRaining { if hasUmbrella { fmt.Println("Go outside") } else { fmt.Println("Stay inside") } } else { fmt.Println("Enjoy the sun") } }
Attempts:
2 left
๐ก Hint
Check the value of isRaining first.
โ Incorrect
Since isRaining is false, the outer if is false, so the else branch runs and prints "Enjoy the sun".
โ Predict Output
expert3:00remaining
Nested if with variable shadowing
What is the output of this Go program?
Go
package main import "fmt" func main() { x := 5 if x > 0 { x := 10 if x > 5 { fmt.Println(x) } else { fmt.Println(0) } } else { fmt.Println(-1) } fmt.Println(x) }
Attempts:
2 left
๐ก Hint
Remember that the inner x shadows the outer x inside the if block.
โ Incorrect
Inside the if block, x is redeclared as 10. So the first print prints 10. Outside the block, x is still 5, so the second print prints 5.