0
0
Goprogramming~20 mins

Relational operators in Go - Practice Problems & Coding Challenges

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