Please anwer the following questions and explain your answer for each you will n
ID: 3626417 • Letter: P
Question
Please anwer the following questions and explain your answer for each you will need to utilize C++ logic1. How many times will the following loop execute, assuming the value of i is not
changed within the loop body?
for ( int i = -10; i <= 10; i += 3 ) { ... }
a. 0 b. 3 c. 7 d. 8 e. 21 f. infinite
2. What will be the exact output by the following code fragment?
int s = 1;
int n = 1;
while ( s < 10 )
{
s = s + n;
cout << s;
++n;
}
a. 24711 c. 1247 e. 23456789
b. 247 d. 123456789 f. 7
3. What will be the exact output by the following code fragment?
int n = 10;
do
{
cout << n;
++n;
}
while ( n < 10 );
cout << n;
a. 1011 c. 1111 e. 1110
b. 1112 d. 1010 f . 10
4. W hat is the value stored in the variable named y after the following code executes?
int n = 10;
if ( n >= 10 )
cout << "a";
else if ( n <= 10 )
cout << "b";
else
cout << "c";
a. a b. c c. b d. ab e. ac f. bc
5. What is the value stored in the variable named y after the following code executes?
int n = 10;
if ( n >= 10 )
cout << "a";
if ( n <= 10 )
cout << "b";
else
cout << "c";
a. a b. c c. b d. ab e. ac f. bc
Explanation / Answer
1. This is the call tree: Run at -10 Run at -7 Run at -4 Run at -1 Run at 2 Run at 5 Run at 8 STOP at 11 So it will go through the loop 7 times. 2. a) 24711 The function will go through until s >= 10, so the trace is: s = 1 + 1 // s = 2 s = 2 + 2 // s = 4 s = 4 + 3 //s = 7 s = 7 + 4 //s = 11, STOP 3. a. 1011 The do part is executed, so 10 is printed, n is incremented, then the while condition is seen and exited, and then n is printed so 11 is printed. Output: 1011 4. output is just "a". Once an if clause is met, it will exit the current series of "if, elseif, else" and move on to the next one. 5. Same question as 4. Hope it helps! Please rate lifesaver.