CPSC 110 – Spring 2002 – Midterm                                             Name :   __________________________

o

                                                                                                                Section: __________________________

                                                                                                                TA:          

 

Also write YOUR name and TA’s name on the cover sheet of the test and on the scantron.

If you have any questions during the test, raise your hand and someone will come to you. It is wise to mark your answers both on the scantron and on this test paper. You MUST mark them on the scantron.

 

Unless otherwise noted assume that ALL code pieces are embedded in a working program

with NO syntax  errors.

 

TRUE / FALSE Question  (mark ‘a’ for true,  ‘b’ for false)

 

  1. The value of an actual variable parameter CANNOT be changed by a procedure  (T / F).

 

  1. A compiler can detect syntax and logical errors (T / F).

 

  1. A while-do  loop continues to execute until the control expression evaluates to false (T / F).

 

  1.  RATE and rate are interpreted as two different variables by the Pascal compiler (T / F)

 

  1. A procedure declaration may contain either formal parameters or actual parameters (T / F).

 

  1. The body of a for loop is always executed at least once (T / F).

 

  1. The control variable of a for loop must always be of type real (T / F).

 

  1. The value of a boolean variable must be either true or false (T / F).

 

  1. Assembly language is a high-level language (T / F).

 

  1. Procedures may change global variables (T / F).

 

  1. Pascal only allows variable parameters (T / F).

 

  1. Alt-F5 compiles a program in the Turbo Pascal environment (T / F).

 

  1. There is no way to declare a constant in Pascal (T / F).

 

  1. Any CASE statement can be rewritten as some form of an if – else if – … – else (T / F).

 

  1. NOT ( (a = 0) AND ( a = 5) ) evaluates to what?  (T / F)

 

 

 

 

 

 

 

 

 

 

 

 

MULTIPLE CHOICE – Select the best answer.  ASSUME NO SYNTAX ERRORS IN CODE.

 

 

  1. Which of the below declarations will cause the statement:
    x := y;
    to generate an error?

 

a.        VAR

x: char; 

y: char;

b.       VAR

x: real; 

y: integer;

c.        VAR

x: real; 

y: real;

d.       VAR

x: integer; 

y: integer;

e.        VAR

x: integer; 

y: real;

 

  1. Which of the following is not a valid procedure declaration?

 

a.        Procedure GetNumber( var Number: real);

b.       Procedure MoreStuff( x, y: real);

c.        Procedure AddThem( 2big : real; value: integer);

d.       Procedure SumIt( var P, B: real; var N: integer);

e.        Procedure Read_Name;

 

 

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

 

a.        subjugate and rule design

b.       top-down design

c.        bottom-up design

d.       piece-meal coding

    1. c & d

 

  1. What is the output of the following program segment (assume embedded in working code)

VAR   n : integer;

BEGIN

n := (10 DIV 3);

writeln(n);

END.

 

a.        3.333333

b.       0.333333

c.        1

d.       2

e.        3

 

 

 

 

  1. The predefined procedure to open a file for output (after an appropriate assign has been made) is:

a.        reset

b.       output

c.        open

d.       append

e.        write

 

  1. Which of the following is not an input device?

a.        keyboard

b.       mouse

c.        scanner

d.       monitor (like the ones in your lab)

e.        all the above are input devices

 

 

  1. Which of the following Boolean expressions evaluates to true?

 

a.        NOT ( true or false)

b.       12 mod 4 = 3

c.        (5 = 6) OR (10 > 5)

d.       NOT ( 5 <= 10)

e.        true and false

 

 

  1. In the following Pascal statement, which mathematical operation will be performed first (aka has the higher precedence)? Assume all variables are type real.
                                                 z  := a + 3 * b / 5 – 18.0;
    1. +
    2. –
    3. *
    4. /
    5. Pascal would not be able to evaluate that statement.

 

  1. The rules of grammar for a programming language are called the ________

a.        grammar

b.       rules

