Q11 /20 The below C++ function returns the string clinic when given an input of string yes.
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
/20
The below C++ function returns the string clinic when given an input of string yes.
#include <iostream>
using namespace std;
int main(void) {
while (1) {
string s;
cin >> s;
if (s == "yes") {
cout << "clinic" << endl;
} else {
break;
}
}
return 0;
}
Choose the correct statement below.
Show answer & explanation
The program uses while(1) which creates an infinite loop. When yes is entered, it prints clinic but does NOT break—it continues looping and prompts for input again. Only non-yes input causes break to exit. Option A is incorrect because yes does not exit. Option B is incorrect because string comparison is exact (yessir ≠ yes). Option C is incorrect because entering yes once does not exit; it loops back for more input.
Step-by-step Derivation:
Code trace: (1) while(1) starts infinite loop. (2) User enters yes. (3) if (s == "yes") evaluates true. (4) cout << "clinic" prints output. (5) No break statement executed—control returns to top of loop. (6) Loop prompts for input again. Result: infinite loop with repeated clinic output until user enters non-yes value.