0
0
Goprogramming~20 mins

Else–if ladder in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Else-If Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
    }
}
ALess than 10
BBetween 20 and 29
C30 or more
DBetween 10 and 19
Attempts:
2 left
💡 Hint
Check which condition matches the value of x first.
Predict Output
intermediate
2: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")
    }
}
AGrade B
BGrade A
CGrade C
DGrade F
Attempts:
2 left
💡 Hint
Check the first condition that is true for score 85.
🔧 Debug
advanced
2: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")
    }
}
A
if n &gt; 10 {
    fmt.Println("Greater than 10")
} else if n &gt; 5 {
    fmt.Println("Greater than 5")
} else if n &gt; 0 {
    fmt.Println("Greater than 0")
} else {
    fmt.Println("Zero or negative")
}
B
if n &gt; 10 {
    fmt.Println("Greater than 10")
} else if n &gt; 5 
    fmt.Println("Greater than 5")
} else if n &gt; 0 {
    fmt.Println("Greater than 0")
} else {
    fmt.Println("Zero or negative")
}
C
}
)"evitagen ro oreZ"(nltnirP.tmf    
{ esle }
)"0 naht retaerG"(nltnirP.tmf    
{ 0 &gt; n fi esle }
)"5 naht retaerG"(nltnirP.tmf    
{ 5 &gt; n fi esle }
)"01 naht retaerG"(nltnirP.tmf    
{ 01 &gt; n fi
D
f n &gt; 10 {
    fmt.Println("Greater than 10")
} else if n &gt; 5 {
    fmt.Println("Greater than 5")
} else if n &gt; 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.
🧠 Conceptual
advanced
2: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"
    }
}
A"C"
B"D"
C"B"
D"A"
Attempts:
2 left
💡 Hint
Check which condition matches x = 7 first.
🚀 Application
expert
3: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")
        }
    }
}
Ab is medium
Ba is greater
Cb is large
Da equals b
Attempts:
2 left
💡 Hint
Check the outer else block and then the inner else-if ladder for b.