CS 31 – Dis 3C – Fall 08                                                                          Quiz 2
 
Grade yourself: ____________                                       Name: ___________________________
 

 

I. True False: (Explanation required)

 

1.      Given the declaration

int x = 0;

The following expression causes a divide by zero error:

(x !=0) || (2/x < 1);

 

2.      Suppose we have these declarations,

int x = -1, y = 0, z = 1;

This Boolean expression is correct and it does what the programmer intends.

x < y < z

 

3.      You want to determine whether time has run out. The following code correctly implements this.

            !time > limit

 

4.      The value of count is 0; limit is 10. Evaluate:

count == 0 && limit < 20

 

5.      In a while loop, the Boolean_Expression is executed before each execution of the loop body.

 

6.      In a do-while loop, a continue statement terminates the loop.

 

7.      A break statement is used in loops only.

 

 

II. Multiple Choice (Many can be correct)

 

1.      If this code fragment were executed in an otherwise correct and complete program, what would the output be? Explain.

int a = 3, b = 2, c = 5;

if (a > b)

a = 4;

if ( b > c)

a = 5;

else

a = 6;

cout << a < endl;

a)      3

b)      4

c)      5

d)      6

e)      None of the above, the cout statement belongs to the else and so is skipped.

 

2.      What is the value of the bool valued expression, 1 < x < 10? Does the value of this depend on the value of x? Explain, and give the expression that the programmer probably meant.

a)      This statement is incorrect as it is always false.

b)      This statement is correct and its value depends on x.

c)      This statement is incorrect and it is always true

d)      This statement is incorrect, but it does depend on the value of x.

 

3.      Which of the following loop statements is guaranteed to iterate the body of the loop at least once?

e)      while(control) body;

f)       do body while(control);

g)      for (initialize; test; update) body;

h)      none of the above

i)        all of the above

 

4.      In the expression (j > 0 && j+1 == 10), which operator executes last?

a)      >

b)      &&

c)      +

d)      ==

 

5.      Nesting of if, if-else, switch, while, do-while, and for statements:

a)      These constructs may not be nested in at all.

b)      These constructs may be nested in any way that meets the needs of algorithms the programmer is coding.

c)      Only control constructs of a given kind may be nested (while loops within while loops; if-else within if-else etc.)

d)      The question does not make sense in C++.

 

6.      Each of the following has at least one error, either intent, or an error that may be caught by the compiler or both).  What is the error? Assume all the variables you see are declared and initialized.

a)      for(int i = 0; i <10; i++);

sum = sum +x;

b)      if (x > 0)

x = 3

else

x =4;

c)      if(x = 0) x = MAXINT;

d)      if x > 0 x = 3;

e)      if (x>0) then x =3;

 

 

III. Free Form Questions:

 

1.      Assume variables first, second, and max of type double are declared and initialized. Write a sequence of lines of code that put the larger value between first and second into max.

 

 

 

 

 

 

 

 

 

 

2.      For each of the following situations, tell which type of loop (while, do-while, or for) would be best in that situation:

a)      Reading a list of an unknown number of homework grades for a single student.

b)      Summing a series such as 1 +1/2 +1/(22) + 1/(23) + ... + 1/(28)

c)      Testing a newly coded library function to see how it performs for different values of its arguments.

d)      Reading in the number of days of vacation taken by an employee.

3.      A numeric integer grade is between 50 and 99. Using integer division or otherwise obtain a int value that is 5, 6, 7, 8, or 9 from the numeric grade.  Write code using a switch statement using this number in the selector expression that assigns letter grades based on this 10 point scheme:

if the numeric_grade is not less than 90, the letter_grade is an A,

if the numeric_grade is not less than 80, the letter_grade is a B,

if the numeric_grade is not less than 70, the letter_grade is C,

if the numeric_grade is not less than 60, the letter_grade is D,

otherwise the letter_grade is F.

 

 

 

 

 

 

 

 

 

 

 

4.      Write the following do-while statement with a while construct, and maybe some extra code.

x = 10;

do

{

    cout << x << endl;

    x = x - 3;

} while ( x > 0 );

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.      What is the output from each of the following loops?

a)   char c = 'A';

do

{

    cout << c << " ";

    c = c + 2;

} while ( c <= 'M' )

cout << endl;

b)   int i = 0;

while (i < 50)

{

    if ( i < 20 && i != 15 )

        cout << 'X';

    i++;

}

cout << endl;

6.      Write a program that reads in exactly 10 integers and outputs the sum.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      Assume year has been previously declared as an int and given a meaningful value. Write a switch statement that for any value of year, produces exactly the same output as the following if statement.

        if (year == 1992)
               cout << "Clinton";
        else if (year == 2008)
               cout << "Who knows?";
        else if (year == 1988  ||  year == 2000)
               cout << "Bush";
        else
               cout << "Someone";
 

8.      The following program sums all entered int values that are greater than 5. It compiles without any error message, and it executes without error message, but nevertheless is wrong. Name all the errors.

// Display the sum of all entered int values

// greater than 5

#include <iostream>

int main()

{

using namespace std;

int x, sum;

while (x < 10)

{

cin >> x;

if (x > 5);

sum = sum +x;

}

cout << "The sum of values > 5 is " << sum << endl;

}

 
 

Bonus Questions:

 

1.      Will you laugh at us when we keep repeating those trivial advices of “always start early” and “develop incrementally”, and keep begging you to take them seriously?

 

2.      Will you point and laugh at the guy who laughed at us because he thought that those advices were way too trivial, and then failed the class because he took our trivial advices too lightly?