Ask 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.)

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. 24 February, 22:42
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers