Computer Science 117 Summer 2002, Session III
Short Quiz, July 25, 2002

The entire quiz is 45 points.
  1. Assume that M and N are integer variables with the values -3 and 5, respectively, X and Y are real variables with the values -3.57 and 4.78, respectively. Find the type and value of each expression. Do not request permission to use a calculator. It's not necessary, nor even helpful. [2 points each]
    1. NINT(X+Y)
      INTEGER, 1
    2. SQRT(Y) < N
      LOGICAL, true
    3. X > 0.0 .OR. Y > 0.0
      LOGICAL, true
    4. REAL(N/2)
      REAL, 2.0
    5. .NOT.((M > N) .AND. (X < Y)) .NEQV. ((M .LE. N) .AND. (X .GT. X))
      LOGICAL, true
  2. Write a logical expression to express the given condition. You may assume that X, Y are variables of type REAL, M, N are variables of type INTEGER, and P, Q are variables of type LOGICAL. [4 points for (d), 2 points for each other part]
    1. X is not larger than Y.
      .NOT. X > Y
    2. P but not Q.
      P .AND. .NOT. Q
    3. 0 <= M <= 10
      0 <= M .AND. M <= 10
    4. Either N is negative, or the square root of N is smaller than X. (Warning: evaluation of this expression must never result in a run-time error.)
      N < 0 .OR. SQRT(ABS(REAL(N))) < X
  3. Assume that N is a variable of type INTEGER, and consider the following block.
    IF (N < 5) THEN
    IF (N == 4) PRINT*, "Hello."
    ELSE IF (N == 3) THEN
    PRINT*, "Goodbye."
    END IF
    1. What is the output of the block if N is assigned the value of 4? [2 points]
      Hello.
    2. What is the output of the block if N is assigned the value of 3? [3 points]
      (There is no output in this case)
  4. Write an assignment statement that will set the logical variable Okay to true if the condition holds and false otherwise. You may assume that X, Y, and Z are variables of type REAL, M, and N are variables of type INTEGER, and OverFlow is a variable of type LOGICAL. [2 points each]
    1. X <= Y <= Z <= X + 1.
      Okay = X <= Y .AND. Y <= Z .AND. Z <= X + 1
    2. Either X or Y is no greater than Z.
      Okay = .NOT. X > Z .OR. .NOT. Y > Z
      alternatively:

      Okay = X <= Z .OR. Y <= Z
    3. N is positive and M is not larger than the square root of N.
      Okay = N > 0 .AND. .NOT. M > SQRT(ABS(REAL(N)))
    4. X is not larger than Y, and OverFlow is false.
      Okay = .NOT. X > Y .AND. .NOT. Overflow
    5. Okay is true and M is not 0.
      Okay = Okay .AND. M /= 0
  5. Write a block of Fortran 90 code which sets a variable Sum of type INTEGER equal to the sum of the squares of all odd positive integers which do not exceed the value of a variable N of type INTEGER. Use I as the index of your loop; you may assume that I is a variable of type INTEGER. Do not write any specification statements or any input or output statements. Your code should have exactly 4 lines. [5 points] SUM = 0
    DO I = 1,N,2
    SUM = SUM + I*I
    END DO

  6. Write a block of Fortran 90 code which sets a variable X of type REAL equal to the square root of the value of a variable N of type INTEGER, provided the value of N is positive. If the value of N is not positive, X should be set to 0. Do not write any specification statements or any input or output statements. There are different ways to do this problem, but you should definitely not write more than 5 lines. [5 points] IF (N > 0) THEN
    X = SQRT(REAL(N))
    ELSE
    X = 0.0
    END IF