First Name _______________________
MUSIC Code HC____________
McGill ID _______________
McGill University
Computers in Engineering
308-208A
Final Exam
Monday December 15th, 1997
2.00 - 5.00 p.m.
Faculty CALCULATORS are allowed. This is a multiple-choice exam to be answered using the red and white sheets for questions 1 to 16. Select what you consider to be the BEST answer of the answers provided for each of these questions. Questions 17 and 18 are to be written on this exam paper.
QUESTION 1.
program question1
implicit none
integer :: I,J
real :: X,Y,Z, P, Q, R
!
I = 3
J = 4
X = 5.2
Y = -2.0
Z = 0.2
P = Z + X * Y
Q = P + 2.0 * Y ** I
R = -Y * 3.0 / (J*1.0)
X = Q + R
Y = X * 2.0 - Z
Z = Z * R
J = 3.0 * Z / (I*1.0)
I = I + 2
write (*,*) I,J,X,Y,Z,P,Q,R
stop
end program question1
What is the output of the above FORTRAN 90 program:
1) 5 0 -24.7 -49.6 0.3 -10.2 -26.2 1.5
2) 3 0.0 -24.7 -49.6 0.3 -10 -26 1
3) 5 0.0 -24 -49 0 10.2 26.2 1.5
4) 3 0 -24.7 -49.6 0.3 -10.2 -26.2 1.5
5) 5 0 -24.7 -49.6 0.3 10.2 -26.2 1.5
QUESTION 2
What is the output of this FORTRAN 90 program?
program question2
implicit none
integer :: i, y(5)
real :: a(5),x(5)
do i=1, 5
x(i) = i+1
y(i) = i - 1 + MOD(i, 2)
end do
do i=1, 5
a(i) = x(y(i))
end do
write (*,*) (a(i),i=1,5)
do i=1, 5
a(y(i)) = x(i)
end do
write (*,*) (a(i),i=1,5)
stop
end program question2
1) 3. 2. 4. 4. 6.
2. 5. 4. 3. 6.
2) 3. 2. 4. 4. 6.
2. 2. 5. 4. 6.
3) 2. 2. 4. 4. 6.
3. 2. 5. 4. 6.
4) 3. 2. 3. 2. 3.
5. 6. 3. 2. 3.
5) 2. 4. 4. 2. 6.
3. 2. 4. 5. 6.
QUESTION 3.
program question3
implicit none
integer :: i, nterm
real :: n,term, s
read (*,*) n
read (*,*) nterm
s = 1
term = 1
do i=1, nterm
term = term * i * 1.0 / n
s = s + term
end do
write (*,*) s
stop
end program question3
What does the program calculate?
(where ! is the symbol for FACTORIAL):
1) 1 + 1!/n + 2!/(n**2) + 3!/(n**3) + 4!/(n**4) + .....
2) 1 + 1/n + 2/(n*2) + 3/(n*3) + 4/(n*4) + ....
3) 1 + n/1! + n/2! + n/3! + n/4! + ....
4) n**n/n!
5) None of the above
QUESTION 4.
PROGRAM QUESTION4
IMPLICIT NONE
INTEGER :: I
INTEGER :: M1(10), M2(10), M3(10)
READ (*,*)(M1(I),I=1,5)
READ (*,*)(M2(I),I=1,5)
READ (*,*)(M3(I),I=1,5)
DO I=1,5
IF( (I < 2) .AND. (M1(I) < M3(I))) THEN
WRITE (*,*) M1(I)
ELSE IF ( (M1(I) == M2(I)) .OR. (M1(I) == M3(I))) THEN
WRITE (*,*) M2(I)
ELSE IF (I == 5) THEN
WRITE (*,*) M3(I)
END IF
WRITE (*,*) 's'
END DO
STOP
END PROGRAM QUESTION4
If the input data is:-
1 9 0 5 9
3 5 7 2 1
0 6 0 4 9
The output of the above program (all shown on one line) with
the input data given is:
1) s 7 s 2 2 s s
2) s s 7 s s 1 s
3) s 1 s s 7 s s
4) s s 7 s 2 1 s
5) s 7 s 1 s s s
QUESTION 5.
What will the following program print?
Program Question5
implicit none
real :: I, J, K, L
integer :: A, B, C
interface ! You can ignore this
subroutine THIS (U, V) ! interface section
implicit none
integer, intent(in out) :: U
integer, intent(in) :: V
end subroutine THIS
subroutine THAT ( X,Y )
implicit none
real, intent(in out) :: X
real, intent(in) :: Y
end subroutine THAT
end interface ! ELF90 wants it
!
! Program begins
!
I=12
A=7
B = 27
C = 4
K = 4.0
J = 4.0
L = 0.0
CALL THIS(B,C)
CALL THAT(J,K)
CALL THAT(L,3.0)
WRITE (*,*)A,B,C
WRITE (*,*)I,J,K,L
Stop
End Program Question5
!
SUBROUTINE THIS (U,V)
implicit none
integer, intent(in out) :: U
integer, intent(in) :: V
integer :: A
U = V**2
A = A+U+V
RETURN
END SUBROUTINE THIS
SUBROUTINE THAT (X,Y)
implicit none
real, intent(in out) :: X
real, intent(in) :: Y
real :: I
X= Y**3
I= I+X+Y
RETURN
END SUBROUTINE THAT
1) 7 16 4
12.0 64.0 4.0 27.0
2) 27 16 4
12.0 4.0 64.0 27.0
3) 7 27 4
12.0 64.0 4.0 27.0
4) 7 16 64
12.0 64.0 4.0 27.0
5) None of the above
QUESTION 6.
What is the output of the following program?
#include<stdio.h>
main()
{
int x = 10;
float j=36.6;
float k=26.6;
float y=3.2;
j = x += y;
x = k -= j;
printf("x=%d y=%f j=%d k=%f \n",x,y,j,k);
}
1) x=13 y=3.2 j=13 k=13.6
2) x=13.4 y=3.2 j=13.2 k=0.4
3) x=13 y=23.4 j=13.2 k=10
4) x=10 y=13.2 j=23.2 k=13.6
5) x=13.2 y=10.2 j=13 k=13.2
QUESTION 7.
What is the output of the following C program?
main()
{
int quack;
quack = 2;
quack += 5;
quack *= 10;
quack -= 6;
quack /= 8;
quack %= 3;
printf ("%d", quack);
}
1) 9
2) 170
3) 66
4) 2
5) 19
QUESTION 8.
What is the output of the following program?
#include<stdio.h>
int counter = 0 ;
void f() { ++counter; }
void g()
{
f();
f();
}
void h()
{
f();
g();
f();
}
int main()
{
f();
printf("%d ", counter);
g();
printf("%d ", counter);
h();
printf("%d ", counter);
return 0;
}
1) 0 1 2
2) 1 2 3
3) 1 4 5
4) 1 3 7
5) 0 2 3
QUESTION 9.
What will the following program print?
1) 11 12 13 14 15
2) 10 8 13 14 3 2
3) 10 11 8 13 7 3
4) 10 11 9 12 8 5
5) None of the above
#include<stdio.h>
#define N 6
#define M 8
int function1(int);
void main(void){
int m[N]={5,6,3,8,2,1};
int i,j;
for(i=0,j=2;i<=5;i++,j--){
if(m[i] < 5 && j < 0)
m[i] = m[i] + 5;
else
m[i] += 5;
if(m[i] <= M-1)
m[i] -= function1(*(m+i));
printf("%d ",m[i]);
}
}
int function1(int x){
int m[M]={9,5,3,7,1,3,3,0};
return m[x];
}
QUESTION 10.
What will the following program print?
1) SPARTAK!!JUVENTUS!SPARTAK!!JUVENTUS!
2) SPARTAK!!JUVENTUS!!SUTNEVUJSPARTAK!
3) SPARTAK!!JUVENTUS!JUVENTUS!SPARTAK!!
4) JUVENTUS!SPARTAK!!SUTNEVUJSPARTAK!!
5) None of the above
#include<stdio.h>
#define SIZE 9
void function1(char *, char *);
void main(void){
char team1[SIZE+1];
char team2[SIZE+1];
strcpy(team1,"SPARTAK!!");
strcpy(team2,"JUVENTUS!");
printf("%s%s",team1,team2);
function1(team1,team2);
printf("%s%s\n",team1,team2);
}
void function1(char *str1, char *str2){
char str3[SIZE+1];
int i;
for(i=0;i<SIZE-1;i++)
str3[i] = str1[i];
str3[i] = '\0';
for(i=0;i<=SIZE-1;i++)
str1[i] = str2[SIZE-1-i];
str1[i] = '\0';
for(i=0;i<SIZE-1;i++)
str2[i] = str3[i];
str2[i] = '\0';
}
QUESTION 11.
Using the Newton-Raphson method to find the approximation to the root of a function F(x), if the initial value of x is 2 and
F(x) = 2(x*x*x)+3(x*x)+152, what is the value of x after 1 iteration?
1) x = 108
2) x = -3
3) x = 36
4) x = -4
5) None of the above.
QUESTION 12.
Using Selection Sort to sort the following array in ascending order: 36 69 51 82 65 71 27 12
After two passes the array would be:
1) 36 51 69 82 65 71 27 12
2) 12 69 51 82 65 71 27 36
3) 12 27 36 71 65 82 69 51
4) 12 27 51 82 65 71 69 36
5) None of the above
QUESTION 13.
Which of the following triangularized matrices represent a solution of this system of linear equations?
1 x + 2 y + 3 z = 9
2 x - y + z = 8
3 x + y - z = 2
1) 1 0 0 2
0 1 0 0
0 0 1 1
2) 1 0 1 1
0 1 0 2
0 0 1 1
3) 1 0 0 2
0 1 0 -1
0 0 1 3
4) 1 0 0 -1
0 1 0 2
0 0 1 3
5) 1 0 0 1
0 1 1 2
0 0 1 1
QUESTION 14.
Which of the following Statements are TRUE.
root almost as quickly as Newton-Raphson method) but requires
computation of the derivative f'(x) which may be costly.
but uses only n+1 evaluations of f(x) where n is the number of
panels used in the approximation
The trapezoidal rule
PanelWidth = (XMax - XMin) / NumberPanels
Sum = f(XMin)/2
X = X_Min + PanelWidth
For k = 1 , NumberPanels
Sum = Sum + f(X)
X = X + PanelWidth
end of for loop
Sum = Sum - f(XMax)/2
Estimate = PanelWidth*Sum
d) Simpson's rule is usually a much better approximation than the
Midpoint, but not as good as Trapezoidal rule.
Select the "best" answer.
1) a and b
2) b and c
3) a, b and c
4) b, c and d
5) None of the above
QUESTION 15.
Consider the following statements:
a) The Runge-Kutta method to estimate a differential equation is
very much better than Euler's method.
b) This pseudo-code implements the Euler algorithm:
x <-- 0
y <-- y0
while ( x <= xf )
y <-- y + h*f(x)
x <-- x + h
c) Given the differential equation y' = 3*y + 2x - 1 on
the interval [0, 0.8], and 2.12 is the value of y at x=0.4
using Euler's method with h=0.2 and initial value of
y=1 (i.e. y(0) = 1 ).
Which of the above statement(s) is(are) false?
1) b only
2) a and b
3) a and c
4) a only
5) None of them
QUESTION 16.
On your IBM computer marked sheet fill in circle ONE = 1
(This is to prove that you are still awake and got this far in
the exam!!)
QUESTION 17.
Write a "C" function which prints first "n" Fibonacci numbers on certain interval [a,b], where n,a,b > 0 and a < b.
Eg. 1 2 3 5 8 13 21 34 ... (Fibonacci Numbers)
Assuming n= 3 and the interval [5,24]
your function will print 5 8 13.
Declare your function as void Fib(int, int, int);
Hint: You should generate the first 1,000 Fibonacci numbers in a loop and print only those which satisfy the answer. Don't worry if there are less then "n" Fibonacci numbers on the interval [a,b].
Write your "C" program on the back of the page.
Make your program as generic as possible.
QUESTION 18.
Write a FORTRAN 90 program
SUBROUTINE Quad(a,b,n,pointm,trap,simp)
which uses a single loop to find the Midpoint (pointm), the trapezoidal (trap), and Simpson's (simp) rules approximations to the integral of a function f(X) (supplied) from a to b using n intervals.
Note you only have to write the SUBROUTINE, not the main program or function f. Try to minimize the number of function evaluations.
Write your program neatly below or on the backs of pages: