Vous êtes sur la page 1sur 7

Bahir Dar University

Bahir Dar Institute of Technology


Faculty of Computing
Principles of Programming Languages (CoSc5252)
Assignment Questions for M.Sc. Computer Science Students
Prepared By: Belisa Mulugeta
ID: BDU0900624PE

Submitted To: Yohannes Biadgligne


Submitted Date: 9/13/18
Assignment II
1. Define static, stack-dynamic, explicit heap-dynamic and implicit heap dynamic
variables. What are their advantages and disadvantages [1]?
Static variables are those that are bound to memory cells before program execution begins
and remain bound to those same memory cells until program execution terminates.
o Advantages: Efficiency (direct addressing, no runtime allocation overhead), for
globally accessible variables, history-sensitive subprogram support.
o Disadvantage: Lack of flexibility (no recursion), no sharing of storage among
variables, Size of data objects must be known at compile time and Data structures
cannot be created dynamically.
Stack-dynamic variables are those whose storage bindings are created when their
declaration statements are elaborated, but whose types are statically bound.
o Advantage: Allows recursion; conserves storage by all subprog.
o Disadvantages: Overhead of allocation and deallocation, Subprograms cannot be
history sensitive and inefficient references (indirect addressing).
Explicit heap-dynamic variables are nameless (abstract) memory cells that are allocated
and deallocated by explicit run-time instructions written by the programmer.
o Advantage: can construct dynamic structures.
o Disadvantage: inefficient, unreliable, heap management cost.
Implicit heap-dynamic variables are bound to heap storage only when they are assigned
values.
o Advantage: They have the highest degree of flexibility, allowing highly generic code
to be written.
o Disadvantage: The run-time overhead of maintaining all the dynamic attributes,
which could include array subscript types and ranges, among others and the loss of
some error detection by the compiler
2. Define lifetime, scope, static scope, and dynamic scope[1]
The scope of a variable is the range of statements over which it is visible and a variable is
visible in a statement if it can be referenced in that statement.
The lifetime of a variable is the time during which the variable is bound to a specific
memory location.
The method of binding names to nonlocal variables called static scoping.
Dynamic scope is Based on calling sequences of program units, not their textual layout
(temporal versus spatial). Can only be determined at run time
3. Some programming languages are typeless. What are the obvious advantages
and disadvantages of having no types in a language [1]?
Advantages: The first is flexibility and dynamic, resulting in quick turnaround of code.
Variable can contain any kind of value including numeric, string, Boolean. Learning the
no typing language is much simpler because one doesn’t have to determine size or how
the compiler will interpret the type later on, only what information must be included.
Disadvantages: A typeless language are reduced reliability due to the ease with which
type errors can be made, coupled with the impossibility of type checking detecting them
and reduced readability.
o You are not in control of the data and variables, the compiler or interpreter is.
o If you miss assign variables, there is no way for the compiler to catch any of your
mistakes. It just “does what you said”, even if it is wrong.
o Supporting programs in a type-less language is much more difficult that in a
strongly types one. It is often very difficult to determine what the original
programmer wanted to do.
4. What is the referencing environment of a statement?
The referencing environment of a statement is the collection of all variables that are visible in
the statement [1].
5. Write a simple assignment statement with one arithmetic operator in some
language you know. For each component of the statement, list the various
bindings that are required to determine the semantics when the statement is
executed. For each binding, indicate the binding time used for the language [4].
y = y +2;
The type of y is bound at compile time.
The set of possible values of y is bound at compiler design time.
The meaning of the operator symbol “+” is bound at compile time, when the types of its
operands have been determined.
The internal representation of the literal 2 is bound at compiler design time.
The value of y is bound at execution time with this statement.
6. Dynamic type binding is closely related to implicit heap-dynamic variables.
Explain this relationship.
Implicit heap dynamic variables are bound to a type at runtime when a value is assigned to a
variable [1].
Dynamic type binding is the binding of a type to a variable at runtime or changing the type of
a variable during runtime. Implicit heap dynamic variables use dynamic type binding [1].
7. Describe a situation when a history-sensitive variable in a subprogram is useful.
To describe a situation when a history-sensitive variable in a subprogram is useful, suppose
that a FORTRAN subroutine is used to implement a data structure as an abstraction.
In this situation, it is essential that the structure persists between different calls to the
managing subroutine [1].
8. Assume the following JavaScript program was interpreted using static-scoping
rules. What value of x is displayed in function sub1? Under dynamic-scoping
rules, what value of x is displayed in function sub1 [4]?
var x;
function sub1( ) {
document. Write("x = " + x + "<br />"); }
function sub2( ) {
var x;
x = 10;
sub1(); }
x = 5;
sub2( );
The value of x in function sub1 (Static scope) is x=5
Under dynamic scoping rules, the value of x in function sub1 is x=10
9. Consider the following Python program:
x = 1;
y = 3;
z = 5;
def sub1( ): a = 7; y = 9; z = 11;
... def sub2( ): global x; a = 13; x = 15; w = 17;
... def sub3( ): nonlocal a;
a = 19;
b = 21;
z = 23;
List all the variables; along with the program units where they are declared, that are visible in the
bodies of sub1, sub2, and sub3, assuming static scoping is used.
Assuming static scoping rules are used in the given python program [2]:
Variable Where Declared
In sub1
a =7 sub1()
y =9 sub1()
z =11 sub1()
x =1 main()
In sub2
a =13 sub2()
x =15 sub2()
w =17 sub2()
y =3 main()
z =5 main()
In sub3
a =19 sub3()
b =21 sub3()
z =23 sub3()
w =17 sub2()
x =15 sub2()
y =3 main()
10. What are the advantages and disadvantages of decimal data types?
Advantage: Able to precisely store decimal values, at least those within a restricted range,
which cannot be done with floating point [6].
Disadvantages: The range of values is restricted because no exponents are allowed and
their representation in memory is mildly wasteful [6].
11. Define ordinal, enumeration, and subrange types.
An ordinal type is one in which the range of possible values can be easily associated with
the set of positive integers [1].
An enumeration type is one in which all of the possible values, which are named
constants, are provided, or enumerated, in the definition [1].
A subrange type is an ordered contiguous subsequence of an ordinal type [1].
12. Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic and
heap-dynamic arrays. What are the advantages of each [1]?
Static array is one in which the subscript ranges are statically bound and storage allocation
is static (done before run time).
o Its advantage is efficiency: No dynamic allocation or deallocation is required.
Fixed stack-dynamic array is one in which the subscript ranges are statically bound, but
the allocation is done at declaration elaboration time during execution.
o Its advantage is space efficiency: A large array in one subprogram can use the same
space as a large array in a different subprogram, as long as both subprograms are not
active at the same time.
Stack-dynamic array is one in which both the subscript ranges and the storage allocation
are dynamically bound at elaboration time.
o Its advantage is flexibility: The size of an array need not be known until the array is
about to be used.
Fixed heap-dynamic array is similar to a fixed stack-dynamic array, in that the subscript
ranges and the storage binding are both fixed after storage is allocated.
o Its advantage is flexibility that means the array’s size always fits the problem.
Heap-dynamic array is one in which the binding of subscript ranges and storage allocation
is dynamic and can change any number of times during the array’s lifetime.
o Its advantage is flexibility: Arrays can grow and shrink during program execution as
the need for space changes.
13. Define union, free union, and discriminated union.
Union is a type whose variables may store different type values at different times during
program execution [1].
When there is no language support for type checking, the union is called free union [1].
If the type checking is supported by language, each union must include a type indicator,
which is called discriminated union [1].
14. Define type error and strongly typed
Type error is the application of an operator to an operand of an inappropriate type.
Strongly typed language has stricter typing rules at compile time, which implies that
errors and exceptions are more likely to happen during compilation [5].
15. Define operator precedence and operator associativity
Operator precedence is a rule used to clarify which procedures should be performed first in a
given mathematical expression [1].
Operator associativity is a property that determines how operators of the same precedence
are grouped in the absence of parentheses [1].
16. Define narrowing and widening conversions.
A narrowing conversion converts a value to a type that cannot store even approximations
of all of the values of the original type [1].
o For example, converting a double to a float in Java is a narrowing conversion,
because the range of double is much larger than that of float.
A widening conversion converts a value to a type that can include at least approximations
of all of the values of the original type [1].
o For example, converting an int to a float in Java is a widening conversion.
17. When might you want the compiler to ignore type differences in an
expression?
When you want to evaluate a string as a number and vice versa [3].
18. Assume the following rules of associativity and precedence for expressions:
Precedence Highest *, /, not
+, –, &, mod
– (unary) =, /=, <, <=, >=, >
And
Lowest or, xor
Associativity Left to right
Show the order of evaluation of the following expressions by parenthesizing all sub expressions and
placing a superscript on the right parenthesis to indicate order [3]. For example, for the expression
a+b*c+d
The order of evaluation would be represented as
((a + (b * c) 1)2 + d) 3
a) a * b - 1 + c
b) a * (b - 1) / c mod d
c) (a - b) / c & (d * e / a - 3)
d) -a or c = d and e
e) a > b xor c or d <= 17
f) -a + b
Expression Order of evaluation
a) a * b – 1 + c ( ( ( a * b )1 - 1 )2 + c )3
b) a * (b – 1) / c mod d ( ( ( a * ( b - 1 )1 )2 / c )3 mod d )4
c) (a – b) / c & (d * e / a – 3) ( ( ( a - b )1 / c )5 & ( ( ( d * e )2 / a )3 - 3 )4 )6
d) – a or c = d and e ( ( - a )1 or ( ( c = d )2 and e )3 )4
e) a > b xor c or d <= 17 ( ( ( a > b )1 xor c )3 or ( d <= 17 )2 )4
f) – a + b ( - ( a + b )1 )2

Reference
1. Robert W.Sebesta, Concept of Programming Language, University of Colorado at Colorado
Spring, Tenth Edition, 2012.
2. https://jessicabanna94.wordpress.com/2013/04/08/chapter-7-concepts-of-programming-
languagesrobert-w-sebesta-mr-tri-djoko-wahjono-ir-m-sc/
3. https://cundysunardy.wordpress.com/2013/04/08/chapter-7-answers/
4. https://wildanarifrahmanmauwa.wordpress.com/2014/10/27/chapter-5-names-bindings-
and-scopes/
5. https://en.wikipedia.org/wiki/Type_safety
6. https://www.chegg.com/homework-help/-11th-edition-chapter-6-problem-2rq-solution-
9780133943047

Vous aimerez peut-être aussi