OA. free
Free
Accenture Core Computer Science Core Computer Science Medium

What happens in a C++ while(true) loop that reads user input into std::string s and...

Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What happens in a C++ while(true) loop that reads user input into std::string s and executes break; when s == 'yes'?

Choose one option.
Show answer & explanation
Answer: A. The loop continues until the exact token 'yes' is entered, then exits cleanly

A while(true) loop with a string input will only execute break; when the condition s == "yes" evaluates to true, meaning the user must enter exactly the string "yes" for the loop to terminate. Once "yes" is entered, the break statement exits the loop cleanly without errors. Options B, C, and D are incorrect: B misunderstands the conditional logic, C invokes a memory error unrelated to this code pattern, and D describes an infinite loop scenario that doesn't apply when the break condition is met.

Step-by-step Derivation:

  1. The loop structure is: while(true) { read s; if(s == "yes") break; }
  2. while(true) creates an infinite loop that runs indefinitely.
  3. On each iteration, user input is read into string s.
  4. The condition s == "yes" compares the entire string for exact equality.
  5. Only when the user enters exactly "yes" (case-sensitive) does the comparison return true.
  6. When true, break; executes, exiting the loop immediately and cleanly.
  7. Any other input ("Yes", "YES", "y", "no", etc.) will not match and the loop continues.