Bird
Raised Fist0
C++programming~20 mins

Relational operators in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Relational Operators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of chained relational operators
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int a = 5, b = 10, c = 15;
    std::cout << (a < b < c) << std::endl;
    return 0;
}
ACompilation error
BUndefined behavior
C0
D1
Attempts:
2 left
💡 Hint
Remember how chained comparisons are evaluated in C++.
❓ Predict Output
intermediate
2:00remaining
Result of relational operator with mixed types
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    int x = 7;
    double y = 7.0;
    std::cout << (x == y) << std::endl;
    return 0;
}
A1
BCompilation error
C0
DRuntime error
Attempts:
2 left
💡 Hint
Think about how C++ compares int and double.
❓ Predict Output
advanced
2:00remaining
Output of relational operators with pointers
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int arr[3] = {1, 2, 3};
    int* p1 = &arr[0];
    int* p2 = &arr[2];
    std::cout << (p1 < p2) << std::endl;
    std::cout << (p2 < p1) << std::endl;
    return 0;
}
AUndefined behavior
B1\n0
CCompilation error
D0\n1
Attempts:
2 left
💡 Hint
Pointers to elements in the same array can be compared.
❓ Predict Output
advanced
2:00remaining
Relational operator with user-defined type and operator overloading
Given the following code, what is the output?
C++
#include <iostream>
class Box {
public:
    int volume;
    Box(int v) : volume(v) {}
    bool operator<(const Box& b) const {
        return volume < b.volume;
    }
};
int main() {
    Box b1(10), b2(20);
    std::cout << (b1 < b2) << std::endl;
    std::cout << (b2 < b1) << std::endl;
    return 0;
}
A1\n0
B0\n1
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Check how operator< is defined for Box.
🧠 Conceptual
expert
2:00remaining
Behavior of relational operators with floating-point NaN values
In C++, what is the result of comparing a floating-point NaN (Not a Number) value with any number using relational operators like <, >, <=, >=?
AComparisons cause a runtime error
BAll comparisons return true except equality
CThey all return false
DNaN is considered less than any number
Attempts:
2 left
💡 Hint
Think about how NaN behaves in floating-point comparisons.

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

  1. Step 1: Understand the meaning of each operator

    The operator == checks equality, <= and >= check less or equal and greater or equal respectively.
  2. Step 2: Identify the operator for 'not equal'

    The operator != means 'not equal' in C++.
  3. Final Answer:

    != -> Option B
  4. 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

  1. Step 1: Recall the correct operator for 'greater than'

    The operator for 'greater than' is >. The operator >= means 'greater than or equal to'.
  2. 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.
  3. Final Answer:

    a > b -> Option A
  4. 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

  1. Step 1: Evaluate the relational expression

    Check if x <= y means 7 is less than or equal to 10, which is true.
  2. Step 2: Determine which branch executes

    Since the condition is true, the if block runs and prints "Yes".
  3. Final Answer:

    Yes -> Option D
  4. 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

  1. Step 1: Identify the operator in the if condition

    The condition uses a = b, which assigns value of b to a instead of comparing.
  2. Step 2: Correct the operator for comparison

    To compare, the operator should be ==, so the condition should be a == b.
  3. Final Answer:

    Using assignment operator instead of equality operator in if condition -> Option C
  4. 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

  1. 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.
  2. Step 2: Determine which block executes

    Since the first condition is true, "Match" is printed and the rest is skipped.
  3. Final Answer:

    Match -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard