module 2 Flashcards

1
Q

data type

A

defines a collection of data objects and a set of predefined
operations on those objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

descriptor

A

collection of the attributes of a variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

object

A

an instance of a user-defined (abstract data) type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

One design issue for all data types

A

What operations are defined and how are
they specified?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

primitive data types

A
  • Almost all programming languages provide a set of primitive data types
  • not defined in terms of other data types
  • Some primitive data types are merely reflections of the hardware
  • Others require only a little non-hardware support for their implementation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

primitive data type examples

A

Integer
Floating Point
Complex
Decimal
Boolean
Character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Value

A

sequences of characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

design issues of character strings

A

Is it a primitive type or just a special kind of array?
Should the length of strings be static or dynamic?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

typical string operations

A

§Assignment and copying
§Comparison (=, >, etc.)
§Catenation
§Substring reference
§Pattern matching

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ordinal type

A

one in which the range of possible values can be easily
associated with the set of positive integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

examples of ordinal types in java

A

§integer
§char
§boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Enumeration Types

A

All possible values, which are named constants, are provided in the definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

design issues of enumeration

A

§Is an enumeration constant allowed to appear in more than one type definition, and if
so, how is the type of an occurrence of that constant checked?
§Are enumeration values coerced to integer?
§Any other type coerced to an enumeration type?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

array design issues

A
  • What types are legal for subscripts?
  • Are subscripting expressions in element references range checked?
  • When are subscript ranges bound?
  • When does allocation take place?
  • Are ragged or rectangular multidimensional arrays allowed, or both?
  • What is the maximum number of subscripts?
  • Can array objects be initialized?
  • Are any kind of slices supported?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

4 categories of arrays

A
  • Static: subscript ranges are statically bound and storage allocation is static
    (before run-time):
    §Advantage: efficiency (no dynamic allocation)
  • Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is
    done at declaration time:
    §Advantage: space efficiency
  • Fixed heap-dynamic: similar to fixed stack-dynamic: storage binding is dynamic
    but fixed after allocation (i.e., binding is done when requested and storage is
    allocated from heap, not stack)
  • Heap-dynamic: binding of subscript ranges and storage allocation is dynamic
    and can change any number of times:
    §Advantage: flexibility (arrays can grow or shrink during program execution)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

rectangular array

A

multi-dimensioned array in which all of the rows have
the same number of elements and all columns have the same number of
elements
F# and C# support rectangular arrays and jagged arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

jagged matrix

A

has rows with varying number of elements:
§Possible when multi-dimensioned arrays actually appear as arrays of arrays
C, C++, and Java support jagged arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

slice

A

some substructure of an array; nothing more than a referencing
mechanism
Slices are only useful in languages that have array operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Access function

A

maps subscript expressions to an address in the array
for single-dimensioned arrays:
address(list[k]) =
address (list[lower_bound]) + ((k-lower_bound) * element_size)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

record

A

a possibly heterogeneous aggregate of data elements in which the
individual elements are identified by names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

design issues for records

A

§What is the syntactic form of references to the field?
§Are elliptical references allowed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

tuple

A

data type that is similar to a record, except that the elements are not
named
Used in Python, ML, and F# to allow functions to return multiple values

Python:
§Closely related to its lists, but immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Lists

A

in Lisp and Scheme are delimited by parentheses and use no commas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

union

A

a type whose variables are allowed to store different type values at
different times during execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

design issue for unions

A

Should type checking be required?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Discriminated vs. Free Unions

A

free union: C and C++ provide union constructs in which there is no language support for
type checking

