114,117 views
27 votes
27 votes
Formal parameters in method headers require including the data type for each parameter in source code.

a. true
b. false

User Sebastien
by
2.8k points

2 Answers

20 votes
20 votes

Final answer:

The statement that formal parameters in method headers require the inclusion of a data type for each parameter is true. This requirement is necessary for the compiler to understand and process the types of data the method will work with.

Step-by-step explanation:

Formal parameters in method headers do require including the data type for each parameter in source code. This is because when defining a method in programming, you need to specify the type of data that the method will be receiving. This information helps the compiler or interpreter to understand what kind of data will be manipulated and ensures that the correct type of data is passed to the function.

For example, in a method that calculates the sum of two numbers, the formal parameters would be defined with data types to indicate that numbers are expected:

public int sum(int number1, int number2) {
return number1 + number2;
}

In this example, int is the data type specified for both parameters, number1 and number2. This tells the compiler that the method expects two integers as input.

User Hakan Ozbay
by
2.5k points
10 votes
10 votes

Answer: Option A) True

Explanation:

Let's take an example:

public static int min (int a, int b)

{

if (a <= b)

return a;

else

return b;

}

We divide method into two parts: header and body.

  • The method header comprises the access modifiers (public static), return type (int), method name (min), and parameters (int a, int b); if this method threw any exceptions, they would appear next.
  • The method body is a block-statement that immediately follows the method header. The parameter names are like variable names; in fact, we often call them parameter variables to make this similarity explicit.

When a method is called, its parameter variables are always initialized by matching arguments first. Then, the method body executes, using these values to compute and return its result; it can also any local variables declard in the block to help in its computation.

If we wrote the statement

System.out.println( Math.min(3,5) );

it would display 3. If we had declared int x = 3, y = 8; and wrote the statement

System.out.println(Math.min (3*x+5,y) );

it would display 8

If we wrote the statement

System.out.println( Math.min(3,5) );

it would display 3. If we had declared int x = 3, y = 8; and wrote the statement

System.out.println(Math.min (3*x+5,y) );

it would display 8

Generally, We call a method by writing its name, followed in parentheses by its arguments (one for each parameter in the method's header) As in the header (where parameters are separated by commas), arguments are are separated by commas as well. When we call a method, Java first evaluates each argument (each can be a simple or complicated expression) and transmits or passes it to its matching parameter; this just means that Java uses each argument's value to initialize it matching parameter in the method. It is equivalent to writing first-parameter = first-argument, then second-parameter = second-argument, etc.

Thus, when calling Math.max(3*x+5,y) above, the first parameter (a) is initialized by the value 14 (3*x+5: the equivalent of a = 3*x+5). Likewise, the second parameter (b) is initialized by the value 8 (y: the equivalent of b = y). Then, Java executes the body of the method, which typically performs some computation using these initialized parameters. It finally returns a result.

User Jason Kleban
by
3.3k points