c.        statements

d.       ontology

e.        syntax

 

  1. Which identifier BEGINS a comment in Pascal?
    1. /*
    2. (*
    3. }
    4. //
    5. <!-

 

  1. <, =, >, <> are all examples of what in Pascal?
    1. comparison operators
    2. unary operators
    3. things never used
    4. both a and b
    5. none of the above

 

 

 

  1. What is the output (if any) of the below program? (assume no syntax errors)

Program lala;

VAR

                n : integer;

BEGIN

                n := –2;

                While ( n < 0) do

                begin

                                writeln(n, ‘ ’);       { there is a space between the single quotes }

                                n := n + 1;

                end;

END.

 

a.        there is no output

b.       –2 –1

c.        –2
–1
–0

d.       –2 –1 0

e.        –2
–1

 

 

  1. Which of the following is NOT a valid “built in” Pascal function ?

a.        sqrt(25);

b.       square(5);

c.        exp(7.3);

d.       trunc(543.67);

e.        abs(-67);

 

 

The next two questions refer to the procedure declaration:

 

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

 

  1. A and B are examples of what:

a.        modular parameters

b.       formal parameters

c.        actual parameters

d.       initial parameters

    1. none of the above

 

  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

 

 

 

 

 

 

  1. How many times is "Howdy" printed by the below code (assume embedded in working program with no syntax errors):

VAR

                count : integer;

BEGIN

                count := 1;

                while (count < 5) do

                begin

                   writeln('Howdy');

                   count := count – 1;

                end;

END.

 

a.        3

b.       4

c.        5

d.       infinite number of times (or until computer breaks or is turned off)

e.        none of the above

 

 

 

  1. Read this question carefully.
    Which of the following SUCCESSFULLY evaluates to false (no runtime error)
    ONLY when short circuit evaluation is used?
    Assume x = 2 and y = 5 both of type integer.

 

a.        (5 + y >= 6) or (x < 50)

b.       (y > 2*x) and ( x > 2*y)

c.        (x > 3) or (y>5)

d.       (x > 45) and ( 8 / (x – 2 ) > 1)

    1. (false) or (y MOD x <> 0)

 

 

  1. What is the output of the following code segment (assume N and M type integer)

 

                N:= 1

                REPEAT

                     FOR M:= 2 DOWNTO 1 DO

                     Begin

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

End;
n := n + 1;

                UNTIL N = 1;

               Writeln;

 

a.        there is no output or none of the below

b.       2 1

c.        2 1 4 2

d.       1 2

e.        1 2 2 4

 

 

 

 

 

 

  1. The Boolean expression
                                                   X  in [‘a’, ‘e’ ‘y’]
    is equivalent to which of the following expressions:

 

a.        (X = ‘e’) and (X = ‘y’) and (X = ‘a’)

b.       (X = ‘a’) and (X = ‘e’) or (X = ‘y’)

c.        (X = ‘e’) or (X = ‘a’) and (X = ‘y’)

d.       (X = ‘e’) or (X = ‘y’) or (X = ‘a’)

    1. None of the above

 

 

  1. Which of the following statements is FALSE about comments in a program?

a.        they increase readability

b.       they are compiled to machine language by the compiler

c.        they provide program documentation

d.       all the above are true

e.        both b and c are false

 

 

  1. How many times is Howdy printed to the screen by the below code?

 

                PROGRAM Loop;

                VAR

                                check: Boolean;

                BEGIN

                                Check := False;

                                REPEAT

                                                Writeln(‘Howdy’);

                                                check := NOT( Check);

                                UNTIL  NOT( check );

                END.

 

a.        0

b.       1

c.        2

d.       infinite loop

    1. error

 

  1. What is the output of the below code?

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

 

                number : = –3;

                CASE number OF

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

                                4     : Writeln(‘second’);

                                5     : Writeln(‘third’);

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

                ELSE

                                Writeln(‘odd’);

 

a.        first

b.       second

c.        third

d.       fourth

    1. odd

 

  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

    1. None of the above

 

 

  1. An if-then-else statement is

 

    1. an unconditional branching statement
    2. a branching statement
    3. a looping statement
    4. an assignment statement
    5. none of the above

 

 

  1. Given the below code, what is(are) the NAME(s) of the file(s) opened for reading from ?
     Assume the code is embedded in a working program with no syntax errors.

assign(file1, 'data3.txt');

rewrite(file1);

assign(file2, 'data1.txt');

reset(file2);

assign(file3, 'data2.txt);

append(file3);

 

    1. data1.txt
    2. data2.txt
    3. data3.txt
    4. data1.txt and data3.txt
    5. data1.txt and data2.txt

 

  1. Which of the following may be used in the body of a procedure?
    1. global variables
    2. parameter variables (of the procedure)
    3. local variables (of the procedure)
    4. both a and c
    5. all of the above

 

 

 

 

 

 

 

 

 

 

 

 

  1. What is the output of the following program?
    Assume no syntax errors.

 

program SamIAm;

VAR

   x, y : integer;

 

procedure GreenEggs(x, y : integer);

BEGIN

writeln(x, ‘  ‘, y, ‘  ‘);   { There is a space between the single quotes }

END;

 

BEGIN                         { main  program }

x:= 2;

y := 1;

GreenEggs ( x,  y );

GreenEggs ( y,  x );

END.

 

a.        2 1 1 2

b.       1 2 2 1

c.        2 1
2 1

d.       2 1
1 2

e.        1 2
2 1

 

 

  1. What is the decimal (base ten) representation of the binary number:   00101 ?

a.        5

b.       6

c.        7

d.       11

e.        3

 

 

  1. In Pascal, procedure declarations are placed:

a.        after the main body of the program

b.       after the variable declarations and before the main body

c.        within the main body

d.       before the (global) variable declarations

e.        anywhere you like

 

 

  1. Which of the following is NOT a valid variable name?

a.        BobHope  

b.       howdy

c.        ghj723

d.       div

e.        lark

 

 

 

 

 

  1. X is defined as type real. It has been assigned the value 4.1876.
    Which of the following will output 4.188 ?
    1. write( TRUNC ( x * 1000) / 1000 : 5 : 3);
    2. write( x : 5 : 4);
    3. write( ROUND ( x * 1000) / 1000 : 5 : 3);
    4. write( TRUNC(x):5 );
    5. write( RND(x) : 5 : 3 );

 

  1. Which of the below data types can NOT be used in a case statement?
    1. char
    2. integer
    3. Boolean
    4. string
    5. all of the above can be used in a case statement

 

  1. Given that a variable of data type integer is made up of two bytes (16 bits) and 216 = 65536
    and based on the values given to integer variables in class and in homework assignments
    and in the book…
    The likely range of values an integer variable may hold is:
    1. Integer Numbers between 0 and 65636.
    2. Integer Numbers from –256 to 255
    3. Integer Numbers between –65536 and 65535
    4. Integer Numbers between –32768 and 32767
    5. All real numbers from negative infinity to infinity.

 

  1. rand(100) will return numbers in what range (assume placed in a working program and no syntax errors)?
    1. 0 to 99
    2. 1 to 100
    3. 5 to 1 billion
    4. it always returns 65 – no chance of anything else
    5. –100 to 100

 

The following question refers to the below code (assume working and no syntax errors):

PROGRAM counting;

VAR

 i, j: integer;

 

BEGIN

     for i := 0 to 2 do

     begin

          for  j := 0 to 2 do

          Begin

               writeln('came here');

          End

     end

END.

 

  1. How many times will the above program print the line “came here”?
    1. 2
    2. 4
    3. 6
    4. 8
    5. who do you apprec… urr… I mean none of the above.

 

Could it be? Is it possible? Why yes, I think it is… this is the end of the test!!!!!!!!!