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.

Computer programming help?

The static method arrayToString takes an array of Strings and returns a String that reports the contents of the array in a form illustrated by the following examples. If the arrays x, y, and z are given by:

String[] x = { "a", "b", "c", "d" };

String[] y = { "membership" };

String[] z = { };

then arrayToString( x ), arrayToString( y ), and arrayToString( z ) have the values:

"[ a, b, c, d ]"

"[ membership ]"

"[ ]"

respectively. Which of the following are suitable definitions of arrayToString?

public static String arrayToString( String[] sArr )

{

String s = "[ ";

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

{

s += sArr[ i ];

if ( i + 1 < sArr.length )

s += ", ";

}

s += " ]";

return s;

}

public static String arrayToString( String[] sArr )

{

String s = "[ ";

int i = 0;

while ( i < sArr.length )

{

s += sArr[ i ] + ", ";

i++;

}

s += "]";

return s;

}

public static String arrayToString( String[] sArr )

{

String s = "[ ";

int i = 0;

while ( i + 1 < sArr.length )

{

s += sArr[ i ] + ", ";

i++;

}

if ( sArr.length > 0 )

s += sArr[ i ];

s += " ]";

return s;

}

I only

II only

I and II only

I and III only

I, II, and III

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    1 and 3. We can eliminate 2 because it adds a comma even at the end. The other two are right because I tested them.

    I would advise you to make a program where you test each method, this way you can see visually what is happening.

    Good luck, cheers!

Still have questions? Get your answers by asking now.