Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Static Method Computer Science?

Consider this static method:

public static int xCheck( String[] strArr )

{

int n;

int k;

n = strArr.length;

for ( k = 0 ; k < strArr.length ; k++ )

{

if ( strArr[ k ].equals( "x" ) )

{

n--;

}

}

return n;

}

If s is initialized as a String[], which of the following best characterizes the value of xCheck( s )?

The number of occurrences of the string "x" as an element of s.

The number of occurrences of strings other than "x" as elements of s.

The index of the first occurrence of "x" as an element of s.

The index of the last occurrence of "x" as an element of s.

The index of the first occurrence of a string other than "x" as an element of s.

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    2nd one.

    I don't know your level of comprehension when it comes to Java, so to explain:

    Run the program. (nothing happens...*but if you called this from a testing class*....)

    ****

    An array CAN BE (remember, nothing actually happened) sent through this method via the parameter strArr

    Int n is defined as the length of this array.

    The array is used in a for-loop.

    An if-loop, which uses the equals() method, checks to see if any index in the array is equal to the string variable x.

    If any such index is found, the variable n is decremented by a value of 1.

    Each time that happens, n goes down by 1 until the conditions of the for-loop are met and it ends, then it returns the value of n.

    Basically you have the length of the array as a number, and everytime "x" is found in the array, that number goes down. The number you have now represents all the other indexes that DO NOT have x in them.

    More examples to clarify:

    Int n = 5 (there are 5 indexes in the array)

    "x" is found 3 times

    Int n is decremented 3 times (meaning that 3 "x"'s were found in the indexes)

    the returned value of n is 2, that means that there are 2 indexes that don't contain the variable "x" in them.

    But go bug a classmate with your multiple choice tests.

  • Anonymous
    5 years ago

    C A. No, as this function's return type is specified as void B. It doesn't add one to the age of "every" person as there is an if statement specifying a certain condition must be met - a person's age being less than 10 C. I believe this is the answer, as I assume the birthday method increases the person's age by 1 D. The keyword in this answer is "until." When a person's age is 10 or more, the loop doesn't stop, but goes to the next iteration. E. Program works

Still have questions? Get your answers by asking now.