This is a closed-book 68-point exam. Write your answers in the bluebook.
If you want to put an answer on the exam itself, say so in the
bluebook and put your name on the exam. Explain your answers clearly.
- (10 points) What output does the following program produce?
You may assume that it compiles and runs (it does--I tried it).
program something(output);
var a, b : integer;
procedure anything( a : integer );
begin
a := 5
end;
procedure nothing( var c : integer; d : integer );
begin
c := 6;
d := 7
end;
procedure everything( var e, f : integer );
var temp : integer;
begin
temp := e;
e := e + f;
f := temp
end;
begin
a := 1;
b := 2;
anything( b );
writeln( 'First: ', a, b );
a := 1;
b := 2;
nothing( a, b );
writeln( 'Second: ', a, b );
a := 1;
b := 2;
everything( a, b );
everything( b, a );
writeln( 'Third: ', a, b )
end.
- (10 points) Write a function that takes a character as input
from the calling routine
and returns true if the character is a digit
('0', '1',...,'9'), and false otherwise.
Note that this function should contain no read,
write, readln, or writeln
calls.
- (10 points) Write -25 and 18 in eight-bit two's complement notation.
What base ten number does the eight-bit two's complement
number 10010011 represent?
- (10 points) A few questions about function/procedure parameters.
- What's the difference between variable and value parameters?
- Give one reason why you would want to use a variable
parameter rather than a value parameter. Illustrate with
a realistic (small) example. (You don't need to write code
for this--just explain a real situation in which a variable
parameter would be useful.)
- What's the difference between actual and formal parameters?
- (8 points) Each of the following code fragments prints out an inexplicable
message some number of times. How many times
does each message get printed?
for i := 1 to 10 do
for j := 2 to 7 do
writeln( 'Shrews choose blue shoes' );
for i := 1 to 10 do
for j := i to 10 do
writeln( 'Ticks pick six sticks' );
- (6 points) Program errors can be classified as "compile-time" errors
or "run-time" errors, depending on when the errors are first evident.
Give an example of a program error that the compiler will
catch (that is, a compile-time error). Give an example of a program
error that the compiler will not catch, but that will appear
when you run the compiled program.
- (2 points) I'm going to Philadelphia for a conference next week.
What should I read on the plane?
- (12 points) The song "99 bottles of beer on the wall," designed especially
for the torture of parents, has 99 verses, starting with
99 bottles of beer on the wall
99 bottles of beer
Take one down and pass it around
98 bottles of beer on the wall
and ending with
1 bottle of beer on the wall
1 bottle of beer
Take one down and pass it around
0 bottles of beer on the wall
Write a procedure that will print out the lyrics to
the song "N bottles of beer on the wall," where N is the
procedure's input parameter. (The procedure will start
with "procedure Beer( N : integer );".)