27
Smoking is prohibited 1 CS 101 CS 101 Second Exam Review Second Exam Review Prepared by Prepared by Dr. Amer Al-Badarneh Dr. Amer Al-Badarneh

Smoking is prohibited 1 CS 101 Second Exam Review Prepared by Dr. Amer Al-Badarneh

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Smoking is prohibited 1

CS 101CS 101

Second Exam ReviewSecond Exam Review

Prepared byPrepared by

Dr. Amer Al-BadarnehDr. Amer Al-Badarneh

Smoking is prohibited 2

MC-1MC-1

What is the output of the following code?What is the output of the following code?int j = 1;while (j < 10){ cout << j << " "; j = j + j%3;}

a.a. 1 4 71 4 7 b.b. 1 4 7 101 4 7 10 c.c. 1 2 5 81 2 5 8 d.d. 1 2 4 5 7 81 2 4 5 7 8

Smoking is prohibited 3

MC-2MC-2

What is the output of the following code? What is the output of the following code? int count = 7; while ( count >= 4 ) cout << count--; cout << ”0”;

a.a. 6543065430 b.b. 7654076540 c.c. 6050403060504030 d.d. 7060504070605040

Smoking is prohibited 4

MC-3MC-3

What is the output of the following code? What is the output of the following code? int k = 4;while (k >= 1) cout << k--;

a.a. 432432 b.b. 43214321 c.c. 4321043210 d.d. infinityinfinity

Smoking is prohibited 5

MC-4MC-4

What is the output of the following code? What is the output of the following code? int i = 1;while ( i = 5 ){ ++i; cout << i << endl;}

a.a. 1234512345 b.b. 12341234 c.c. 123123 d.d. 66666… for infinity66666… for infinity

Smoking is prohibited 6

MC-5MC-5

What is the output of the following code? 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.a. 610610 b.b. 611611 c.c. 511511 d.d. 510510

Smoking is prohibited 7

MC-6MC-6

What is the output of the following code? What is the output of the following code? int j;for (j = 0; j < 5; ){ cout << j; j++ ;}

a.a. 0 1 2 3 40 1 2 3 4 b.b. NothingNothing c.c. 1 2 3 4 51 2 3 4 5 d.d. 000000… for infinity.000000… for infinity.

Smoking is prohibited 8

MC-7MC-7

What is the output of the following code? What is the output of the following code?

for(int j = 10; j > 5; j--) cout << j << " ";

a.a. 10 11 12 13 14 1510 11 12 13 14 15 b.b. 9 8 7 6 5 4 3 2 1 09 8 7 6 5 4 3 2 1 0 c.c. 10 9 8 7 6 510 9 8 7 6 5 d.d. 10 9 8 7 610 9 8 7 6

Smoking is prohibited 9

MC-8MC-8

What will happen if you try to compile and run the What will happen if you try to compile and run the following code?following code?int a =1;for( ; ; ){ if (a > 5) break; cout << a++;}

a. Creates a syntax errora. Creates a syntax error b. Nothing will printb. Nothing will print c. Printing out 12345c. Printing out 12345 d. Infinity loopd. Infinity loop

Smoking is prohibited 10

MC-9MC-9

What is the output of the following code?What is the output of the following code?for (int i = 1; i <= 5; i++){ if( i > 3 ) break; else continue; cout << i;}

a.a. 123456123456 b.b. 12341234 c.c. 44 d.d. nothing nothing

Smoking is prohibited 11

MC-10MC-10

What is the output of the following code? What is the output of the following code? int x = 1;for(;;){ if(x%4 == 0) break; cout << x; x++;}

a.a. 12341234 b.b. 121233 c.c. 11 d.d. infinite loopinfinite loop

Smoking is prohibited 12

MC-11MC-11

What is the output of the following code? What is the output of the following code?

for int (i = 0; i < 10; i++); cout << i%2;

a.a. 0101010101010101010101010101 b.b. 1010101010101010101010101010 c.c. 00 d.d. 11

Smoking is prohibited 13

MC-12MC-12

What is the output of the following code? What is the output of the following code?

int k = 1, s = 0;for( ; k <= 8; k *= 2, s += k); cout << s;

a.a. 0261402614 b.b. 1414 c.c. 3030 d.d. 00

Smoking is prohibited 14

MC-13MC-13

What is the output of the following code? 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.a. 00 b.b. 1515 c.c. 1010 d.d. 11

Smoking is prohibited 15

MC-14MC-14

Select the for statement that is equivalent to the Select the for statement that is equivalent to the following for statement:following for statement:

for (j = 0; j < 8; j++)

a.a. for (k = 8; k > 1; k -= 1)for (k = 8; k > 1; k -= 1) b.b. for (k = 8; k >= 0; k -= 1)for (k = 8; k >= 0; k -= 1) c.c. for (k = 8; k > 0; k -= 1)for (k = 8; k > 0; k -= 1) d.d. for (k = 8; k >= 1; k -= 1)for (k = 8; k >= 1; k -= 1)

