Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What is the output of the following code segment? (Write \"infinite loop\" if

ID: 3801880 • Letter: 1

Question

1. What is the output of the following code segment? (Write "infinite loop" if the loop never ends.)

string str = "stressedOUT";
for (string::size_type index = str.length() - 1; index != string::npos; --index)
{
    if (str[index] != 'O' && str[index] != 'U' && str[index] != 'T')
        cout << str[index];
}
cout << endl;

2.

Given the following functions:

int foo()
{
int num = 11;
    return --num;
}

int bar()
{
static int num = 11;
    return --num;
}

The output of the following statement is ________ .
cout << foo() + foo() + bar() + bar() << endl;

3.

Suppose str = "ABCDEFGH". The output of the statement

cout << str.length() << str[str.length() - 1] << endl;

is _______.

7G

8I

8G

8H

9G

9H

4.

Consider the declaration:

enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER};

which of the following statements is valid and true?

SOCCER-- == BASEBALL

BASEBALL++ == SOCCER

HOCKEY + BASEBALL < SOCCER

FOOTBALL <= SOCCER

5.

Suppose str is a string variable that have been initialized to "ABCDE". The output of the statement

cout << str.length() << endl;

is ____________________.

6.

Given the following function prototype, adding the additional prototype ____________________ will result in a compiler error.
bool initiateDestructSequence();

void initiateDestructSequence(const string& authorizationCode);

int initiateDestructSequence();

bool initiateDestructSequence(int countdown);

int initiateDestructSequence(string& authorizationCode, int countdown);

any of the above

7G

8I

8G

8H

9G

9H

4.

Explanation / Answer

1. What is the output of the following code segment? (Write "infinite loop" if the loop never ends.)

string str = "stressedOUT";
for (string::size_type index = str.length() - 1; index != string::npos; --index)
{
    if (str[index] != 'O' && str[index] != 'U' && str[index] != 'T')
        cout << str[index];
}
cout << endl;

Program:

#include <iostream>
using namespace std;

int main() {
   string str = "stressedOUT";
for (string::size_type index = str.length() - 1; index != string::npos; --index)
{
if (str[index] != 'O' && str[index] != 'U' && str[index] != 'T')
cout << str[index];
}
cout << endl;
   return 0;
}

Output:

desserts

Answer: desserts

2. Program

#include <iostream>

using namespace std;
int foo()
{
int num = 11;
return --num;
}
int bar()
{
static int num = 11;
return --num;
}

int main() {
cout << foo() + foo() + bar() + bar() << endl;
   return 0;
}

Output: 39

Answer: 39

3.Program:

#include <iostream>

using namespace std;
int main() {
string str = "ABCDEFGH";

cout << str.length() << str[str.length() - 1] << endl;
   return 0;
}

Output: 8H

Answwer: 8H

Consider the declaration:

enum sports {BASKETBALL, FOOTBALL, HOCKEY, BASEBALL, SOCCER};

which of the following statements is valid and true?

Answer: HOCKEY + BASEBALL < SOCCER

FOOTBALL <= SOCCER

5.Program:

#include <iostream>
using namespace std;
int main() {
string str = "ABCDE";

cout << str.length() << endl;
   return 0;
}

Output: 5

Answer: 5

6.

Given the following function prototype, adding the additional prototype ____________________ will result in a compiler error.
bool initiateDestructSequence();

Answer: Option 2. int initiateDestructSequence();