Programming Concepts: Notation, Precedence, Data Types, Memory, and Garbage Collection - P, Exams of Programming Languages

Various programming concepts including notation systems (infix, prefix, and postfix), precedence rules, data types (primitive and abstract), memory management (compaction, static and dynamic allocation, and garbage collection), and related topics such as coroutines, assertions, and timing patterns. It also includes examples and exercises.

Typology: Exams

Pre 2010

Uploaded on 05/14/2008

koofers-user-0t7
koofers-user-0t7 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Final
Exam
CMSC
330
Sections
0201,
0202
15
December
1998
Point
total:
175
____
(please
print)
The
point
value
for
each
question
is
indicated
(in
parentheses).
Do
your
own
work;
use
your
time
wisely;
enjoy
semester
break.
(15)
1.
On
the
line
provided
write
the
letter
corresponding
to
each
statement
below
that
isTRUE
-
ft
3
n
/n
______
-
a.
Of
the
three
notations
for
arithmetic
expressions
(i.e.,
infix,
postfix,
and
prefix),
the
one
with
the
greatest
potential
for
ambiguity
is
infix.
b.
One
advantage
to
prefix
and
postfix
notation
is
that
parentheses
are
not
necessary.
c.
Precedence
rules
determine
whether
expressions
are
to
be
evaluated
right-to-left
or
left-to-right.
d.
Using
prefix
notation
eliminates
problems
caused
by
side effects.
e.
The
use
of
simultaneous
substitutions
to
multiple
expressions
to
find
a
consistent
set
of
values
to
satisfy
all
of
the
expressions
is
known
as
backtracking.
f.
Primitive
data
types
are
built
into
programming
languages;
the
set
of
primitive
data
types
is
essentially
the same
for
all
common
programming
languages.
g.
A
coercion^
converts
a
data
object
from
one
type
to
another;
it
is
implemented
by
the
programmer.
-
h.
The
advantages
of
subranges
include
smaller
storage
requirements.
i.
The
disadvantages
of
subranges
include
restrictions
on
type
checking.
j.
Relational
operators
do
not
work
with
enumerated
data
types.
k.
Boolean
variables
may
be
assigned
one
of
three
values:
true,
false,
and
unknown.
1.
Structured
data
objects
are
limited
to
homogeneous
components.
-m.
An
advantage
of
using
sequential
storage for
structured
data
objects
is
that
it
enables
random
selection
of
a
component
by
calculation
of
its
memory location.
n.
Abstract
data
types
do
not
include
the
operations
for
the
data
objects.
o.
Methods
are
inherited
operations;
if
a
method
does
not
exist
for
an
object,
the
method
of
a
child
of
the
object
is
used
if
it
exists.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Programming Concepts: Notation, Precedence, Data Types, Memory, and Garbage Collection - P and more Exams Programming Languages in PDF only on Docsity!

Final Exam CMSC 330 Sections 0201, 0202 15 December 1998 Point total: 175


(please print)

The point value for each question is indicated (in parentheses). Do your own work; use your time wisely; enjoy semester break.

(15) 1. On the line provided write the letter corresponding to each statement below that

isTRUE- ft 3 n /n______

  • a. Of the three notations for arithmetic expressions (i.e., infix, postfix, and prefix), the one with the greatest potential for ambiguity is infix. b. One advantage to prefix and postfix notation is that parentheses are not necessary. c. Precedence rules determine whether expressions are to be evaluated right-to-left or left-to-right. d. Using prefix notation eliminates problems caused by side effects. e. The use of simultaneous substitutions to multiple expressions to find a consistent set of values to satisfy all of the expressions is known as backtracking. f. Primitive data types are built into programming languages; the set of primitive data types is essentially the same for all common programming languages. g. A coercion^ converts a data object from one type to another; it is implemented by the programmer.
  • h. The advantages of subranges include smaller storage requirements. i. The disadvantages of subranges include restrictions on type checking. j. Relational operators do not work with enumerated data types. k. Boolean variables may be assigned one of three values: true, false, and unknown.
  1. Structured data objects are limited to homogeneous components. -m. An advantage of using sequential storage for structured data objects is that it enables random selection of a component by calculation of its memory location. n. Abstract data types do not include the operations for the data objects. o. Methods are inherited operations; if a method does not exist for an object, the method of a child of the object is used if it exists.

