CS 0007 Chapters 1-5 Quiz Name: _________________________
10/11/99
Part 1: Program Segment Questions
For this section, you will be asked to write program segments. Do not write an entire program. Try to write as legibly as possible. Be careful with spacing an indenting. The two segments are worth 25 points each.
Hints:
Enter an integer: 2
Enter an integer: 4
Enter an integer: 10
Enter an integer: 1
…and return 17 through the second parameter.
Part 2: Scope
Local variables are variables declared within a scope. Global variables are variables declared outside a scope. Parameters in functions and procedures act as local variables with respect to scope. Use the following program to answer the questions in this part. Each of the questions is worth 5 points.
program scope (intput,output);
var
a,d: integer;
function addemup (x,y:integer):integer;
var
stop:boolean;
selection:char;
z:integer;
begin {addemup}
code
end; {addemup}
procedure menu;
var
choice:char;
procedure getinput (var option:char);
var b,c:integer;
begin {getinput}
code
end; {getinput}
begin {menu}
code
end; {menu}
begin {scope}
code
end. {scope}
Part 3: Parameters
Determine the output of the following program (15 points):
program blah (input,output);
var
x,y,z:integer;
procedure Silly (var x,y:integer);
var
z:integer;
begin {Silly}
z:=x;
x:=y;
y:=z;
writeln (x,y,z);
end; {Silly}
begin {blah}
x:=1; y:=3; z:=2;
writeln (x,y,z);
Silly (x,z);
writeln (y,z,x);
end. {blah}
Part 4: Debugging
Determine if there are any bugs in the following program. If there is, circle the word/symbol/statement/construct that is in error and label it with a number. Then at the bottom of this page, show your labels and write the corresponding reason for the statement being in error. This question is worth 10 points.
program myproggie (input,output);
var
x,y,sum,z:real;
a,b,c:integer;
function add(num1,num2:real):integer;
begin {add}
add:=num1+num2;
end; {add}
begin {myproggie}
write (‘Enter a real number: ‘);
readln (x);
write (‘Enter another real number: ‘);
readln (y);
sum:=add(x,y);
write (‘Enter two integers: ‘);
readln (a,b);
c:=a+b/2;
writeln (‘The average is ‘c);
if 12 > a+b then do
writeln (‘Foo’);
else
z:=12;
end; {myproggie}