Você está na página 1de 4

Practice Exam 1 With Answers

Name:_______________________ Section:_____

EXAM 1
CS 1001
D-Term 1995

1). (20 points)_____

Write a complete Fortran program that reads in two input


values. The first value read in is a real number, R, and
represents the radius of a circle. The second value read in
is a single alphanumeric character, CHAR, and is used to
determine if the program should calculate and output the
circumference of the circle with radius R, the area of the
circle with radius R, or the diameter of the circle with
radius R. (If the user types 'C' for CHAR, calculate the
circumference; if the user types 'A', calculate the area; if
the user types 'D', calculate the diameter). You do not have
to comment the program. The program does not have to recover
from errors in user input. Use the value 3.14159 for pi.

Formulae: circumference = 2 * pi * radius

area = pi * radius2

diameter = 2 * radius

ANSWER:

program question1
real r, circum, area, diameter, pi
parameter (pi=3.14159)
character *1 char

print *, 'Enter radius'


read *, r
print *, 'Enter C, A, or D'
read *, char

if (char.eq.'C') THEN
circum = 2 * pi * r
print *, 'circumference = ', circum
else if (char .eq. 'A') THEN
area = pi * r**2
print *, 'area = ', area
else if (char .eq. 'D') THEN
diameter = 2 * r
print *, 'diameter = ', diameter
endif

stop
end

Questions 2 - 6 are multiple choice. Circle the letter that


corresponds to the best answer to each question.
2). (5 points) _____

Which of the following is NOT an advantage of a high-level


language?

a). it is easier to use than machine language


b). its statements resemble English
c). it is portable
d). memory can be referenced symbolically
ANS: e). it is easy for the machine to understand

3). (5 points) _____

Which of the following Fortran 77 constants has the value


5000.0?

a). 0.5E-4
b). 5.0E-4
c). 5.0E4
d). 5.0E-3
ANS: e). 5.0E3

4). (5 points) _____

Assume that the REAL variable Price is to be rounded to the


nearest hundreds place. For example, if Price is 89.3475
before rounding, then after rounding it should be 89.35.
Which of the following statements accomplishes this?

a). Price = REAL (INT (Price + 0.005))


b). Price = INT (Price * 100.0) / 100.0
c). Price = INT (Price + 0.5)
ANS: d). Price = REAL (INT ((Price + 0.005) * 100.0)) / 100.0
e). none of the above

5). (5 points) _____

Which of the following groups of statements performs the


following operation correctly: if x is greater than 100.0 or
is less than or equal to 0.0, print 'out'.

a). IF (x .GT. 100.0) THEN


IF (x .LE. 0.0) THEN
print *, 'out'
ENDIF
ENDIF

b). IF (x .LE. 0.0) THEN


IF (x .GT. 100.0) THEN
print *, 'out'
ENDIF
ENDIF

ANS: c). IF (x .GT. 100.0) THEN


print *, 'out'
ELSE IF (x .LE. 0.0) THEN
print *, 'out'
ENDIF

d). all of the above


e). none of the above

6). (5 points) _____

For what range of values of variable x does the following code


segment print the letter 'C'?

IF (x .LE. 200) THEN


IF (x .LT. 100) THEN
IF (x .LE. 0) THEN
print *, 'A'
ELSE
print *, 'B'
ENDIF
ELSE
print *, 'C'
ENDIF
ELSE
print *, 'D'
ENDIF

a). 0 < x < 100


b). x <= 0
ANS: c). 100 <= x <= 200
d). x > 200

7). (5 points) _____

Write a logical assignment statement to carry out the


following operation (make sure you write an assignment
statement and not an IF-statement).

assign a value of .TRUE. to EvenNumber if M is an even


number; otherwise assign a value of .FALSE. (Hint:
use the MOD function.)

ANSWER:
EvenNumber = MOD(M, 2) .eq. 0

8). (10 points) _____

Rewrite the following IF-structure as a single logical


assignment statement.

IF ((First .EQ. 'A') .OR. (Last .EQ. 'Z')) THEN

IF (Second .EQ. 'B') THEN


Answer = .TRUE.
ELSE
Answer = .FALSE.
ENDIF
ELSE
Answer = .TRUE.
ENDIF

ANSWER:
Answer = (First .EQ. 'A' .OR. Last .EQ. 'Z') .AND. Second .EQ. 'B'
+ .OR. .NOT. (First .EQ. 'A' .OR. Last .EQ. 'Z')

(other answers are possible)

9). (25 points) _____

What are the types and values of the following expressions?

EXPRESSION TYPE VALUE

'120' .GE. '62' Logical .FALSE.

15.5 + 6 / 4 Real 16.5

18/4 * 7/2 Integer 14

.NOT. ('z' .LT. 'a') .AND. (MOD(12,5) .GE. 2)

Logical .TRUE.

'Ham' .LT. 'Hamel' Logical .TRUE.

10). (15 points) _____

Write the complement of the following expression by applying


DeMorgan's Theorem. Choose operators that allow you to write
a valid complement that requires no parentheses.

x .GT. y .AND. z .LT. q .AND. .NOT. flag

ANSWER (This is really the correct answer!):

x .LE. y .OR. z .GE. q .OR. flag

Você também pode gostar