Computer Science 157 – Spring 2001

02/19/01                 Mid Term Test # 1                        Test Form A

 

Name:

 

Instructions:

                Answer ALL questions on the answer sheet provided.

                Hand in TEST, Answer Sheet, and Help Sheet, each with your name on it.

                Remember to put the letter of your test form on your answer sheet.

                Please ask any clarifying questions you have (come up).

 

Completion (3 points each)

 

1. A(n) _______ translates statements in a high level programming language into machine language statements that can be directly executed on the computer.

 

2. In a(n) ________ language, addition may require a sequence of statements such as shown below:

LOAD 4,A

LOAD 5,B

ADD 4,5

STORE 4,C

 

3. In the disciplined design/development approach called ________, the plan for solution is determined at a high-level abstract level specified as pseudocode, then each step is refined into more detail. The process is continued until the program can be easily written from the design/plan pseudocode.

 

 

Short Answer (points as shown)

 

 (5 points)

4. Explain the difference between == and = in C.

 

(5 points)

5. What is wrong with the following sequence of statements?

int x;

int y;

y = x + 1;

x = 5;

 

 

Valid/Invalid (4 points each)

For each, tell whether valid or invalid. If invalid, tell why!

Generally they are taken out of context, we assume that valid context surrounds them (e.g. inside a program, variables declared etc)

 

6.

double 2ndPlace;

 

7.

  int z;

  z = 7 / 3;

 

8.

  int number;

  number = 5;

  number++;

 

 

 

9.

  int x;

  int j;

  x = 17;

  j = 12;

  if ((x < 13) | | (j > 5)) {

     j += x;

    }

 

10.

  /* (x and y are declared and have values) */

  if (x > y)

     y++;

     printf("y: %d \n",y);

  else

     x++;

 

 

Doing and Understanding: (points as shown)

 

(6 points)

11. What is displayed by the following code fragment?

                int x;

                double y;

                x = 7;

                y = 4.0;

                y = x / y;

                  printf(“X: %d  Y:  %4.2f \n”,x,y);

 

 

(7 points)

12. What is displayed by the following code fragment:

  int v;

  int w;

  int x;

  v = 8;

  w = 3;

  x = 11;

  v += w;

  printf(“X:  %2d  V: %2d  W: %2d \n”,x,v,w);

 

 

(7 points)

13. What is displayed by the following code fragment:

 

  int a;

  a = 8;

  if ((a > 5) || (a < 0)) {

     printf("ifthen \n");

    }

  else {

     printf("else \n");

   }

 

 

 

 

 

 

(8 points)

14. What is displayed by the following code fragment:

 

int  x;

int y;

int z;

z = 2;

x = 3;

for (y = 1; y < x; y++) {

      z = z + y;

      printf(“ y:  %3d   z:  %3d \n”,y,z);

}

 

 

(8 points)

15. What is displayed by the following code fragment?

 

double x;

double y;

x  = 13.0;

y = 17.5;

while (x < y) {

   y = y - 1;

   x = x + 1;

   printf(“%4.1f  %4.1f \n”, x, y);

  }

 printf(“done \n”);

 

 

Writing Code (points as shown)

 

(10 points)

16. Assuming that a user has entered values for variables “payRate” (pay per hour, regular time), and “hrsWorked” (hours a person has worked in a week), and that the company pays the pay rate for up to 40 hours, and “time-and-a-half” (1 ½ times normal pay rate) for any hours worked above 40, write a sequence of C statements to calculate and display the gross earnings (“earnings”) for the week. (you don’t have to declare the variables or get the user input).

 

(15 points)
17. Write a sequence of C statements to do the following:

                Assuming that the program has already declared variables, and that the variable “numPlayers” has been filled with the number of golfers, and the variable “par” has been filled with the par for the golf course. Your statements should - for each golfer find out their score (“score”) and process it appropriately. After all golfers have been handled, your program should report how many of the golfers scored below par (“numBelow”).