Bird
0
0

Which of the following method signatures correctly declares a method that returns an integer?

easy📝 Syntax Q12 of 15
Java - Methods and Code Reusability
Which of the following method signatures correctly declares a method that returns an integer?
Apublic String getNumber() { return 5; }
Bpublic void getNumber() { return 5; }
Cpublic int getNumber() { return 5; }
Dpublic int getNumber() { System.out.println(5); }
Step-by-Step Solution
Solution:
  1. Step 1: Check method return type and return statement

    The method must declare int as return type to return an integer value.
  2. Step 2: Match return type with returned value

    public int getNumber() { return 5; } declares int and returns 5, which is an integer, so it is correct.
  3. Final Answer:

    public int getNumber() { return 5; } -> Option C
  4. Quick Check:

    Return type matches returned value [OK]
Quick Trick: Return type must match returned value type [OK]
Common Mistakes:
  • Using void but returning a value
  • Returning wrong type value
  • Missing return statement in non-void method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes