Cs 150 2nd le reviewer

Page 1

CS 150 – Programming Languages 2nd Long Exam Reviewer

Chapter 4 Names  String of _______ used to identify some entity in a program  Special words: used to make programs more readable by naming actions to be performed a) Keyboard - word that is special only in a certain contexts (by positioning) b) Reserved word - word that cannot be used as a name c) Predefined names - between reserved words and user-defined names -have predefined meanings that can be redefined by the user  _________ vs. Definitions Specify attributes and cause storage allocation Specify types and other attributes Variables  Abstraction of a computer memory call or collection of cells  Attribures: 1. Name - often referred to as identifiers 2. Address - memory address with which it is associated - sometimes called “l-value” - aliases: names that access a single memory location 3. Type - determines the range of values the variable can have and the set of operations that are defined for values of the type 4. Value - contents of the memory cell or cell associated with the variable - sometimes called “r-value” Binding  It is an association (attribute to entity, operation to symbol)  Binding time: time at which a binding takes place 1. Binding of Attributes to Variables a) Static - occurs before run time AND remains unchanged throughout program exec. b) Dynamic - occurs during run time OR can change in the course of prog. exec. 2. Type of Bindings - ________ of a variable to a data type a) Variable Declarations - create static bindings to types - Explicit declarations: statement that lists variable names with types - Implicit declarations: means of associating variables with types through default conventions instead of dec. statements : first instance/appearance = declaration b) Dynamic Type Binding - variable is bound to a type when it is assigned a value in a assigned statement 1


-

Advantage: provides a great deal of programming flexibility Disadvantage: error detection capability of compiler is diminished : cost -> type checking must be done at ________ : implemented using interpreters rather than compilers c) Type Inference – types of mist expressions can be determined without ________ the programmer to specify the types of the variables 3. Storage Bindings and Lifetime -> Allocation - taking a memory cell from a pool of available memory -> Deallocation - returning an unbound memory cell back to the pool of available memory -> Lifetime - time during which a variable is bound to a specific memory location a) Static variables - variables that are bound to memory cells before program execution begins and remain bound to then until program execution terminates - applicable to global variables - “history sensitive”: variables that retain values between separate executions of the subprogram - Advantage: efficiency -> direct addressing : no runtime overhead is incurred for alloc. and dealloc. - Disadvantage: reduced flexibility (recursion not supported) b) Stack-Dynamic Variable - variables whose storage bindings are created when their declaration statements are elaborated - elaboration - storage allocation and binding process indicated by declaration - happens during run time - allocated from the run-time stack - all attributes other than storage are statically bound - Advantage: _______ subprograms are supported - Disadvantage: runtime overhead of alloc. and dealloc. : local variables (variables declared in subprograms) cannot be history sensitive c) Explicit Heap-Dynamic Variables - nameless objects whose storage is alloc. and dealloc. by explicit run-time instructions specified by the programmer - Can only be referred through pointer variables - Type is determined at compile time - Used for dynamic structures like linked lists and trees - Disadvantage: difficulty of using them correctly : cost of references, alloc. and dealloc. d) Implicit Dynamic Variables – variables that are bound to heap storage only when they are assigned values - All attributes are bound ____ time they are assigned

2


Type Checking - activity of ensuring that the operands of an operator are of compatible types - compatible types - legal for an operator - can be implicitly converted to a legal type - coercion - automatic _________ - type error - application of an operator to an operand of an inappropriate type Strong Typing - a strongly typed language is one in which each name in a program in the language has a single type associated with it, and that type is known at compile time - a language is strongly typed if type errors are always detected Type Compatibility a) Name type compatibility - same type name b) Structure type compatibility - types have identical structures c) Declaration equivalence - type is defined with the name of another type Scope - range of statements in which the variable is visible - visible: if it can be referenced - local vs nonlocal visible within a block but is not declared there declared in a program unit a) Static scope - scope can be determined prior to execution b) Blocks - section of code with stack dynamic variables that have their storage allocated when the section is entered and deallocated when the section is exited c) Evaluation of static scoping d) Dynamic Scope - based on the calling sequence of subprograms Scope and Lifetime - they are related Referencing Environments  collection of all names that are visible in the statement  statically scoped: depends on the blocks  dynamically scoped: locally declared variables plus all the variables in other subprograms that are active *active - execution has begun but not yet terminated Named Constants  variable that is bound to a value only at the time it is bound to storage  value cannot be changed  in static binding of values, it is called “manifest constants” Variable Initialization  binding of a variable to a value at the time is bound to storage  occurs once for static variables  occurs every allocation time for dynamic

3


Chapter 5 Primitive Data Types - data types that are not defined in terms of other types a) Numeric Types i. Integer ii. Floating point - Precision: accuracy of the fractional part of a value - Range: combination of the range of fraction and range of exponents - sign bit + 8 bits exponent + 23 bits fraction (mantissa) (regular) - sign bit + 11 bits exponent + 52 bits fraction (double precision) iii. Decimal - stores a fixed number of decimal digits, without the decimal point at a fixed position in the value b) Boolean Types ďƒ˜ For C & C++: 0 for false, nonzero for true c) Character Types - stored in computers as numeric codings (ASCII) Character String Types a) String length options: 1) Static length string: length is declared alongside variable declaration 2) Limited dynamic length strings 3) Dynamic length strings User-Defined Ordinal Types - one in which the range of possible values can be easily associated with the set of positive integers a) Enumeration Types - one in which all of the possible value are enumerated in the definition b) Subrange Types - contiguous subsequence of an ordinal type Array Types - homogeneous aggregate of data elements a) Subscript bindings and array categories 1) Static Array: subscript ranges are statically bound : storage alloc. is static 2) Fixed Stack-Dynamic Array: subscript ranges are statically bound : alloc. is done during execution 3) Stack-Dynamic Array: subscript ranges and storage alloc. are dynamic : remain fixed once bound 4) Heap-Dynamic Array: both dynamic, can change b) Slices - some substructure of an array Record Types - possibly heterogeneous aggregate of data elements a) Fully qualified reference - all intermediate record names are named in reference b) Elliptical reference - field is named but any or all of the encoding record names can be omitted as long as the resulting reference is unambiguous

