Final Exam

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

what is the Lazy approach to reclaiming garbage

(mark sweep) in which reclamation occurs only when the list of available space becomes empty

what is the Eager approach to reclaiming garbage

(reference counters) one approach for garbage collection, in which reclamation is incremental and is done when inaccessible cells are created

22. What are the advantages of named constants?

A named constant is a variable that is bound to a value only once, it is useful for program readability and reliability, and it used to parameterize the program so it is easy to modify the program.

what is row major order and column major order?

A way of storing a 2-dimensional array in memory. o The matrix 3 4 7 6 2 5 1 3 8 is stored as 3, 4, 7, 6, 2, 5, 1, 3, 8 in row major order(c) and 3, 6, 1, 4, 2, 3, 7, 5, 8 in column major order(fortran)

what are the advantages and disadvantages of decimal data types?

Advantages: being able to precisely store decimal values, which cannot be done with floating-point. Disadvantages: • The range of values is restricted because no exponents are allowed. • They take more storage than binary numbers

3. What is an alias?

Alias happens when multiple variables have the same address, which means when more than one variable name is used to access the same memory location.

Why are reference variables in C++ better than pointers for formal parameters?

Because they provide for two-way communication between the caller function and the called function. Passing a pointer as parameter accomplishes the same two way communication, but pointer formal parameters require explicit dereferencing making the code less readable and less safe.

6. Define binding and binding time.

Binding: is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol. Binding time: The time at which the binding takes place is called binding time.

What is an Imperative (procedural) Language?

Building blocks are procedures and statements

Why are C and C++ not strongly typed?

C and C++ are not strongly typed because both include union types which are not type checked.

7. After language design and implementation what are the four times bindings can take place in a program?

Compile time, load time, link time, or run time.

10. What are the advantages and disadvantages of dynamic type binding?

Dynamic type binding: • when the type of a variable is not specified by a declaration statement, • nor it can be determined by the spelling of its name. • Instead the variable is bound to a type when it is assigned a value in an assignment statement. • When the assignment statement is executed the variable being assigned is bound to the type of the value of the expression on the right side of the assignment • In Python, Ruby, JavaScript, and PHP type binding is dynamic • Example: in JavaScript you can write list = [1, 2, 3] and then change list to list=47. Advantages: • Any variable can be assigned any type value. • Variable's type can change any number of time during program execution • More programming flexibility Disadvantages: • Program less reliable o Error detection capability is diminished • Allows any variable to be assigned a value of any type o Incorrect type of the right side are not detected as errors • binding cost o Type checking is done at run time o Storage used for the variable must be of varying size o Variable must have run-time descriptor • Usually implemented using pyre interpreter

What is the primary advantage of name type equivalence?

Easy to implement but is more restrictive

9. What are the advantages and disadvantages of implicit declarations?

Explicit declaration: is a statement in a program that lists variable names and specifies that they are a particular type. It creates static binding to types. Implicit declaration: is a means of associating variables with types through default conventions, rather than declaration statements. It creates static binding to types

What are some Imperative (procedural) Languages?

FORTRAN (1954) Pascal (1970) C (1971)

What advantages do Java and C# reference type variables have over the pointers in other languages?

Flexibility and compatibility.

What are the required entries in java array descriptor, and when must they be stored (at compile time or run time)

For a single-dimensioned array the descriptor is element type, index type, index lower bound, index upper bound, and address. And this is done at compile time.

What is implementation?

How a program executes (on a real machine)

What is a descriptor in an implementation?

In an implementation, a descriptor is the collection of memory cells that store the attributes of a variable.

13. How is a reference to a nonlocal variable in a static-scoped program connected to its definition?

In static-scoped language with nested subprograms, suppose a reference is made to a variable x in subprogram sub1. • First search for the declarations of subprogram sub1 • If no declaration is found there o Search continues to the next larger enclosing unit (sub1's parent) o And so forth o Until a declaration for x is found or o the largest unit's declarations have been searched without success in that case, an undeclared variable error is reported. • The static parent of subprogram sub1, and its static parent and so forth up to and including the largest enclosing subprogram, are called static ancestors of sub1. • Example consider the following JavaScript function big in which two functions are nested sub1 and sub2 function big(){ function sub1(){ var x = 7; sub2(); } function sub2(){ var y = x; } var x = 3; sub1(); } • Under static scoping: o The reference to the variable x in sub2 is to the x declared in the procedure big, because: The search for x begins from the procedure in which x occurred, sub2, No declaration for x is found in sub2 The search continues to the static parent of sub2, big, where the declaration of x is found. The x declared in sub1 is ignored because it is not in the static ancestry of sub2.

Why Java is not strongly typed?

Java is based on C++ which is not strongly typed, types can be explicitly cast which could result in type error.

12. Define lifetime, scope, static scope and dynamic scope.

Lifetime: • Lifetime of a variable is the period of time beginning when the method is entered and ends when the execution of the method terminates Scope: • The range of statements in which the variable is visible Visible variable for a statement means that it can be referenced or assigned in that statement Local variable in a program unit or block means that it is declared there Nonlocal variable in a program unit or block means that it is not declared there but it is visible there. static scope • Binding names to none local variables is called static scoping dynamic scope • Based on the calling sequence of subprograms not on their spatial relationship to each other. • This scoping can be determined only at run-time

What is structure type equivalence?

Means that two variables have equivalent types if their types have identical structure.

What is name type equivalence?

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

18. What is the purpose of the let constructs in functional languages?

Most functional programming languages include a construct that is related to the blocks of imperative languages. This construct is named let. Example in scheme (LET ( (name1 expression1) ............. (namen expressionn)) expression ) (LET ( (top (+ a b)) (bottom (- c d))) (/ top bottom) )

What is the potential danger of case-sensitive names?

Names that look very similar denote different entities. For example, HELLO, hello, Hello look similar, but there is no connection between them.

Are the tuples of Python mutable?

No they are immutable, if a tuple needs to be changed it can be converted to an array with the list function. After the change, it can be converted back to a tuple with the tuple function.

what are some examples of logic programming languages?

PROLOG

4. Which category of C++ reference variables is always aliases?

Pointers, when two pointer variables point to the same memory location.

12. What languages support negative subscripts?

Ruby and Lua support negative subscripts.

What are some examples of OO programing languages?

Smalltalk (1969) • C++ (1986) • OCaml (1987) • Ruby (1993) • Java (1995)

14. What is the general problem with static scoping?

Some variable declarations can be hidden from some other code segments. Back to JavaScript example: the declaration of x happened in both big and sub1 functions. In sub1 every simple reference to x is to the local x. therefore the outer x is hidden from sub1.

16. What is a static ancestor of a subprogram? What is a dynamic ancestor of a subprogram?

Static ancestor: The static parent of subprogram sub1, and its static parent and so forth up to and including the largest enclosing subprogram, are called static ancestors of sub1. Dynamic ancestor: dynamic scoping calls sequence of subprograms dynamic parent is known as the calling function. • The value of x cannot be determined at compile time, it depends on the calling sequence • Begin the search with local declaration • When the search of local declaration fails the declaration of dynamic parent or calling function are searched. • If the declaration for x is not found there, the search continues in that function's dynamic father and so forth • Until the declaration of x found • If none found then there is an error function big(){ function sub1(){ var x = 7; sub2(); } function sub2(){ var y = x; } var x = 3; sub1(); } For the sequence: first big calls sub1 which calls sub2 • First the search proceeds the local search of sub2, nothing is found • The search continues to sub1() which is the dynamic parent of sub1(), x is declared there o So the reference to x in sub2() is to the x declared in sub1() • Next sub2() is directly called from big() o The dynamic parent of sub2() is big o The reference to x is declared in big

8. Define static binding and dynamic binding.

Static binding: when binding first occurs before run time begins and remains unchanged throughout program execution. Dynamic binding: when binding first occurs during runtime or can changed in the course of program execution.

11. Define static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic variables. What are their advantages and disadvantages?

Static variable: • is bound to memory cells before program execution begins • and remain bound to those same memory cells until program execution terminates. • Advantage: o Subprograms are history sensitive o Efficiency o No run-time overhead is incurred for allocation and deallocation • Disadvantages: o Reduced flexibility o Cannot support recursive subprograms o Storage cannot be stored among variables Stack-dynamic variable: • Whose storage bindings are created when their declaration statements are allocated, during the run time • But whose types are statically bound • Example the variable declaration in Java, that appears at the beginning of the method o Allocated at the beginning of the method and o Deallocated when the method completes its execution • Advantages o Supports recursive subprograms Each copy of the subprograms has its own version of the local variable • Disadvantages: o Run-time overhead of allocation and deallocation is slower compared to static variables o Subprograms cannot be history sensitive Explicit heap-dynamic variable: • Are nameless memory cells • Allocated and deallocated by the programmer • Using explicit run-time instructions written by the programmer • C++ example: int *intnode; // Creates a pointer intnode = new int; // Create the heap-dynamic variable ......... delete intnode; // Deallocate the heap-dynamic variable to which //intnode points • In Java all data except the primitive data types are objects o Objects are explicitly heap dynamic and are accessed through reference variables • Advantages: o Used to construct dynamic structures, linked lists and trees • Disadvantages: o Difficulty of using pointers and reference variables correctly o Cost of references to variables o Complexity of the required storage management implementation implicit heap-dynamic variable: • Bound to heap storage only when they are assigned values • Advantages o Highest degree of flexibility Generic code • Disadvantages o Run-time overhead o Loss of some error detection by the compiler

What is an access function for an array?

The access function for a multidimensional array is the mapping of its base address and a set of index values to the address in memory of the element specified by the index values.

What are the design issues for names?

The following are the primary design issues for names: • Are names case sensitive? • Are the special words of the language reserved words or keywords? o Reserved word is a special word that cannot be used as a name, and redefined by the user o Keywords can be redefined by the user

What is Syntax?

What a program looks like

what is semantics

What a program means (mathematically)

What is a Functional Language?

a language that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

what is strongly typed?

a programming language is strongly typed if type errors are always detected. This requires that the types of all operands can be determined at compile time or at run time.

What is a Object-Oriented Languages?

a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods

What is a Logic-Programming Language?

a set of sentences in logical form, expressing facts and rules about some problem domain.

What is an array slice?

a slice of an array is some substructure of that array

In what ways are the user-defined enumeration types of C# more reliable than those of C++?

a) No arithmetic operations are legal on enumeration, for example preventing adding two weekdays. b) No enumeration variable can be assigned a value outside its range.

