Challenge - 5 Problems
Relational Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Remember that 5 is less than 10, so a > b is false.
✗ Incorrect
Since 5 is not greater than 10, a > b is false. Since 5 is less than or equal to 10, a <= b is true.
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Characters are compared by their Unicode values.
✗ Incorrect
The Unicode value of 'A' (65) is less than 'a' (97), so x < y is true. They are not equal, so x == y is false.
❓ Predict Output
advanced2: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); } }
Attempts:
2 left
💡 Hint
Floating-point arithmetic can cause precision issues.
✗ Incorrect
Due to floating-point precision, 0.1 + 0.2 is not exactly 0.3 but slightly larger, so d1 == d2 is false and d1 > d2 is true.
❓ Predict Output
advanced2: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); } }
Attempts:
2 left
💡 Hint
Boolean values can be compared with == and != operators.
✗ Incorrect
b1 is true and b2 is false, so b1 == b2 is false and b1 != b2 is true.
❓ Predict Output
expert2: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); } }
Attempts:
2 left
💡 Hint
When comparing int and double, int is promoted to double.
✗ Incorrect
i == d is true because 10 equals 10.0. i >= d is true for the same reason. (double) i > d is false because 10.0 is not greater than 10.0.