Ask Question
14 May, 20:38

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;

}

+2
Answers (1)
  1. 14 May, 22:15
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers