Ask Question
6 May, 15:56

Assume the following method has been defined:

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

int c = 0;

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

if (a[i]. length () > x)

c++;

}

return c;

}

What is output by the following code?

String b [] = {"aardvark", "banana", "cougar", "daikon", "elephant", "fog", "get"};

System. out. println (mystery (b, 5));

1. 4

2. 5

3. 2

4. 3

5. 0

+4
Answers (1)
  1. 6 May, 19:44
    0
    2. 5

    Looking closely at the function mystery, you should notice that it iterates through the array of strings and counts the number of those strings that have a length greater than the value "x" passed to it. The second piece of code declares and initializes an array of strings and then calls mystery with that array and the value of 5. So it will return the count of words in the string array that have a length greater than 5.

    The strings are:

    aardvark", length = 8, will be counted.

    "banana", length = 6, will be counted.

    "cougar", length = 6, will be counted.

    "daikon", length = 6, will be counted.

    "elephant", length = 8, will be counted.

    "fog", length = 3, will NOT be counted.

    "get", length = 3, will NOT be counted.

    So there will be 5 strings counted. So the answer is "2. 5"
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Assume the following method has been defined: public static int mystery (String a[], int x) { int c = 0; for (int i = 0; i < a. length; ...” 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