206k views
1 vote
Which of the following method signatures correctly specifies an array as a return type?

a) private array testMethod ( int x )
b) private int[ ] testMethod ( int x )
c) private double[5] testMethod ( int x )
d) private double testMethod[ ] ( int x )
e) can't tell from this code

1 Answer

3 votes

Answer:

private int[ ] testMethod ( int x )

Step-by-step explanation:

Before going to the reason first understand the syntax of declare the function.

Syntax:

Access_modifier return_type name (argument_1, argument_2,...);

Access_modifier is used for making the restriction to access the values.

it is public, private and protected.

Return_type: it is used to define return type of the function.

it can be int, float, double.

In the question:

Option 1: private array testMethod ( int x )

it is not valid because array is not valid return type.

Option 2: private int[ ] testMethod ( int x )

int[ ] denotes the integer array. so, the function return the integer array.

Option 3: private double[5] testMethod ( int x )

it is not valid because double[5] is not valid return type. we cannot enter the size of the array.

if we replace double[5] to double[ ], then it valid and the function return double type array.

Option 4: private double testMethod[ ] ( int x )

it return type is double. So, it return the decimal value but not array.

Therefore, the correct option is (b).

User Cuong Le Ngoc
by
5.8k points