0
0
C++programming~20 mins

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

Choose your learning style9 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.