what is an associative array?

an unordered collection of data elements that are indexed by an equal number of values called keys.

What is coercion?

automatic type conversion • Coercion rules have an important effect on the value of type checking. • Coercion causes loss of the benefits of type checking • Suppose a program had the two int variables a and b and the float variable d. If the programmer meant to type a + b but mistakenly typed a and d, • The error will not be detected by the compiler • And the value of a would simply be coerced to float

What is a descriptor?

collection of a variables

What are some attributes of a good programming languages?

cost of use portability of programs programming environment Clarity, simplicity, and unity Orthogonality(every combination of features is meaningful) Naturalness for the application Support for abstraction Security & safety Ease of program verification

What is Enumeration type?

in this type all the possible values are • named constants that are • provided or enumerated in the definition. • Example in C# enum days{Mon, Tue, Wed, Thu, Fri, Sat, Sun}

what is a C++ reference type?

is a constant pointer that is always implicitly dereferenced, their common use is formal parameters in function definitions.

what is a dangling pointer?

is a pointer that contains the address of a heap-dynamic variable that has been deallocated.

What is Subrange type?

is a subsequence of an ordinal type. Subrange types were introduced in Pascal and Ada.

What is a union?

is a type whose variables may store different type values at different times during program execution

What is lost heap dynamic variables?

is an allocated heap-dynamic variable that is no longer accessible to the user program. Such variables are called garbage, not useful for original purpose and they cannot be reallocated for some new use in the program.

What is Ordinal type?

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

what is a character string type?

is one in which the values consists of sequences of characters.

what is compatibility type?

is one that either is legal for the operator or is allowed under language rules to be implicitly converted by compiler generated code to a legal type.

what is a reference type?

is similar to a pointer, with one important and fundamental difference a pointer refers to an address in memory, while a reference refers to an object or a value in a memory.

what is type checking?

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

What is type error?

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

5. What is the l-value? and what is the R-value?

l-value: The address of the variable, because the address is required when the name of a variable appears in the left side of an assignment. r-value: The value of the variable, because the value of the variable is required to appear in the right side of ab assignment.

20. Describe the encapsulation of an F# let inside a function and outside all functions.

let n1 = let n2 = 7 let n3 = n2 + 3 n3;; let n4 = n3 + n1;; • The scope of n1 extends over all of the code. • The scope of n2 and n3 ends when the indentation ends (the use of n3 in let n4 = n3 + n1 is an error) • The last line of the let n1 scope is the value bound to n1 and this could be any expression

What languages have no type coercions?

o C and C++ have a great deal of coercions o ML and F# have no coercion o Java and C# have half as many assignment with no coercions as C++.

Examples of array slice in python

o Vector = [2, 4, 6, 8, 10, 12, 14, 16] o mat = {[1, 2, 3], [4, 5, 6], [7, 8, 9]} o default lower bound for Python is 0 o the slice vector[3:6] is a three elements array with the fourth through the sixth element which is [8, 10, 12] o mat[1] will return [4, 5, 6] which is the second row of mat o part of the row can be specified mat[0][0:2] returns the first and the second element of the first row which are 1, 2 o vector[0:7:2] references every other element of vector starting with the subscript 0, which is [2, 6, 10, 14]

what is a free union?

programmers are allowed to complete freedom from type checking in their use

In what primarily imperative language do lists serve as arrays?

python

What are the advantages of user-defined enumeration types?

readability and reliability

What is a Decimal data type?

stores a fixed number of decimal digits, with the implied decimal point at a fixed position in the value.

what is a discriminated union?

type checking of unions requires that each union construct include a type indicator

Are the unions of F# discriminated?

yes

10. What happened when a nonexistent element of an array is referenced in Perl?

yields undefinded

what is a record?

• A record is an aggregate of data elements in which the individual elements are identified by names and accessed through offsets from the beginning of the structure. • In C and C++ records are supported by struct data type

17. What is a block?

• A section of code has its own local variables whose scope is minimizes. Such variables are typically stack dynamic Their storage is allocated when the section is entered and deallocated when section is exited o Example: C function Void sub() { Int count; ....... While(......) { Int count; Count++; .......... } ...... }

what is a tuple?

• A tuple is a data type that is similar to a record, except that the elements are not named.

21. What are the advantages and disadvantages of dynamic scoping?

• Advantages • Disadvantages

11. How does JavaScript support sparse arrays?

• Arrays can be sparse which means subscript values need not be contiguous. • Example: o suppose we have an array named list that has 10 elements with the subscripts 0....9 o and consider the following statement list[50] = 42; o Now list has 11 elements and length 51. o The elements with the subscripts 11....49 are not defined and therefore do not require storage.

Records in Cobol

• COBOL example: 01 EMPLOYEE-RECORD • 02 EMPLOYEE-NAME o 05 FIRST PICTURE IS X(20) o 05 MIDDLE PICTURE IS X(10) o 05 LAST PICTURE IS X(20) • 02 HOURLY-RATE PICTURE IS 99v99 The numerals 01, 02, 05 are level numbers which indicate by their relative values the hierarchical structure of the record Any line that is followed by a line with a higher-level number is itself a record.

What are the two common problems with pointers?

• Dangling pointers: is a pointer that contains the address of a heap-dynamic variable that has been deallocated. The type might be changed The value might be changed While the pointer is the same which is dangerous • Lost Heap-Dynamic variables: is an allocated heap-dynamic variable that is no longer accessible to the user program. Such variables are called garbage, not useful for original purpose and they cannot be reallocated for some new use in the program.

example of a tuple in Python

• Example Python myTuple = (3, 5.8, 'apple') myTuple[1] returns 3 tuple indexing starts at 1 elements of tuples are not necessarily the same type

What is the structure of an associative array?

• Example: Perl. o In Perl arrays are called hashes, because in the implementation their elements are stored and retrieved with hash functions o The namespace for Perl hashes is distinct o Each hash element consists of two parts: a key which is a string and a value which is a scalar (number, string, or reference) o Hashes can be set to literal values with the assignment statement as %salaries = ("Gary" => 75000, "Perry" => 57000, "Mary" => 55750, "Cedric" => 47850)

What is the purpose of F# tuple pattern

• In F# the elements of a tuple with more than two elements are often referenced with tuple pattern on the left side of a let statement • A tuple pattern is simply a sequence of names, one for each element of the tuple with or without the delimiting parentheses. • When a tuple pattern is the left side of a let construct, it is a multiple assignment for example let tup = (3, 5, 7);; let a, b, c = tup;; this assign 3 to a, 5 to b, and 7 to c.

what are some some functional languages

• LISP (1958) • ML (1973) • Scheme (1975) • Haskell (1987)

13. What languages support array slices?

• Languages supports slices are: Python, Perl, Ruby

On what are Python's list comprehensions based?

• Powerful mechanism for creating arrays • The idea is: a function is applied to each of the elements of a given array and A new array is constructed from the results o The syntax of Python's list comprehension is: [Expression for iterate_var in array if condition] • Example: [x * x for x in range(12) if x % 3 == 0] The rang creates the array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] The conditional filters out all numbers in the array that are not divisible by 3, and squares the remaining numbers. This list comprehension returns the following array: [0, 9, 36, 81]

what are the design issues for a character string type?

• Should strings be a special kind of character array or a primitive type? • Should strings have static or dynamic length?

what is a Fixed heap-dynamic array?

• Subscript ranges and the storage binding are both fixed after storage is allocated. • Both subscript ranges and storage bindings are done when the user program requests them during execution and • storage is allocated from the heap rather than the stack. • Advantages: o Flexibility • Disadvantage: o Heap allocation time is longer than stack allocation time. • Example o C and C++ provide fixed heap-dynamic arrays In C malloc and free are general heap allocation and deallocation In C++ new and delete are used for heap allocation and deallocation o Java generic arrays are fixed heap-dynamic o C# provides fixed heap-dynamic arrays

what is a Fixed stack-dynamic array ?

• Subscript ranges are statically bound, but • The allocation is done at declaration elaboration time during execution • Advantages: o Space efficiency • Disadvantages: o Required allocation an deallocation time • Example o Arrays declared in C and C++ functions without static modifier

What is the action of scheme function CAR?

• The CAR function returns the first element of its list parameter • Example: (CAR '(A B C)) The quote before the parameter list is to prevent the interpreter from considering the list a call to the function A with parameters B and C This call to CAR returns A

In what way does scheme's CDR function modify its parameters?

• The CDR function returns its parameter list minus its first element • Example: (CDR '(A B C)) • This function will return the list (B C)

What is a heap-dynamic array?

• The binding of subscript ranges and storage allocation is dynamic and can change any number of times during the array's life time • Advantages: o Flexibility: array can grow and shrink as needed • Disadvantage: o Allocation and deallocation take longer time and may happen many times during execution of the program • Example o C# objects of list class are generic heap-dynamic, (elements are added using add method and access to the elements is used by subscripting) o Java array list is generic heap-dynamic, (subscripting is not supported)

19. What is the difference between the names defined in an ML let construct from the variables declared in C block?

• The first n expressions are evaluated and the values are assigned to the associated names • Then the final expression is evaluated and the return of value of LET is that value • They are like local variables in a block in an imperative language in that their scope is local to the call to LET. Example ML: let val name1 = expression1 .................. val namen = expressionn in expression end; let val top = a + b val bottom = c - d in top/bottom end;

15. 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 • In a static scoped language is o the variables declared in its local scope, plus o the collection of all variables of its ancestor scopes that are visible o needed when the statement is being compiled • Example void sub1(){ int a, b; . . . <-----------------------------------1 } /*end of sub1()*/ void sub2(){ int c, b; . . . <-----------------------------------2 sub1(); }/*end of sub2*/ void main(){ int c, d . . . <-----------------------------------3 sub2(); }/*end of main*/ The referencing environment of the indicated program are as follows: POINT REFERENCEING ENVIRONMENT 1 Local a and b of sub1, c of sub2, d of main 2 Local b and c of sub2 and d of main 3 C and d of main

what is a static array?

• The subscript ranges are statically bound • Storage allocation is static • Advantages: o No dynamic allocation and deallocation is required • Disadvantages: o The storage for the array is fixed for the entire execution time of the program. • Example o Arrays declared in C and C++ functions that includes static modifier

What are the design issues of pointer types?

• What are the scope and lifetime of a pointer variable? • What is the lifetime of a heap-dynamic of a pointer 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?

What are the design issues for arrays?

• What types are legal for subscripts? • Are subscripting expressions in element references range checked? • When subscript ranges bound? • When does array allocation take place? • Are ragged or rectangular multidimensional arrays allowed, or both? • Can arrays be initialized when they have their storage allocated? • What kind of slices are allowed, if any?

Limited dynamic length strings?

• allow strings to have varying length up to a declared fixed maximum • set by the variables definition. • String variables can store any number of characters between zero and the maximum. • Strings in C

what are Dynamic length strings?

• allows strings to have varying length with no maximum • strings in JavaScript

What are Static length strings?

• the length is static and • set when the string is created, • strings in Python (strings in Python are primitive data types)

What is the action of F# function tl?

• tl is a method of the list class • tl function returns the tail of the list • example the list [1; 2; 5; 7] List.hd will return 1 (head) List.tl will return [2; 5; 7] (tail)


Ensembles d'études connexes

Abnormal Psychology Exam 2 Learning Curve 11a

View Set

Sadlier-Oxford level b unit 11 vocab

View Set

Principles of Management - University of Minnesota Libraries Publishing

View Set

Chapter 4.1 Intro to probability

View Set

Ch 6 Lesson 2 How do plants use sunlight to make food?

View Set