Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+18 votes

String word = "a";
while (word.length() < 10) {

word = "b" + word + "b";


asked in CSC211_Winter2018 by (1 point)

2 Answers

+14 votes

I believe it will go through it 5 times because after the first time word.length = 3, and then + 2 with each subsequent loop. After 5 times through the loop, word.length = 11 which is > 10.

answered by (1 point)
+13 votes

The while loop should execute the word = "b" + word + "b"; code five times.
The string word has an initial length of 1, then after the loop checks that 1 < 10, it adds a "b" on either end of the string, updating its length to 3. This process continues three more times until word.length() is no longer < 10, and the string is then printed.

answered by (1 point)