The three main logical operators in Go are AND (&&), OR (||), and NOT (!). They help combine or invert boolean values.
Click to reveal answer
beginner
How does the AND (&&) operator work in Go?
The AND operator (&&) returns true only if both conditions are true. If either is false, the result is false.
Click to reveal answer
beginner
What does the OR (||) operator do in Go?
The OR operator (||) returns true if at least one of the conditions is true. It returns false only if both are false.
Click to reveal answer
beginner
Explain the NOT (!) operator in Go.
The NOT operator (!) reverses the value of a boolean. If the value is true, ! makes it false, and if false, ! makes it true.
Click to reveal answer
intermediate
What is short-circuit evaluation in Go's logical operators?
Short-circuit evaluation means Go stops checking conditions as soon as the result is known. For example, in AND (&&), if the first is false, Go skips the second because the result can't be true.
Click to reveal answer
Which operator returns true only if both conditions are true?
A& (bitwise AND)
B|| (OR)
C&& (AND)
D! (NOT)
✗ Incorrect
The && operator returns true only when both conditions are true.
What does the expression !(true) evaluate to in Go?
Afalse
Berror
Ctrue
Dnil
✗ Incorrect
The NOT operator (!) reverses true to false.
If the first condition in an OR (||) expression is true, what happens next?
AThe second condition is checked
BThe expression returns true immediately
CThe expression returns false
DThe program crashes
✗ Incorrect
Go uses short-circuit evaluation and returns true immediately if the first condition in OR is true.
Which operator would you use to check if a condition is NOT true?
A!
B&&
C||
D==
✗ Incorrect
The ! operator negates a boolean value.
What is the result of true && false in Go?
Anil
Btrue
Cerror
Dfalse
✗ Incorrect
AND (&&) returns true only if both are true; here one is false, so result is false.
Describe how the AND, OR, and NOT logical operators work in Go with simple examples.
Think about true and false combinations and how each operator changes the result.
You got /4 concepts.
Explain what short-circuit evaluation means in Go's logical operators and why it is useful.
Consider when Go stops checking conditions early.
You got /3 concepts.
Practice
(1/5)
1. Which logical operator in Go means AND and returns true only if both conditions are true?
easy
A. !
B. ||
C. &
D. &&
Solution
Step 1: Understand the meaning of logical AND
The AND operator returns true only when both conditions it connects are true.
Step 2: Identify Go's AND operator
In Go, the AND operator is written as &&, which matches the description.
Final Answer:
&& -> Option D
Quick Check:
AND = && [OK]
Hint: AND is &&, OR is ||, NOT is ! [OK]
Common Mistakes:
Confusing & (bitwise AND) with && (logical AND)
Mixing OR (||) with AND (&&)
Using ! as AND operator
2. Which of the following is the correct syntax for the logical NOT operator in Go?
easy
A. not x
B. !x
C. ~x
D. x!
Solution
Step 1: Recall Go syntax for logical NOT
In Go, the logical NOT operator is a single exclamation mark placed before the variable or expression.
Step 2: Match options with correct syntax
!x uses !x, which is the correct syntax for NOT in Go.
Final Answer:
!x -> Option B
Quick Check:
NOT = ! [OK]
Hint: NOT always uses ! before the condition [OK]
Common Mistakes:
Writing 'not x' like in some other languages
Using ~ which is bitwise NOT, not logical NOT
Placing ! after the variable
3. What is the output of this Go code snippet?
package main
import "fmt"
func main() {
a := true
b := false
fmt.Println(a && !b || b)
}
medium
A. true
B. false
C. Syntax error
D. Compilation error
Solution
Step 1: Evaluate !b
b is false, so !b is true.
Step 2: Evaluate a && !b
a is true and !b is true, so a && !b is true.
Step 3: Evaluate (a && !b) || b
true || false is true.
Final Answer:
true -> Option A
Quick Check:
True AND NOT False OR False = True [OK]
Hint: Evaluate NOT first, then AND, then OR [OK]
Common Mistakes:
Evaluating OR before AND
Forgetting to negate b
Assuming false || true is false
4. Identify the error in this Go code snippet:
package main
import "fmt"
func main() {
x := true
y := false
if x & y {
fmt.Println("Both true")
}
}
medium
A. Using & instead of && in if condition
B. Missing parentheses around condition
C. fmt.Println syntax error
D. Variable declaration error
Solution
Step 1: Check logical operators in if condition
The code uses & which is bitwise AND, not logical AND.
Step 2: Identify correct operator for boolean logic
Logical AND in Go is &&, so & causes a type error here.
Final Answer:
Using & instead of && in if condition -> Option A
Quick Check:
Logical AND needs &&, not & [OK]
Hint: Use && for logical AND, & is bitwise [OK]
Common Mistakes:
Using & instead of && for booleans
Assuming & works like && in conditions
Ignoring compiler error messages
5. Given a variable score (int), which condition correctly checks if score is between 50 and 100 inclusive using logical operators in Go?
hard
A. score >= 50 || score <= 100
B. score > 50 && score < 100
C. score >= 50 && score <= 100
D. score > 50 || score < 100
Solution
Step 1: Understand the range condition
We want score to be at least 50 and at most 100, so both conditions must be true.
Step 2: Choose correct logical operator
AND (&&) combines conditions that both must be true; OR (||) would allow values outside the range.
Step 3: Check inclusivity
Using >= and <= includes 50 and 100 themselves.
Final Answer:
score >= 50 && score <= 100 -> Option C
Quick Check:
Between means AND, not OR [OK]
Hint: Use && to combine conditions that both must be true [OK]
Common Mistakes:
Using || which allows values outside range
Using > and < excludes boundary values
Confusing AND and OR logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.