0
0
Javaprogramming~10 mins

If 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 the number is positive.

Java
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 '>' to check positivity.
Using '<' which checks for negative numbers.
2fill in blank
medium

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

Java
if (number [1] 2 == 0) {
    System.out.println("Even number");
}
Drag options to blanks, or click blank then click option'
A/
B%
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division '/' instead of modulus '%'.
Using multiplication '*' or addition '+' which are not correct here.
3fill in blank
hard

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

Java
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 '<=' which check for younger ages.
Using '==' which only checks exactly 18.
4fill in blank
hard

Fill both blanks to check if a number is between 10 and 20 (inclusive).

Java
if (number [1] 10 && number [2] 20) {
    System.out.println("Number is between 10 and 20");
}
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' for the upper bound.
Using '>' instead of '>=' for the lower bound.
5fill in blank
hard

Fill all three blanks to print the grade based on score: A if score >= 90, B if score >= 80, else C.

Java
if (score [1] 90) {
    System.out.println("Grade A");
} else if (score [2] 80) {
    System.out.println("Grade B");
} else {
    System.out.println("Grade [3]");
}
Drag options to blanks, or click blank then click option'
A>=
CC
DB
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' in conditions.
Printing wrong grade letters in else block.