Ask Question
23 October, 08:42

Consider the following methods:

public static double average (int nums[]) {

int sum = 0;

for (int i = 0; i < nums. length; i++) {

sum + = nums[i];

}

return (1.0 * sum) / nums. length;

} / / average

public static int[] mystery (String a[]) {

int temp [] = new int[a. length];

for (int i = 0; i < a. length; i++) {

temp[i] = a[i]. length ();

}

return temp;

} / / mystery

What is output by running the following?

String spelling[] = {"against", "forms", "belief", "government", "democratic", "movement", "understanding", "single", "followed", "scenario"};

System. out. println (average (mystery (spelling)));

1. 10

2. 8.5

3. 8.1

4. Error, you cannot average Strings.

5. 8

+3
Answers (1)
  1. 23 October, 09:44
    0
    3. 8.1

    Looking at the functions "average", and "mystery" it's pretty obvious that "average" returns a double precision average of the value of an integer array. The function "mystery" returns an array of integers with each value in the array being the length of the string in the array of strings passed to "mystery" in the same ordinal position.

    The main body of the code initializes an array of strings and then passes that array to "mystery" who's output is then passed into the function "average". Since the lengths of the words passed to "mystery" is 7, 5, 6, 10, 10, 8, 13, 6, 8, 8, the sum will be 81, so the average will be 81/10 = 8.1 which matches option "3".
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the following methods: public static double average (int nums[]) { int sum = 0; for (int i = 0; i < nums. length; i++) { sum + = ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers