ROSENTHAL

Name

A22.0002 Departmental Final Exam SPRING 1994

The following table is for the instructors use only

All your work is to be done on the exam. Be sure to write your name on each page of the exam.

1. (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop is invalid, write invalid and give the reason.

```_____	a. FOR J:= 0 TO 0 DO

_____	b. FOR J:=  1 DOWNTO 5 DO

_____   c. FOR CH:= '#' TO 7 DO

_____   d. FOR J:=  '1' TO '5' DO

_____   e. FOR CH:= 5 DOWNTO 3 DO
```

ROSENTHAL

Name

2. (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason.

```	a. FOR J:= 3 DOWNTO 1 DO BEGIN
FOR K:= 1 TO 3 DO BEGIN
write('*')
END;
writeln
END

b. FOR J:= 1 TO 3 DO  BEGIN
FOR K:= J TO 1 DO BEGIN
write('*')
END;
writeln
END

c. FOR J:= 4 TO 6 DO  BEGIN
FOR J:= 1 TO 5 DO BEGIN
write('*')
END;
writeln
END
```

3. (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type

```(a) True AND True
(b) CASE Character OF
'a': writeln('one');
'2': writeln('Two');
'3': writeln('Three')
END{CASE};
(c) One OR Two AND Three
(d) 5 > '4'
(e) if x:=4 then writeln('ow')
```

ROSENTHAL

Name

4. (5 points) Complete the program segment that adds the odd numbers from 1 to N, where N is read into the program. So if N is 8, then the program would add 1, 3, 5, 7.

```writeln('enter your number');

----------------

Sum := _____

FOR J:= 1 TO ______ DO

IF _______________________then

Sum:=_________________
```

5. (5 points) Complete the program segment that prints the value of the variable letter, if letter is a capital vowel. The vowels are (A, E, I, O, or U).

```IF letter_____________________then

_________________
```

ROSENTHAL

Name

6. (10 points) Complete the program segment that reads a group of numbers ending with a sentinel of -9 (a sentinel is the number or character that indicates the end of the data) and places the even numbers in an array called A. For instance, if the input were: 12 13 43 6 16 -9, the program would place 12 in A[1], 6 in A[2] and 16 in A[3].

```VAR j, number:integer;
A: ARRAY[1..100] OF integer;
BEGIN

j:=________
WHILE number <> -9 DO BEGIN
IF __________________________THEN
BEGIN
j:=________
A[____]:= Number;
________________
END
END
```

7. (10 points) What does the following program produce?

```program pattern;
const star = '*';
VAR j:integer;
BEGIN
for j:= 4 downto 1 do
writeln(star:j, star:10 - (2 * j) );
for j:= 1 to 4 do
writeln(star:j, star:10 - (2 * j) );
END.
```

ROSENTHAL

8. (10 points) Given the following procedures:

```procedure star(number:integer);
var j:integer;
begin
for j:= 1 to number Do
write('*');
writeln
end;

procedure plus(number:integer);
var j:integer;
begin
for j:= 1 to number Do
write('+');
writeln
end;
```

and your goal is to produce the following pattern:

```****
++++
***
+++
**
++
```

Complete the program that produces the pattern:

```	 for j:=__________________DO
begin

________

________
end
```

ROSENTHAL

Name

9. (10 points) Given the following heading: procedure pattern(number: integer; symbol:char);

complete the procedure that when activated by, for instance, pattern(5, '*'), would produce
*****
and when activated by, for instance, pattern(3, '-') would produce ---

```procedure pattern(number:integer; symbol:char);
VAR j:integer;
BEGIN
__________________
BEGIN

________________
END
END;
```

10. (20 points) DO EITHER A OR B

A. Write a program that reads a sentence at least four characters long into an array of characters and prints every thing but the last three characters. The sentence must end with an asterisk. Thus if the sentence was hello there*, the program would print:

hello the as output. In other words, the re* would be stored in the array but would not be printed.

B. Write a program that reads a sentence that ends with a period, for instance, hello there. and prints the sentence with each letter e replaced with an asterisk. Thus in our example the program would print h*llo th*r*.