CPSC 203 – 500 EXAM ONE
Fall 2005 (WITH KEY)
NO CALCULATOR IS ALLOWED!!
Full Name (Please Print):
UIN:
ScorePossible Points
Part One 30 pts
Part Two 25 pts
Part Three 20 pts
Part Four 25 pts
Total100 pts
PART ONE (30 points). Each question is worth 3 points. Circle the correct answer.
- Assuming n is declared as an INTEGER, what value is stored in n after the following Fortran 90 statement executes:
n = 9.0 / 2.0
a. 4b. 4.5c. 5.d. The statement is illegitimate
2. Which of the following is a valid variable name for Fortran 90?
a. I want to go home b. 2_Personc. Time2d. ABC$
3. Which of the following is an error for Fortran 90?
a. WRITE (*,*) ‘Man’’s best friend’
b. WRITE (*,*) “Man’s best friend”
c.WRITE (*,*) ‘”Man’’s best friend”’
d. WRITE (*,*) “‘Man”’s best friend’.
4. What values are stored in X and Y respectively after the following statements are executed?
INTEGER :: I, J, X
REAL :: Y
I = 9.6
J = 3
X = I ** J
Y = I / J
a. 729, 3.0b. 729, 3.2c. 884.736, 3.0d. 884.736, 3.2 e. Compile error
- What is the output to the screen if number equals to 0?
SELECT CASE(number)
CASE(:-1)
WRITE(*, *) “None sense”
CASE(1:20)
WRITE(*, *) “Much better”
CASE(33:)
WRITE(*, *) “Hot”
CASE DEFAULT
WRITE(*, *) “So cool”
END SELECT
a. None senseb. Much betterc. Hotd. So cool
6. Which of the following is true in Fortran?C
a. (B+C)D is a legal statement.
b. 1,000,000. is a valid real constant.
c. INTEGER and integer are treated the same.
d. a statement, A=B, is true when A and B are the same.
7. What is printed after the following statements are executed?A
REAL:: a = -10.3, b = 0.103, c = 103.456
write (*, 200) a, b, c
200 FORMAT (2F6.3, F8.3)
a. ******0.103 103.456
b. -10.300 0.103 103.456
c. *******0.103 103.4560
d. -10.3 .103 103.456
a. A b. B c. C d. D
8. Given the following statements, what is stored in result ? A
LOGICAL :: flag = .false., result
REAL :: x=2.0 , y=5.0
result = (x .LT. y) .AND. (.NOT. flag)
a. T b. F
c. 1 d. Compile error
9. What is the output by the following code?B
outer: Do i = 1, 3, 2
Do j = 1, 3
If (i == j) CYCLE outer
k = i * j
write(*,*) i, '*', j, '=', k
End Do
End Do outer
a. 1*3 = 3 b. 3*1 = 3 c. 2*1 = 2 d. 1* 1 = 1
2*3 = 6 3*2 = 6 2*3 = 6 3*3 = 9
10. What is output by the following code? Suppose Result is REAL. C
Result = 1 + 2./5 + 3/2
a. 2.9 b. 2 c. 2.4 d. 2.0 e. Compile error
PART TWO (25 points). Each question is worth 5 points.
11. Define ‘syntax error’ and when it is detected ?
Errors in Fortran grammar. It is detected during compile time
12. 19F16 (hexadecimal) is equivalent to 2 (base 2).
000110011111 2
or
110011111 2
13. What is output by the following statements?
character(len = 20) :: a
character(len=15) :: b, c
a = 'hello 203 class'
b = 'Hi, world '
c = a(1:6) // b(5:9)
write(*,*) c
hello world
14. Write Fortran statements that calculate and print the summation of the Base 10 logarithms of even integers between 1 and 1000, and display the result. Hint: You must use a DOloop, theintrinsic function LOG10 andreal(value) to change an integer type value to a real type value.
INTEGER :: index, sum=0
DO index = 2, 1000, 2
sum = sum + LOG10(real(index))
END DO
WRITE (*,*) sum
15. The followed code explains how to grade students. Write a Fortran program with SELECT and CASE statements instead of IF THEN ELSE statements. Assume that the number of the grade is INTEGER.
IF( grade > 90) THEN
WRITE (*,*) ‘The grade is A’
ELSE IF (grade > 80) THEN
WRITE (*,*) ‘The grade is B’
ELSE IF ( grade > 70 ) THEN
WRITE (*,*) ‘The grade is C’
ELSE IF ( grade > 60 ) THEN
WRITE (*,*) ‘The grade is D’
ELSE
WRITE(*,*) ‘The grade is F’
END IF
select case (x)
case (91:100)
write(*,*) "The grade is A"
case (81:90)
write(*,*) "The grade is B"
case (71:80)
write(*,*) "The grade is C"
case (61:70)
write(*,*) "The grade is D"
case default
write(*,*) "The grade is F"
end select
PART THREE (20 points).
16. Write a complete Fortran program that receives an input value x (integer type) and calculate and print from the following equation.
PROGRAM EXAM_1
INTEGER :: x,y
WRITE (*,*) "Enter your number for multiplication"
READ (*,*) x
if ( x > 5) then
y = (-3)*x**2 / 2 + 5
ELSE if ( (x <= 5) .AND. (x > 0)) then
y = 3*x**3/2 + 5
else
y = 3*x**2 + 5
END IF
WRITE(*,*) "y = ",y
END PROGRAM EXAM_1
PART FOUR (25 points).
17. Write a complete Fortran program to calculate factorial of value N and display the result. The factorial function is defined as
N! = 1N=0
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1N >0
for any user-specified value of N. Write the program with anIterative (or counting) loop for calculation of factorial. Also use a While loop so that the program repeats the calculation for each legal value of input N (0 or positive integers) entered into the program.
PROGRAM factorial_cal
IMPLICIT NONE
INTEGER:: n, i,factorial
Do
WRITE(*,*) 'Please enter a number n:'
READ(*,*)n
factorial = 1
IF(n < 0) EXIT
DO i = 1, n
factorial = factorial*i
END DO
WRITE(*,*) 'Factorial = ', factorial
END Do
END PROGRAM factorial_cal
1