Bird
Raised Fist0
Goprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does an else-if ladder do in Go programming?
easy
A. Checks multiple conditions one by one and runs the first true block
B. Runs all conditions regardless of their truth value
C. Only runs the last condition in the ladder
D. Skips all conditions and runs default code always

Solution

  1. Step 1: Understand else-if ladder behavior

    An else-if ladder checks conditions in order, from top to bottom.
  2. Step 2: Identify which block runs

    Only the first condition that is true will run its code block; others are skipped.
  3. Final Answer:

    Checks multiple conditions one by one and runs the first true block -> Option A
  4. Quick Check:

    Else-if ladder = first true block runs [OK]
Hint: Remember: else-if stops at first true condition [OK]
Common Mistakes:
  • Thinking all conditions run even if one is true
  • Believing else-if runs only the last condition
  • Confusing else-if with separate if statements
2. Which of the following is the correct syntax for an else-if ladder in Go?
easy
A. if condition1 { } else if condition2 { } else { }
B. if condition1 { } elseif condition2 { } else { }
C. if condition1 { } else if(condition2) { } else { }
D. if condition1 then { } else if condition2 { } else { }

Solution

  1. Step 1: Recall Go else-if syntax

    Go uses else if as two separate words without parentheses around the condition.
  2. Step 2: Compare options

    if condition1 { } else if condition2 { } else { } matches Go syntax exactly; others use wrong keywords or parentheses.
  3. Final Answer:

    if condition1 { } else if condition2 { } else { } -> Option A
  4. Quick Check:

    Go else-if = "else if" two words, no parentheses [OK]
Hint: Use 'else if' with space, no parentheses in Go [OK]
Common Mistakes:
  • Writing 'elseif' as one word
  • Adding parentheses around else-if condition
  • Using 'then' keyword
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
  x := 15
  if x < 10 {
    fmt.Println("Less than 10")
  } else if x < 20 {
    fmt.Println("Less than 20")
  } else {
    fmt.Println("20 or more")
  }
}
medium
A. Less than 10
B. Less than 20
C. 20 or more
D. No output

Solution

  1. Step 1: Check first condition (x < 10)

    x is 15, which is not less than 10, so skip this block.
  2. Step 2: Check second condition (x < 20)

    15 is less than 20, so this block runs and prints "Less than 20".
  3. Final Answer:

    Less than 20 -> Option B
  4. Quick Check:

    15 < 20 = true, prints "Less than 20" [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing first condition without checking value
  • Ignoring else-if and picking else block
  • Assuming all blocks print
4. Find the error in this Go else-if ladder code:
if score >= 90 {
  fmt.Println("A")
} else if score >= 80
  fmt.Println("B")
} else {
  fmt.Println("C")
}
medium
A. else block should come before else if
B. Wrong comparison operator used
C. Missing braces {} after else if condition
D. No error, code is correct

Solution

  1. Step 1: Check else-if syntax

    In Go, the else-if condition must be followed by braces {} enclosing the code block.
  2. Step 2: Identify missing braces

    The else-if block lacks opening brace after condition, causing syntax error.
  3. Final Answer:

    Missing braces {} after else if condition -> Option C
  4. Quick Check:

    Braces required after else-if condition [OK]
Hint: Always use braces {} after if and else-if conditions [OK]
Common Mistakes:
  • Omitting braces after else-if condition
  • Placing else before else-if
  • Using wrong operators
5. You want to write a Go program that prints "Child" if age < 13, "Teen" if age is between 13 and 19 inclusive, "Adult" if age is 20 or more. Which else-if ladder correctly implements this?
hard
A. if age <= 13 { fmt.Println("Child") } else if age >= 13 && age <= 19 { fmt.Println("Teen") } else { fmt.Println("Adult") }
B. if age <= 13 { fmt.Println("Child") } else if age > 13 && age < 19 { fmt.Println("Teen") } else { fmt.Println("Adult") }
C. if age < 13 { fmt.Println("Child") } else if age > 13 && age < 19 { fmt.Println("Teen") } else { fmt.Println("Adult") }
D. if age < 13 { fmt.Println("Child") } else if age >= 13 && age <= 19 { fmt.Println("Teen") } else { fmt.Println("Adult") }

Solution

  1. Step 1: Check child condition

    Child is age less than 13, so condition is age < 13.
  2. Step 2: Check teen condition

    Teen is age between 13 and 19 inclusive, so condition is age >= 13 && age <= 19.
  3. Step 3: Check adult condition

    Adult is age 20 or more, so else block covers this.
  4. Final Answer:

    if age < 13 { ... } else if age >= 13 && age <= 19 { ... } else { ... } -> Option D
  5. Quick Check:

    Conditions match age groups exactly [OK]
Hint: Use inclusive ranges carefully with >= and <= [OK]
Common Mistakes:
  • Using wrong comparison operators for ranges
  • Overlapping or missing age values
  • Incorrect else-if order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard