Prog Lang Quiz 2, Unit 5: Binding & Scope

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the difference between Reserve Words and Keywords?

"Keywords are specific words with predefined meanings in a programming language, while reserved words can include both keywords and other reserved identifiers, which may have broader purposes in some languages." ...or more simply... a "keyword" is a word that is special only in certain contexts. a "reserved word" is a special word that cannot be used as a user-defined name

What are the two main types of Binding?

1. Static Binding - first occurs before run time and remains unchanged throughout program execution. The type of the object is determined at compile time. 2. Dynamic Binding - first occurs at run time or can change during the program execution.

What is a Name?

A string of characters used to identify some entity in a program.

What is block scoping of a variable?

Aka local scoping. variables declared inside a block of code are only accessible within that block (local region of code) and its nested blocks, creating a limited and well-defined scope for those variables. In block scoping, variables have the narrowest possible scope, providing better control over their lifetimes and reducing the risk of naming conflicts.

What is Type Binding?

Before a variable can be referenced in a program, it must be bound to a data type. It can occur statically at compile-time (static typing) or dynamically at runtime (dynamic typing), and it can be implicit (type inference) or explicit (type annotations) depending on the programming language's design and type system ...See Advantages and disadvantages below... Advantages: Type Safety, Performance Optimization, Documentation Disadvantages: Rigidity(can make the language less flexible and require more verbose (lengthy/wordy) code), Complexity, Learning Curve

What is a Reserve Word?

Depending on the language, these are words that are reserved from being used in any other way. Ex: int, float, double, char, void, public, static, throws, return

What could happen if you have too many reserved words in your language?

Having too many reserved words in a programming language can reduce code readability, increase the learning curve for developers, and lead to unintended conflicts with user-defined names, making maintenance and code portability more challenging.

What is the difference between lifetime and scope?

Lifetime refers to the duration or existence of a variable in memory from the moment it is created or allocated until it is no longer accessible or deallocated. Scope refers to the region or portion of code within which a variable is recognized, accessible, and can be used. Lifetime and scope are related because the scope of a variable can affect its lifetime. For example, a local variable in a function has a scope limited to that function, and its memory is often deallocated when the function exits, impacting its lifetime. Variables with local scope typically have a lifetime tied to the duration of the scope in which they are defined. When the scope exits, the variables often go out of scope and may have their memory deallocated (automatic storage duration). Variables with global scope typically have a longer lifetime, spanning the entire program's execution (static storage duration).

What is Binding Site?

