Bird
0
0

Why does this method cause a compilation error?

medium📝 Debug Q7 of 15
Java - Methods and Code Reusability
Why does this method cause a compilation error?
public String getText() {
  System.out.println("Hello");
}
AVoid return type required
BIncorrect print syntax
CMethod name is invalid
DMissing return statement for non-void method
Step-by-Step Solution
Solution:
  1. Step 1: Check method return type

    The method declares return type String, so it must return a String value.
  2. Step 2: Verify method body

    The method only prints text but does not return anything, causing compilation error.
  3. Final Answer:

    Missing return statement for non-void method -> Option D
  4. Quick Check:

    Non-void methods must return a value [OK]
Quick Trick: Non-void methods must always return a value [OK]
Common Mistakes:
  • Thinking print counts as return
  • Ignoring missing return statement
  • Confusing method name rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes