Challenge - 5 Problems
Logical Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of combined logical operators
What is the output of this Go program?
package main
import "fmt"
func main() {
a := true
b := false
c := true
fmt.Println(a && b || c)
}Go
package main import "fmt" func main() { a := true b := false c := true fmt.Println(a && b || c) }
Attempts:
2 left
๐ก Hint
Remember that && has higher precedence than || in Go.
โ Incorrect
The expression evaluates as (a && b) || c. Since a && b is false, and c is true, the whole expression is true.
โ Predict Output
intermediate2:00remaining
Result of negation and OR operator
What will this Go code print?
package main
import "fmt"
func main() {
x := false
y := true
fmt.Println(!x || y)
}Go
package main import "fmt" func main() { x := false y := true fmt.Println(!x || y) }
Attempts:
2 left
๐ก Hint
Negation (!) flips the boolean value.
โ Incorrect
Negation of false is true, so !x is true. true || y is true.
โ Predict Output
advanced2:00remaining
Output of chained logical operators with variables
What is the output of this Go program?
package main
import "fmt"
func main() {
a := false
b := false
c := true
d := true
fmt.Println(a || b && c && d)
}Go
package main import "fmt" func main() { a := false b := false c := true d := true fmt.Println(a || b && c && d) }
Attempts:
2 left
๐ก Hint
Remember operator precedence: && before ||.
โ Incorrect
b && c && d is false because b is false. a || false is false.
โ Predict Output
advanced2:00remaining
Output of logical AND with short-circuit
What does this Go code print?
package main
import "fmt"
func main() {
x := false
y := true
fmt.Println(x && y)
}Go
package main import "fmt" func main() { x := false y := true fmt.Println(x && y) }
Attempts:
2 left
๐ก Hint
Logical AND returns false if the first operand is false.
โ Incorrect
Since x is false, x && y is false without evaluating y.
โ Predict Output
expert2:00remaining
Output of complex logical expression with parentheses
What is the output of this Go program?
package main
import "fmt"
func main() {
a := true
b := false
c := true
d := false
fmt.Println((a || b) && (c && !d))
}Go
package main import "fmt" func main() { a := true b := false c := true d := false fmt.Println((a || b) && (c && !d)) }
Attempts:
2 left
๐ก Hint
Evaluate inside parentheses first, then combine.
โ Incorrect
(a || b) is true, (c && !d) is true, so true && true is true.