click below
click below
Normal Size Small Size show me how
COMP PROG SA 4
Reviewer
| Question | Answer |
|---|---|
| What is the code snippet that will print all numbers from 1 - 100 in reverse in vertical manner? 1. for (int x = 101; x > 0; x--) cout << x << endl; 2. for (int x = 100; x > 1; x++) cout << x << endl; 3. for (int x = 100; x > 0; x--) | for (int x = 100; x > 0; x--) cout << x << endl; |
| int x = 1, y = 2, z = 3; for int (ctr = 1; ctr <= 10; ctr++) { cout << ctr << " iteration" << endl; cout << "\t x = " << x << endl; cout << "\t y = " << y << endl; cout << "\t z = " << z << endl; x += y; y += z; z += ctr; } If the condition ctr | 0 |
| Simulate the following code snippet: int a = 1, b = 1, c = 1; for(int ctr = c; ctr <= 10; ctr++) { cout << ctr << " iteration" << endl; a++; b += 5; c += a; cout << "\t a = " << a << endl; cout << "\t b = " << b << endl; cout << "\t c = " << c << | 51 |
| Given the following code snippet: for (x = 1; x <= 1000; x++) cout << x << endl; What will you do to modify the given code that will only print multiples of 100. | Put an if statement that will check if the value is divisible by 100. (?) |
| Simulate the following code snippet: int x = 1, y = 2, z = 3; for (int ctr = 1; ctr <= 10; ctr++) { cout << ctr << " iteration" << endl; cout << "\t x = " << x << endl; cout << "\t y = " << y << endl; cout << "\t z = " << z << endl; x += y; y += | 1;2;3 |
| Which for loop will cause an infinite loop? 1. for (i = 5; i <= 10; i++) 2. for (i - 5; i > 0; i--) 3. for (i = 0; i < 5; i++) 4. for (i = 5; i = 10; i++) | for (i = 5; i = 10; i++) |
| What is the correct syntax of for loop? | for (initialization; condition; increment/decrement) |
| Simulate the following code snippet: int a = 1, b = 1, c = 1; for (int ctr = c; ctr <=10; ctr++) { cout << ctr << " iteration" << endl; a++; b += 5; c += a; cout << "\t a = " << a << endl; cout << "\t b = " << b << endl; cout << "\t c = " << c << | 66 |
| TRUE OF FALSE: After every execution of for loop's body, the increment/decrement part of for loop executes that updates the loop counter | TRUE (?) |
| The statement i++; is equivalent to ___________ - i = i + i; - i = i + 1; - i = i - 1; - i--; | i = i + 1' |
| What is the output of the C++ Program? #include <iostream> using namespace std; int main() { while (true) { cout << "C++"; break; } return 0; } | C++ |
| What is the output of the C++ Program? #include <iostream> using namespace std; int main() { int a = 25; while (a <= 27) { cout << a << " "; a++; } return 0; } | 25 26 27 |
| Which looping process is best used when the number of iterations is known? 1. do-while 2. while 3. all looping processes require that iterations be known 4. for | for |
| A while loop that never stops is said to be the infinite while loop, when we give the condition in such way so that it never returns ______. | False |
| If there is more than one statement in the block of a while loop, which of the following must be placed at the beginning and the ending of the loop block? - angle brackets < > - parentheses ( ) - brackets [ ] - braces { } | braces { } (?) |
| What is the code that will print this output: 1 2 3 5 8 13 21 34 55 | int sum = 0; int x = 1, y = 2; cout << x << " " << y << " "; while sum (sum < 50) { sum = x + y; cout << sum << " "; x = y; y = sum; } |
| What was the last value of a that was printed on screen? #include <iostream> using namespace std; int main() { // Local variable declaration: int a = 10; // while loop execution while (a < 20) { cout << "value of a: " << a << endl; a++; } retur | 19 |
| What is the output of this C++ Program? int var = 1; while (var <= 1) { cout << var; var++; } | 1 |
| What is the value of num after the 5th iteration? #include <iostream> #include <math.h> using namespace std; int main() { int num = 1; while (num <= 20) { cout << "Num: " << num << endl; num += 3; } return 0; } | 13 |
| Which looping process checks the test condition at the end of the loop? 1. do-while 2. for 3. while 4. no looping process checks the test condition at the end of the loop | do-while |
| What is the effect of writing a break statement inside a loop? | It cancels remaining iterations (?) |
| TRUE OR FALSE: A loop becomes infinite loop if a condition never becomes true | FALSE |
| A continue statement causes the execution to skip to ________ | the next iteration of the loop |
| #include <iostream> using namespace std; int main() { int a = 32; do { cout << a << " "; a++ if (a > 35) break; } while(1); return 0; } | 32 33 34 |
| What is the value of num after the 5th iteration? #include <iostream> #include <math.h> using namespace std; int main() { int num = 1; do { cout << "Num: " << num << endl; num += 3; } while (num <= 20); return 0; } | 19 |
| What should be in the blank to output 01234? int i = 0; do { cout << i; i++; } while (_______); | i < 5 |
| What numbers will the following code display on the computer screen? int x = 20; do { cout << x << endl; x = x -4; } while (x > 10) | 20, 16 ,12 |
| continue statement is used to come out of the loop instantly | FALSE (?) |
| What is the value of num after 3rd iteration? #include <iostream> #include <math.h> using namespace std; int num = 1; do { cout << "Num: " << num << endl; num += 3; } while (num <= 20); return 0; } | 7 |
| Determine the output. s = 0; for (j = 1; j < 3; j++) s = s + j * j; cout << s; | 5 |
| Simulate the following code snippet int x = 1, y = 2, z = 3; for (int ctr = 1; ctr <= 10; ctr++) { cout << ctr << " iteration" << endl; cout << "\t x = " << x << endl; cout << "\t y = " << y << endl; cout << "\t z = " << z << endl; x += y; y += z | 10 |