Final Exam Practice
Give two examples of ways that Java is not a strongly typed language.
1.) Coercion: the implicit conversion of data from one type to another. 2.) Operator and method overloading (use + to mean both integer and float addition)
The Effect of Binding Time: Early binding times (before run time) are associated with greater _______. Late binding times (at run time) are associated with greater______.
1.) efficiency 2.) flexibility
Activation record instance
Concrete example of an activation record, a collection of data in the form of an activation record
disadvantages to garbage collection
Consumes computing resources in deciding which memory to free The moment when the garbage is actually collected can be unpredictable, resulting in stalls scattered throughout a session.
Name and explain the two dangers of pointers.
Dangling pointers and Memory leaks (or lost heap-dynamic variables)
static link
In languages that allow nested subprograms and have static scope, the static link is used to establish the values of nonlocal variables.
dynamic link
In languages without nested functions this allows for a stack trace after crashes. Note that in a program that only has static variables this isn't necessary because all the variables are in predictable locations within the memory footprint of the executing program, but once stack-dynamic variables are allowed recursion is possible, so multiple ARIs for the same subprogram might be active, and the order of those ARIs is unknown. In addition, in languages that allow nested subprograms and have dynamic scope, the dynamic link is necessary to establish the values of nonlocal variables.
Dangling pointers
Occurs when a pointer has been deleted, meaning that the memory location that it's pointing at has been deallocated, or unbound. The computer thus believes that that memory is free, and might attempt to reuse it. This could lead to an aliasing problem, where two variables refer to the same memory, and the programmer might inadvertently change a variable's value without realizing it.
what are the pros and cons of dynamic binding?
Pros: More programming flexibility: can write generic programs to fit all types of variables Cons: less reliable due to error detection (Incorrect type on right side can change left side0 Time cost: type checking must be done at runtime
counting semaphore
Semaphores which allow an arbitrary resource count
static vs. dynamic type binding
Static binding: occurs before runtime and remains unchanged throughout the program Dynamic binding: if a binding occurs first during runtime and remains unchanged throughout the program execution
scope vs lifetime
Static scope is textual or spatial, whereas lifetime is a temporal concept
Activation record
The format, or layout, of the noncode part (local variables data listed) of a subprogram Data it describes is relevant only during activation, or execution of the subprogram
What type of binding is shown here? var sum = 0 var name = "poopyhead" var dec = 0.0
This is an example of an implicit declaration of static binding.
What type of binding is shown here? (in Javascript) List = [3,4,5] List = 6.212
This is an example of dynamic binding.
lifetimes of variables
Time during which the variable is bound to a specific memory location (start: bound, end: unbound)
What is short-circuit evaluation?
When the result is determined without evaluating all of the operands and/or operators. Sometimes the value of the first operand determines the value of the entire binary operation.
What is the difference between explicit and implicit declaration?
While both of these are types of static binding, in an explicit declaration, a statement lists variable names and specifies that they are a particular type. An implicit declaration associates variables with types through default conventions (can use naming conventions of type inference)
referencing environment
________ of a statement is the collection of all variables that are visible in the statement
How is can short-circuit evaluation be advantageous?
a != 0 && b / a > 5 In this example, if a == 0, then b / a would be NaN, which can't be compared to an int, and which would lead to a runtime error. But if this expression short circuits, then in the case where a == 0, once the lefthand side is evaluated to be false, the remainder of the expression won't be evaluated (as F and X == F).
Examples of short-circuit evaluation:
a != 0 && b / a > 5 if a == 0 a > 10 || b / a > 5 when a = 15
Memory leaks
aka lost heap-dynamic variables In this situation, pointers are set to null, but the implicit heap-dynamic variables to which they pointed are not deallocated
strongly typed language
all type errors can be caught before run time
implicit heap-dynamic variables
bound to heap storage only when they are assigned values good: most flexible bad: runtime overhead and loss of some error detection think linked lists
Static variables
bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates • good for global vars and history-sensitive subprograms • efficient because variables can be directly addressed • bad because reduces flexibility and uses storage
advantages to garbage collection
easier for the programmer code is written only once, not once for every class or subprogram ever written Making pointer deletion automatic removes the possibility of dangling pointers
two types of static binding
explicit declaration and implicit declaration
Dynamic binding
if a binding occurs first during runtime and remains unchanged throughout the program execution
What type of binding uses naming conventions to specify type?
implicit declaration, which is a type of static binding
What are in mode, inout mode, and out mode?
in mode → parameters passed in and unaltered inout mode → parameters passed in and returned out mode → variable not passed in as parameter but it is returned
Implicit declaration
is a means of associating variables with types through default conventions, rather than declaration statements
explicit heap-dynamic variables
nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions written by the programmer • in heap • can only be accessed by pointer or reference variables • static binding done at compile time
What is statement level concurrency?
o Executing two or more high-level language statements simultaneously o Largely a matter of how data should be distributed over multiple memories and which statements can be executed concurrently
What is unit level concurrency?
o Executing two or more subprogram units simultaneously o Supported by MIMD(mult instr mult data)/multiprocessor computers
Static binding
occurs before runtime and remains unchanged throughout the program
garbage collection
pointer and reference variables are deallocated implicitly when they are no longer in use. In other words, periodically during program execution the program must stop its normal execution and check the heap for any memory locations that are not referenced. Those locations are then deallocated and returned to the pool of free memory.
binary semaphore
semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available)
Explicit declaration
statement in a program that lists variable names and specifies that they are a particular type
name and descirbe the binding types
static binding: Occurs before runtime begins Remains unchanged throughout the program execution Types: Explicit declaration and Implicit declaration dynamic binding: Binding first occurs during runtime or can change during the course of program execution
What are the four categories of variables according to lifetimes?
static, stack dynamic, explicit heap dynamic, implicit heap dynamic
stack-dynamic variables
storage bindings are created when their declaration statements are elaborated, but whose types are statically bound • so, static binding during program execution • Ex: local vars in a method whose declarations are deallocated when method completes execution • Good: useful in recursion and since subprograms share the same memory space for their locals. • Bad indirect addressing causes slower accesses. More runtime overhead. • int a = b + 3; b is a stack dynamic variable because it has not been bound
Scope
the range of statements in which the variable is visible, meaning where it can be referenced two types: static and dynamic
Statically scoped language
the scope of bindings is determined at compile time Used by almost all but a few programming languages More intuitive to user compared to dynamic scoping
Dynamically scoped language
the scope of bindings is determined at run time
Referential transparency
when a function accepts some value, and returns some corresponding value, without affecting other code elsewhere in the program, and always returns the same output given the same input. 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.
alias
when more than one variable name can be used to access the same memory location
Name the four levels of concurrency in a program.
• Instruction level • Statement level • Unit level • Program level
What is the difference between physical and logical concurrency?
• Physical concurrency Assuming that more than one processor is available, several program units from the same program literally execute simultaneously. Supports multiple threads of control. • Logical concurrency Allows the programmer and the application software to assume that there are multiple processors providing actual concurrency, when in fact the actual execution of programs is taking place in interleaved fashion on a single processor
Pros and cons of pass by reference
• Pros Efficient with time and space Duplicate space is not needed Copying is not needed Cons Slower access to formal parameters because of indirect addressing Inadvertent and erroneous changes may be made to the actual parameter Can create aliases (many examples) void fun( Classy x, Classy y) { //if same var passed in twice, alias created}
Pros and cons of pass by value
• Pros Fast access time • Cons Additional storage for copies required*(for formal and actual) for an array, whole array must be copied
Dynamic scoping and rule
• based on the calling sequence of subprograms, not their spatial relationship to one another • thus the scope of bindings can only be determined at run time • Scope rule: the "current" binding for a given name is the one encountered most recently during execution
pass by reference
• inout mode parameters • transmits an access path, usually just an address, to the called subprogram • the called subprogram has access to the actual parameter in the calling program unit. (In effect, the actual parameter is shared with the called subprogram)
pass by value
• value of actual parameter is used to initialize the corresponding formal parameter • the formal parameter then acts as a local variable in the subprogram • in-mode semantics • typically the actual value is COPIED