Relational operators compare two values and return a boolean result: true or false. They help decide if one value is greater, smaller, equal, or not equal to another.
Click to reveal answer
beginner
List the six main relational operators in Java.
The six main relational operators are: < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), != (not equal to).
Click to reveal answer
beginner
What is the result type of a relational operator expression in Java?
The result is always a boolean value: either true or false.
Click to reveal answer
beginner
Explain the difference between == and = in Java.
== is a relational operator that checks if two values are equal. = is an assignment operator that sets a value to a variable.
Click to reveal answer
beginner
What will be the output of this code snippet?
int a = 5, b = 10;
System.out.println(a > b);
The output will be false because 5 is not greater than 10.
Click to reveal answer
Which operator checks if two values are NOT equal in Java?
A<=
B==
C!=
D=
✗ Incorrect
!= means not equal, so it returns true if values differ.
What is the result type of the expression (5 < 10)?
Aboolean
Bint
CString
Ddouble
✗ Incorrect
Relational operators always return a boolean value: true or false.
Which operator would you use to check if a number is greater than or equal to another?
A<=
B>=
C==
D!=
✗ Incorrect
>= means greater than or equal to.
What does the expression (a == b) check?
AIf a is equal to b
BIf a is not equal to b
CIf a is greater than b
DIf a is assigned b
✗ Incorrect
== checks if two values are equal.
Which of these is NOT a relational operator in Java?
A>=
B!=
C<
D&&
✗ Incorrect
&& is a logical AND operator, not a relational operator.
Explain what relational operators do in Java and give examples.
Think about how you compare numbers or values in daily life.
You got /4 concepts.
Describe the difference between == and = in Java and why confusing them can cause errors.
One is for comparison, the other for setting values.
You got /4 concepts.
Practice
(1/5)
1. Which of the following relational operators in Java checks if two values are not 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 'not equal'
The operator '!=' means 'not equal to' in Java.
Final Answer:
!= -> Option C
Quick Check:
Not equal operator = != [OK]
Hint: Remember: '!=' means not equal in Java [OK]
Common Mistakes:
Confusing '!=' with '=='
Using '=' instead of '=='
Mixing up '<' and '<='
2. Which of the following is the correct syntax to check if variable a is greater than or equal to variable b in Java?
easy
A. a => b
B. a >= b
C. a =< b
D. a =>= b
Solution
Step 1: Recall the correct relational operator syntax
The operator for 'greater than or equal to' is written as '>=' in Java.
Step 2: Check each option for correct syntax
a >= b uses '>=', which is correct. Other options have invalid syntax.
Final Answer:
a >= b -> Option B
Quick Check:
Greater or equal operator = >= [OK]
Hint: Use '>=' for greater or equal, not '=>' or '=<' [OK]
Common Mistakes:
Writing '=>' instead of '>='
Using '=' instead of '=='
Confusing '<=' and '=>'
3. What will be the output of the following Java code?
int x = 5; int y = 10; System.out.println(x < y); System.out.println(x == y); System.out.println(x != y);
medium
A. false false true
B. false true false
C. true true false
D. true false true
Solution
Step 1: Evaluate each relational expression
x is 5 and y is 10. So, x < y is true, x == y is false, and x != y is true.