(20) 2. On the line provided, write the letter corresponding to each statement below that is TRUE.

a. A variation in subprogram control frequently used for error handling is coroutines. b. Exception handlers can not be explicitly invoked by the programmer. ^c. An assertion is a type of exception that states a relationship that must hold during program execution. xl. Propagation of an exception is along the dynamic chain. e. The advantage of coroutines is that when control returns to the coroutine it returns to the first statement of the coroutine. f. The variation on program control that is based on a real or simulated time scale is called a timing pattern. g. A synchronization approach commonly used for hardware devices is message passing. -h. Mutual exclusion is a principle that prohibits multiple tasks from accessing the same data object simultaneously. i. When critical regions are used to implement mutual exclusion, multiple tasks must be in the critical region at the same time. ^-j. When the rendezvous is used to communicate among concurrent tasks, multiple tasks must be in the rendezvous at the same time. sk. The global referencing environment is a subset of the nonlocal referencing environment. A. Compaction of memory is sometimes necessary to generate contiguous blocks of memory. m. Static memory allocation is necessary to enable recursion and dynamic data structures. In stack-based storage management, storage is freed in reverse order of allocation (i.e., LEFO). o. In heap management, storage is freed in reverse order of allocation (i.e., LIF'O). p. Two problems of memory management are garbage and dangling participles. Some languages (e.g., C) permit the programmer to directly control some aspects of dynamic memory management. ^r. Some languages (e.g., C) permit the programmer to directly control some aspects of static memory management. ^-s. During garbage collection, other processing is suspended. --1 Two approaches to direct reuse of memory are first fit and best fit; first fit is more time efficient.

(10) 8. Use the following Pascal-like declaration and statements for parts a andb:

var X: array [-2.. 0, 0.. 1] of integer;

for i := -2 to 0 do forj:=0to 1 do

Assume the following:

Integers take one memory location ' Xx

Descriptors include the virtual origin and the upper and lower bounds and multiplier for

each dimension

Descriptor for X begins at location 1000 and the storage for array X begins at 2000

X is stored in row major order

(a). Fill in the relevant sections of the following memory map and give the access formula

forX[ij].

NAME

VALUE

O d- o

NAME

, Q

VALUE

£>

Acess formula: 1-value (X[ij]) =

(b). Fill in the relevant sections of the following memory map for the descriptor of

X[*,l]. Assume that the descriptor begins at location 3000.

NAME VALUE NAME VALUE

3000 \j

^

,^

^ 3002

Acess formula: 1-value (X[i,lj) =

rJ r

,/n

(12) 9. Assume that the two-stage mark-sweep algorithm for garbage collection is used for this LISP example. A stack and heap are indicated for six points in the computation of the function combine. Indicate how garbage collection would proceed during execution of combine. Each word consists of two parts; the first part can either be an element of data or a pointer, the second part is a pointer. Shaded words indicate words that are not available as free space. Mark words by outlining them. (In LISP, the cons function concatenates two lists).

User-defined function: (defun combine (x) (cons '(q r s) x )) _ \ -^'^ 'V , *s ^ Function call: (combine '(a b)) ;, ..-^ f

stack heap

Prior to call

Call Mark Post sweep combine= '(q r s a b)

Post return

(12) 13. Give the output for the following program. The var preceding a parameter indicates pass by reference. The command writein prints the output and provides a carriage return.

program Main; var i, j: integer; / procedure Prod (var i: integer; function F (k : integer): integer); var h: integer; begin h:=1; writein ("i =", i. "j =", j, "h =", h, "in Prod"); h := F(i); writein ("i =", i, "j =", j, "h =", h, "in Prod"); end;

procedure Proc2; varj: integer; function G (varj: integer):integer; begin j := 2;

writein ("i =", i, "j =", j, "in G"); end; begin j := 3; writein ("i =", i, "j =", j, "in Proc2"); Prod G, G); writein ("i =", i, "j =", j, "in Proc2"); end;

begin i:=4; j:=5; Proc2; writein ("i =", i, "j =", j, "in Main"); end.

OUTPUT

f - "b.

C '

11

(15) 12. The following code is Pascal-like. Execute the program 4 times, each time assuming the parameters are passed using one of the following methods: call by name, call by reference, call by value, and call by value-result.

Program parameter var i: integer; A: array [1 ..3] of integer; Prod (j, k: integer); begin V- ;,,,.,

k:=k + 1; f^ Lu>

print ("i = ", i., "j = ", j, "k = ", k); i := 4; j:=j-2; ft£i> print ("i = ", i, "j = ", j, "k = ", k); end;

begin fori:= 1 to 3 do A[i] := i + 1; for i:= 1 to 3 do print ("A[", i, "] = ", A[i]); i:=2; Prod (i, A[i]); print ("i = ", i); for i:= 1 to 3 do print ("A[", i,"] =", A[i]); end.

Give the output for each method:

l&

i

j

L

^ j'

V/>-

i

t

K flti^ .. ————"———

^

k_ _A_r£L--

_^£-J-—— jc^ j —— n&

A ^ 3. f-

13 !^

fv 'T. -^ M

A T-^M

Call by name

; - ' J = i K-^ L - z. ±--3 ^ L-Z

""'o.r'

Call by reference

i- 1 j-i y-.-l
L *-L y-i_ K=f

L'

flt*^

Call by value

L-iV, '^

L^ J=l K ^ <.--*

A^'?^rv^

Call by value-

result

i -i j--b *=i t.^ J--' i*-- L- r

*£T

(7) 3. Use the BNF provided to develop a parse tree for a!2[b,c3].

<O::= {, }*

:~ a|b|c|d|...|z <£>::= 0|1|2|3|4|5|6|7|8|

3 ]

(6) 4. Generate the finite state automaton for the regular grammar below.

A -> aA | bC | c B -> aA | bB | cC C -> aB | bC | c

(14) 10. Use the Smalltalk subclass definitions and methods for each subclass to provide the indicated output.

Object subclass: # ClassA ClassA subclass: # ClassB ClassB subclass: # ClassC instance VariableNames:'' instance VariableNames:'' instance VariableNames:'' class VariableNames:'' class VariableNames:'' class VariableNames:'' poolDirectories:'' poolDirectories:'' poolDirectories:'' category: nil! category: nil! category: nil!

ICIassA methodsFor: 'basic'! picklt 'pick classA' printNl! peellt self picklt!! steamlt 'steam ClassA' printN!

jClassB methodsFor: 'basic'! picklt 'pickclassB' printNl! steamlt self peel It!!

iCIassC methodsFor: 'basic'! steamlt self peellt!!

Statement Output

x |x_ ClassA new.x picklt! x |x_ ClassA new.x peellt! ——P x |x_ ClassB new.x peellt! x |x_ ClassB new.x steamlt!

x |x_ ClassC new.x picklt! x |x_ ClassC new.x peellt! x |x_ ClassC new.x steamlt!

8

(10) ,15. The psuedo code for two Ada tasks is provided. Task A reads data from an input device into a buffer. Task B processes the data in the buffer. Following the tasks is a set of conditions. Indicate the statement number of both tasks that would be active for each condition.

task A; task B; begin begin A1 -input first data set loop loop B1 wait (StartB); A2 signal (StartB); B2 - process data set A3 wait (StartA); B3 signal (StartA); A4 - input next data set endloop; endloop; end task B; end A;

Condition task A task B

A is reading in the first data set N_

B is processing the first data set n A is reading in the third data set

B has completed processing the fourth data set / ,

13