Skip to main content

Unit 2 · Selection and Iteration

String Traversal

String Traversal MCQ Practice

Question 1 of 15

Answered 0 of 15

Choose one answer.

What is printed by the following code segment?

String word = "level";
int matches = 0;
int position = 0;
while (position < word.length())
{
    String piece = word.substring(position, position + 1);
    if (piece.equals("l"))
    {
        matches++;
    }
    position++;
}
System.out.print(matches);
Choose one answer.