Assume that the following code is correctly inserted into a program: int s = 0;
ID: 3687714 • Letter: A
Question
Assume that the following code is correctly inserted into a program: int s = 0; for (i = 0; i < 5; i++) { s = 2 * s + i; cout << s << " "; } cout << endl; a. What is the final value of s? (i) 11 (ii) 4 (iii) 26 (iv) none of these b. If a semicolon is inserted after the right parenthesis in the for loop statement, what is the final value of s? (i) 0 (ii) 1 (iii) 2 (iv) 5 (v) none of these c. If the 5 is replaced with a 0 in the for loop control expression, what is the final value of s? (i) 0 (ii) 1 (iii) 2 (iv) none of these
Explanation / Answer
a)(iii)26
final value of s is 26
the s value for each iteration are as follows 0
1
4
11
26
b) (iv)5
if we put selmicolon right side of the for the loop will make i value as 5;
theblock of code will not be looped.
s=2*s+i; //s=0,i=5
s=2*0+5=5
c) (i) 0
if we replace 5 with 0 the condition in loop is not satisfied so the loop wil not execute the block of code so the s vlaue will be the initially declare value s=0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.