discriminated: each union include a type indicator called a
discriminant (Supported by ML, Haskell, and F#)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

pointer type variable

A

a range of values that consists of memory addresses
and a special value
* Provide the power of indirect addressing
* Provide a way to manage dynamic memory
* A pointer can be used to access a location in the area where storage is
dynamically created (usually called a heap)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Design Issues of Pointers

A
  • What are the scope of and lifetime of a pointer variable?
  • What is the lifetime of a heap-dynamic variable?
  • Are pointers restricted as to the type of value to which they can point?
  • Are pointers used for dynamic storage management, indirect addressing, or
    both?
  • Should the language support pointer types, reference types, or both?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Problems with Pointers

A
  • Dangling pointers (dangerous):
    §A pointer points to a heap-dynamic variable that has been deallocated
  • Lost heap-dynamic variable:
    §An allocated heap-dynamic variable that is no longer accessible to the user program
    (often called garbage):
    o Pointer p1 is set to point to a newly created heap-dynamic variable
    o Pointer p1 is later set to point to another newly created heap-dynamic variable
    o The process of losing heap-dynamic variables is called memory leakage
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Reference Types

A

used
primarily for formal parameters:
§Advantages of both pass-by-reference and pass-by-value

  • Java extends C++’s reference variables and allows them to replace pointers
    entirely:
    §References are references to objects, rather than being addresses
  • C# includes both the references of Java and the pointers of C++
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Type checking

A

the activity of ensuring that the operands of an operator are of
compatible types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

compatible type

A

one that is either legal for the operator, or is allowed under
language rules to be implicitly converted, by compiler-generated code, to a legal
type: this automatic conversion is called a coercion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Type error

A

the application of an operator to an operand of an inappropriate type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

If all type bindings are static…

A

nearly all type checking can be static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

If type bindings are dynamic…

A

type checking must be dynamic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

what makes a programming language strongly typed?

A

type errors are always detected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Advantage of strong typing

A

allows the detection of the misuses of variables that
result in type errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

examples of strong typing

A
  • Language examples:
    §C and C++ are not: parameter type checking can be avoided; unions are not type
    checked
    §Java and C# are, almost: because of explicit type casting
    §ML and F# are
  • Coercion rules strongly affect strong typing: they can weaken it considerably
    (C++ versus ML and F#)
  • Although Java has just half the assignment coercions of C++, its strong typing is
    still far less effective than that of Ada
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Name type equivalence

A

means the two variables have equivalent types if they
are in either the same declaration or in declarations that use the same type
name

Easy to implement but highly restrictive:
§Subranges of integer types are not equivalent with integer types
§Formal parameters must be the same type as their corresponding actual parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Structure Type Equivalence

A

means that two variables have equivalent types if
their types have identical structures
More flexible, but harder to implement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

what are the problems with structured type equivalence?

A

Consider the problem of two structured types:
§Are two record types equivalent if they are structurally the same but use different
field names?
§Are two array types equivalent if they are the same except that the subscripts are
different?
e.g., [1..10] and [0..9]
§Are two enumeration types equivalent if their components are spelled differently?
§With structural type equivalence, you cannot differentiate between types of the same
structure:e.g., different units of speed, both float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

expressions

A

the fundamental means of specifying computations in a
programming language
To understand expression evaluation, need to be familiar with the orders of
operator and operand evaluation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

dominant role of assignment statements

A

Essence of imperative languages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

one of the motivations for the development of the first
programming languages

A

Arithmetic evaluation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Arithmetic expressions

A

consist of operators, operands, parentheses, and function calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

In most languages, binary operators are

A

infix, except in Scheme and LISP, in which
they are prefix; Perl also has some prefix binary operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Most unary operators are

A

prefix, but the ++ and –- operators in C-based languages
can be either prefix or postfix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

Typical precedence levels (operators)

A

§parentheses
§unary operators
§** (if the language supports it)
§*, /
§+, -

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Operator Precedence Rules

A

define the order in
which “adjacent” operators of different precedence levels are evaluated in expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Operator Associativity Rule

A

define the order in
which adjacent operators with the same precedence level are evaluated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

Typical associativity rules:

A

Left to right, except **, which is right to left
Sometimes unary operators associate right to left (e.g., in FORTRAN)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

Precedence and associativity rules can be overriden with

A

parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

Operand evaluation order

A

§Variables: fetch the value from memory
§Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction
§Parenthesized expressions: evaluate all operands and operators first
§The most interesting case is when an operand is a function call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

Two possible solutions to the problem of functional side effects

A

§Write the language definition to disallow functional side effects:
o No two-way parameters in functions
o No non-local references in functions
o Advantage: it works!
o Disadvantage: inflexibility of one-way parameters and lack of non-local references
§Write the language definition to demand that operand evaluation order be fixed:
o Disadvantage: limits some compiler optimizations
o Java requires that operands appear to be evaluated in left-to-right order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

referential transparency

A

if any two expressions in
the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Advantage of referential transparency

A

Semantics of a program is much easier to understand if it has referential
transparency

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

operator overloading

A

Use of an operator for more than one purpose
Some are common (e.g., + for int and float)
Some are potential trouble (e.g., * in C and C++):
§Loss of compiler error detection (omission of an operand should be a detectable
error)
§Some loss of readability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

narrowing conversion

A

converts an object to a type that cannot
include all of the values of the original type e.g., float to int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

widening conversion

A

an object is converted to a type that can
include at least approximations to all of the values of the original type, e.g., int
to float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Relational Expressions

A
  • Use relational operators and operands of various types
  • Evaluate to some Boolean representation
  • Operator symbols used vary somewhat among languages (!=, /=, ~=, .NE.,
    <>, #)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

Boolean Expressions

A

Operands are Boolean and the result is Boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

Short Circuit Evaluation

A

An expression in which the result is determined without evaluating all of the operands and/or operators

Example: (13 * a) * (b / 13 – 1)
§If a is zero, there is no need to evaluate (b /13 - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

types of assignments

A
  • Functional language: expressions are the building blocks:
    §If computation is expression evaluation that depends only on the referencing
    environment for that evaluation
    §Expressions in a purely functional language are referentially transparent: value
    depends only on the referencing environment
  • Imperative language: computation usually ordered series of changes to the
    values of variables in memory:
    §Assignments provide the principal means for these changes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

side effect of assignments

A

a programming construct influences subsequent computation in any
way other than by returning a value for use in the surrounding context:
§Expressions: always produce value and may have side effects
§Statements: executed solely for the side effects
§Imperative programming: computing by means of side effects

65
Q

Value model of variables

A

an expression is either an l-value or an r-value, based on the context in which it appears

66
Q

how are the semantics of an assignment different across languages?

A

Context based: a variable may refer to the value of the variable (r-value) or its location (l-value) – a named container for a value

67
Q

Reference model of variables

A

a variable is a named reference for a value –
every variable is an l-value:

68
Q

Assignment Statements

A
  • The general syntax:

<target_var> <assign_operator> <expression>
The assignment operator:
= Fortran, BASIC, the C-based languages
\:= Ada
* = can be bad when it is overloaded for the relational operator for equality:
§That’s why the C-based languages use == as the relational operator
</expression></assign_operator></target_var>

69
Q

Combination Assignment Operators

A

Imperative languages frequently update variables and can use statements like a
= a + 1; that result in redundant address calculations:

70
Q

Unary Assignment Operators

A

Unary assignment operators in C-based languages combine increment and
decrement operations with assignment
sum = ++count (count incremented, then assigned to sum)
sum = count++ (count assigned to sum, then incremented)

71
Q

Mixed-Mode Assignment

A
  • In Fortran, C, Perl, and C++, any numeric type value can be assigned to any
    numeric type variable
  • In Java and C#, only widening assignment coercions are done
  • In Ada, there is no assignment coercion
72
Q

control structure

A

a control statement and the statements whose execution it
controls

73
Q

design question for control structures

A

Should a control structure have multiple entries?

74
Q

selection statement

A

provides the means of choosing between two or more
paths of execution

75
Q

2 types of selection statements

A

§Two-way selectors
§Multiple-way selectors

76
Q

Two-Way Selection Statements

A

if control_expression
then clause
else clause

77
Q

design questions for 2 way selection statements

A

§What is the form and type of the control expression?
§How are the then and else clauses specified?
§How should the meaning of nested selectors be specified?

78
Q

Multiple-Way Selection Statements

A

Allow the selection of one of any number of statements or statement groups

79
Q

design issues for multiple-way selection statements

A

§What is the form and type of the control expression?
§How are the selectable segments specified?
§Is execution flow through the structure restricted to include just a single selectable
segment?
§How are case values specified?
§What is done about unrepresented expression values?

80
Q

approaches to implementing multiple selectors

A

§Multiple conditional branches
§Store case values in a table and use a linear search of the table
§When there are more than ten cases, a hash table of case values can be used
§If the number of cases is small and more than half of the whole range of case values
are represented, an array whose indices are the case values and whose values are
the case labels can be used

81
Q

iterative statements

A

The repeated execution of a statement or compound statement is accomplished
either by iteration or recursion

82
Q

design issues for iteration control statements

A

§How is iteration controlled?
§Where is the control mechanism in the loop?

83
Q

Counter-Controlled Loops

A

A counting iterative statement has a loop variable, and a means of specifying
the initial and terminal, and stepsize values

84
Q

design issues of Counter-Controlled Loops

A

§What are the type and scope of the loop variable?
§Should it be legal for the loop variable or loop parameters to be changed in the loop
body, and if so, does the change affect loop control?
§Should the loop parameters be evaluated only once, or once for every iteration?
§What is the value of the loop variable after loop termination?

85
Q

Logically-Controlled Loops (& design issues)

A

Repetition control is based on a Boolean expression

Design issues:
§Pretest or posttest?
§Should the logically controlled loop be a special case of the counting loop statement
or a separate statement?

86
Q

User-Located Loop Control Mechanisms

A
  • Sometimes it is convenient for the programmers to decide a location for loop
    control (other than top or bottom of the loop)
  • Simple design for single loops (e.g., break)
87
Q

Design issues for nested loops:

A

§Should the conditional be part of the exit?
§Should control be transferable out of more than one loop?

88
Q

what controls loop iteration?

A

The number of elements in a data structure

89
Q

Control mechanism for iteration

A

a call to an iterator function that returns the next element in some chosen order, if there is one; else loop is terminated

90
Q

Unconditional Branching

A
  • Transfers execution control to a specified place in the program
  • Represented one of the most heated debates in 1960’s and 1970’s
  • Major concern: Readability
  • Some languages do not support goto statement (e.g., Java)
  • C# offers goto statement (can be used in switch statements)
91
Q

Guarded Commands

A
  • Designed by Dijkstra
  • Purpose: to support a new programming methodology that supported verification
    (correctness) during development
  • Basis for two linguistic mechanisms for concurrent programming (in CSP)
  • Basic Idea: if the order of evaluation is not important, the program should not
    specify one
92
Q

Selection Guarded Command

A

Form:
if <Boolean> -> <statement>
[] <Boolean> -> <statement>
...
[] <Boolean> -> <statement>
fi</statement></Boolean></statement></Boolean></statement></Boolean>

when construct is reached:
§Evaluate all Boolean expressions
§If more than one are true, choose one non-deterministically
§If none are true, it is a runtime error

93
Q

Loop Guarded Command

A

do <Boolean> -> <statement>
[] <Boolean> -> <statement>
...
[] <Boolean> -> <statement>
od</statement></Boolean></statement></Boolean></statement></Boolean>

for each iteration:
§Evaluate all Boolean expressions
§If more than one are true, choose one non-deterministically; then start loop again
§If none are true, exit loop

94
Q

Two fundamental abstraction facilities

A

Process abstraction:
Data abstraction:

95
Q

subprogram definition

A

describes the interface to and the actions of the
subprogram abstraction

96
Q

subprogram call

A

an explicit request that the subprogram be executed

97
Q

subprogram header

A

the first part of the definition, including the name, the
kind of subprogram, and the formal parameters

98
Q

parameter profile (aka signature) of a subprogram

A

the number, order, and
types of its parameters

99
Q

protocol

A

a subprogram’s parameter profile and, if it is a function, its return
type

100
Q

prototypes

A

Function declarations in C and C++

101
Q

A subprogram declaration provides

A

the protocol, but not the body, of the
subprogram

102
Q

A formal parameter

A

a dummy variable listed in the subprogram header and
used in the subprogram

103
Q

An actual parameter

A

represents a value or address used in the subprogram call
statement

104
Q

Actual/Formal Parameter Correspondence

A
  • Positional:
    §The binding of actual parameters to formal parameters is by position: the first actual
    parameter is bound to the first formal parameter and so forth
    §Safe and effective
  • Keyword:
    §The name of the formal parameter to which an actual parameter is to be bound is
    specified with the actual parameter
    §Advantage: parameters can appear in any order, thereby avoiding parameter
    correspondence errors
    §Disadvantage: user must know the formal parameter’s names
105
Q

two categories of subprograms

A

§Procedures are collection of statements that define parameterized computations
§Functions structurally resemble procedures but are semantically modeled on
mathematical functions:
o They are expected to produce no side effects
o In practice, program functions have side effects

106
Q

Design Issues for Subprograms

A
  • Are local variables static or dynamic?
  • Can subprogram definitions appear in other subprogram definitions?
  • What parameter passing methods are provided?
  • Are parameter types checked?
  • If subprograms can be passed as parameters and subprograms can be nested,
    what is the referencing environment of a passed subprogram?
  • Are functional side effects allowed?
  • What types of values can be returned from functions?
  • How many values can be returned from functions?
  • Can subprograms be overloaded?
  • Can subprogram be generic?
  • If the language allows nested subprograms, are closures supported?
107
Q

advantages/disadvantages of stack-dynamic local variables

A

§Advantages:
o Support for recursion
o Storage for locals is shared among some subprograms
§Disadvantages:
o Allocation/de-allocation, initialization time
o Indirect addressing
o Subprograms cannot be history sensitive

108
Q

advantages/disadvantages of static local variables

A

Advantages and disadvantages are the opposite of those for stack-dynamic local
variables

109
Q

draw the semantic models of parameter passing

A
  • in mode
  • out mode
  • inout mode
110
Q

Two important considerations for Parameter Passing

A

§Efficiency
§One-way or two-way data transfer

But the above considerations are in conflict:
§Good programming suggest limited access to variables, which means one-way
whenever possible
§But pass-by-reference is more efficient to pass structures of significant size

111
Q

Conceptual Models of Transfer

A
  • Physically move a value
  • Move an access path to a value
  • Approaches:
    §Pass-by-Value (In Mode)
    §Pass-by-Result (Out Mode)
    §Pass-by-Value-Result (inout Mode)
    §Pass-by-Reference (Inout Mode)
    §Pass-by-Name (Inout Mode)
112
Q

types of binding for subprograms (3)

A
  • Shallow binding:
    §The environment of the call statement that enacts the passed subprogram
    §Most natural for dynamic-scoped languages
  • Deep binding:
    §The environment of the definition of the passed subprogram
    §Most natural for static-scoped languages
  • Ad hoc binding:
    §The environment of the call statement that passed the subprogram
113
Q

Calling Subprograms Indirectly happens when

A

there are several possible subprograms to be called and the
correct one on a particular run of the program is not know until execution (e.g.,
event handling and GUIs)

114
Q

draw an example of parameter-passing methods with:
* Function header: void sub(int a, int b, int c, int d)
* Function call in main: sub(w, x, y, z)
§Pass w by value
§Pass x by result
§Pass y by value-result
§Pass z by reference

A

answer is on slide 37

115
Q

Design Issues for Functions

A
  • Are side effects allowed?
    §Parameters should always be in-mode to reduce side effect (like Ada)
  • What types of return values are allowed?
    §Most imperative languages restrict the return types
    §C allows any type except arrays and functions
    §C++ is like C but also allows user-defined types
    §Java and C# methods can return any type (but because methods are not types, they
    cannot be returned)
    §Python and Ruby treat methods as first-class objects, so they can be returned, as
    well as any other class
116
Q

Overloaded Subprograms

A

overloaded subprogram is one that has the same name as another
subprogram in the same referencing environment:
§Every version of an overloaded subprogram has a unique protocol

  • C++, Java, C#, and Ada include predefined overloaded subprograms
  • In Ada, the return type of an overloaded function can be used to disambiguate
    calls (thus two overloaded functions can have the same parameters)
  • Ada, Java, C++, and C# allow users to write multiple versions of subprograms
    with the same name
117
Q

Generic Subprograms

A

A generic or polymorphic subprogram takes parameters of different types on
different activations
* Overloaded subprograms provide ad hoc polymorphism

118
Q

Subtype polymorphism

A

a variable of type T can access any object of
type T or any type derived from T (OOP languages)

119
Q

parametric polymorphism

A

A cheap compile-time substitute for dynamic binding
A subprogram that takes a generic parameter that is used in a type expression
that describes the type of the parameters of the subprogram

120
Q

closure

A

a subprogram and the referencing environment where it was
defined
§The referencing environment is needed if the subprogram can be called from any
arbitrary place in the program

121
Q

when are closures needed?

A

only needed if a subprogram can access variables in nesting scopes
and it can be called from anywhere

A static-scoped language that does not permit nested subprograms doesn’t need
closures

122
Q

coroutine

A

a subprogram that has multiple entries and controls them itself –
supported directly in Lua

Also called symmetric control: caller and called coroutines are on a more equal
basis

  • Coroutines repeatedly resume each other, possibly forever
  • Coroutines provide quasi-concurrent execution of program units (the
    coroutines); their execution is interleaved, but not overlapped
123
Q

resume

A

A coroutine call
The first resume of a coroutine is to its beginning, but subsequent calls enter at
the point just after the last executed statement in the coroutine

124
Q

General semantics of calls to a subprogram:

A

§Parameter passing methods
§Stack-dynamic allocation of local variables
§Save the execution status of calling program
§Transfer of control and arrange for the return
§If subprogram nesting is supported, access to nonlocal variables must be arranged

125
Q

subprogram linkage

A

The subprogram call and return operations of a language

126
Q

Call Semantics:

A

§Save the execution status of the caller
§Pass the parameters
§Pass the return address to the called
§Transfer control to the called

127
Q

Return Semantics

A

§If pass-by-value-result or out mode parameters are used, move the current values of
those parameters to their corresponding actual parameters
§If it is a function, move the functional value to a place the caller can get it
§Restore the execution status of the caller
§Transfer control back to the caller

128
Q

Required storage:

A

Status information, parameters, return address, return value for functions,
temporaries

129
Q

Environment Pointer (EP)

A

must be maintained by the run-time system. It
always points at the base of the activation record instance of the currently
executing program unit

130
Q

when are activation record instances created?

A

when a subprogram is
called

131
Q

where do activation record instances reside?

A

on the run-time stack

132
Q

where does the dynamic link point

A

to the top of an instance of the activation record of the
caller

133
Q

The activation record format is static, but its size may be

A

dynamic

134
Q

what does an activation record look like?

A

(stack top)
Local variables
parameters
dynamic link
return address

135
Q

dynamic chain, or call chain

A

collection of dynamic links in the stack at a given time

136
Q

local_offset

A

can be determined by the compiler at compile
time
Local variables can be accessed by their offset from the beginning of the
activation record, whose address is in the EP.

137
Q

Nested Subprograms

A

Some non-C-based static-scoped languages (e.g., Fortran 95+, Ada, Python,
JavaScript, Ruby, and Swift) use stack-dynamic local variables and allow
subprograms to be nested

The process of locating a non-local reference:
§Find the correct activation record instance
§Determine the correct offset within that activation record instance

138
Q

static chain

A

a chain of static links that connects certain activation record
instances
* The static link in an activation record instance for subprogram A points to one of
the activation record instances of A’s static parent
* The static chain from an activation record instance connects it to all of its static
ancestors
* static_depth is an integer associated with a static scope whose value is the
depth of nesting of that scope

139
Q

Blocks

A

user-specified local scopes for variables

140
Q

2 ways to implement dynamic scoping

A
  • Deep Access: non-local references are found by searching the activation record
    instances on the dynamic chain:
    §Length of the chain cannot be statically determined
    §Every activation record instance must have variable names
  • Shallow Access: put locals in a central place:
    §One stack for each variable name
    §Central table with an entry for each variable name
141
Q

Referential transparency means that:

A

The execution of a function always produces the same result, given the same parameters.

142
Q

Iteration is a principal mechanism in

A

Imperative programming languages

143
Q

A cactus stack is used to provide support for:

A

Declaring more than one coroutine in the same nonglobal scope.

144
Q

A closure includes only a referencing environment.

A

FALSE

145
Q

The subroutine is a principal mechanism for process abstraction

A

TRUE

146
Q

Loop categories do not include:

A

Type-controlled.

147
Q

Pointers are just addresses of memory locations

A

FALSE

148
Q

The goto statement is often used in modern programming languages.

A

FALSE

149
Q

A programming language must provide a special syntax for recursive functions.

A

FALSE

150
Q

The size of a union is the sum of the sizes of its members.

A

FALSE

151
Q

Unlike expressions, statements always have the corresponding value.

A

FALSE

152
Q

A slice is nothing more than a referencing mechanism.

A

TRUE

153
Q

The earliest fundamental abstraction facility is:

A

Process abstraction.

154
Q

Subprograms can be called indirectly, i.e., there are pointers to functions.

A

TRUE

155
Q

Two different subprograms cannot have the same name.

A

FALSE

156
Q

A subprogram that has multiple entries and controls them itself is called:

A

Coroutine

157
Q

Shallow binding is most natural for dynamic-scoped languages.

A

TRUE

158
Q

Procedures are expected to produce no side effects.

A

FALSE

159
Q

Static scoping is implemented using:

A

Static chain