Date: Tue, 14 Jan 1997 20:08:44 GMT Server: NCSA/1.4.1 Content-type: text/html Last-modified: Wed, 20 Nov 1996 01:43:15 GMT Content-length: 3812
Midterm #2 A22.0002 Afternoon session
April 24, 1995 Samuel Marateck
Do any 5 problems and do all the work on the exam sheet
Your Name
1. (20 points points) What does the following program produce as output?
PROGRAM one(input, output); CONST Blank = ' '; VAR J, K:integer; BEGIN FOR j:= 1 TO 5 DO BEGIN write('*':j); FOR k:= 1 to (6 - j) DO BEGIN write(blank) END; writeln('*' END END.
2. (20 points) Write a program that reads a number N (an integer) from the keyboard and:
(a) Prints all the numbers less than or equal to N that are prime numbers. A prime number is one that is divisible only by 1 and the number itself. When a number is divisible by another number there is no remainder. For example, 7 is a prime number because it is not divisible by 2, 3, 4, 5, 6.
Hint: Test whether the values of the loop control variable, J are divisors of N. Here are some questions that should help you do the problem:
Is N read before or in the loop?
If j is the loop control
variable, what is its upper limit?
What is the test determining whether j is a divisor
of N?
3. (20 points) What does the following program produce?
PROGRAM two(input, output); VAR J:integer; letter:char; PROCEDURE plot(J:integer); VAR I:integer; BEGIN FOR I:= 1 TO J DO write('*':i); writeln END {plot}; BEGIN FOR letter:= 'a' TO 'e' DO BEGIN J:= ord(letter) - ord('a'); plot(j) END END.
4. (20 points) Evaluate the following (you may use the table on the board):
(a) (3 <= 7) AND NOT (9 = 8) (b) NOT (4 >= 5) OR (3 <= 3) (c) NOT ((5 <= 9) OR (3 < 2)) (d) (6 <= 8) OR (3 > 5) OR (3 = 3)
5. (20 points) What does the following procedure produce for plot(1,'x')?
Procedure plot ( top : Integer; ch:char); Var j : Integer; Begin Case top Of 1 : For j := 1 To 5 Do Begin Writeln (ch:j, ch:(11 - (2 * j))) End; 2 : For j := 5 DownTo 1 Do Begin Writeln (ch:j, ch:(11 - (2 * j))) End End End;
6. (20 points) Write a procedure called MakeX that produces the block letter X by using procedure plot shown in problem 5. The block letter should be formed from asterisks (*).