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

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.

Choose one option.
Show answer & explanation
Answer: D. The program will loop infinitely if `yes` is entered repeatedly

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 (yessiryes). 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.