Smoking is prohibited 16

MC-14MC-14

The following for loop:The following for loop:

for (int i = 4 ; i < 9 ; i++)cout << ”x”;

a.a. will print ‘x’ 4 times.will print ‘x’ 4 times. b.b. will print ‘x’ 5 times.will print ‘x’ 5 times. c.c. will print ‘x’ 6 times.will print ‘x’ 6 times. d.d. will print ‘x’ repeatedly due to an endless will print ‘x’ repeatedly due to an endless

loop.loop. e.e. will not compile because braces are missing.will not compile because braces are missing.

Smoking is prohibited 17

MC-15MC-15

What is the output of the following code?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=80+8=8 1+7=81+7=8 2+6=82+6=8 3+5=83+5=8 4+4=84+4=8 5+3=85+3=8 6+2=86+2=8 7+1=87+1=8

Smoking is prohibited 18

MC-16MC-16

What is the output of the following code?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 00 0 0 10 1 0 20 2 0 30 3 0 40 4 1 01 0 1 11 1 1 21 2 1 31 3 1 41 4

Smoking is prohibited 19

MC-17MC-17

What is the output of the following code? What is the output of the following code?

int i;for(i = 1; i <= 5; i++) cout << i << i%2 << endl;

1111 2020 3131 4040 5151

Smoking is prohibited 20

MC-18MC-18

Write a C++ program to display the following Write a C++ program to display the following B BB BBB BBBB BBBBB

#include <iostream.h>#include <iostream.h> void main(){void main(){ int i, j;int i, j; for(i = 1; i <= 5; i++){for(i = 1; i <= 5; i++){ for(j = 1; j <= i; j++)for(j = 1; j <= i; j++) cout << ‘B’;cout << ‘B’; cout << endl;cout << endl; }} }}

Smoking is prohibited 21

MC-19MC-19

What three parts of a counting loop must be What three parts of a counting loop must be coordinated in order for the loop to work properly? coordinated in order for the loop to work properly?

a. initializing the counter, testing the counter, a. initializing the counter, testing the counter, changing the counter changing the counter

b. initializing the condition, changing the b. initializing the condition, changing the condition, terminating the loop condition, terminating the loop

c. the while, the assignment, and the loop body c. the while, the assignment, and the loop body

d. the while statement, the if statement, and d. the while statement, the if statement, and sequential execution. sequential execution.

Smoking is prohibited 22

MC-20MC-20

What makes a loop a counting loop? What makes a loop a counting loop?

a. A loop control variable is tested in the while a. A loop control variable is tested in the while statement, and is changed each time the loop body statement, and is changed each time the loop body executes. executes.

b. A counter is counted upwards by one until it hits b. A counter is counted upwards by one until it hits a particular limit. a particular limit.

c. A counter is counted downwards by one until it c. A counter is counted downwards by one until it hits zero. hits zero.

d. No loop control variables are used. d. No loop control variables are used.

Smoking is prohibited 23

MC-21MC-21

What does this code print on the monitor?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 a. 1 2 3 4 5 6 b. 0 2 4 6 8 b. 0 2 4 6 8 c. 0 2 4 c. 0 2 4 d. 0 2 4 6 d. 0 2 4 6

Smoking is prohibited 24

MC-22MC-22

What does this code print on the monitor?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 a. 1 2 3 4 5 6 7 b. 7 6 5 4 b. 7 6 5 4 c. 6 5 4 3 c. 6 5 4 3 d. 7 6 5 4 3 d. 7 6 5 4 3

Smoking is prohibited 25

MC-23MC-23

What does this code print on the monitor?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 a. -2 -1 1 2 3 4 b. -2 -1 1 2 3 b. -2 -1 1 2 3 c. -3 -4 -5 -6 -7 c. -3 -4 -5 -6 -7 d. -2 -1 0 1 2 d. -2 -1 0 1 2

Smoking is prohibited 26

MC-24MC-24

What does this code print on the monitor?What does this code print on the monitor? int count = 1;

while ( count < 5 ) { cout << count + " "; }

a. 1 2 3 4 a. 1 2 3 4 b. 1 2 3 4 5 b. 1 2 3 4 5 c. 2 3 4 c. 2 3 4 d. 1 1 1 1 1 1 1 1 1 1 1 . . . . d. 1 1 1 1 1 1 1 1 1 1 1 . . . .

Smoking is prohibited 27

MC-25MC-25

What condition should be used so that the code What condition should be used so that the code writes out: writes out: 1 2 3 4 5 6 7 81 2 3 4 5 6 7 8 int count = 1; while ( ___________ ) { cout << count + " "; count = count + 1; }

a. count < 8 a. count < 8 b. count < 9 b. count < 9 c. count+1 <= 8 c. count+1 <= 8 d. count != 8 d. count != 8