Problem 22
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
Problem 24
Write a for statement to add all the multiples of 3 between 1 and 100
Problem 25
What is the output of the following code? Is there a relationship between the variables x and y? If yes, state the relationship? What is the output? int x = 19683; int i; int y = 0; for (i = x; i >= 1; i = i / 3) y++; cout << "x = " << x << ", y = " << y << endl;
Problem 26
Suppose that the input is 5 3 8. What is the output of the following code? Assume all variables are properly declared. cin >> a >> b >> c; for (j = 1; j < a; j++) { d = b + c; b = c; c = d; cout << c << " "; } cout << endl;
Problem 27
What is the output of the following \(\mathrm{C}++\) program segment? Assume all variables are properly declared. for (j = 0; j < 8; j++) { cout << j * 25 << " - "; if (j != 7) cout << (j + 1) * 25 - 1 << endl; else cout << (j + 1) * 25 << endl; }
Problem 28
Suppose that the input is 38 35 71 44 -1. What is the output of the following code? Assume all variables are properly declared. sum = 0; cin >> num; for (j = 1; j <= 3; j++) { cin >> num; sum = sum + num; } cout << "Sum = " << sum << endl;
Problem 29
Which of the following apply to the while loop only? To the do...while loop only? To both? a. It is considered a conditional loop. b. The body of the loop executes at least once. c. The logical expression controlling the loop is evaluated before the loop is entered. d. The body of the loop may not execute at all.
Problem 32
How many times will each of the following loops execute? What is the output in each case? a. x = 5; y = 50; do x = x + 10; while (x < y); cout << x << " " << y << endl; b. x = 5; y = 80; do x = x * 2; while (x < y); cout << x << " " << y << endl; c. x = 5; y = 20; do x = x + 2; while (x >= y); cout << x << " " << y << endl; d. x = 5; y = 35; while (x < y) x = x + 10; cout << x << " " << y << endl; e. x = 5; y = 30; while (x <= y) x = x * 2; cout << x << " " << y << endl; f. x = 5; y = 30; while (x > y) x = x + 2; cout << x << " " << y << endl;
Problem 33
Write an input statement validation loop that prompts the user to enter a number less than 20 or greater than 75.
Problem 34
Rewrite the following as a for loop. int i = 0, value = 0; while (i <= 20) { if (i % 2 == 0 && i <= 10) value = value + i * i; else if (i % 2 == 0 && i > 10) value = value + i; else value = value - i; i = i + 1; } cout << "value = " << value << endl; What is the output of this loop?