Test2
What is a naming encapsulation (namespace) used for and why?
(namespace) used for and why? to create a new scope for names to help avoid naming conflicts
What are the two categories of subprograms?
1. Functions - functions structurally resemble procedures but are semantically modeled on mathematical functions (they are expected to produce no side effects in practice, program functions have side effects) 2. Procedures - procedures are collection of statements that define parameterized computations
What four items are included in the activation record for languages with stack-dynamic local variables?
1. Local variables 2. Parameters 3. Dynamic link 4. Return address
What are the three parts of a simple activation record?
1. Local variables 2. Parameters 3. Return address
What are the two options for actual/formal parameter correspondence?
1. Positional 2. Keyword
What are the two fundamental abstraction facilities?
1. Process Abstraction 2. Data Abstraction
Name four design issues for counter-controlled loops.
1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. Should it be legal for the loop variable or loop parameters to be changed in the loop body? If so, does the change affect loop control? 4. Should loop parameters be evaluated only once or for every iteration?
What are the two iteration methods?
1. counter-controlled loops (i < len; i++) 2. logically-controlled loops while (true)
What are the three models of parameter passing?
1. in mode 2. out mode 3. inout mode
What are the two primary features of abstract data types?
1. information hiding 2. packaging of data with their associated operations / program organization
What do control statements provide?
Control statements provide some means of- selecting among alternative control flow paths (of statement execution)- causing the repeated execution of statements
Present at least three arguments against the use of goto's and at least three arguments for the use of goto's when appropriate.
For: (1) can provide better performance if you can escape extra code and improve resource allocation (2) can be used for end of function error handlers or multi-level breaks from loops (3) improve program speed, size, and clarity Against: (1) can lead to overly complicated code with hard readability (2) complicate verifying correctness of programs with loops (3) programs can be easier to modify or change without gotos
Provide a reason why the Java implementers decided to use implicit garbage collection, as opposed to the explicit garbage collection of C++.
Implicit garbage collection removes the necessity of forcing users to explicitly deallocate objects, thereby eliminating the possibility of user-created dangling pointers.
Why are there destructors in C++ and not in Java?
Java has garbage collection
What does "Exclusivity of Objects" mean and what is the advantage and disadvantage of the exclusivity of objects?
Meaning: Everything is an object Adv
What is the Java scoping mechanism?
Package Scope
What is a static link?
Points to activation record of its static parent (rather than changing based on the call order)
What is a coroutine?
a subprogram that has multiple entries and controls them itself also called symmetric control
Provide an example of how to create an iterator in C.
for(p=root; p!=NULL;traverse(p)) {}
multidimensional arrays as parameters
if a multidimensional array is passed to a subprogram and the subprogram is separately compiled, the compiler needs to know the declared size of that array to build the storage mapping function
What is an alternative method of providing encapsulation used in Ada, JavaScript, and Fortran 95
nested functions/subprograms
Where can you use user-defined overloaded operators
operators can be overloaded in Ada and C++
parameter passing methods
pass-by-value pass-by-result pass-by-value-result pass-by-reference pass-by-name
Provide an example of an iterator.
perl foreach @arr = (1, 2, 3, 4, 5); $sum = 0; foreach $i (@arr) {$sum += $i;}
Provide an argument for Pythons use of indentation for compound statements in control structures.
The same structure is going to be used by everyone so readability will be much easier
nested subprograms
some non-C-based static-scoped languages (e.g., Fortran 95, Ada, JavaScript) use stack-dynamic local variables and allow subprograms to be nested all variables that can be non-locally accessed reside in some activation record instance in the stack
Where does parameter communication take place?
through run-time stack
one purpose of dynamic binding in an oo lang
used to support polymorphic variables/functions
What is an overloaded subprogram?
an 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
design issues for functions
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
pass-by-name (inout mode) Provide one example
by textual substitution formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment allows flexibility in late binding ex Assembly language macros
What is a control structure?
control statement & statements whose execution it controls
How is the iteration controlled in an iterator? based on data structure
controlled by the number of elements in the data structure control mechanism - call to an iterator function that returns the next element in some chosen order, if there is one; else loop is terminated
Name one language that does not support unconditional branching and one language that does support unconditional branching. Label each.
does not support unconditional branching: Java does support unconditional branching: C++
what is the method used to implement access to nonlocal variables in a dynamically scoped language?-
dynamic link using dynamic chain
fundamentals of subprograms
each subprogram has a single entry point the calling program is suspended during execution of the called subprogram control always returns to the caller when the called subprogram's execution terminates
the process of locating a non-local reference:
find the correct activation record instance determine the correct offset within that activation record instance
locating a non-local reference
find the offset (easy) find the correct activation record instance static semantic rules guarantee that all non-local variables that can be referenced have been allocated in some activation record instance that is on the stack when the reference is made
What is C's approach to encapsulation? What is the disadvantage with this approach?
C creates files that store subprograms. They can be accessed with header files. One major disadvantage is header dependence. If the header is changed in the library, it cannot be used unless the user changes it as well.
In Java, what is the language construct that provides abstract data types?
Classes
What is the name of the data structure used for the local variables, parameters, dynamic link, and return address?
activation record
what is a typical activation record for a language with stack-dynamic local variables
activation record created on stack dynamically created at run-time thus, additional run-time overhead
What is the difference between formal parameters and actual parameters?
actual - actual value or address in calling routine formal - dummy variable used in subprogram
concept of abstraction
an abstraction is a view or representation of an entity that includes only the most significant attributes nearly all programming languages support process abstraction with subprograms
Describe an example of where an iterator would be useful when accessing an associative array (or hash) and provide a simple example in Perl.
Assigning values to strings is where an iterator would be useful when accessing an associative array. For example, writing a program to calculate scrabble scores: my %letters = ( W => 4, X => 5, etc);
Provide an argument for and an argument against the exclusive use of boolean expressions in the control statements in Java (as opposed to allowing arithmetic expressions as in C++).
For: It is more reliable to use exclusive Boolean expressions in control statements. Programmers are able to avoid errors Against: It takes out the flexibility of being able to do more in control statements
static scoping
a static chain is 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
two methods for implementing dynamic scoping
can be done with deep access or shallow access
What is a static chain?
chain of static links that connects certain activation record instances and connects the ARI to all of its static ancestors
parameters that are subprogram names issues
it is sometimes convenient to pass subprogram names as parameters issues are parameter types checked? what is the correct referencing environment for a subprogram that was sent as a parameter?
What does encapsulation provide for large programs?
program organization
What is unusual about the C/C++ switch statement?
programmers have to explicitly break from switch statement
Two types of inheritance
single multiple
how do you implement subprograms with stack-dynamic local variables: activation record
the activation record format is generally static some languages support dynamic sized activation records
keyword
the name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter parameters can appear in any order keyword example lst_stats ( List => list1, Length => len1, Sum => sum1, Average => ave1 )
shallow access
the shallow access method does not use the stack to store the variables a central table is used in an attempt to reduce overhead the table has a stack for each variable, the top being the most recent program entity with that variable
pass-by-Value (in mode)
the value of the actual parameter is used to initialize the corresponding formal parameter normally implemented by copying when copies are used, additional storage is required storage and copy operations can be costly
Why does C++ provide a friend function or class?
to provide access to private members to units or functions unrelated to the class
What are the three common solutions for the nesting problem for two-way selectors?
(Java) - static semantics - an else matches with the nearest if (Perl) - all then and else clauses must be compound (delimited by braces) (Python) - indentation denotes if/else pairings (indentation matters)
What are the two primary local referencing environments? adv disadv?
1. Local variables can be stack-dynamic (bound to storage) advantages: support for recursion storage for locals is shared among some subprograms disadvantages: allocation/de-allocation, initialization time indirect addressing subprograms cannot be history sensitive 2. Local variables can be static more efficient (no indirection) no run-time overhead cannot support recursion
What constructs do 1) Perl and 2) Fortran use instead of break/continue?
1. Perl - last 2. Fortran - exit
Why are subprograms with stack-dynamic local variables more complex? adv disadv?
1. The compiler must generate code to cause implicit allocation and deallocation of local variables. 2. Recursion must be supported, in which there are multiple simultaneous activations of a subprogram and there may be many activations records. multiple simultaneous activations of a subprogram possibility many activation records limited only by available memory advantages memory usage disadvantage slower, additional run-time overhead
Name three design issues for two-way selection statements.
1. What is the form and type of the control expression (if (x =y ) < 3) valid?) 2. How are the 'then' and 'else' clauses specified (brackets or reserved words?) 3. How should the meaning of nested selectors be specified? (dangling else clauses)
Name two design issues for logically controlled loops?
1. pre-test (while) or post-test (do while)? mid-test (if, break)? 2. should logically controlled loops be a special case of the counting loop statement? (use expression rather than counter?)
List at least three of the actions associated with subprogram calls?
1. save execution state of caller 2. parameter passing 3. pass (or save) return address
In C++, what are the two language constructs that provide abstract data types?
1. struct 2. class
What are the two parts for a simple subprogram?
1. the actual code 2. local variables and data that can change
Explain two motivations/purposes for the guarded statements.
1. to make sure statements are executed correctly 2.better readability when using multiple statements
What are the two general categories of selection statements?
1. two-way (if then else) 2. multi-way (C++ switch)
What is the advantage of type checking parameters?
1.increases reliability 2.scripting languages do not check
The text notes that when non-local variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every non-local access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.
A faster alternative to string comparisons is assigning distinct integer values to all variable names in the program and using these integer values, instead of the string variable name, in the activation records. Comparisons would then be between integer values, which are much faster than string comparisons, where each character must be compared.
In C++ it is possible to suggest to the compiler that a function be in-lined (via the inline qualifier). Provide one (1) advantage and one (1) disadvantage for allowing the in-lining of functions.
Advantage: Can be faster due to less overhead and yield less resources for small embedded systems Disadvantage: Requires more resources like registers and too much inlining can reduce performance of certain operations like fetching from cache, increasing compile time, and create larger binary files
Fortran 95 uses unique closing reserved words on compound statements (i.e., "end if" and "end do"). Present one advantage and one disadvantage to this approach.
Advantage: better readability, easier to navigate through with multiple control statements Disadvantage: More characters to write and more keywords are needed to be used in the language
In early Fortran implementations, parameters were passed by reference (access path transmission) only. State the advantage and disadvantage of this design choice.
Advantage: subprograms have faster access to parameters when they're passed by references than when they're passed by value Disadvantage: more difficult to use recursion when values can't be passed
Name and explain one alternative to a static chain. What is an alternative method to implement access to non-local variables in a statically scoped language?
Displays - static links are stored in a single array.
Explain the difference between how C++ and Perl handle nested selections (noncompound). Provide a simple example for each language.
For nested selection statements in C++ an else clause is always paired with the nearest unmatched then clause (above the else clause). Perl requires that all then and else clauses be compound (even for single statements) brackets vs no brackets
What are the disadvantages of designing an abstract data type to be a pointer? Explain fully.
Problems with comparisons like checking equality of pointers or checking equality of structures referenced by the pointers
dvantages of data abstraction
Program organization & modifiability (everything is together) Reliability - users cannot directly access the internal representation, allowing representation to be changed without affecting user code
pass-by-value-result (inout mode), Where are the formal parameters stored when using pass-by-value-result?
a combination of pass-by-value and pass-by-result sometimes called pass-by-copy formal parameters have "local storage" disadvantages: those of pass-by-result those of pass-by-value
What is an iterator?
a general data-based iteration statement that uses a user-defined data structure to go through the structured elements
basic definitions for fundamentals of subprograms
a subprogram definition describes the interface to and the actions of the subprogram abstraction a subprogram call is an explicit request that the subprogram be executed a subprogram header is the first part of the definition, including the name, the kind of subprogram, and the formal parameters the parameter profile (aka signature) of a subprogram is the number, order, and types of its parameters the protocol is a subprogram's parameter profile and, if it is a function, its return type function declarations in C and C++ are often called prototypes a subprogram declaration provides the protocol, but not the body, of the subprogram a formal parameter is a dummy variable listed in the subprogram header and used in the subprogram an actual parameter represents a value or address used in the subprogram call statement
design issues for abstract data types?
a syntactic unit to define an ADT built-in operations assignment comparison common operations iterators accessors constructors destructors parameterized ADTs
introduction to data abstraction
an abstract data type is a user-defined data type that satisfies the following two conditions: 1)the representation of, and operations on, objects of the type are defined in a single syntactic unit 2)the representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition floating point as an abstract data type underlying representation is hidden can not manipulate part of the representation operations are clearly defined
formal parameter default values
in certain languages (e.g., C++, Ada), formal parameters can have default values (if not actual parameter is passed)
implementing parameter-passing methods
in most language parameter communication takes place thru the run-time stack pass-by-reference are the simplest to implement; only an address is placed in the stack a subtle but fatal error can occur with pass-by-reference and pass-by-value-result: a formal parameter corresponding to a constant can mistakenly be changed
encapsulation constructs
large programs have two special needs(issues): 1)some means of organization, other than simply division into subprograms 2)some means of partial compilation (compilation units that are smaller than the whole program) obvious solution: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units) such collections are called encapsulation
pass-by-reference (inout mode)
passes an access path also called pass-by-sharing passing process is efficient (no copying and no duplicated storage) disadvantages: slower accesses (compared to pass-by-value) to formal parameters potentials for un-wanted side effects un-wanted aliases (access broadened) advantages: efficient - no copying, no duplicating storage
deep access
references to non-local variables is done via the dynamic chain following the dynamic links in the applicable activation record instances this requires the variable name and value to be stored in the activation record instance this incurs additional overhead
Where are activation records typically stored?
stack
what is an iterator and how is it controlled
statement that uses user defined data structure to go through the structure elements. controlled by the number of elements.
what is the method used to implement access to non-local variables in a statically scoped language?
static link using a static chain
What are the three ways a client can reference a name from a namespace in C++.
std::cout using std::cout; using namespace std;
What is subprogram linkage?
subprogram call and return operations of a language
What is a polymorphic (or generic) subprogram (i.e. template)?
takes parameters of different types on different activations overloaded subprograms provide ad hoc polymorphism a subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism
What does a parameterized abstract data type allow?
the ADT can store any type of elements, making it more generic and flexible
positional
the binding of actual parameters to formal parameters is by position safe and effective positional example lst_stats ( list1, len1, sum1, ave1 )
unconditional branching
transfers execution control to a specified place in the program represented one of the most heated debates in 1960s and 1970s well-known mechanism: goto statement major concern: readability loop exit statements are camouflaged gotos
What are the two design considerations for parameter passing? Why are they in conflict?
two important considerations 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
What unique user-controlled loop mechanism does Perl support?
until
design issues for subprograms
what parameter passing methods are provided? are parameter types checked? are local variables static or dynamic? can subprogram definitions appear in other subprogram definitions? can subprograms be overloaded? can subprogram be generic?
pass-by-result (out mode), What is passed to the subprogram when using pass-by-result parameter passing?
when a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to callers actual parameter when control is returned to the caller requires extra storage location and copy operation potential problem: sub(p1, p1) whichever formal parameter is copied back will represent the current value of p1
In Java, suppose you wish to write a method that prints a heading on a new output page, along with a page number that is 1 in the first activation and that increases by 1 with each subsequent activation. Can this be done without parameters and without reference to a non-local variable? If so, how?
yes - make the variable static