Programming Languages Exam
Name ______________________________
This is a long exam. Do not get bogged down on any question. Do the ones you can do first.
The total number of points you can earn on this exam is 105.
If you estimate your total earned points within 5 points of your actual earned points, I will add 5 points to your total score. You do not have to add up your points, it is sufficient to indicate the points per page in the empty column.
Question Page |
Max Points Possible |
Estimated Earned Points |
Actual Earned Points |
Short Answers – page 1 |
10 |
|
|
Short Answers – page 2 |
10 |
|
|
Short Answers – page 3 |
10 |
|
|
Scope Question |
10 |
|
|
FOR LOOP |
10 |
|
|
Grammar Question |
15 |
|
|
Comparative Languages |
15 |
|
|
Parameter Passing |
16 |
|
|
Ada For Loop Question |
10 |
|
|
|
105 |
|
|
I have observed the provisions of the honor code in completing this exam.
_______________________
Signature
Programming Languages Exam
Short Answers - Page 1
Name
____________ Max
Points: 10 Points Earned:
_____
The three languages we have looked at thus far this semester have different approaches to the use of non-local variables.
Briefly describe how each provides access to non-local variables (if they do).
FORTRAN only access is through parameter
list or through common block |
Pascal through parameter list and any
variable not declared locally which is declared in a surrounding block (an
outer block)is accessible |
Ada through parameter list and using a
with/use combination or a with statement and a fully qualified reference to a
variable in another program unit. |
Our book list the three most important criteria for evaluating a programming language as readabilit, writability and reliability. Pick one of these and list three (3) programming language elements that affect it.
Criterion: |
Readability |
Writability |
Reliability |
Element 1 |
simplicity/orthogonality control structures |
simplicity/orthogonality control structures |
simplicity/orthogonality control structures |
Element 2 |
data types & structures |
syntax design data types & Structures |
syntax design data types & Structures |
Element 3 |
syntax design |
support for abstraction expressivity |
support for abstraction expressivity type checking exception handling restricted aliasing |
Our text indicates four (4) basic categories of languages. List the four (4) categories and for each, indicate a language that is representative of the category.
Category |
Representative
Language |
imperative |
Ada, FORTRAN, Pascal, C |
functional |
Lisp, Scheme, ML |
Logic |
Prolog |
object oriented |
C++, Java, C#, Smalltalk |
Programming Languages Exam
Short Answers - Page 2
Name
____________ Max
Points: 10 Points Earned:
_____
One of the attributes of variables that we have discussed in class is name. Briefly and clearly describe three (3) decisions language designers make with regard to variable names.
length - how long can a legal variable name be? how many characters are relevant? |
character set - what characters are allowed? Are any required or forbidden in initial position? |
case sensitivity - does case sensitivity matter? |
reserved or key words - |
We have discussed two types of bindings: static and dynamic. Provide a definition for dynamic binding and tell where we might see an example of a dynamically bound variable in a programming language.
Definition: a dynamic binding first orrucs
during run time or can change in the course of program execution |
Example: pointer variables are
dynamically bound to memory locations objects |
When this course is over and you need to learn a new programming language, what are five (5) distinct types of statements you will expect to find in the new language.
1. input |
2. output |
3. assignment |
4. selection |
5. iteration |
Programming Languages Exam
Short Answers - Page 3
Name
____________ Max
Points: 10 Points
Earned: _____
What is the difference between the use of the semi-colon in Pascal and Ada?
in Pascal: in Pascal, the semi-colon is a
statement separator |
in Ada: the semi-colon is a statement
terminator |
What is the difference between scalar types and structured types?
scalar types are: types without component parts |
structured types are: types which have component pars -
like records and arrays |
FORTRAN uses implicit type declarations while Ada and Pascal do not.
What do we mean by implicit typing? in implicit typing, the type of
the variable is determined either by its name (as in FORTRAN) or by what is
assigned to it (as in Snobol) |
Do you consider implicit typing a plus or a minus? plus minus |
Why? plus - provides for greater
flexibility minus - makes language less
reliable more subject to programmer error |
Some programming languages have key words, and some languages have reserved words.
What is a key word? a word that has pre-defined
meaning when used in a particular context |
Give an example of a key word in a language we've
looked at this semester: if, do, |
What language did your key word come from? FORTRAN |
Programming Languages Exam
Scope Question
Name ____________ Max Points: 10 Points Earned: _____
Here's a program written in pseudocode (although it may look like a real language).
program a; const x = 4; var z: integer;
begin {a} z := 8; q(x); end program a; |
|
|
|||
p |
|||||
r |
|||||
q |
|||||
a |
2. What value will be printed if we assume that the program is using static scope rules? 56
3. What value will be printed if we assume that the program
is using dynamic scope rules? 14
Programming Languages Exam
FOR LOOP
Name
____________ Max
Points: 10 Points
Earned: _____
Here is a pseudo-code for loop (which represents a syntactically correct Pascal, Ada or Java FOR loop or a FORTRAN DO loop. Answer each of the questions which follow the loop with regard to any three of the following four languages: FORTRAN, Pascal, Ada and Java. Cross out the column for the language you are not choosing.
var m, n : integer;
for i := m to n do
begin
output i
<3>
end;
Question |
FORTRAN |
Pascal |
Ada |
Java |
Does changing the value of n at point <3> have any effect on the number of times the loop is executed ? |
no |
no |
no |
yes |
Is it possible to jump out of the loop at point <3> |
yes |
yes |
yes |
yes |
Is it possible to print the value of i after the loop has completed normally? |
yes |
yes |
no |
depends where it is declared |
Is it possible to have the value of i be decremented as it goes from m to n? |
yes |
yes |
yes |
yes |
Programming Languages Exam
Grammar Question
Name ____________ Max Points: 15 Points Earned: _____
Given the language specification below
<start> ::= <someCs> <someDs>
<someCs> ::= c c
<someCs> ::= c c <someCs>
<someDs> ::= d
<someDs> ::= d d <someDs>
a. Draw 5 different parse trees generating legal strings in this language
<start>
/ \
<someCs>
<someDs> / |
\ | c c <someCs> d /
\ c c |
<start> / \ <someCs> <someDs>
/ \ | c c d |
|
<start> / \ <someCs> <someDs>
/ \ / | \ c c d d
<someDs> | d |
|
<start> /
\ <someCs>
<someDs>
/ | \ /
| \ c c <someCs> d
d <someDs> / \ | c c d |
<start>
/ \
<someCs>
<someDs> / |
\ / | \ c c <someCs> d d <someDs> /
\ / |
\ c c d d
<someDs> | d |
b. Show each of the strings you generated
c c c c d c c d c c d d d c c c c d d d c c c c d d d d d
c. Describe in concise English what the strings in this language consist of. Be careful to be accurate and precise.
An even number of Cs followed by an odd number of Ds. There must be at least 2 Cs and 1 D.
Programming Languages Exam
Comparative Languages
Name
____________ Max Points: 15 Points Earned: _____
Rewrite the FORTRAN code containing a computed GOTO statement shown below in Pascal without GOTO statements. Only write what is necessary to replace this code. Assume that all declarations, etc. are in place.
if number = 1 then number := number * 2 else if number = 2 then number := number - 1 else if number = 3 then number := 16; number := number * number ; |
GOTO (10,20,30) NUMBER
10 NUMBER = NUMBER * 2
GOTO 60
20 NUMBER = NUMBER – 2
GOTO 60
30 NUMBER = 16
60 NUMBER = NUMBER * NUMBER
What would be stored in the memory locations shown below after execution of the following FORTRAN input statement? The input data is shown in the box below.
READ (5, 20) A,B, I, X
20 FORMAT (F3.2, F2.1, I3, F2.2)
3254657384729385712 |
A |
3.45 |
B |
4.6 |
I |
573 |
X |
.84 |
Write the FORTRAN output and FORMAT statements that would output each of the stored values with a single space separating each from the other.
WRITE
(6, 21) A,B,I,X
21 FORMAT (1X, F4.2, 1X, F3.1, 1X, I3, 1X, F3.2)
Show the output produced by the output statement you wrote above. (Each box represents an output column)
3 |
. |
4 |
5 |
|
4 |
. |
6 |
|
5 |
7 |
3 |
|
. |
8 |
4 |
|
|
|
|
Programming Languages Exam
Parameter Passing
Name
____________ Max
Points: 16 Points Earned:
_____
Below is a pseudo-code subprogram.
subprogram swap ( number1, number2)
local variable x
x <- number 1
number1 <- number2
number2 <- x
end subprogram swap
Each part of this question asks you what will be printed upon return from the subprogram assuming that the pseudo-code represents syntactically correct code in the language specified. For each part, you should assume that:
value1 and value2 are variables holding ordinary integers
value1 is initially 7
value2 is initially 12
swap is called with parameters value1, value2 in that order
In each case, what will be printed after the return from swap, if value1 is printed followed by value2?
a. The language is FORTRAN
Value1 |
12 |
Value2 |
7 |
b. The language is Pascal and number1 is a value parameter and number2 is a var parameter
Value1 |
7 |
Value2 |
7 |
c. The language is Ada and both parameters are in out parameters
Value1 |
12 |
Value2 |
7 |
d. The language is Java
Value1 |
7 |
Value2 |
12 |
Programming Languages Exam
Ada For Loop Question
Name
____________ Max
Points: 10 Points Earned: _____
with Ada.Integer_text_io;
with Ada.text_io;
procedure testLoopFor
is
i : integer := 13;
begin
for i in 3..5 loop
ada.Integer_text_io.put (i,3);
for i in reverse 4..6 loop
ada.Integer_text_io.put (i,3);
end loop;
ada.Integer_text_io.put (i,3);
end loop;
ada.Integer_text_io.put (i,3);
end testLoopFor;_
The questions below all refer to the Ada code above
a.
Will the above Ada program compile? NO because of typo – underscore in red at end of code snippet
¼§Ïtestloopfora.adb:17:18: identifier cannot
start with underline
b.
Show the output!
ÏÏ«Ï ----jGRASP exec: C:\GNAT\bin\testLoopForA
ÏϧÏ
ÏÏ§Ï 3 6
5 4 3 4 6
5 4 4 5 6
5 4 5 13
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.
c. How many numbers will be printed? 16