Relational operators compare two values and return true or false depending on the comparison result.
Click to reveal answer
beginner
List the six common relational operators in C++.
The six common relational operators are: 1. == (equal to) 2. != (not equal to) 3. < (less than) 4. > (greater than) 5. <= (less than or equal to) 6. >= (greater than or equal to)
Click to reveal answer
beginner
What is the output of this code?
int a = 5, b = 10; std::cout << (a < b);
The output is 1 because 5 is less than 10, so the expression a < b is true. In C++, true prints as 1.
Click to reveal answer
beginner
How do relational operators help in decision making in C++?
Relational operators are used in conditions to decide which code to run. For example, in an if statement, they check if a condition is true or false to choose the path.
Click to reveal answer
beginner
What is the difference between == and = in C++?
== is a relational operator that checks if two values are equal. = is an assignment operator that sets a value to a variable. Using = instead of == in a condition causes errors or unexpected behavior.
Click to reveal answer
Which operator checks if two values are NOT equal in C++?
A!=
B==
C<=
D>=
✗ Incorrect
The operator != means 'not equal to' and returns true if values differ.
What does the expression (7 > 3) return in C++?
Afalse
B1
C0
DError
✗ Incorrect
7 is greater than 3, so (7 > 3) is true, which prints as 1.
Which relational operator means 'less than or equal to'?
A>=
B==
C<=
D!=
✗ Incorrect
The operator <= means 'less than or equal to'.
What is the result of (5 == 5) in C++?
Afalse
B5
CError
Dtrue
✗ Incorrect
5 equals 5, so (5 == 5) is true.
Which operator is used to assign a value to a variable?
A=
B<
C!=
D==
✗ Incorrect
The = operator assigns a value to a variable, not to compare.
Explain what relational operators do in C++ and give examples.
Think about how you check if one number is bigger or smaller than another.
You got /3 concepts.
Describe how relational operators are used in decision making with if statements.
Imagine deciding what to do based on comparing two things.
You got /4 concepts.
Practice
(1/5)
1.
Which of the following relational operators checks if two values are not equal in C++?
easy
A. >=
B. !=
C. <=
D. ==
Solution
Step 1: Understand the meaning of each operator
The operator == checks equality, <= and >= check less or equal and greater or equal respectively.
Step 2: Identify the operator for 'not equal'
The operator != means 'not equal' in C++.
Final Answer:
!= -> Option B
Quick Check:
Not equal operator = != [OK]
Hint: Remember: != means not equal in C++ [OK]
Common Mistakes:
Confusing == with !=
Thinking <= means not equal
Mixing up >=
Using = instead of ==
2.
Which of the following is the correct syntax to compare if a is greater than b in C++?
int a = 5, b = 3;
if (___) {
// code
}
easy
A. a > b
B. a => b
C. a >< b
D. a >= b
Solution
Step 1: Recall the correct operator for 'greater than'
The operator for 'greater than' is >. The operator >= means 'greater than or equal to'.
Step 2: Check syntax correctness
a > b is the correct syntax to check if a is strictly greater than b. Other options are invalid syntax.
Final Answer:
a > b -> Option A
Quick Check:
Greater than operator = > [OK]
Hint: Use > for greater than, >= for greater or equal [OK]
Common Mistakes:
Using => instead of >=
Using >< which is invalid
Confusing >= with >
Missing spaces causing syntax errors
3.
What will be the output of the following C++ code?
int x = 7, y = 10;
if (x <= y) {
std::cout << "Yes";
} else {
std::cout << "No";
}
medium
A. No
B. 10
C. 7
D. Yes
Solution
Step 1: Evaluate the relational expression
Check if x <= y means 7 is less than or equal to 10, which is true.
Step 2: Determine which branch executes
Since the condition is true, the if block runs and prints "Yes".
Final Answer:
Yes -> Option D
Quick Check:
7 <= 10 is true, so output is Yes [OK]
Hint: Check condition truth to decide output [OK]
Common Mistakes:
Confusing <= with <
Assuming else block runs
Printing variable values instead of strings
Ignoring the condition result
4.
Find the error in the following C++ code snippet:
int a = 4, b = 9;
if (a = b) {
std::cout << "Equal";
} else {
std::cout << "Not Equal";
}
medium
A. Incorrect variable declaration
B. Missing semicolon after if statement
C. Using assignment operator instead of equality operator in if condition
D. Using std::cout incorrectly
Solution
Step 1: Identify the operator in the if condition
The condition uses a = b, which assigns value of b to a instead of comparing.
Step 2: Correct the operator for comparison
To compare, the operator should be ==, so the condition should be a == b.
Final Answer:
Using assignment operator instead of equality operator in if condition -> Option C
Quick Check:
Use == for comparison, not = [OK]
Hint: Use == to compare, = to assign [OK]
Common Mistakes:
Using = instead of ==
Forgetting to compare variables
Assuming assignment returns boolean false
Ignoring compiler warnings
5.
Given the code below, what will be the output?
int a = 5, b = 5, c = 10;
if (a == b && c > b) {
std::cout << "Match";
} else if (a != b || c < b) {
std::cout << "Mismatch";
} else {
std::cout << "Unknown";
}
hard
A. Match
B. Unknown
C. Mismatch
D. Compilation error
Solution
Step 1: Evaluate the first if condition
Check a == b (5 == 5) is true, and c > b (10 > 5) is true, so the whole condition is true.
Step 2: Determine which block executes
Since the first condition is true, "Match" is printed and the rest is skipped.
Final Answer:
Match -> Option A
Quick Check:
Both conditions true, so output is Match [OK]
Hint: Check && needs both true, || needs one true [OK]
Common Mistakes:
Confusing && and || operators
Ignoring short-circuit evaluation
Assuming else if runs after true if
Misreading relational comparisons
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.