Bird
Raised Fist0
Javaprogramming~20 mins

Relational operators in Java - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. Which of the following relational operators in Java checks if two values are not equal?
easy
A. ==
B. >=
C. !=
D. <

Solution

  1. Step 1: Understand the meaning of relational operators

    Relational operators compare two values and return true or false based on the comparison.
  2. Step 2: Identify the operator for 'not equal'

    The operator '!=' means 'not equal to' in Java.
  3. Final Answer:

    != -> Option C
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Remember: '!=' means not equal in Java [OK]
Common Mistakes:
  • Confusing '!=' with '=='
  • Using '=' instead of '=='
  • Mixing up '<' and '<='
2. Which of the following is the correct syntax to check if variable a is greater than or equal to variable b in Java?
easy
A. a => b
B. a >= b
C. a =< b
D. a =>= b

Solution

  1. Step 1: Recall the correct relational operator syntax

    The operator for 'greater than or equal to' is written as '>=' in Java.
  2. Step 2: Check each option for correct syntax

    a >= b uses '>=', which is correct. Other options have invalid syntax.
  3. Final Answer:

    a >= b -> Option B
  4. Quick Check:

    Greater or equal operator = >= [OK]
Hint: Use '>=' for greater or equal, not '=>' or '=<' [OK]
Common Mistakes:
  • Writing '=>' instead of '>='
  • Using '=' instead of '=='
  • Confusing '<=' and '=>'
3. What will be the output of the following Java code?
int x = 5;
int y = 10;
System.out.println(x < y);
System.out.println(x == y);
System.out.println(x != y);
medium
A. false false true
B. false true false
C. true true false
D. true false true

Solution

  1. Step 1: Evaluate each relational expression

    x is 5 and y is 10. So, x < y is true, x == y is false, and x != y is true.
  2. Step 2: Write the output in order

    The printed output will be: true false true.
  3. Final Answer:

    true false true -> Option D
  4. Quick Check:

    5 < 10 = true, 5 == 10 = false, 5 != 10 = true [OK]
Hint: Compare values step-by-step to get true/false results [OK]
Common Mistakes:
  • Mixing up == and !=
  • Assuming x == y is true
  • Confusing output order
4. Identify the error in the following Java code snippet:
int a = 7;
int b = 3;
if (a <> b) {
System.out.println("Not equal");
}
medium
A. The operator '<>' is invalid in Java
B. Missing semicolon after if statement
C. Variables a and b are not declared
D. The print statement syntax is incorrect

Solution

  1. Step 1: Check the relational operator used

    The operator '<>' is not a valid relational operator in Java. Java uses '!=' for not equal.
  2. Step 2: Verify other syntax elements

    Semicolons and print statement are correct. Variables are declared properly.
  3. Final Answer:

    The operator '<>' is invalid in Java -> Option A
  4. Quick Check:

    Use '!=' for not equal, not '<>' [OK]
Hint: Use '!=' for not equal; '<>' is invalid in Java [OK]
Common Mistakes:
  • Using '<>' instead of '!='
  • Forgetting semicolons
  • Incorrect print syntax
5. Given the code below, what will be the output?
int a = 4;
int b = 4;
int c = 5;
boolean result = (a <= b) && (c > b);
System.out.println(result);

Choose the correct output.
hard
A. true
B. false
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Evaluate each relational expression

    a <= b is 4 <= 4, which is true. c > b is 5 > 4, which is true.
  2. Step 2: Evaluate the logical AND operation

    Both conditions are true, so (true && true) is true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    4 <= 4 and 5 > 4 = true [OK]
Hint: Both conditions must be true for && to be true [OK]
Common Mistakes:
  • Confusing && with ||
  • Misreading <= as < only
  • Assuming false if equal values
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