Limited access

Upgrade to access all content for this subject

List Settings
Sort By
Difficulty Filters
Page NaN of 1946

Consider the following method.

// Return the string replacing every z with an s
public static String replaceZwithS(String phrase)
     String out="";
     for (int i=0; i<phrase.length(); i++)
         String c = phrase.substring(i,i+1);
         if (phrase.substring(i, i+1).equals("z"));
             c = "s";
         out += c;
     return out;

The following code from the main method should produce the output "busser", but instead produces "ssssss".


Which of the following BEST describes why this error is occurring?


The if statement is always true, so every character is replaced with an "s".


The for statement itself is incorrect.


The semi-colon at the end of the if statement means that "s" will always be assigned to c.


c should only be concatenated to out when the character is not a "z".


The following statement is incorrect: String c = phrase.substring(i,i+1);.

Accuracy 0%
Select an assignment template