0
0
Javaprogramming~10 mins

If–else statement in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is positive.

Java
int number = 10;
if (number [1] 0) {
    System.out.println("Positive number");
}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
Using '==' checks for equality, not positivity.
2fill in blank
medium

Complete the code to print "Even" if the number is even.

Java
int num = 4;
if (num % 2 [1] 0) {
    System.out.println("Even");
}
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will check for odd numbers instead.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix the error in the if statement condition to check if age is at least 18.

Java
int age = 20;
if (age [1] 18) {
    System.out.println("Adult");
}
Drag options to blanks, or click blank then click option'
A<
B>=
C<=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' checks for younger ages.
Using '==' only checks for exactly 18.
4fill in blank
hard

Fill both blanks to complete the if-else statement that prints if a number is positive or not.

Java
int n = -5;
if (n [1] 0) {
    System.out.println("Positive");
} else {
    System.out.println("[2]");
}
Drag options to blanks, or click blank then click option'
A>
BNegative
CZero
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in the if condition would check for negative numbers instead.
Printing "Zero" in else is incorrect because zero is neither positive nor negative.
5fill in blank
hard

Fill all three blanks to complete the if-else if-else statement that categorizes a score.

Java
int score = 75;
if (score [1] 90) {
    System.out.println("Grade A");
} else if (score [2] 75) {
    System.out.println("Grade B");
} else {
    System.out.println("[3]");
}
Drag options to blanks, or click blank then click option'
A>=
CGrade C
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' will reverse the logic.
Printing wrong grade labels in else block.