SYCS-135� Introduction to Programming

 

Quiz 7 - 11/4/99

 

Name:� ���������������������� SOLUTIONS�������������������������������������

 

 

 

_B__ 1.����������� What is the output of the following code fragment? (All variables are of type int.)

�����������������������

����������������������� � limit = 8;

����������������������� � cout << 'H';

����������������������� � loopCount = 10;

����������������������� � do

����������������������� � {

����������������������� ����� cout << 'E';

����������������������� ����� loopCount++;

����������������������� � } while (loopCount <= limit);

����������������������� � cout << "LP";

 

����������� A)������� HEEELP

����������� B)������� HELP

����������� C)������� HLP

����������� D)������� HEELP

����������� E)�������� none of the above

 

_A__ 2.����������� To produce the output� 2 4 6 8 10, what is the loop condition for the following loop?

�����������

����������������������� � n = 0;

����������������������� � do

����������� ����������� � {

����������������������� ����� n = n + 2;

����������������������� ����� cout << n << ' ';

����������������������� � } while ( ??? );

 

����������� A)������� n < 10

����������� B)������� n > 8

����������� C)������� n <= 10

����������� D)������� n < 8

����������� E)�������� n >= 2

 

_D__ 3.����������� What is the value of sum after execution of the following code? (All variables are of type int.)

�����������������������

����������������������� � sum = 0;

����������������������� � for (counter = 2; counter <= 5; counter++)

����������������������� ����� sum = sum + 2 * counter;

 

����������� A)������� 10

����������� B)������� 18

����������� C)������� 30

����������� D)������� 28

����������� E)�������� none of the above

 

 

 

 

_D__ 4.����������� What is the output of the following code fragment? (beta is of type int.)

�����������������������

����������������������� � beta = 5;

����������������������� � do

����������������������� � {

����������������������� ��� ��switch (beta)

����������������������� ����� {

����������������������� ��������� case 1 : cout << 'R';

����������������������� ������������������ break;

����������������������� ��������� case 2 :

����������������������� ��������� case 4 : cout << 'O';

����������������������� ������������������ break;

����������������������� ��������� case 5 : cout << 'L';

����������������������� ����� }

����������������������� ����� beta--;

����������������������� � } while (beta > 1);

����������� ����������� � cout << 'X';

 

����������� A)������� ROOX

����������� B)������� LOORX

����������� C)������� X

����������� D)������� LOOX

����������� E)�������� ROOLX

 

 

5.�� When used for a count-controlled loop, the C++ _______FOR____________ statement is the looping structure that collects into one location the initialization, testing, and incrementation of the loop control variable.