Quiz 3                                                                                                                    Name: _____________________

 

CPSC 110                                                                                                             ID: ________________________

 

(c)opyright 2002 Brent M. Dingle                                                                   Section: ____________________

 

                                                                                                                                TA: ________________________

 

INSTRUCTIONS:

All answers should be PRINTED. If I am not able to easily read the answer, you will NOT receive any credit for it. Please use a PENCIL and an eraser (if/when needed).

 

Part I  - True or False, Circle your answer  ( 20 points, 2 points per question)

 

1.

True  or   False

The body of a repeat-until loop is always executed at least once.

 

 

 

2.

True  or   False

The value of a boolean variable must be either true or false.

 

 

 

3.

True  or   False

A repeat-until loop continues to execute until the control expression evaluates to false.

 

 

 

4.

True  or   False

The control variable of a for loop must always be of type integer.

 

 

 

5.

True  or   False

At the end of a program using input and output files, only the input file must be closed.

 

 

 

6.

True  or   False

Every CASE statement must have an ELSE.

 

 

 

7.

True  or   False

Every procedure must have at least one parameter.

 

 

 

8.

True  or   False

true AND false evaluates to _________
(circle what would correctly fill in the blank)

 

 

 

9.

True  or   False

(1010 = 11) OR (0 = 15 MOD 5) evaluates to _________

 

 

 

10.

True  or   False

The value of an actual variable parameter CAN be changed by a procedure.

 

 

 

 

 

 

 

 

 

 

 

 

Part II  - Multiple Choice, (24 points, 4 points per question)

    Select the best answer by circling A, B, C, D, or E in the boxes.

 

  1. When designing an algorithm, the method of breaking down the main task into subtasks is called:

a.       bottom-up design

b.      subjugate and rule design

c.       top-down design

d.      a & c

e.       b & c

 

Answer 11:

A

B

C

D

E

 

 

The next 2 questions refer to the below code

(assume embedded in a working program and number is type integer):

 

            CASE number OF

                        0..3 : Writeln(‘first’);

                        4     : Writeln(‘second’);

                        5     : Writeln(‘third’);

                        6..9 : Writeln(‘fourth’);

            ELSE

                        Writeln(‘odd’);

 

  1. What is the output if number is initialized to 2 ?

a.       first

b.      second

c.       third

d.      fourth

    1. odd

 

Answer 12:

A

B

C

D

E

 

 

  1. What is the output if number is initialized to 69 ?

a.       first

b.      second

c.       third

d.      fourth

e.       odd

 

Answer 13:

A

B

C

D

E

 

 

The next two questions refer to the procedure declaration:

 

        PROCEDURE Example(var A: integer; B: integer);

 

  1. A and B are examples of what:

a.       modular parameters

b.      actual parameters

c.       initial parameters

d.      formal parameters

    1. none of the above

 

Answer 14:

A

B

C

D

E

 

 

  1. More specifically A is a(n)________ parameter and B is a(n) ________ parameter

 

    1. actual variable, actual variable
    2. actual variable, actual value
    3. actual value, actual variable
    4. formal variable, formal value
    5. formal value, formal variable

 

Answer 15:

A

B

C

D

E

 

 

  1. If the following procedure declaration was given in a program,


PRODECURE Stuff( X, Y: integer; var Z: integer);


Which of the following would be a valid procedure call? (Assume A and B are variables of type integer and the call is made in a working program at an appropriate place)

 

a.       Stuff(4, A, 4 + B);

b.      Stuff(A, 4 + B, B);

c.       Stuff(A, B, 4);

d.      All of the above

e.       None of the above

 

Answer 16:

A

B

C

D

E

 

 

 

 

 

Part III  - Short Answer  ( 36 points, point values vary), Circle your answers.

 

  1. (5 points) How many times (if any) will the following FOR loop execute?
    (i.e. how many times will the writeln be executed?)

 

FOR N :=   -1 to 5 DO

Begin

      Writeln(N);

End;

7 or Seven  (no partial credit)

 

 

 

  1. (5 points) What is the output (if any) of the following code segment
    (assume embedded in a working program and N is type integer)

 

            FOR N:= 1 DOWNTO 3 DO

            BEGIN

                        Write(N, ‘ ‘);       { there is a space between those single quotes }

           END;

 

There is no output = 5 points

If they said 1 2 3  or 3 2 1 give them 1 point

If they leave the question blank (i.e. there is nothing written here) give them 1 point for the benefit of the doubt.

 

  1. (10 points) What is the output (if any) of the following code segment
    (assume N and M are type integer and the code is in a working program)

 

            FOR N:= 1 TO 3 DO

            BEGIN

                 FOR M:= 3 DOWNTO 1 DO

                 Begin

                        Write(N * M, ‘ ‘);       { there is a space between those single quotes }

                    End;

            END;

 

Run this and check it, it should be 3 2 1 6 4 2 9 6 3

Take one point off if they did not write all the numbers ‘on the same line’

If they have something consistent like 1 2 3 2 4 6 3 6 9 give them 5 points, check with the other TA’s to make sure you are consistent.

Give them 2 points if they wrote anything (involving numbers).

 

 

 

 

  1. (6 points) Fill in the blanks below so that the below WHILE loop would output:
    2 4 6 8    (all on the same line)

 

N := 0;

_WHILE__  (N < ___7___) DO          { a while loop } 

BEGIN

            _N_   :=   _N_    +   _2_ ;      or N := 2 + N

           

            _Write_ ( N, ‘  ‘ );      zero points if they put WriteLN

END;

           

 

  1. ( 5 points) What does the following segment of code output (if anything) ?
    (assume no syntax errors and embedded in a working program)
    Be sure to write the output (if any) as it would appear on the screen.

 

            FOR N:= 0 TO 5 DO

            Begin

               IF (N MOD 3 = 0) THEN

                       Writeln(N, ‘ ‘, N DIV 3);        { there is a space between the single quotes } 

            End;

 

            0 0               1 point for each number and 1 point for

3 1               writing it as a grid  ( -1 point for each extra number to a

max of –4, if they also had the correct answer)

 

  1. ( 5 points) Given the below code, what is(are) the NAME(s) of the file(s) opened for outputting to ?    i.e. What file(s) is(are) opened for writing?

Assume the code is embedded in a working program with no syntax errors.

 

assign(file1, 'data1.txt');

rewrite(file1);

assign(file2, 'data2.txt');

reset(file2);

assign(file3, 'data3.txt);

append(file3);

 

            data1.txt  = 2 points   (if they said file1 give them only 1 point)

          data3.txt = 2 points    (if they said file3 give them only 1 point)

          1 point if they wrote anything and

none of what they wrote was file2 or data2.txt

 

 

 

Part IV  - Programming  ( 20 points, point values vary)

     Be as ‘brief’ as you think possible. Use GOOD coding style!
     (comments are optional, for this)

 

  1. (20 points, 2 points per blank) Complete the following program by filling in the blanks so that the below program will follow the below guidelines:

 

 

PROGRAM CashRegister;

CONST

            RATE = _0.06_ ;  only give them ONE point for .06 – it doesn’t work

            WIDTH = 6;       { field width for money amounts } 

VAR

            price, bill : _real______;                      { Blank 2 is to the left }

 

            number : ___integer_____;                 { Blank 3 is to the left }

 

PROCEDURE Total(var N : integer; var P, B : real);

BEGIN

            ____B__ := _N__ * ( P + RATE * P);   { Blanks 4 and 5 are to the left }

END;  { Total } 

 

BEGIN   { main }

            Writeln(‘Enter the price: ’);

 

            Readln( _price_  );        { Blank 6 is to the left }

 

            Writeln(‘Enter the number of items: ’);

 

            Readln( _number_ );          { Blank 7 is to the left }

 

            { Call the procedure Total } 

            Total( number, __price_ ,  _bill_);  { Blanks 8, 9 are to the left }

 

            Writeln( number, ‘ items at $’, price : WIDTH:2);

 

            Writeln(‘Total Bill $’, __bill__ : WIDTH:2, ‘ with tax.’);

END.