Bird
Raised Fist0
Goprogramming~20 mins

Relational operators in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Relational Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of chained relational operators in Go
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    a := 5
    b := 10
    c := 15
    fmt.Println(a < b && b < c)
    fmt.Println(a > b || b < c)
    fmt.Println(!(a == 5))
}
A
true
true
false
B
false
true
true
C
true
false
false
D
false
false
true
Attempts:
2 left
๐Ÿ’ก Hint
Remember && means AND, || means OR, and ! means NOT.
โ“ Predict Output
intermediate
2:00remaining
Comparing strings with relational operators
What is the output of this Go code snippet?
Go
package main
import "fmt"
func main() {
    s1 := "apple"
    s2 := "banana"
    fmt.Println(s1 < s2)
    fmt.Println(s1 == s2)
    fmt.Println(s1 > s2)
}
A
true
false
false
B
false
true
false
C
false
false
true
D
true
true
false
Attempts:
2 left
๐Ÿ’ก Hint
Strings are compared lexicographically (dictionary order).
โ“ Predict Output
advanced
2:00remaining
Output of relational operators with mixed types
What error or output does this Go program produce?
Go
package main
import "fmt"
func main() {
    var x int = 10
    var y float64 = 10.0
    fmt.Println(x == y)
}
Atrue
Bfalse
Ccannot use y (type float64) as type int in comparison
Dcompilation error: mismatched types int and float64
Attempts:
2 left
๐Ÿ’ก Hint
Go is strict about comparing different types.
โ“ Predict Output
advanced
2:00remaining
Relational operators with structs
What happens when you run this Go code?
Go
package main
import "fmt"
func main() {
    type Point struct { x, y int }
    p1 := Point{1, 2}
    p2 := Point{1, 2}
    fmt.Println(p1 == p2)
    fmt.Println(p1 != p2)
}
A
false
true
Bcompilation error: invalid operation: p1 == p2 (struct containing uncomparable fields)
C
true
false
Druntime panic
Attempts:
2 left
๐Ÿ’ก Hint
Structs can be compared if all their fields are comparable.
๐Ÿง  Conceptual
expert
2:00remaining
Relational operator behavior with nil interfaces
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var p *int = nil
    var i interface{} = p
    fmt.Println(i == nil)
    fmt.Println(p == nil)
}
A
true
true
B
false
true
C
false
false
D
true
false
Attempts:
2 left
๐Ÿ’ก Hint
An interface holding a nil pointer is not itself nil.

Practice

(1/5)
1. Which of the following relational operators in Go checks if two values are equal?
easy
A. >
B. !=
C. ==
D. <=

Solution

  1. Step 1: Understand the meaning of relational operators

    Relational operators compare two values and return true or false based on the comparison.
  2. Step 2: Identify the operator for equality

    The operator '==' checks if two values are equal in Go.
  3. Final Answer:

    == -> Option C
  4. Quick Check:

    Equality operator = == [OK]
Hint: Remember: double equals (==) means equal in Go [OK]
Common Mistakes:
  • Confusing '=' (assignment) with '==' (equality)
  • Using '!=' for equality check
  • Thinking '>' means equal
2. Which of the following is the correct syntax to check if variable a is not equal to variable b in Go?
easy
A. a !== b
B. a <> b
C. a =! b
D. a != b

Solution

  1. Step 1: Recall Go syntax for not equal operator

    In Go, the not equal operator is written as '!='.
  2. Step 2: Match the syntax with the options

    a != b uses 'a != b', which is correct syntax for not equal in Go.
  3. Final Answer:

    a != b -> Option D
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Not equal in Go is always '!=' not '<>' or '!==' [OK]
Common Mistakes:
  • Using '<>' which is not valid in Go
  • Swapping '=' and '!' to '=!'
  • Using JavaScript style '!==' which is invalid in Go
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
    x := 10
    y := 20
    fmt.Println(x > y)
}
medium
A. false
B. 10
C. true
D. Compilation error

Solution

  1. Step 1: Understand the comparison x > y

    Variable x is 10 and y is 20. The expression checks if 10 is greater than 20.
  2. Step 2: Evaluate the expression

    Since 10 is not greater than 20, the expression evaluates to false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    10 > 20 = false [OK]
Hint: Compare values carefully; 10 > 20 is false [OK]
Common Mistakes:
  • Assuming 10 > 20 is true
  • Confusing output with variable values
  • Expecting a number instead of boolean
4. Identify the error in the following Go code snippet:
package main
import "fmt"
func main() {
    a := 5
    b := 10
    if a = b {
        fmt.Println("Equal")
    }
}
medium
A. Variables 'a' and 'b' are not declared properly
B. The '=' operator is used instead of '==' in the if condition
C. Missing parentheses around the if condition
D. fmt.Println syntax is incorrect

Solution

  1. Step 1: Check the if condition syntax

    The condition uses 'a = b' which is an assignment, not a comparison.
  2. Step 2: Correct the operator for comparison

    To compare values, '==' must be used instead of '='.
  3. Final Answer:

    The '=' operator is used instead of '==' in the if condition -> Option B
  4. Quick Check:

    Use '==' for comparison, not '=' [OK]
Hint: Use '==' to compare, '=' only assigns [OK]
Common Mistakes:
  • Using '=' instead of '==' in conditions
  • Thinking parentheses are mandatory in Go if
  • Assuming fmt.Println syntax is wrong
5. Given the following Go code, what will be the output?
package main
import "fmt"
func main() {
    a := 15
    b := 15
    c := 20
    result := (a == b) && (c > b)
    fmt.Println(result)
}
hard
A. true
B. false
C. Compilation error
D. 15

Solution

  1. Step 1: Evaluate the first comparison (a == b)

    Both a and b are 15, so (a == b) is true.
  2. Step 2: Evaluate the second comparison (c > b)

    c is 20 and b is 15, so (c > b) is true.
  3. Step 3: Combine with logical AND

    Both conditions are true, so true && true is true.
  4. Final Answer:

    true -> Option A
  5. Quick Check:

    (15 == 15) && (20 > 15) = true [OK]
Hint: Both conditions true? Result is true [OK]
Common Mistakes:
  • Confusing && with || operator
  • Thinking one true means overall false
  • Expecting numeric output instead of boolean
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