Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Java - Methods and Code Reusability
What will be the output of this code?
public class Demo {
  public static void greet() {
    System.out.println("Hello!");
  }
  public static void main(String[] args) {
    greet();
  }
}
ANo output
Bgreet
CCompilation error
DHello!
Step-by-Step Solution
Solution:
  1. Step 1: Identify method call in main

    The main method calls greet(), which prints "Hello!".
  2. Step 2: Understand output of greet()

    The greet method prints "Hello!" to the console.
  3. Final Answer:

    Hello! -> Option D
  4. Quick Check:

    Method call output = printed message [OK]
Quick Trick: Method prints output when called inside main [OK]
Common Mistakes:
  • Expecting method name as output
  • Thinking no output occurs without return
  • Assuming compilation error without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes