Which of the following method headers could be used for a method that finds the largest value in an array of integers that is smaller than a given value? Mark ALL that apply.
public static int maxSmallerThan(int max, int[ ] arr)
public static int maxSmallerThan(int[ ] arr, int val)
public static int[ ] maxSmallerThan(int max, int answer)
public static void maxSmallerThan(int[ ] arr, int max, int
answer)
The answer to the above problem is as follows:
Option A - CORRECT - public static int
maxSmallerThan(int max, int[ ] arr)
This is correct because the input parameters are the ones that
would be correct, One parameter has to be the integer array as we
need to find the largest in an array. And the other parameter has
to the value which has to be compared to get the largest number in
array smaller than this value. And the return type is also int,
which is correct to return the largest value smaller than the given
value.
Option B - CORRECT -public static int
maxSmallerThan(int[ ] arr, int val)
Here also the input parameters are correct, and only the name is
different which does not make a difference in terms of the result.
Even the return type is correct. So its a correct option.
Option C - Wrong -public static int[ ]
maxSmallerThan(int max, int answer)
This is wrong option because the input parameter does not have any
array of integers. So without an array, how can we find the largest
element in array. So its wrong here itself. And also the return
type is set to an array of integers but we need to return an
integer value. So wrong.
Option D - Wrong -public static void maxSmallerThan(int[ ] arr, int max, int answer)
This is wrong because the input parameter has three values but only two are required-one array of int and second an int value which would be reference point for largest value smaller than this. Nothing apart from these two is required, so its wrong. Also the return type is void which is wrong again.
Feel free to comment for any query.
Get Answers For Free
Most questions answered within 1 hours.