Name:_____________________

 

SID (Last 4):_____________________

 

Login: _____________________

 

 

 

 

 

CS 10 - Winter 2005

Midterm

Friday, February 4

 

 

 

 

 

 

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. return is a valid identifier. 

FALSE

  1. t1, t2, and t3 are good names for variables that store Time objects that represent the scheduled times for breakfast, lunch, and dinner. 

FALSE

  1. Time is a built-in or fundamental data type of the C++ language.

FALSE

  1. A program with a logic error will not compile.

FALSE

  1. Given that x has been declared as a double, the following statement is legal.

4.321 = x;

FALSE

  1. Given that y has been declared as an int, the following expression is legal.

5 == y

TRUE

  1. The following statement correctly constructs a Line object.

Line side_top = Line( (4,5), (5,5) );

FALSE

 

Multiple Choice.  (2 pts each) 

 

  1. Of the following steps in developing a program, which should come before any of the others?

 

    1. Write the include directives
    2. Write an algorithm
    3. Write C++ code
    4. Write the source code
    5. Compile to object code
    6. Run executable

 

  1. If the int variable a has the value 10 what is the value of the following expression?

(0 == (a % 2))

 

a.       0

b.       5

c.       10

d.       true

e.       invalid expression (syntax error)

f.        None of the above

 

  1. What is output by the following code?

Time t1 = Time(4,5,30);

t1.add_seconds(60);

cout << t1.get_seconds();

 

a.       0

b.       6

c.       30

d.       (4,6,30)

e.       90

f.        None of the above

 


Use the following code fragment for the next 3 questions.

 

int x  = 10;

int a;

cout << “Enter an integer”;

cin >> a;

 

if (x < a)

{

        x  = x * 4;

}

else if (x == a)

{

        x = x + 4;

}

else

{

        x++;

        a = x;

}

 

  1. If the user enters a 10 for a, what will be the value of x after the code fragment executes?

a.       10

b.       11

c.       14

d.       15

e.       40

f.        none of the above

 

 

  1. If the user enters a 20 for a, what will be the value of x after the code fragment executes?

a.       10

b.       14

c.       20

d.       40

e.       45

f.        none of the above

 

  1. If the user enters a 0 for a, what will be the value of x after the code fragment executes?

a.       0

b.       10

c.       11

d.       14

e.       40

f.        none of the above

 

 

 

 

 


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

 

 

  1. To use cin and cout objects in your program you must include _the iostream library______ .

 

 

  1. A computer’s _____CPU___________ performs program control, arithmetic, and data movement.

 

 

  1. List one example of a secondary storage device used by desktop computers. ___hard disk_______

 

 

  1. get_center() is a _____member_______ function of a Circle object.

 

 

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

 

  1. double x = 1.0;

cout << fixed << setprecision(1);

if (x <= 10.0)

{

                cout << x << “  “;

                x = x + 10.275;

}

cout << x << “\n”;

 

1.0 11.3

 

  1. double x = 1.0;

cout << fixed << setprecision(3);

if (x > 2.0)

{

                cout << x << “  “;

                x = x + 10.275;

}

cout << x << “\n”;

 

1.000

 

  1. int y = 28;

if (y < 10)

{

                y = y % 5;

                cout << y << “\n”;

}

else

{

                y = y % 10;

                cout << y << “\n”;

}

cout << y << “\n”;

 

8

8

 

  1. (12 pts) Write CODE, not a program, that outputs “Grab your jacket” if a variable called temperature has a value below 70 or a variable called rainfall has a value above 0.2. Otherwise, output “No jacket required”. You may assume the variables temperature and rainfall have been declared and given values already.

 

 

if (temperature < 70 or rainfall > 0.2)

{

                cout << “Grab your jacket”;

}

else

{

                cout << “No jacket required”;

}

 

 

 


  1. (36 pts) Write a PROGRAM that allows the user to enter an exam score. You may assume they will enter an integer. Then output their grade given the scale 100 or above is an A+, 90 – 99 is an A, 80 – 89 is a B, 70 – 79 is a C, 60 – 69 is a D and any score below 60 is an F. Finally, in addition to outputting the grade, if their grade is a C or better, also output the message, “Great job!”, otherwise also output the message, “Study harder.”

 

 

 

#include <iostream>

 

 using namespace std;

 

int main()

{

                int score;

                cout << “Enter the exam score”;

                cin >> score;

 

                cout << “Grade: “;

                if (score >= 100)

                {

                                cout << “A+ ”;

                }

else if (score >= 90)

                {

                                cout << “A ”;

                }

else if (score >= 80)

                {

                                cout << “B ”;

                }

else if (score >= 70)

                {

                                cout << “C ”;

                }

else if (score >= 60)

                {

                                cout << “D ”;

                }

                else

{

                cout << “F “;

}

 

if (score >= 70)

{

                cout << “Great Job!\n”;

}

else

{

                cout << “Study harder.\n”;

}

 

                return 0;

}