You are on page 1of 4

Practice Exam 2

Name:_________________________ Section:______

EXAM 2
CS 1001
D Term 1995

1). (6 points)_____

Rewrite the following WHILE loop as a DO loop.

sum = m
next = -1
DO WHILE (next .LE. m)
sum = sum + next
next = next + 2
ENDDO

ANSWER: sum = m
DO 5 next = -1, m, 2
sum = sum + next
5 CONTINUE

2). (9 points)_____

Rewrite the following DO loop as a WHILE loop.

DO 30 M = J, K, -L
J=M*2
print *, J
30 CONTINUE

ANSWER: M = J
DO WHILE (M .GE. K)
J=M*2
print *, J
M=M-L
ENDDO

3). (12 points)_____

What is printed by the following code segment if all variables


are of type INTEGER?

k=0
m=0
p=0
DO WHILE (p.LT.10)
k=k+1
m=m+p
print *, p, k, m
p=p+k
ENDDO
ANSWER:

0 1 0
1 2 1
3 3 4
6 4 10

4). (10 points)_____

Given the following code segment:

INTEGER I
REAL X, Y
CHARACTER *4 Alpha, Beta

I = 27
X = 142.7883
Y = 64.7
Alpha = 'ABCD'
Beta = 'WXYZ'
print 27, I, X, Y, Alpha, Beta

Write a FORMAT statement to go with the given print statement


so that the following output is generated (blanks are
indicated by the #'s).

##27142.79##65.ABCDWXY

ANSWER:

27 FORMAT (I4, F6.2, 2x, F3.0, A, A3)

5). (18 points)_____

For the read statement:

read 29, Alpha, X, I

What data values are stored for the following FORMAT


statements when Alpha is of type CHARACTER *4, X is of type
REAL, and I is of type INTEGER? For each part, assume the
user types in the following data line, beginning in column 1.

Test2413/1995

a). 29 FORMAT (A5, 1x, F2.1, 1x, I3)

Alpha = est2 X = 1.3 I = 199

b). 29 FORMAT (A3, 1x, F4.2, 3x, I1)

Alpha = Tes# X = 24.13 I=9


^
|
blank space
Questions 6 - 7 are multiple choice. Circle the letter
corresponding to the choice that best answers the question.
(5 points each)

6). In a subroutine that gets a value from the keyboard and


communicates that value to the main program via an argument,
the argument used is considered:

a). an input argument


--> b). an output argument
c). an input/output argument
d). a local variable
e). an actual argument

7). In a subroutine that receives a value from the main program


via an argument and then displays the argument's value on the
screen, that argument is considered:

--> a). an input argument


b). an output argument
c). an input/output argument
d). a local variable
e). none of the above

8). (15 points)_____


What will be printed by the following program?

PROGRAM Mixup
INTEGER J, L, M
M=4
CALL Sub (M, L, 5)
print *, M, L
CALL Sub (L, J, M)
print *, L, J, M
STOP
END

SUBROUTINE Sub (L, M, J)


INTEGER J, L, M
IF (L .EQ. 3) THEN
M=6
J=4
ELSE
M=J+3
L=L+1
ENDIF
RETURN
END

ANSWER:

5 8
9 8 5

9). (10 points)_____

Write a Fortran function of type LOGICAL called IsADigit


defined by the following PRE and POST-conditions:

PRE: ch is of type CHARACTER *1 and is assigned


POST: IsADigit returns .TRUE. if ch is one of the digit
characters '0' through '9'; otherwise, it returns
.FALSE.

ANSWER:
LOGICAL FUNCTION IsADigit (ch)
CHARACTER *1 ch

IF (ch .ge. '0' .and. ch .le. '9') THEN


IsADigit = .TRUE.
ELSE
IsADigit = .FALSE.
ENDIF

RETURN
END

10). (10 points)_____

Write a Fortran subroutine called Rectangle defined by the


following PRE- and POST-conditions:

PRE: length and width are assigned, positive, REAL values;


printit is an assigned LOGICAL value.
POST: area is the area of the rectangle with sides length and
width; perimeter is the perimeter of the rectangle with
sides length and width; if printit is .TRUE., the values
for area and perimeter are displayed on the terminal.

ANSWER:

SUBROUTINE Rectangle (length, width, printit)


REAL length, width, area, perimeter
LOGICAL printit

area = length * width


perimeter = 2 * length + 2 * width

IF (printit) THEN
print *, 'area = ', area, 'perimeter = ', perimeter
ENDIF

RETURN
END

You might also like