Bird
0
0

Given this method:

hard📝 Application Q15 of 15
Java - Methods and Code Reusability
Given this method:
public static String checkNumber(int n) {
  if (n > 0) return "Positive";
  else if (n < 0) return "Negative";
  else return "Zero";
}

What will checkNumber(0) return?
A"Zero"
B"Negative"
C"Positive"
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if-else conditions

    If n > 0 returns "Positive", if n < 0 returns "Negative".
  2. Step 2: Check the else case for n = 0

    Since 0 is neither greater nor less than 0, else returns "Zero".
  3. Final Answer:

    "Zero" -> Option A
  4. Quick Check:

    0 triggers else return "Zero" [OK]
Quick Trick: Check all conditions including else for exact return [OK]
Common Mistakes:
  • Assuming 0 is positive or negative
  • Missing else return case
  • Expecting null when no return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes