0
0
Goprogramming~20 mins

Logical operators in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Logical Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
}
Atrue
Bpanic at runtime
CCompilation error
Dfalse
Attempts:
2 left
๐Ÿ’ก Hint
Remember that && has higher precedence than || in Go.
โ“ Predict Output
intermediate
2: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)
}
Atrue
Bfalse
CCompilation error
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint
Negation (!) flips the boolean value.
โ“ Predict Output
advanced
2: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)
}
ACompilation error
Btrue
Cfalse
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint
Remember operator precedence: && before ||.
โ“ Predict Output
advanced
2: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)
}
Atrue
BCompilation error
Cpanic at runtime
Dfalse
Attempts:
2 left
๐Ÿ’ก Hint
Logical AND returns false if the first operand is false.
โ“ Predict Output
expert
2: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))
}
Afalse
Btrue
CCompilation error
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint
Evaluate inside parentheses first, then combine.