The association or connection between an identifier (such as a variable or function) and its corresponding value or memory location. Variable Binding Site: def add(a, b){ //code } // 'add' is the function name, and the binding site is the function definition where 'add' is bound to the code block that defines the function. Function Binding Site: def add(a, b) { return a + b; } // 'add' is the function name, and the binding site is the function definition where 'add' is bound to the code block that defines the function. Dynamic Binding Sites: x = 5; // 'x' is bound to the value 5. x = "hello"; // 'x' is rebound to the string "hello."

Variable with Stack Dynamic Lifetime

The storage duration of the variable has a lifetime That is determined dynamically during the execution of a program. It is tied to the program's call stack, which keeps track of function calls and their local variables. Variables with stack dynamic storage duration are usually allocated and deallocated automatically as functions are called and return. When a function is called, its local variables are allocated on the stack, and when the function returns, those variables are automatically deallocated. Variables with stack dynamic storage duration have a limited lifetime within the scope of the function in which they are defined. Once the function exits, the memory used by these variables is automatically reclaimed. Most local variables in programming languages like C and C++ have stack dynamic storage duration.

Variables with Implicitly Heap Dynamic Lifetime

The storage duration of the variable has a lifetime that allocated on the heap (dynamically) without the programmer needing to explicitly manage their allocation and deallocation. These variables are typically created and managed by the programming language runtime or a specific framework or library, rather than the programmer manually using memory allocation functions. These variables are allocated and deallocated automatically by the programming language runtime or a memory management system. The programmer does not need to explicitly allocate or deallocate memory for them. Many programming languages that support implicitly heap-dynamic variables use garbage collection mechanisms to automatically identify and free memory that is no longer in use. This helps prevent memory leaks and reduces the programmer's burden of memory management. Implicitly heap-dynamic variables often have a more flexible lifetime than stack-based variables. They can be created and accessed across different parts of the program without concerns about scoping or function calls. Examples of implicitly heap-dynamic variables can be found in languages like Java, C#, and Python, where objects are created on the heap and managed by a garbage collector.

Variables with Explicitly Heap Dynamic Lifetime

The storage duration of the variable has a lifetime that is allocated and deallocated explicitly by the programmer during runtime. These variables are allocated on the heap (also known as the free store in some programming languages) rather than the stack, and their lifetime is not tied to a specific scope or function call. The memory for these variables is allocated dynamically at runtime using functions like malloc (in C), new (in C++), or equivalent functions in other programming languages. The programmer specifies the size of the memory block to be allocated. It's the programmer's responsibility to explicitly deallocate the memory when it's no longer needed using functions like free (in C), delete (in C++), or similar methods in other languages. Failure to do so can lead to memory leaks. The lifetime of heap-dynamic variables is not tied to a specific function or scope. They exist until explicitly deallocated. This allows for more flexible memory management compared to stack-based variables. Heap-dynamic variables can be accessed and modified from different functions or scopes, making them suitable for data structures that need to be shared and manipulated across various parts of a program. Unlike stack-based variables, heap-dynamic variables may not be automatically initialized to a default value. It's the programmer's responsibility to initialize them explicitly.

Variables with Static lifetime:

The storage duration of the variable has a lifetime that spans the entire duration of the program. These variables are often initialized only once, typically at the beginning of the program's execution. Their initial values are maintained throughout the program. These variables are often declared at the global level, outside of any functions and can be accessed from any part of the program. In languages like C and C++, they are declared using the static keyword

What is Binding Time?

The time at which binding takes place. - e.g., language design time, - e.g., compile time, - e.g., load time, - e.g., link time,

What is global scoping?

Variables declared in the global scope are accessible from anywhere in the program, including within functions and code blocks. Global variables typically have a program-wide lifetime and are often defined outside of any specific function or block.

What is a Keyword?

Words that have a special meaning in a programming language. Keywords may only be used for their intended purpose

What is a named constant?

a computer memory location whose contents cannot be changed during run time; created using the Const statement

What is a block of code?

a series of lines of code between beginning and ending curly braces Can be a function, a loop, or an if statement.

What is static scoping of a variable and the 2 types of static scope?

determined by the physical structure of the code, and it is fixed at compile-time. the scope of a variable is determined by where it is declared in the source code. There are 2 types of static scope: 1. nested static scope 2. non-nested static scope

What is an example of Static Type Binding?

int x = 5; // 'x' is statically bound to the integer type. double y = 3.14; // 'y' is statically bound to the double-precision floating-point type. // The following line would result in a compilation error because of a type mismatch: // int result = x + y; // Error: Incompatible types, cannot convert from double to int. // Proper type casting is required for the operation: double result = x + y; // Valid: The result is implicitly cast to a double. System.out.println("Result: " + result);

What are the 6 attributes of a variable?

name, - address (in memory), - value, - type, - lifetime, and - scope

What is meant by Scope of a Variable?

refers to the region or portion of code where a variable is recognized, accessible, and can be used. It determines where in your code you can refer to and manipulate the variable.

What is Binding?

the association between an attribute and an entity. - e.g., between variable and type. - e.g., between variable and value.

Referencing Environment

the collection of all variables that are visible in the statement responsible for associating variable references in the code with the appropriate variables declared in the program. This concept is closely related to the concept of scope.

What is the Lifetime of a variable and the 4 categories?

the time during which the variable is bound to a specific memory location. helps describe how long a variable exists and when it can be accessed. variable can have four storage durations: 1. Static Lifetime 2. Stack Dynamic Lifetime 3. Explicitly Heap Dynamic Lifetime 4. Implicitly Heap Dynamic Lifetime

What is an example of Dynamic Type Binding?

variable is bound to a type when it is assigned a value in an assignment statement Binding is first done during runtime or that the binding can change during runtime. Scripting languages (Python, Ruby, PHP, and JavaScript) all use dynamic type binding. This is a less efficient way to handle types and it can cause serious errors which are hard to find. Why would languages do this? These languages are generally not compiled, so they can't do the binding at compile time. The efficiency problem is not a big deal, since the overall interpretation time is greater than the binding time. for example in JavaScript: x = 5 // 'x' is dynamically bound to an integer. y = 3.14 // 'y' is dynamically bound to a floating-point number. name = "John" // 'name' is dynamically bound to a string. // Types can change during runtime: x = "Hello" // Now 'x' is dynamically bound to a string. // You can perform operations without explicit type declarations: result = x + name // 'result' is dynamically bound to the concatenation of two strings. console.log("Result:" + result)

What is nested static scope?

variables declared in an inner scope can access variables declared in outer scopes.

What is non-nested static scope?

variables declared in inner scopes cannot access variables declared in outer scopes. Each scope is treated as a separate and isolated scope.


Set pelajaran terkait

chapter 12/13 study guide (11/14)

View Set

AP psychology unit 3 classroom review

View Set

Adults II: Mod 2: Repro, Ch 50, 51, 52, 53

View Set

CH 25 STRUCTURE AND FUNCTION OF CARDIAC SYSTEM

View Set

AP Euro - Thirty Years' War and Treaty of Westphalia

View Set

Epidemiology Outbreak Investigation

View Set

Chapter 40: Hospice Care (Theory)

View Set

SVA 1 - Kapitel 1 - vad är en myt ? sidan 36-40

View Set

RN Targeted Medical Surgical Neurosensory and Musculoskeletal Online Practice 2019

View Set