For each question, choose the correct method declaration or invocation that matches the given scenario. Select one option from the multiple-choice options provided.
a) public void sum(int a, int b)
b) public void sumIntegers(int a, int b)
c) public int sumNumbers(int a, int b)
d) public int sum(int a, int b)
Answer: d) public int sum(int a, int b)
Explanation:
public
to make it accessible from outside the class.int
which indicates that the method should return an integer value.sum
which is descriptive and indicates the purpose of the method.a
and b
, both of type int
, which allows the method to perform addition on two integers.public static void printMessage(String message) {
System.out.println(message);
}
Which of the following is the correct method invocation to call this method with the message "Hello, World!"?
a) printMessage("Hello, World!")
b) String.printMessage("Hello, World!")
c) System.printMessage("Hello, World!")
d) printMessage.message("Hello, World!")
Answer: a) printMessage("Hello, World!")
Explanation:
printMessage
followed by parentheses ( )
."Hello, World!"
is passed to the method, which matches the parameter type String
defined in the method declaration.