Download presentation
Presentation is loading. Please wait.
1
Smoking is prohibited 1 CS 101 Second Exam Review Prepared by Dr. Amer Al-Badarneh
2
Smoking is prohibited 2MC-1 What is the output of the following code? int j = 1; while (j < 10) { cout << j << " "; j = j + j%3; } a.1 4 7 b.1 4 7 10 c.1 2 5 8 d.1 2 4 5 7 8
3
Smoking is prohibited 3MC-2 What is the output of the following code? int count = 7; while ( count >= 4 ) cout << count--; cout << ”0”; a.65430 b.76540 c.60504030 d.70605040
4
Smoking is prohibited 4MC-3 What is the output of the following code? int k = 4; while (k >= 1) cout << k--; a.432 b.4321 c.43210 d.infinity
5
Smoking is prohibited 5MC-4 What is the output of the following code? int i = 1; while ( i = 5 ) { ++i; cout << i << endl; } a.12345 b.1234 c.123 d.66666… for infinity
6
Smoking is prohibited 6MC-5 What is the output of the following code? int i = 1, s = 0; while ( i < 5 ) { s = s + i; i = i + 1; } cout << i << s; a.610 b.611 c.511 d.510
7
Smoking is prohibited 7MC-6 What is the output of the following code? int j; for (j = 0; j < 5; ) { cout << j; j++ ; } a.0 1 2 3 4 b.Nothing c.1 2 3 4 5 d.000000… for infinity.
8
Smoking is prohibited 8MC-7 What is the output of the following code? for(int j = 10; j > 5; j--) cout << j << " "; a.10 11 12 13 14 15 b.9 8 7 6 5 4 3 2 1 0 c.10 9 8 7 6 5 d.10 9 8 7 6
9
Smoking is prohibited 9MC-8 What will happen if you try to compile and run the following code? int a =1; for( ; ; ) { if (a > 5) break; cout << a++; } a. Creates a syntax error b. Nothing will print c. Printing out 12345 d. Infinity loop
10
Smoking is prohibited 10MC-9 What is the output of the following code? for (int i = 1; i <= 5; i++) { if( i > 3 )break; elsecontinue; cout << i; } a.123456 b.1234 c.4 d.nothing
11
Smoking is prohibited 11MC-10 What is the output of the following code? int x = 1; for(;;) { if(x%4 == 0) break; cout << x; x++; } a.1234 b.123 c.1 d.infinite loop
12
Smoking is prohibited 12MC-11 What is the output of the following code? for int (i = 0; i < 10; i++); cout << i%2; a.01010101010101 b.10101010101010 c.0 d.1
13
Smoking is prohibited 13MC-12 What is the output of the following code? int k = 1, s = 0; for( ; k <= 8; k *= 2, s += k); cout << s; a.02614 b.14 c.30 d.0
14
Smoking is prohibited 14MC-13 What is the output of the following code? int i, sum = 0; for (i = 1; i < 5; i += 1) sum = sum + i; cout << sum; a.0 b.15 c.10 d.1
15
Smoking is prohibited 15MC-14 Select the for statement that is equivalent to the following for statement: for (j = 0; j < 8; j++) a. for (k = 8; k > 1; k -= 1) b. for (k = 8; k >= 0; k -= 1) c. for (k = 8; k > 0; k -= 1) d. for (k = 8; k >= 1; k -= 1)
16
Smoking is prohibited 16MC-14 The following for loop: for (int i = 4 ; i < 9 ; i++) cout << ”x”; a.will print ‘x’ 4 times. b.will print ‘x’ 5 times. c.will print ‘x’ 6 times. d.will print ‘x’ repeatedly due to an endless loop. e.will not compile because braces are missing.
17
Smoking is prohibited 17MC-15 What is the output of the following code? int i, j; for (i=0, j=8; i<8; i++, j--) cout<<i<<"+"<<j<<"="<<i+j<<endl; 0+8=8 1+7=8 2+6=8 3+5=8 4+4=8 5+3=8 6+2=8 7+1=8
18
Smoking is prohibited 18MC-16 What is the output of the following code? int i, j; for ( i = 0; i < 2; ++i ) for ( j = 0; j < 5; ++j ) cout << i << " " << j << endl; 0 0 0 1 0 2 0 3 0 4 1 0 1 1 1 2 1 3 1 4
19
Smoking is prohibited 19MC-17 What is the output of the following code? int i; for(i = 1; i <= 5; i++) cout << i << i%2 << endl; 11 20 31 40 51
20
Smoking is prohibited 20MC-18 Write a C++ program to display the following B BB BBB BBBB BBBBB #include #include void main(){ int i, j; for(i = 1; i <= 5; i++){ for(j = 1; j <= i; j++) cout << ‘B’; cout << endl; } }}}}
21
Smoking is prohibited 21MC-19 What three parts of a counting loop must be coordinated in order for the loop to work properly? a. initializing the counter, testing the counter, changing the counter b. initializing the condition, changing the condition, terminating the loop c. the while, the assignment, and the loop body d. the while statement, the if statement, and sequential execution.
22
Smoking is prohibited 22MC-20 What makes a loop a counting loop? a. A loop control variable is tested in the while statement, and is changed each time the loop body executes. b. A counter is counted upwards by one until it hits a particular limit. c. A counter is counted downwards by one until it hits zero. d. No loop control variables are used.
23
Smoking is prohibited 23MC-21 What does this code print on the monitor? int count = 0; while ( count <= 6 ) { cout << count + " "; count = count + 2; } a. 1 2 3 4 5 6 b. 0 2 4 6 8 c. 0 2 4 d. 0 2 4 6
24
Smoking is prohibited 24MC-22 What does this code print on the monitor? int count = 7; while ( count >= 4 ) { cout << count + " "; count = count - 1; } a. 1 2 3 4 5 6 7 b. 7 6 5 4 c. 6 5 4 3 d. 7 6 5 4 3
25
Smoking is prohibited 25MC-23 What does this code print on the monitor? int count = -2 ; while ( count < 3 ) { cout << count + " "; count = count + 1; } a. -2 -1 1 2 3 4 b. -2 -1 1 2 3 c. -3 -4 -5 -6 -7 d. -2 -1 0 1 2
26
Smoking is prohibited 26MC-24 What does this code print on the monitor? int count = 1; while ( count < 5 ) { cout << count + " "; } a. 1 2 3 4 b. 1 2 3 4 5 c. 2 3 4 d. 1 1 1 1 1 1 1 1 1 1 1....
27
Smoking is prohibited 27MC-25 What condition should be used so that the code writes out: 1 2 3 4 5 6 7 8 int count = 1; while ( ___________ ) { cout << count + " "; count = count + 1; } a. count < 8 b. count < 9 c. count+1 <= 8 d. count != 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.