Bird
0
0

Which of the following is the correct way to declare a method in Java?

easy📝 Syntax Q12 of 15
Java - Methods and Code Reusability
Which of the following is the correct way to declare a method in Java?
Agreet void() { System.out.println("Hello"); }
Bvoid greet() System.out.println("Hello");
Cvoid greet[] { System.out.println("Hello"); }
Dvoid greet() { System.out.println("Hello"); }
Step-by-Step Solution
Solution:
  1. Step 1: Check method declaration syntax

    In Java, a method declaration starts with return type, method name, parentheses, and braces.
  2. Step 2: Identify the correct syntax

    void greet() { System.out.println("Hello"); } follows this pattern correctly: 'void greet() { ... }'. Others have syntax errors.
  3. Final Answer:

    void greet() { System.out.println("Hello"); } -> Option D
  4. Quick Check:

    Method syntax = returnType name() { } [OK]
Quick Trick: Method syntax: returnType name() { } [OK]
Common Mistakes:
  • Placing return type after method name
  • Using brackets [] instead of parentheses ()
  • Omitting curly braces {}

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes