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 20Answera.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
What is the syntax of a Java method? a. public static int methodName(int a, int b) { // body } b. public int methodName(int a, int b) { // body } c. public static void methodName(int a, int b) { // body } d. public void methodName(int a, int b) { // body } e. all
Which of the following declares an abstract method in an abstract Java class? _____A. public abstract method(); B. public abstract void method(); C. public void abstract method(); D. public void method() {}
How do you declare a generic method in Java?Question 12Answera.<T> returnType methodName(T arg)b.returnType methodName<T>(T arg)c.returnType<T> methodName(T arg)d.<T> methodName(returnType arg)
In Java, you have an interface IFlyable with a method Fly(). You have a class Airplane that implements this interface. Which of the following is the correct way to declare the Fly() method in the Airplane class?a.public void Fly() { /*...*/ }b.internal void Fly() { /*...*/ }c.private void Fly() { /*...*/ }d.protected void Fly() { /*...*/ }
Which of the following method declarations is correct for a static method named displayFacts() if the method receives an int argument? a. public static int displayFacts() b. public void displayFacts(int data) c. public static void displayFacts(int data) d. public void displayFacts(static int)
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.