Challenge - 5 Problems
Relational Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2: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)) }
Attempts:
2 left
๐ก Hint
Remember && means AND, || means OR, and ! means NOT.
โ Incorrect
The first line checks if 5 is less than 10 AND 10 is less than 15, both true, so true.
The second line checks if 5 is greater than 10 OR 10 is less than 15, second is true, so true.
The third line negates if 5 equals 5, which is true, so negation is false.
โ Predict Output
intermediate2: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) }
Attempts:
2 left
๐ก Hint
Strings are compared lexicographically (dictionary order).
โ Incorrect
"apple" is lexicographically less than "banana", so first is true.
They are not equal, so second is false.
"apple" is not greater than "banana", so third is false.
โ Predict Output
advanced2: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) }
Attempts:
2 left
๐ก Hint
Go is strict about comparing different types.
โ Incorrect
Go does not allow direct comparison between int and float64 types, causing a compilation error.
โ Predict Output
advanced2: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) }
Attempts:
2 left
๐ก Hint
Structs can be compared if all their fields are comparable.
โ Incorrect
Both p1 and p2 have the same values, so p1 == p2 is true and p1 != p2 is false.
๐ง Conceptual
expert2: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) }
Attempts:
2 left
๐ก Hint
An interface holding a nil pointer is not itself nil.
โ Incorrect
The interface i holds a nil value but is not itself nil, so i == nil is false.
The pointer p is nil, so p == nil is true.