Friday 30 January 2015

Latest Fortran Interview Questions and Answers

41. How do we know where various steps go in a Fortran program? 
Some commands have special locations, but most are located by the needs of the specific program. The PROGRAM card is always first. Statements giving variable types (INTEGER, REAL, LOGICAL, CHARACTER, ...) should precede "executable" statements. The END card must always be at the end of the program unit.

42. When accessing a data file in a program can I change directories? 
Yes if you have a subdirectory called "test" under the location that your program, you can open the file "my.data" in "test" for reading on unit 11 with the command: OPEN(11,file='test/my.data')

43. Subprograms. What do they do and how do the help program. 
They do anything that the main program can do. They help you organize your total program (main program + subprograms) by grouping specific tasks in a well defined location. They can save you repeating similar program structures at several places in your code. Also, for many tasks someone has already written a subprogram to do the job and you save a lot of work by picking up their subprogram and plugging it into your work. You will see an example of this when we start solving systems of linear equations (say 10 equations and 10 unknowns). You will define the equations, then let somebody else's subprogram solve them.

44. How do we know where various steps go in a Fortran program?
Some commands have special locations, but most are located by the needs of the specific program. The PROGRAM card is always first. Statements giving variable types (INTEGER, REAL, LOGICAL, CHARACTER, ...) should precede "executable" statements. The END card must always be at the end of the program unit.

45. Could you give us sample questions on determining output of code containing recent commands? 
DIMENSION A(5),B(5),C(5)
DATA A/1.,2.,3.,4.,5./,B/5*2/,C/5*0.0/,IMIN,IMAX,ISTEP/2,5,2/
DO 100 I=IMIN,IMAX,ISTEP
100 C(I)=A(I)*B(I)
WRITE(6,2000) I,(C(J),J=1,5)
2000 FORMAT(' I=',I5,' C=',/, (1P,4E15.7))
STOP
END
What is the value of "I" printed out. What are the values of "C" printed? Also check my sample "essay" question on the back of this handout.

46. When accessing a data file in a program can I change directories?
Yes if you have a subdirectory called "test" under the location that your program, you can open the file "my.data" in "test" for reading on unit 11 with the command: OPEN(11,file='test/my.data')

47. What a DATA statement does. 
It simply puts a specific value into a Fortran variable. "DATA A/1.0/" is the same as "A=1.0" in terms of results. The only difference is that the DATA statement sets A to 1.0 before the program starts to execute. The biggest advantage occurs when you are trying to give a variable a value in a FUNCTION or SUBROUTINE that is used a large number of times. The program on the left is faster than the program on the right below.
Do 100 i=1,1000000 do 100 i=1,1000000
100 y(i)=addon(x(i)) 100 y(i)=addon(x(i))
stop stop
end end
function addon(x) function addon(x)
data b/1.12345/ b=1.12345
addon=x+b addon=x+b
return return
end end
Yes, its a stupid example, but applies to real programs.

48. Can more than one variable be stored in a Data Statement? 
Yes. For example you can set initial values for A, B, and C with either of the following statements.
DATA A/1.0/,B/2.0/,C/3.0/
DATA A,B,C/1.0,2.0,3.0/
About the exact use of arrays and their purpose
The simple answer is "pay attention to the lectures and examples for the rest of the semester." Arrays are used when you have a large number of numbers on which you want to do identical or similar analysis. Let's say I have the temperature and pressure from 10,000 measuring stations in the US, and want to calculate the air density at each of these points. I load the temperatures and pressures into arrays with a dimension of 10000, and pair by pair I march through and evaluate a function to give density in another array.
dens(i)=rho(t(i),p(i))
Yes, I could have read the data in a pair at a time and printed results one line at a time without using an array. However, what if this is just the beginning? What if I'm going to take all off my temperatures, pressures, densities and several other measurements from each station and use it as part of a complicated calculation to predict the weather for the next 24 hours? I have to keep all of the data in the computer. Arrays provide a convenient way of storing and retrieving all of this information within a program. Please note that while the program is executing, information contained in arrays, like other Fortran variables, is kept in the machine memory where access times are much faster than when the information is in a disk file.

49. Could you go over colons again in dealing with arrays? 
Within Fortran 77 there is only one use for colons associated with arrays, and that is when establishing the size and permitted range of indices in a DIMENSION or type (REAL, INTEGER, ...)statement. For example "DIMENSION A(-4:10)" assigns 15 words of memory to the array A, and makes a note that A(-4) corresponds to the first of these sequential words. For Fortran 90 using a colon within an executable statement is just shorthand for writing a DO loop involving just one line.
C(1:9)=A(1:9)+B(1:9) is the same as: DO 100 I=1,9
100 C(I)=A(I)+B(I)
C(1:9:2)=A(1:9:2)+B(1:9:2) is the same as DO 100 I=1,9,2
100` C(I)=A(I)+B(I)
Write a few 5-10 line programs until you are comfortable with how this behaves.

50. Are We Actually Going to Need Character Variables? Why are they used? 
You could probably get through all of the programming applications in this class without resorting to character variables. However, if you are going to create programs with legible output and/or flexible input/output you will need them. You have already seen me use them to store names of files for use in input (look at the sample program trig3.f). That is one of the most common uses for simple programs. Also, take a look at the tricks I play in plot1.f and plot2.f to generate creative output with the help of character variables. When you start driving graphics subroutines to view your data, you will find that character variables a useful and generally mandatory.
Postscript: My daughter (Senior, physics) burst out laughing when she saw this question while trying to throw me off of the family PC. Yes, she is cruel, but her experience may be closer to what you will use soon than the crazy things that I do. She is doing quite a bit of Fortran programming for Dr. Garrison in the Chem department. She says she couldn't get along without charactervariables to keep track of file names and manage printed and graphical output.
More Questions & Answers :-
Part1  Part2  Part3  Part4  Part5  Part6

No comments:

Post a Comment