Relational operators compare two values and return a boolean result: true or false. They help decide if one value is equal to, greater than, or less than another.
Click to reveal answer
beginner
List the common relational operators in Go.
== (equal to), != (not equal to), < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to).
Click to reveal answer
beginner
What does the expression '5 < 10' return in Go?
It returns true because 5 is less than 10.
Click to reveal answer
intermediate
Can relational operators be used with strings in Go?
Yes, strings can be compared using relational operators like == and != to check equality or inequality. Also, < and > compare strings lexicographically (dictionary order).
Click to reveal answer
beginner
What is the result of '7 != 7' in Go?
It returns false because 7 is equal to 7, so they are not different.
Click to reveal answer
Which operator checks if two values are NOT equal in Go?
A<=
B==
C>
D!=
✗ Incorrect
The '!=' operator returns true if the two values are different.
What does the expression '10 >= 5' evaluate to?
Afalse
Bnil
Ctrue
Derror
✗ Incorrect
10 is greater than or equal to 5, so the expression is true.
Which relational operator means 'less than or equal to'?
A==
B<=
C>=
D!=
✗ Incorrect
The '<=' operator means less than or equal to.
Can you use relational operators to compare two strings in Go?
AYes, including == and <
BNo, only numbers can be compared
COnly == can be used
DOnly != can be used
✗ Incorrect
Go allows comparing strings with ==, !=, <, >, <=, and >=.
What is the result of '3 == 4' in Go?
Afalse
Btrue
C3
D4
✗ Incorrect
3 is not equal to 4, so the expression returns false.
Explain what relational operators do in Go and give examples.
Think about how you compare numbers or words in real life.
You got /3 concepts.
Describe how you can compare strings using relational operators in Go.
Imagine checking if two words are the same or which comes first alphabetically.
You got /3 concepts.
Practice
(1/5)
1. Which of the following relational operators in Go checks if two values are equal?
easy
A. >
B. !=
C. ==
D. <=
Solution
Step 1: Understand the meaning of relational operators
Relational operators compare two values and return true or false based on the comparison.
Step 2: Identify the operator for equality
The operator '==' checks if two values are equal in Go.
Final Answer:
== -> Option C
Quick Check:
Equality operator = == [OK]
Hint: Remember: double equals (==) means equal in Go [OK]
Common Mistakes:
Confusing '=' (assignment) with '==' (equality)
Using '!=' for equality check
Thinking '>' means equal
2. Which of the following is the correct syntax to check if variable a is not equal to variable b in Go?
easy
A. a !== b
B. a <> b
C. a =! b
D. a != b
Solution
Step 1: Recall Go syntax for not equal operator
In Go, the not equal operator is written as '!='.
Step 2: Match the syntax with the options
a != b uses 'a != b', which is correct syntax for not equal in Go.
Final Answer:
a != b -> Option D
Quick Check:
Not equal operator = != [OK]
Hint: Not equal in Go is always '!=' not '<>' or '!==' [OK]
Common Mistakes:
Using '<>' which is not valid in Go
Swapping '=' and '!' to '=!'
Using JavaScript style '!==' which is invalid in Go
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
x := 10
y := 20
fmt.Println(x > y)
}
medium
A. false
B. 10
C. true
D. Compilation error
Solution
Step 1: Understand the comparison x > y
Variable x is 10 and y is 20. The expression checks if 10 is greater than 20.
Step 2: Evaluate the expression
Since 10 is not greater than 20, the expression evaluates to false.
Final Answer:
false -> Option A
Quick Check:
10 > 20 = false [OK]
Hint: Compare values carefully; 10 > 20 is false [OK]
Common Mistakes:
Assuming 10 > 20 is true
Confusing output with variable values
Expecting a number instead of boolean
4. Identify the error in the following Go code snippet:
package main
import "fmt"
func main() {
a := 5
b := 10
if a = b {
fmt.Println("Equal")
}
}
medium
A. Variables 'a' and 'b' are not declared properly
B. The '=' operator is used instead of '==' in the if condition
C. Missing parentheses around the if condition
D. fmt.Println syntax is incorrect
Solution
Step 1: Check the if condition syntax
The condition uses 'a = b' which is an assignment, not a comparison.
Step 2: Correct the operator for comparison
To compare values, '==' must be used instead of '='.
Final Answer:
The '=' operator is used instead of '==' in the if condition -> Option B
Quick Check:
Use '==' for comparison, not '=' [OK]
Hint: Use '==' to compare, '=' only assigns [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Thinking parentheses are mandatory in Go if
Assuming fmt.Println syntax is wrong
5. Given the following Go code, what will be the output?
package main
import "fmt"
func main() {
a := 15
b := 15
c := 20
result := (a == b) && (c > b)
fmt.Println(result)
}
hard
A. true
B. false
C. Compilation error
D. 15
Solution
Step 1: Evaluate the first comparison (a == b)
Both a and b are 15, so (a == b) is true.
Step 2: Evaluate the second comparison (c > b)
c is 20 and b is 15, so (c > b) is true.
Step 3: Combine with logical AND
Both conditions are true, so true && true is true.
Final Answer:
true -> Option A
Quick Check:
(15 == 15) && (20 > 15) = true [OK]
Hint: Both conditions true? Result is true [OK]
Common Mistakes:
Confusing && with || operator
Thinking one true means overall false
Expecting numeric output instead of boolean
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.