Ask Question
7 December, 10:08

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

+1
Answers (1)
  1. 7 December, 10:14
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers