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 |
12 |
|
|
Short Answers – page 3 |
8 |
|
|
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: _____
Our text book lists five (5) reasons for studying the concepts of programming languages. Briefly but clearly describe three (3) of them.
1. increased capacity to express
ideas |
2. improved background for choosing appropriate languages |
3. increased ability to learn new languages |
4. better understanding of the significance of implementation |
5. overall advancement of computing |
Our book lists 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 book indicates discusses the six (6) programming "domains" below. For 4 of them, indicate a language that is representative of the domain
Domain |
Representative
Language |
scientific applications |
FORTRAN or Algol 60 |
business applications |
COBOL |
artificial intelligence |
LISP or Scheme or Prolog |
systems programming |
C or PL/S or BLISS or extended Algol |
scripting languages |
JavaScript or Perl or PHP |
special purpose languages |
rpg or apt or gpss |
Programming Languages Exam
Short Answers - Page 2
Name
____________ Max Points: 12 Points
Earned: _____
Our book says that a variable is a sextuple of attributes. Identify three (3) of these attributes and describe what is meant when we refer to them.
Attribute |
Description |
1. name |
how the programmer refers to it |
2. address |
memory location (address) with
which it is associated |
3. value |
contents of abstract memory cell |
4. type |
its set of values and its set of
operations |
5. lifetime |
when is it accessible |
6. scope |
where is the variable known |
We have discussed two types of bindings: static and dynamic. Provide a definition for static binding and tell where we might see an example of a statically bound variable in a programming language.
Definition: first occurs before run time
and remains unchanged throughout program execution |
Example: assignment of value to a
constant; association of main program variables with memory locations |
What is the difference in the use of indentation in FORTRAN and in Pascal?
in FORTRAN: indentation is
meaningful. Statements and labels are
required to be in specific columns |
in Pascal: indentation is only
an aid to the understanding of the person reading the code |
What is the difference between discrete types and scalar types?
discrete types are: those
such that between any two successive values, there is no other value types which are mathematically discrete |
scalar types are: types which
do not have component parts include floating point which is not discrete because between any two
floating point numbers there are other numbers |
Programming Languages Exam
Short Answers - Page 3
Name
____________ Max Points: 8 Points
Earned: _____
FORTRAN uses implicit type declarations while Ada and Pascal do not.
What do we mean by implicit typing? when the value or name assigned to a variable determines its type |
Do you consider implicit typing a plus or a minus? a plus - a minus - |
Why? plus makes the language more
flexible minus makes the language less
safe - doesn't catch certain kinds of programmer errors |
Pascal programs are single compilation units while Ada and FORTRAN programs can consist of multiple compilation units.
Which do you
think is a better approach? single multiple |
Why? (i.e. what
is an advantage of the approach you think is better) single means everything is
bundled together and you can see it all at once multiple makes it possible to
separately compile (and test) each unit |
Some programming languages have key words, and some languages have reserved words.
What is a reserved word? A reserved word is a word that
may not be used as a variable or program unit name in a program |
Give an example of a reserved word in a language we've
looked at this semester: procedure |
What language did your reserved word come from? Ada |
Note: integer and writeln aren't reserved words in Pascal
FORTRAN doesn't have reserved words
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 := 7; q(x); end program a; |
|
|
|||
p |
|||||
r |
|||||
q |
|||||
a |
What value will be printed if we assume that the program is
using static scope rules? 79
What value will be printed if we assume that the program is using dynamic scope rules? 81
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 |
Can the programmer assign a value to i at point <3>? |
yes/no* |
yes |
no |
yes |
Is it possible to jump out of the loop at point <3> |
yes |
yes |
yes |
yes |
Does i have to be explicitly declared anywhere? |
no |
yes |
no |
yes |
Is it possible to increment i by a value other than 1? |
yes |
no |
no |
yes |
* the compiler most of you were
using didn't allow it. FORTRAN IV did
Programming Languages Exam
Grammar Question
Name ____________ Max Points: 15 Points Earned: _____
Given the language specification below
<start> ::= <someAs> <someBs>
<someAs> ::= a a
<someAs> ::= a a <someAs>
<someBs> ::= b
<someBs> ::= b b <someBs>
a. Draw 5 different parse trees generating legal strings in this language
<start>
/ \
<someAs> <someBs> / |
\ | a a <someAs> b /
\ a a |
<start> / \ <someAs> <someBs>
/ \ | a a b |
|
<start> / \ <someAs> <someBs>
/ \ / | \ a a b
b <someBs> | b |
|
<start> /
\ <someAs>
<someBs>
/ | \ /
| \ a a <someAs> b
b <someBs> / \
| a a b |
<start>
/ \
<someAs>
<someBs> / |
\ / | \ a a <someAs> b b <someBs> /
\ / |
\ a a b b <someBs>
| b |
b. Show each of the strings you generated
a a a a b a a b a
a b b b a a a a b b b a
a a a b b b b b
c. Describe in concise English what the strings in this language consist of. Be careful to be accurate and precise.
An even
number of As followed by an odd number of Bs.
There must be at least 2 As and 1 B.
Programming Languages Exam
Comparative Languages
Name
____________ Max Points: 15 Points Earned: _____
Rewrite the FORTRAN code containing computed GOTO statement shown below in Ada without GOTO statements. Only write what is necessary to replace this statement. Assume that all declarations, etc. are in place.
with ada.text_io; |
GOTO (30,40,50) NUMBER
30 NUMBER = NUMBER + 3
GOTO 60
40 NUMBER = NUMBER * 7
GOTO 60
50 NUMBER = 23
60 NUMBER = 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 (F2.1, F3.2, I4, F4.1)
3254657384729385712 |
A |
3.2 |
B |
5.46 |
I |
5738 |
X |
479.2 |
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, F3.1, 1X, F4.2, 1X, I4, 1X,
F5.1)
Show the output produced by the output statement you wrote above. (Each box represents an output column)
3 |
. |
2 |
|
5 |
. |
4 |
6 |
|
5 |
7 |
3 |
8 |
|
4 |
7 |
2 |
. |
9 |
|
Programming Languages Exam
Parameter Passing
Name
____________ Max Points: 16 Points
Earned: _____
Below is a pseudo-code subprogram.
subprogram swap ( aReal3, aReal2)
local variable x
x <- aReal 3
aReal3 <- aReal2
aReal2 <- 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 a legal real (i.e. floating point) value
value1 is initially holding 23.45
value2 is initially -92.13
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 |
-92.13 |
Value2 |
23.45 |
b. The language is Pascal and aReal3 is a var parameter and aReal2 is a value parameter
Value1 |
-92.13 |
Value2 |
-92.13 |
c. The language is Ada and both parameters are in out parameters
Value1 |
-92.13 |
Value2 |
23.45 |
d. The language is Java
Value1 |
23.45 |
Value2 |
-92.13 |
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 := 7;
begin
for i in reverse 4..6 loop
ada.Integer_text_io.put (i,3);
for i in 3..5 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? It
won't but I intended it to! Inadvertent
understcore (in red above) is the problem. It was a typo
¼§Ïtestloopforb.adb:17:18: identifier cannot
start with underline
b.
Show the output!
Ï«Ï ----jGRASP exec: C:\GNAT\bin\testLoopForB
ÏϧÏ
ÏÏ§Ï 6 3
4 5 6 5 3
4 5 5 4 3
4 5 4 7
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.
c.
How many numbers will be printed? 16
d.
How many times will the number 6 be printed? 2
e.
How many times will the number 3 be printed? 3