Logical Operators

Question Click to View Answer

What does the following code print to the console?

package main

import "fmt"

func main() {
    fmt.Println(4 == 99)
}

false is printed to the console because 4 is not equal to 99.

In this example, 4 and 99 are the operands and == is the operator.

What does the following code print to the console?

fmt.Println(10 == 10)

true is printed to the console because 10 equals 10.

The == operator returns true when both operands are the same.

What does the following code print to the console?

fmt.Println("cat" != "dog")

true is printed to the because the string "cat" is not the same as the string "dog".

The != operator returns true when the operands are different.

What does the following code print to the console?

fmt.Println("mouse" != "mouse")

false is printed because the string "mouse" is equivalent to the string "mouse".

The != operator returns true when the operands are different.

What does the following code print to the console?

fmt.Println(true && true)

true is printed to the console.

The && operator returns true when both of the operands are true.

What does the following code print to the console?

fmt.Println(false && true)

false is printed to the console.

The && operator returns false when either one of the operands is false.

What does the following code print to the console?

fmt.Println(false || true)

true is printed to the console.

The || operator returns true when either one of the operands is true.

What does the following code print to the console?

fmt.Println(false || false)

false is printed to the console.

The || operator returns false when both of the operands are false.

What does the following code print to the console?

fmt.Println(!true)

false is printed to the console.

In this example, ! is the operator and true is the operand. The ! operator returns false when the operand is true.

What does the following code print to the console?

fmt.Println(!false)

true is printed to the console.

The ! operator returns true when the operand is false.

What does the following code print to the console?

fmt.Println(true && (4 == 4))

true is printed to the console.

4 == 4 evaluates to true and true && true evaluates to true for the final result.

What does the following code print to the console?

fmt.Println("lion" == "cat" || 99 != 88)

true is printed.

"lion" == "cat" evaluates to false and 99 != 88 evaluates to true. false || true evaluates to true for the final result.