First Name _______________________
MUSIC Code HU____________
McGill ID _______________
McGill University
Computers in Engineering
308-208B
Final Exam
Wednesday April 21st, 1999
9.00 am - 12.00 noon.
Examiner: Prof. G. Ratzer
Associate Examiners: C. K. Kim, S. Li
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, which must be returned. This exam consists of 13 pages.
QUESTION 1.
What is the output of this FORTRAN 90 program:
program question1
implicit none
integer :: I,J,K
real :: A,B,C
A = 2.1E1 + 4
B = 37/7
C = 37.0/7.0
I = 2.2 + MOD(78,5)
J = 89/4*4+2-1*2
K = C
PRINT *,I,J,K,A,B,C
stop
end program question1
1) 6 5 5.0000 25.0000 5 5
2) 5 88 5 25.0000 5.00000 5.28571
3) 6 5 5.0000 25.0000 5 5.28571
4) 5 5 5 6.10000 5.00000 5.28571
5) 6 88 5 25.0000 5.00000 5.28571
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 :: x,term, s
read (*,*) x
read (*,*) nterm
s = 1
term = 1
do i=1, nterm
term = term * i * 1.0 / x
s = s + term
end do
write (*,*) s
stop
end program question3
What does the above FORTRAN 90 program calculate?
(where ! is the symbol for FACTORIAL):
1) 1 + 1!/x + 2!/(x**2) + 3!/(x**3) + 4!/(x**4) + .....
2) 1 + 1/x + 2/(x*2) + 3/(x*3) + 4/(x*4) + ....
3) 1 + x/1! + x/2! + x/3! + x/4! + ....
4) x**n/x!
5) None of the above
QUESTION 4.
PROGRAM HALF_ADDER
IMPLICIT NONE
LOGICAL :: A, B, SUM, CARRY
PRINT *, "ENTER LOGICAL INPUTS A AND B: "
READ *, A, B
SUM = (A .OR. B) .AND. .NOT. (A .AND. B)
CARRY = A .AND. B
PRINT *, "CARRY, SUM =", CARRY,SUM
STOP
END PROGRAM HALF_ADDER
For the above FORTRAN 90 program and the entered input values shown, which is the INCORRECT output.
1) ENTER LOGICAL INPUTS A AND B:
T T
CARRY, SUM = T F
2) ENTER LOGICAL INPUTS A AND B:
T F
CARRY, SUM = F T
3) ENTER LOGICAL INPUTS A AND B:
F T
CARRY, SUM = F T
4) ENTER LOGICAL INPUTS A AND B:
F F
CARRY, SUM = F F
5) NONE OF THE ABOVE
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 C program?
#include<math.h>
#include<stdio.h>
#define N 15
main()
{
int i,j,line,a[N];
for (i=2;i<N;i++) a[i]=i;
for (i=2;i<sqrt(N);i++)
for (j=i+1;j<N;j++)
{
if (a[i]!=0&&a[j]!=0)
if (a[j]%a[i]==0)
a[j]=0;
}
printf("\n");
for (i=2,line=0;i<N;i++)
{
if (a[i]!=0)
{printf("%5d",a[i]);
line++;}
if (line==10)
{printf("\n");
line=0;}
}
}
1) 1 3 5 7 9 11
2) 2 4 6 8 10 12
3) 2 3 5 7 9 11
4) 2 3 5 7 11 13
5) 2 3 5 8 13 15
QUESTION 7.
Consider the following C program.
Given the input string: abcdef
what is the output?
#include<stdio.h>
main()
{
char str[100];
printf("input string:\n");
scanf("%s",str);
x(str);
printf("%s\n",str);
}
x(str)
char str[];
{
char t;
int i;
int j;
for (i=0,j=strlen(str);i<strlen(str)/2;i++,j--)
{
t=str[i];
str[i]=str[j-1];
str[j-1]=t;
}
}
1)abcdef
2)bcdefa
3)fedcba
4)cdefba
5)defcba
QUESTION 8.
What is the output of the following C program?
#include <stdio.h>
char input[] = "SSSWILTECH1\1W\1WALLMP1";
main()
{
int i,c;
for( i=1; (c=input[i]) !='\0'; i++) {
switch(c) {
case 'a': putchar('i'); continue;
case '1': break;
case 1 : while( (c=input[++i]) !='\1' && c !='\0' );
case 9 : putchar('S');
case 'E': case 'L': continue;
default: putchar(c); continue;
}
putchar(' ');
}
putchar('\n'); return;
}
1) SSWITCH SWAMP
2) SSWITCH WWAMP
3) SSWITCH WAMP
4) SWITCH WWAMP
5) None of the above
QUESTION 9.
What will the following C program print?
#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];
}
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
QUESTION 10.
What will the following C program print?
#include<stdio.h>
#define N 5
main()
{
int i,j,a[N][N];
for (i=1;i<N;i++)
{ a[i][i]=1;
a[i][1]=1;
}
for (i=3;i<N;i++)
for (j=2;j<=i-1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for (i=1;i<N;i++)
{for (j=1;j<=i;j++)
printf("%6d",a[i][j]);
printf("\n");
}
printf("\n");
}
1)1
1 1
1 2 1
1 2 2 1
2)1
1 2
1 3 1
1 4 4 4
3)1
1 2
1 3 1
1 4 4 1
4)1
1 1
1 2 1
1 3 3 1
5)None of above
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.
Given input data GA,D,A,G,L,A2,A1,A3,A4,Z,ZA,E in order -
fill up the following hash table. When a new identifier gets hashed into a full bucket, we use the simplest solution to find the next closest unfilled bucket. Assume the hash table is used circularly. The Hash function f(x)= order of alphabet of the 1st character of x – that is it converts the first letter of the input from A-Z to 1-26.
-----------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 - 26
-----------------------------------------------
1) A A2 A1 D A4 A3 GA G E L Z ZA
-----------------------------------------------
2) A A1 A2 A3 A4 D G GA E L Z ZA
-----------------------------------------------
3) A A1 A2 D A4 A3 G GA ZA E L Z
-----------------------------------------------
4) A A2 A1 D A3 A4 GA G ZA E L Z
-----------------------------------------------
5) None of the above
QUESTION 13.
Which of the following triangularized matrices represent a
solution of this system of linear equations?
x + 2y + z = 9
2x + 3y - 2z = 7
4x + 4y + z = 18
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 3
0 0 1 2
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" program which tests the first thousand integers (from 1 to 1,000 inclusive) to see if they are PERFECT numbers. A PERFECT number is one, where its factors add up to the number.
Your program should count and print how many perfect numbers are in the range 1 to 1,000 and add them up. Your output should look like this:-
__________________________________________________________________
Program to count and total the PERFECT numbers between 1 and 1,000
Perfect number 1 is 1 – factors are – 1
Perfect number 2 is 6 – factors are – 1 2 3
Perfect number 3 is 28 – factors are – 1 2 4 7 14
. . . .
. . . .
There are ?? perfect numbers and their total is ?????
__________________________________________________________________
Write your "C" program below and on the back of the page.
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: