0
0
Javaprogramming~10 mins

Nested if statements 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 ([1] > 0) {
    System.out.println("Positive number");
}
Drag options to blanks, or click blank then click option'
Anum
Bcount
Cvalue
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared.
Forgetting to compare the variable to 0.
2fill in blank
medium

Complete the code to check if a number is positive and even.

Java
int number = 8;
if (number > 0) {
    if (number [1] 2 == 0) {
        System.out.println("Positive 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 operators incorrectly.
3fill in blank
hard

Fix the error in the nested if statement to check if a number is positive and less than 100.

Java
int number = 50;
if (number > 0) {
    if (number [1] 100) {
        System.out.println("Number is positive and less than 100");
    }
}
Drag options to blanks, or click blank then click option'
A<
B>=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than instead of less than.
Using equality operator which only checks if number equals 100.
4fill in blank
hard

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

Java
int number = 15;
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 strict greater than or less than which excludes boundaries.
Mixing up the order of operators.
5fill in blank
hard

Fill both blanks to print messages based on nested if conditions for age and score.

Java
int age = 20;
int score = 85;
if (age [1] 18) {
    if (score [2] 80) {
        System.out.println("Adult with high score");
    } else {
        System.out.println("Adult with low score");
    }
} else {
    System.out.println("Not an adult");
}
Drag options to blanks, or click blank then click option'
A>=
B>
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of >= for age.
Using <= instead of > for score.