Friday 30 January 2015

Fortran Interview Questions and Answers for freshers and experienced pdf free download

21. What does "COMMON/CONTROL/ A, B, C" do to the values of A, B, C? 
It doesn't do anything to the values. It just establishes a place in memory for the values to be stored and retrieved. Every subroutine and function in your program that contains the line "COMMON/CONTROL/ A, B, C" will agree that operations using variables A, B, or C will get numbers from or put numbers in the same appropriate location in memory. That is, they agree that a reference to "A" means the same in all routines with common CONTROL, etc.. To give A, B, or C a value, you have two options. You can assign values at compilation time with a BLOCK DATA routine:
BLOCK DATA ABCVAL
COMMON/CONTROL/ A, B, C
DATA A,B,C / 1.0, 2.0, 3.0/
END
You can also assign and use values with executable statements. Say subroutines SUB1 and SUB2 contain this common block. If SUB1 contains the lines "A=1.0", and "B=2.0", and SUB2 has the line "C=A+B", then the value of C after this line in SUB2 is executed is 3.0.

22. What does "COMMON/CONTROL/ A, B, C" do to the values of A, B, C? 
It doesn't do anything to the values. It just establishes a place in memory for the values to be stored and retrieved. Every subroutine and function in your program that contains the line"COMMON/CONTROL/ A, B, C" will agree that operations using variables A, B, or C will get numbers from or put numbers in the same appropriate location in memory. That is, they agree that a reference to "A" means the same in all routines with common CONTROL, etc.. To give A, B, or C a value, you have two options. You can assign values at compilation time with a BLOCK DATA routine:
BLOCK DATA ABCVAL
COMMON/CONTROL/ A, B, C
DATA A,B,C / 1.0, 2.0, 3.0/
END
You can also assign and use values with executable statements. Say subroutines SUB1 and SUB2 contain this common block. If SUB1 contains the lines "A=1.0", and "B=2.0", and SUB2 has the line"C=A+B", then the value of C after this line in SUB2 is executed is 3.0.

23. Why doesn't Fortran have intrinsic functions for something as simple as factorial? 
Two reasons. Factorial isn't all that common in heavy duty scientific and engineering applications. When it does occur, it almost always in a context where it is more computationally efficient to generate it as you go. You need 2! first then 3!, then 4!, etc. You are basically stuck doing a factorial within the context of a do loop unless you get really good and learn to write "recursive functions", but then you are just fooling yourself and writing another form of do loop. When you are taking the factorial of a large number and don't need an exact answer you can resort to Stirling's Approximation. A Fortran statement that will load the value of this approximation into the variablenfact is:
nfact = sqrt(2*3.1415963*n)*(n/2.71828)**n
by 20! this has a 0.4% error. Try larger numbers on the machine for a better feeling of the approximation.

24. Do we need to prompt the user for input on our programs? 
Always! In this class, any programmed "read" from the terminal must be preceded by writing some intelligible message asking for input.

25. What directory is used by the compiler for compiling a Fortran Program? Where does f77 live? 
For the work in this class, you should assume that everything happens in whatever directory you are in when you type the "f77". Type "pwd" if you don't know the answer to this question. The executable file called "f77" resides both in /bin and /usr/bin on these machines. This is very unusual. To locate an executable file use the "whereis" command (e.g. "whereis f77"). Unfortunately the manual pages on f77 aren't connected properly and are listed under IBM's other name for their compiler, "xlf". Try "man xlf" for more information on the compiler, but don't expect too much. IBM likes to force people to buy manuals and special CD-ROM packages.

26. Why do you put so many lines of empty space in your programs? 
I hope the lines aren't totally empty. They should contain a "c" in column one. These "blank" lines are just to make the comments stand out from Fortran code lines or to highlight key blocks of a program.

27. Do spaces mater in equations? 
No. Spaces are generally added for clarity. Some compilers get upset if you write things like " INTEGERI,J" rather than INTEGER I,J". Simple neatness will keep you out of these problems. Remember that a space is required in column 6 if you aren't continuing from the previous line. The following are all equivalent:
x=x*y**2*sin(x)
x=x * y**2 * sin(x)
x = x * y ** 2 * sin ( x )

28. Is there any way to use variables in a format statement? 
Yes, but you have to use one format statement to build a second using a write to a character string. For example if you want to include the value of "n" as the number of real numbers per line you would do the following:
PARAMETER (N=4)
CHARACTER FORM1*16
REAL A(N,N)
DO 10 I=1,N
DO 10 J=1,N
A(I,J)=J*100+I
10 CONTINUE
WRITE(FORM1,2000)N
2000 FORMAT('(1X,',I3,'F6.1)')
WRITE(6,FORM1) A
STOP
END
Is it possible to lay out a two dimensional array with the double DO loops:
DO 10 J=1,4
DO 20 K=1,4
A(J,K)=SUM(T(1:N)**(J+K-2))
20 CONTINUE
10 CONTINUE
yes, if you have a Fortran 90 compiler.

29. About CALL statements. Should they be within subroutines that they are calling for? 
Think of a CALL as a GO TO statement. If you say "CALL INPUT1", Fortran effectively looks for the statement called "SUBROUTINE INPUT1" and goes there to execute more instructions. Just before taking the jump, the program makes some notes so that the code in the SUBROUTINE knows about location of arguments, and knows that when it hits a RETURN statement, it should GO TO the statement in your code following "CALL INPUT1". You can see when thinking of this as a GO TO operation, there are too many opportunities for an infinite loop if SUBROUTINE INPUT1 contains a line that says "CALL INPUT1". There are also some subtle data management nightmares. To avoid all of this, older Fortran standards refused to let you include "CALL INPUT1" in SUBROUTINE INPUT1. This continues to be a good idea. However, for those who like to live on the edge Fortran 90 will let you include "CALL INPUT1" in SUBROUTINE INPUT1. This process is called recursion. I never use it, and don't recommend it. You will get another story from a certified Computer Scientist. Those folks really love recursion, and to be fair can do some fairly slick things with it.

30. Can you give us a complete list of the Fortran commands and what they do? 
No, but there are too many pages involved, and copyright problems with the standard Fortran manuals sold by computer software vendors. You're stuck with the text and Web pages, buying something else from a bookstore, or buying a Fortran package for your PC.
More Questions & Answers :-
Part1  Part2  Part3  Part4  Part5  Part6

No comments:

Post a Comment