4


Pointer Types - are in which the variables have a range of values that consists of memory addresses and a special value, ____ - 2 distinct kinds of uses: a) Provide same of the power of indirect addressing b) Provide a method of dynamic storage management - Dynamic variables: variables that are dynamically alloc. from the heap - Anonymous variables: variables without names - 2 fundamental pointer operations: a) Assignment - sets a pointer variable to the address of the same object b) Dereferencing - Domain type: type of object to which a pointer can point - Dangling pointers / dangling reference: pointer that contains the address of a dynamic variable that has been deallocated - Lost object: allocated dynamic object that is no longet accessible to the user program but may still contain useful data - Solutions to the dangling pointer problems: a) _______ - acts as a mediator between pointers and dynamic variables b) Locks-and-keys approach - only same blocks and keys are allowed access - Heap management: ďƒ˜ Reference Counter Method (eager approach) ďƒ˜ Garbage Collection Approach (lazy approach

5


Chapter 6 Arithmetic Expressions  Constructions of operators, operands, parenthesis, and function cells  Operator Evaluation Order a) Precedence - define the order in which the operators of different precedence levels are evaluated b) Associativity - right associativity (right most occurrence is evaluated first) - left associativity (left most occurrence is evaluated first) c) Parenthesis - a parenthesis part has precedence over its adjacent unparenthesized parts  Operand Evaluation a) Side effects - functional side effects: occurs when the function changes either one of its parameters or a global variables  Conditional Expressions - ternary operator ( ? : ) Overloaded Operators – when operators have more than 1 use Type Conversions  Narrowing conversion: converting an object to a type that cannot include all of the values of the original type  Widening conversion: converting an object to a type that can include at least approximations of all the values of the original type a) Coercion in Expressions - Mixed-mode expressions: expressions with an operator with operands of different types - Disadvantage: eliminate the benefits of type checking : lowers reliability b) Explicit Type Conversions - “casts” Relational and Boolean Expressions a) Relational Expressions - Relational operator: operator that compares the values of its two operands b) Boolean Expressions - Consist of Boolean variables, B. constants, relational expressions, and Boolean operators Short-Circuit Evaluation  The result is determined without evaluating all of the operands and/or operators Assignment Statements a) Simple Assignments b) Multiple Targets c) Conditional Targets d) Compound Assignment Operators e) Unary Assignment Operators f) Assignment as an Expression

a,b = 0 a? b:c=0 = a? then b=0 else c=0 a+=b ++, - -

6


Chapter 7 Compound Statements - allow a collection of statements to be abstracted to a single statement Selection Statements - provide the means of choosing between two or more execution paths in a program a) Two-way Selection Statements  Nesting Selections - to which “then clause” is an “else clause” paired?  Special Words and Selection Closure - wage of “end” and “end if” b) Multiple Selection Constructs - allows the selection of one of any number of statements or statement groups Iterative Statements - causes a statement or a collection of statements to be executed zero, one, or more times - primary possibilities for iteration control a) Logical b) Counting c) Combination - location of the control mechanism -> top (pretest) or bottom (posttest) of loop - iteration construct; iteration statement + loop body a) Counter-Controlled Loops  loop variable - where the count value is maintained  initial + terminal values  stepsize - difference between adjacent loop variable values b) Logically Controlled Loops - based on a Boolean expression c) User-Located Loop Control Mechanisms - break; and continue; d) Use-Defined Iteration Control  iterator: called at the beginning of each iteration, and each time it is called, the ______ returns on element from a particular data structure in some specific order : must be history sensitive Unconditional Branding  Unconditional branch statement - transfer execution control to a specified place in the prog.  “goto” -> effects on recodability, reliability, and maintainability a) Label forms b) Restrictions on Branches Guarded Commands - like normal selection and loop statements, only the choices are chosen at random because all Boolean expressions are evaluated

7


Chapter 8 Basic Definitions  Subprogram definition - describes the interface to and the actions of the subprogram abstraction  Subprogram call - explicit request that the subprogram be executed  Subprogram header - first line of the definition - purposes: a) Specifies that the ff syntactic unit is a subprogram definition of same particular kind b) Provides a name for the subprogram c) May optionally specify a list of parameters  Parameter profile - number, order, and types of its formal parameters  Protocol - parameter profile + return type  Subprogram declarations - prototypes ( c )  Parameterized computation: subprogram with parameter access to the dare it is process  Formal parameters: parameters in the subprogram header : during variables  Actual parameters: list of parameters to be bound to the formal parameters : included in the subprogram call  Positional parameters: correspondence between actual and formal parameters  Keyword parameters: the name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter Procedures and Functions define new user-defined operators; no side effects; with return val. collections of statements that define parameterized computations; who return val. Parameter-Passing Methods  Semantic Models: (pass-by-value) 1. IN MODE - they (formal param) can receive dara from the corres. actual param (pass-by-result) 2. OUT MDOE - they can transmit data to the actual parameter (pass-by-value-result) 3. INOUT MODE - they can do both (pass-by-reference) (pass-by-name

CREDITS: Notes by Camille Salazar Encoded by Gerald Roy Campañano

8


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.