Bird
Raised Fist0
Javaprogramming~10 mins

Else–if ladder in Java - Interactive Code Practice

Choose your learning style10 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 num = 10;
if (num [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 only if the number is zero.
2fill in blank
medium

Complete the else-if ladder to check if a number is zero.

Java
int num = 0;
if (num > 0) {
    System.out.println("Positive number");
} else if (num [1] 0) {
    System.out.println("Zero");
} else {
    System.out.println("Negative number");
}
Drag options to blanks, or click blank then click option'
A==
B<
C>=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>=' will not correctly check for zero.
Using '!=' checks for not equal, which is incorrect here.
3fill in blank
hard

Fix the error in the else-if ladder condition to check if a number is negative.

Java
int num = -5;
if (num > 0) {
    System.out.println("Positive number");
} else if (num [1] 0) {
    System.out.println("Negative number");
} else {
    System.out.println("Zero");
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' checks for positive numbers, not negative.
Using '==' checks for equality, not negativity.
4fill in blank
hard

Fill both blanks to complete the else-if ladder that prints the size category based on age.

Java
int age = 20;
if (age [1] 12) {
    System.out.println("Child");
} else if (age [2] 19) {
    System.out.println("Teenager");
} else {
    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 '==' instead of range checks.
Mixing up '<' and '>=' operators.
5fill in blank
hard

Fill all three blanks to complete the else-if ladder that categorizes temperature.

Java
int temp = 30;
if (temp [1] 0) {
    System.out.println("Freezing");
} else if (temp [2] 30) {
    System.out.println("Cold");
} else if (temp [3] 60) {
    System.out.println("Warm");
} else {
    System.out.println("Hot");
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' for freezing condition.
Confusing '>=' and '<' in conditions.

Practice

(1/5)
1. What does an else-if ladder in Java help you do?
easy
A. Run all conditions regardless of their truth
B. Check multiple conditions one by one until one is true
C. Only check the first condition and ignore others
D. Skip conditions and run else block always

Solution

  1. Step 1: Understand the purpose of else-if ladder

    An else-if ladder lets you test several conditions in order, stopping at the first true one.
  2. Step 2: Compare options with this behavior

    Only Check multiple conditions one by one until one is true correctly describes checking conditions one by one until one is true.
  3. Final Answer:

    Check multiple conditions one by one until one is true -> Option B
  4. Quick Check:

    Else-if ladder = check conditions one by one [OK]
Hint: Else-if ladder stops at first true condition [OK]
Common Mistakes:
  • Thinking all conditions run regardless
  • Confusing else-if with separate if statements
  • Believing else runs before conditions
2. Which of the following is the correct syntax for an else-if ladder in Java?
easy
A. if(condition) { } else if condition { } else { }
B. if(condition) { } else(condition) { } else { }
C. if condition { } else if(condition) { } else { }
D. if(condition) { } else if(condition) { } else { }

Solution

  1. Step 1: Recall Java syntax for else-if ladder

    Java requires parentheses around conditions and braces for blocks: if(condition) { } else if(condition) { } else { }
  2. Step 2: Check each option for syntax errors

    Only if(condition) { } else if(condition) { } else { } uses correct parentheses and braces. Others miss parentheses or use wrong keywords.
  3. Final Answer:

    if(condition) { } else if(condition) { } else { } -> Option D
  4. Quick Check:

    Correct else-if syntax = if(condition) { } else if(condition) { } else { } [OK]
Hint: Use parentheses for conditions and braces for blocks [OK]
Common Mistakes:
  • Omitting parentheses around conditions
  • Writing else with a condition (should be else if)
  • Missing braces for code blocks
3. What will be the output of this Java code?
int score = 75;
if(score >= 90) {
  System.out.println("Grade A");
} else if(score >= 80) {
  System.out.println("Grade B");
} else if(score >= 70) {
  System.out.println("Grade C");
} else {
  System.out.println("Fail");
}
medium
A. Grade C
B. Grade B
C. Fail
D. Grade A

Solution

  1. Step 1: Check conditions in order with score = 75

    score >= 90? No (75 < 90). Next condition.
  2. Step 2: Check next conditions

    score >= 80? No (75 < 80). Next condition.
    score >= 70? Yes (75 >= 70). So print "Grade C" and skip else.
  3. Final Answer:

    Grade C -> Option A
  4. Quick Check:

    75 is between 70 and 79 = Grade C [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing Grade B or A by mistake
  • Ignoring else-if order
  • Assuming else runs even if condition true
4. Find the error in this else-if ladder code snippet:
int num = 10;
if(num > 10) {
  System.out.println("Greater than 10");
} else if num == 10 {
  System.out.println("Equal to 10");
} else {
  System.out.println("Less than 10");
}
medium
A. Missing semicolon after print statements
B. Using else instead of else-if
C. Missing parentheses around condition in else-if
D. No error, code is correct

Solution

  1. Step 1: Check syntax of else-if condition

    In Java, conditions must be inside parentheses. Here, else if num == 10 lacks parentheses.
  2. Step 2: Verify other parts

    Print statements have semicolons, else-if is used correctly, so no other errors.
  3. Final Answer:

    Missing parentheses around condition in else-if -> Option C
  4. Quick Check:

    Conditions need parentheses [OK]
Hint: Always put conditions inside parentheses [OK]
Common Mistakes:
  • Forgetting parentheses in else-if
  • Confusing else and else-if syntax
  • Missing semicolons (not the issue here)
5. You want to write a program that assigns a letter grade based on score:
- 90 and above: A
- 80 to 89: B
- 70 to 79: C
- 60 to 69: D
- Below 60: F
Which else-if ladder correctly implements this?
hard
A. if(score >= 90) { grade = 'A'; } else if(score >= 80) { grade = 'B'; } else if(score >= 70) { grade = 'C'; } else if(score >= 60) { grade = 'D'; } else { grade = 'F'; }
B. if(score > 90) { grade = 'A'; } else if(score > 80) { grade = 'B'; } else if(score > 70) { grade = 'C'; } else if(score > 60) { grade = 'D'; } else { grade = 'F'; }
C. if(score >= 90) { grade = 'A'; } else if(score > 80) { grade = 'B'; } else if(score >= 70) { grade = 'C'; } else if(score >= 60) { grade = 'D'; } else { grade = 'F'; }
D. if(score >= 90) { grade = 'A'; } else if(score >= 80) { grade = 'B'; } else if(score >= 70) { grade = 'C'; } else if(score > 60) { grade = 'D'; } else { grade = 'F'; }

Solution

  1. Step 1: Understand the grade ranges and conditions

    Grades are assigned for score ranges including boundaries: 90+, 80-89, 70-79, 60-69, below 60.
  2. Step 2: Check each option's condition operators

    if(score >= 90) { grade = 'A'; } else if(score >= 80) { grade = 'B'; } else if(score >= 70) { grade = 'C'; } else if(score >= 60) { grade = 'D'; } else { grade = 'F'; } uses >= for lower bounds correctly. The other options use > in some places, which excludes boundary values (like 80 or 60).
  3. Final Answer:

    if(score >= 90) { grade = 'A'; } else if(score >= 80) { grade = 'B'; } else if(score >= 70) { grade = 'C'; } else if(score >= 60) { grade = 'D'; } else { grade = 'F'; } -> Option A
  4. Quick Check:

    Use >= for inclusive ranges [OK]
Hint: Use >= for inclusive score ranges [OK]
Common Mistakes:
  • Using > instead of >= excludes boundary scores
  • Overlapping or missing ranges
  • Incorrect else placement
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