Which of the following is the correct way to declare a method in Java?Question 20Answera.int myMethod( ) { }b.void myMethod { }c.myMethod( ) { }d.int myMethod;
Question
Which of the following is the correct way to declare a method in Java?
Question 20
Answer
a. int myMethod( ) { }
b. void myMethod { }
c. myMethod( ) { }
d. int myMethod;
Solution
The correct way to declare a method in Java is:
a. int myMethod( ) { }
This is because in Java, a method declaration has six components:
-
Modifier- Defines access type of the method i.e. from where it can be accessed in your application. In this case, it is not mentioned which means it has default access modifier.
-
Return type- Method may return a value. int is a return type in this case which means this method will return an integer once it is called.
-
Method Name- This is the name of the method. Method name should be written in small letters. This is the name by which the method can be called.
-
Parameter List- Input to the method. These are optional, method may contain zero parameters. This is empty in this case.
-
Exception list- The exceptions that are thrown by this method. This is not mentioned here.
-
Method body- This is enclosed between braces. The code you need to be executed to perform your intended operations.
Similar Questions
Which keyword is used to declare a generic method's type parameter in Java?Question 5Answera.classb.genericc.template
Which method is called when an object is created in Java?Question 16Answera.constructorb.main()c.init()d.finalize
Which keyword must precede the main method in a Java program?*1 pointpublicmainvoidstatic
rect answerWhich of the following is a method having same name as that of its class?
Which of the following keyword is used to create an object in java?Options: Pick one correct answer from belowcreatenewimplementsextends
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.