0
0
Javaprogramming~20 mins

Relational operators in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Relational Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of relational operators with integers
What is the output of this Java code snippet?
Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;
        System.out.println(a > b);
        System.out.println(a <= b);
    }
}
Afalse\nfalse
Btrue\nfalse
Cfalse\ntrue
Dtrue\ntrue
Attempts:
2 left
💡 Hint
Remember that 5 is less than 10, so a > b is false.
Predict Output
intermediate
2:00remaining
Relational operators with characters
What will this Java program print?
Java
public class Main {
    public static void main(String[] args) {
        char x = 'A';
        char y = 'a';
        System.out.println(x < y);
        System.out.println(x == y);
    }
}
Afalse\nfalse
Bfalse\ntrue
Ctrue\ntrue
Dtrue\nfalse
Attempts:
2 left
💡 Hint
Characters are compared by their Unicode values.
Predict Output
advanced
2:00remaining
Relational operators with floating-point numbers
What is the output of this Java code?
Java
public class Main {
    public static void main(String[] args) {
        double d1 = 0.1 + 0.2;
        double d2 = 0.3;
        System.out.println(d1 == d2);
        System.out.println(d1 > d2);
    }
}
Afalse\ntrue
Bfalse\nfalse
Ctrue\ntrue
Dtrue\nfalse
Attempts:
2 left
💡 Hint
Floating-point arithmetic can cause precision issues.
Predict Output
advanced
2:00remaining
Relational operators with boolean values
What will this Java program print?
Java
public class Main {
    public static void main(String[] args) {
        boolean b1 = true;
        boolean b2 = false;
        System.out.println(b1 == b2);
        System.out.println(b1 != b2);
    }
}
Afalse\ntrue
Btrue\nfalse
Cfalse\nfalse
Dtrue\ntrue
Attempts:
2 left
💡 Hint
Boolean values can be compared with == and != operators.
Predict Output
expert
2:00remaining
Relational operators with mixed types and casting
What is the output of this Java code?
Java
public class Main {
    public static void main(String[] args) {
        int i = 10;
        double d = 10.0;
        System.out.println(i == d);
        System.out.println(i >= d);
        System.out.println((double) i > d);
    }
}
Aeslafn\eurtn\eurt
Btrue\ntrue\nfalse
Crue\ntrue\nfalse
Dtrue\ntrue\nfals
Attempts:
2 left
💡 Hint
When comparing int and double, int is promoted to double.