Ask Question
4 February, 06:30

What output is produced by the following program segment? Why? (Recall that name. charAt (i) is the i-th character in the string, name.)

String name;

int i;

boolean startWord;

name = "Richard M. Nixon";

startWord = true;

for (i = 0; i < name. length (); i++) {

if (startWord)

System. out. println (name. charAt (i));

if (name. charAt (i) = = ' ')

startWord = true;

else

startWord = false;

}

+5
Answers (1)
  1. 4 February, 08:59
    0
    The output is:

    R

    M

    N

    Explanation:

    The code snippet print the beginning letter of each word in the given name.

    In the for loop snippet:

    first the program check if startWord is true and it is true, then it print the value of the character at index 0. Then it check if value of character is empty. If it is empty, startWord is initialized to true else it is initialized to false.

    The loop only print a character when the value of i are 0, 8 and 11 which are also the beginning character of a word.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “What output is produced by the following program segment? Why? (Recall that name. charAt (i) is the i-th character in the string, name.) ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers