Name:_____________________

 

SID (Last 4):_____________________

 

Login: _____________________

 

 

 

 

 

CS 10 - Fall 2004

Midterm

Wednesday, October 27

 

 

 

 

 

 

Be sure to read each problem carefully and follow the directions.  Points may be taken off if you do not follow the directions.  For example, if the problem asks you to write code or a statement, do not write an entire program.  Please feel free to ask if you have any questions.  Illegible answers will not be graded.

 

 

You will not be graded on commenting.  You do not need to write any comments.  If, however, you do something strange that you think may be hard for the grader to figure out what you are doing, you may want to comment that part.  Partial credit will be given.

 

 

Please work wisely.  Do not spend too much time on any one problem.

 

 

You will be required to show your student ID when you hand in your exam.

 

 

 

 

 

 

 

 

 

 

TOTAL POINTS = 100
Mark  a  for TRUE and mark  b  for FALSE. (2 pts each)

 

  1. a_b_c is a valid identifier. 

TRUE

  1. 7th_inning is a valid identifier. 

FALSE

  1. If 3 variables are read using 1 input statement, the values must be entered on the same line.

FALSE

  1. Assuming that m is a Message variable and c is a Circle variable, the following statement is valid.

Line myline(m.get_start(), c.get_center());

TRUE

  1. Integers, strings, and Points are all fundamental types of the C++ language.

FALSE

  1. A condition and a boolean variable are the same thing.

FALSE

  1. The statements in the body of a while loop may never be executed.

TRUE

 

Multiple Choice.  (2 pts each)  

 

  1. What would be the contents of the string s after the following code.

Point p(4, 4);

Message m(p, “football”);

String s = m.get_text() + m.get_text().substr(4,4);

 

    1. football
    2. footballfootball
    3. footballfoot
    4. footballball
    5. None of the above

 

  1. Assume that we have the following variable definitions in a program:

int a = 11, b = 4, c = 10, d = 2;

What will be the value computed for the expression (a % b + c - d * 3)?

 

a.        6

b.       30

c.        33

d.       7

e.        None of the above

 

  1. What value does a have after the following code?

int a, b;

a = 5;

a++;

a = a + 1;

b = a;

b++;

a = b;

 

a.        5

b.       6

c.        7

d.       8

e.        None of the above

 

  1. (3 parts) How many times will the following loop be executed?

int x = 3;

int a = 12;

 

while(x <= a)

{

        x  = x + 4;

        a++;

}

 

a.        0

b.       2

c.        3

d.       4

e.        none of the above

 

  1. Using the above code, what will the value of x be after execution?

a.        3

b.       7

c.        19

d.       23

e.        none of the above

 

  1. Using the above code, what will the value of a be after execution?

a.        12

b.       13

c.        15

d.       16

e.        none of the above

 

 

 

 

 

Fill in the blank.  Clearly write in the correct answer.  (2 pts each)

 

 

  1. In C++, every _________statement______________________ ends with a semi-colon.

 

 

  1. ________Compiling___________ is the process of translating a source program into an object program.

 

 

  1. An infinite loop has a Boolean expression that is always _____true___________.

 

 

  1. The condition in the following if structure contains a ________logic____________ error.

if (x = 0)

{

                                cout << “x is 0”;

}

else

{

                                cout << “x is not 0”;

}


What is the exact output of the following 3 code fragments? (6 pts each)

 

  1. double x = 1.0;

cout << fixed << setprecision(2);

while (x < 10.0)

{

                x = x + 3.5;

                cout << x << “  “;

}

cout << x << endl;

 

 

4.50  8.00  11.50  11.50

 

 

 

 

 

 

  1. double x = 18.0;

cout << fixed << setprecision(1);

while (x >= 2.0)

{

                cout << x << “  “;

                x = x / 3.;

}

cout << x << endl;

 

 

18.0  6.0  2.0  0.7

 

 

 

 

 

 

  1. int y = 2;

while (y < 100)

{

                y = y * 2;

                cout << setw(8) << y << endl;

}

cout << y << endl;

 

               _______4
               _______8
               ______16
               ______32
               ______64
               _____128
               128

  1. (12 pts) Write CODE, not a program, to output whether a student passed or failed an exam.  Assume that the exam score is in a variable called    score.  A passing score is 60 or higher, below 60 is considered failing.

 

 

 

if (score >= 60)

{

                cout << “Passing score.”;

}

else

{

                cout << “Failing score.”;

}

 

 

 


  1. (36 pts) Write a PROGRAM that allows the user to enter as many non-negative floating-point values as they want.  When they are finished entering values, they will enter a -1 to indicate they are done.  Then you should output the average of the values (not including the -1).

 

 

 

 

#include <iostream>

 

 using namespace std;

 

int main()

{

                double number;

                double sum = 0;

                int count = 0;

 

                cout << “Enter a positive number (-1 to quit): “;

                cin >> number;

 

                while (number >= 0)

                {

                                sum = sum + number;

                                count++;

 

                                cout << “Enter another number (-1 to quit):  “;

                                cin >> number;

                }

 

                double ave;

                ave = sum / count;

                cout << “The average is: “ << ave << endl;

 

                return 0;

}