0
0
Goprogramming~20 mins

If statement in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested if statements
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("Inside nested if")
        }
    } else {
        fmt.Println("Outside if")
    }
}
ANo output
BOutside if
CInside nested if
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions in the nested if statements carefully.
โ“ Predict Output
intermediate
2:00remaining
If statement with else if
What will this Go program print?
Go
package main
import "fmt"
func main() {
    score := 75
    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")
    }
}
AGrade A
BGrade C
CGrade B
DGrade F
Attempts:
2 left
๐Ÿ’ก Hint
Check which condition matches the score value first.
โ“ Predict Output
advanced
2:00remaining
If statement with short statement
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")
    }
}
A0
BNo
CCompilation error: x undefined
D5
Attempts:
2 left
๐Ÿ’ก Hint
Look at how the variable x is declared inside the if statement.
โ“ Predict Output
advanced
2:00remaining
If statement with multiple conditions
What will this Go program output?
Go
package main
import "fmt"
func main() {
    a, b := 4, 7
    if a > 3 && b < 10 {
        fmt.Println("Condition met")
    } else {
        fmt.Println("Condition not met")
    }
}
ACondition met
BCondition not met
CCompilation error
DNo output
Attempts:
2 left
๐Ÿ’ก Hint
Both conditions must be true for the if block to run.
โ“ Predict Output
expert
2:00remaining
If statement with variable shadowing
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    x := 10
    if x := 5; x > 3 {
        fmt.Println(x)
    }
    fmt.Println(x)
}
A5\n10
B10\n10
C5\n5
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Notice the variable x declared inside the if statement shadows the outer x.