Podcast
Questions and Answers
In the context of programming languages, which of the following considerations is MOST crucial when designing a type?
In the context of programming languages, which of the following considerations is MOST crucial when designing a type?
- The processor speed required to execute operations on the type.
- The availability of pre-existing libraries for the type.
- Scope, lifetime, type checking, initialization, and type compatibility. (correct)
- The number of characters allowed in the type's name.
Which aspect of variables is MOST directly related to the von Neumann architecture that imperative languages abstract?
Which aspect of variables is MOST directly related to the von Neumann architecture that imperative languages abstract?
- The speed at which variables can be declared.
- The use of variables in mathematical equations.
- The way variables are displayed on the screen.
- The attributes which characterize variables. (correct)
What is the primary difference between reserved words and keywords in a programming language?
What is the primary difference between reserved words and keywords in a programming language?
- There is no practical difference; the terms are interchangeable.
- Keywords have predefined meanings within the language, while reserved words are strictly prohibited from use as identifiers. (correct)
- Reserved words cannot be used as variable names, while keywords can.
- Reserved words are used for comments, while keywords are used for code execution.
Consider a programming language that is NOT case-sensitive. How would this impact variable naming and code readability?
Consider a programming language that is NOT case-sensitive. How would this impact variable naming and code readability?
Which of the following aspects regarding variable attributes is LEAST relevant to the design of a programming language?
Which of the following aspects regarding variable attributes is LEAST relevant to the design of a programming language?
Which of the following scenarios represents a static binding?
Which of the following scenarios represents a static binding?
In the context of programming languages, what is the primary trade-off between using explicit and implicit variable declarations?
In the context of programming languages, what is the primary trade-off between using explicit and implicit variable declarations?
Which of the following best describes the concept of type inferencing in programming languages?
Which of the following best describes the concept of type inferencing in programming languages?
Which of the following binding times allows for the greatest flexibility during program execution?
Which of the following binding times allows for the greatest flexibility during program execution?
Consider a scenario where a variable's data type is determined by the initial value assigned to it. Which languages utilize this approach?
Consider a scenario where a variable's data type is determined by the initial value assigned to it. Which languages utilize this approach?
Which of the following languages utilize nested subprogram definitions to create nested static scopes?
Which of the following languages utilize nested subprogram definitions to create nested static scopes?
In a programming language with static scoping, how is a variable's reference resolved?
In a programming language with static scoping, how is a variable's reference resolved?
What is a primary disadvantage of implicit heap-dynamic variables?
What is a primary disadvantage of implicit heap-dynamic variables?
Consider a scenario where a variable 'x' is declared in an outer scope and a variable with the same name 'x' is declared in an inner scope (block). How would Ada typically allow access to the 'hidden' variable in the outer scope?
Consider a scenario where a variable 'x' is declared in an outer scope and a variable with the same name 'x' is declared in an inner scope (block). How would Ada typically allow access to the 'hidden' variable in the outer scope?
What distinguishes local variables from nonlocal variables within a program unit?
What distinguishes local variables from nonlocal variables within a program unit?
In C++, how are dynamic objects typically created and destroyed, and what is a key disadvantage of this approach?
In C++, how are dynamic objects typically created and destroyed, and what is a key disadvantage of this approach?
What is the role of scope rules in a programming language?
What is the role of scope rules in a programming language?
Which programming language category commonly uses implicit heap-dynamic allocation for its variables?
Which programming language category commonly uses implicit heap-dynamic allocation for its variables?
In the context of programming languages, what is the primary difference between a keyword and a reserved word?
In the context of programming languages, what is the primary difference between a keyword and a reserved word?
Which of the following is a potential disadvantage of case-sensitive variable names in a programming language?
Which of the following is a potential disadvantage of case-sensitive variable names in a programming language?
What is the significance of the 'type' attribute in the context of a variable's characteristics?
What is the significance of the 'type' attribute in the context of a variable's characteristics?
How do aliases primarily affect program readability?
How do aliases primarily affect program readability?
What is a 'binding' in the context of programming languages?
What is a 'binding' in the context of programming languages?
Why might a programming language limit the length of variable names?
Why might a programming language limit the length of variable names?
In the context of variables, what is the difference between an 'l-value' and an 'r-value'?
In the context of variables, what is the difference between an 'l-value' and an 'r-value'?
Consider two variables, x
and y
, that are aliases. What does this imply?
Consider two variables, x
and y
, that are aliases. What does this imply?
What considerations should be made when deciding on the number of reserved words in a programming language?
What considerations should be made when deciding on the number of reserved words in a programming language?
In a language like Perl, where variable names begin with special characters (e.g., $
, @
), what purpose do these characters serve?
In a language like Perl, where variable names begin with special characters (e.g., $
, @
), what purpose do these characters serve?
In a dynamically-scoped language, what primarily determines the referencing environment of a statement?
In a dynamically-scoped language, what primarily determines the referencing environment of a statement?
If a variable's lifetime extends throughout the entire execution of a program, but its scope is limited to a specific function, what can be inferred?
If a variable's lifetime extends throughout the entire execution of a program, but its scope is limited to a specific function, what can be inferred?
How do 'readonly' named constants in C# differ from 'const' named constants?
How do 'readonly' named constants in C# differ from 'const' named constants?
What is the primary advantage of using named constants in programming?
What is the primary advantage of using named constants in programming?
Which of the following scenarios exemplifies a situation where a variable's lifetime exceeds its scope?
Which of the following scenarios exemplifies a situation where a variable's lifetime exceeds its scope?
In the context of variable attributes, which of the following describes the process of associating an attribute with a program entity?
In the context of variable attributes, which of the following describes the process of associating an attribute with a program entity?
What is the primary consequence of a programming language that allows widespread dynamic scoping?
What is the primary consequence of a programming language that allows widespread dynamic scoping?
What is meant by 'strong typing' in programming languages?
What is meant by 'strong typing' in programming languages?
In which languages is it legal to declare variables anywhere a statement can appear, but considered error-prone?
In which languages is it legal to declare variables anywhere a statement can appear, but considered error-prone?
Consider a variable declared within a block of code. In which of the following languages is the scope of that variable the entire block, regardless of the declaration's position?
Consider a variable declared within a block of code. In which of the following languages is the scope of that variable the entire block, regardless of the declaration's position?
In which programming languages can variables be declared directly within the initialization section of for
loop statements?
In which programming languages can variables be declared directly within the initialization section of for
loop statements?
Which languages support a program structure consisting of function definitions in a file, allowing variable declarations outside function definitions?
Which languages support a program structure consisting of function definitions in a file, allowing variable declarations outside function definitions?
In PHP, how can a function access global variables that are declared outside of it?
In PHP, how can a function access global variables that are declared outside of it?
How are global variables handled differently in Python compared to languages like C or C++?
How are global variables handled differently in Python compared to languages like C or C++?
Which statement best describes a key problem with static scoping as programs evolve?
Which statement best describes a key problem with static scoping as programs evolve?
What distinguishes dynamic scoping from static scoping in programming languages?
What distinguishes dynamic scoping from static scoping in programming languages?
Consider a scenario where a function big
calls sub1
, which in turn calls sub2
. If sub2
references a variable x
, how does static scoping determine which x
is used?
Consider a scenario where a function big
calls sub1
, which in turn calls sub2
. If sub2
references a variable x
, how does static scoping determine which x
is used?
What is a primary disadvantage of using dynamic scoping in a programming language?
What is a primary disadvantage of using dynamic scoping in a programming language?
Flashcards
Variable Attributes
Variable Attributes
Attributes of a variable include scope, lifetime, type checking, initialization, and type compatibility.
Name Design Issues
Name Design Issues
A name's attributes include case sensitivity and whether words are reserved or keywords.
Von Neumann Architecture
Von Neumann Architecture
Imperative languages are based on this computer architecture model.
What is a Name?
What is a Name?
Signup and view all the flashcards
Keywords vs Reserved words?
Keywords vs Reserved words?
Signup and view all the flashcards
Binding
Binding
Signup and view all the flashcards
Static Binding
Static Binding
Signup and view all the flashcards
Dynamic Binding
Dynamic Binding
Signup and view all the flashcards
Type Binding
Type Binding
Signup and view all the flashcards
Explicit Declaration
Explicit Declaration
Signup and view all the flashcards
Name Length
Name Length
Signup and view all the flashcards
Special Characters in Names
Special Characters in Names
Signup and view all the flashcards
Case Sensitivity
Case Sensitivity
Signup and view all the flashcards
Variable
Variable
Signup and view all the flashcards
Aliases
Aliases
Signup and view all the flashcards
Variable Type
Variable Type
Signup and view all the flashcards
l-value
l-value
Signup and view all the flashcards
r-value
r-value
Signup and view all the flashcards
Binding Time
Binding Time
Signup and view all the flashcards
Dynamic Objects
Dynamic Objects
Signup and view all the flashcards
Implicit Heap-Dynamic Variables
Implicit Heap-Dynamic Variables
Signup and view all the flashcards
Scope of a Variable
Scope of a Variable
Signup and view all the flashcards
Local Variables
Local Variables
Signup and view all the flashcards
Nonlocal Variables
Nonlocal Variables
Signup and view all the flashcards
Static Scope
Static Scope
Signup and view all the flashcards
Static Ancestors
Static Ancestors
Signup and view all the flashcards
Blocks
Blocks
Signup and view all the flashcards
Variable Lifetime
Variable Lifetime
Signup and view all the flashcards
Referencing Environment
Referencing Environment
Signup and view all the flashcards
Static-Scoped Referencing Environment
Static-Scoped Referencing Environment
Signup and view all the flashcards
Active Subprogram
Active Subprogram
Signup and view all the flashcards
Dynamic-Scoped Referencing Environment
Dynamic-Scoped Referencing Environment
Signup and view all the flashcards
Named Constant
Named Constant
Signup and view all the flashcards
Manifest Constant
Manifest Constant
Signup and view all the flashcards
Strong Typing
Strong Typing
Signup and view all the flashcards
Variable Declaration Location
Variable Declaration Location
Signup and view all the flashcards
C# Variable Scope in Blocks
C# Variable Scope in Blocks
Signup and view all the flashcards
Variable Scope in 'for' Loops
Variable Scope in 'for' Loops
Signup and view all the flashcards
Global Variable Declaration
Global Variable Declaration
Signup and view all the flashcards
Declaration vs. Definition
Declaration vs. Definition
Signup and view all the flashcards
PHP Global Variable Scope
PHP Global Variable Scope
Signup and view all the flashcards
Python 'global' Keyword
Python 'global' Keyword
Signup and view all the flashcards
Static Scoping
Static Scoping
Signup and view all the flashcards
Dynamic Scoping
Dynamic Scoping
Signup and view all the flashcards
Static vs. Dynamic Scoping
Static vs. Dynamic Scoping
Signup and view all the flashcards
Study Notes
- Imperative languages are abstractions of von Neumann architecture.
- Memory and processors are components of the von Neumann architecture.
- Variables are characterized by scope, lifetime, type checking, initialization, and type compatibility.
Names
- Design issues for names include case sensitivity and reserved words or keywords.
- Names that are too short cannot be connotative.
- FORTRAN 95 allows a maximum name length of 31 characters.
- C99 has no name length limit, but only the first 63 characters are significant; external names are limited to 31 characters.
- C#, Ada, and Java have no limit on name length, and all characters are significant.
- C++ has no limit on name length, but implementers often impose one.
- In PHP, all variable names must begin with dollar signs.
- In Perl, all variable names begin with special characters that specify the variable's type.
- In Ruby, variable names that begin with
@
are instance variables, while those that begin with@@
are class variables. - Case sensitivity in names can reduce readability because names that look alike are treated as different.
- C-based languages use case-sensitive names, while other languages do not.
- Special words aid readability and delimit or separate statement clauses.
- A keyword is special only in certain contexts, as in Fortran.
- A reserved word cannot be used as a user-defined name.
- COBOL has 300 reserved words, potentially leading to collisions.
Variables
- A variable is an abstraction of a memory cell.
- Variables are characterized by the sextuple of attributes: name, address, type, value, lifetime, and scope.
- Not all variables have names.
- Address is the memory address the variable is associated with.
- A variable may have different addresses at different times during execution or in different places within a program.
- Aliases occur when two variable names can access the same memory location, and are created via pointers, reference variables, and C/C++ unions.
- Aliases can harm readability.
- Type determines the range of values for a variable and the set of operations defined for values of that type, including precision for floating-point types.
- The value is the contents of the location with which the variable is associated.
- The "l-value" of a variable is its address.
- The "r-value" is the value of a variable.
- An abstract memory cell is the physical cell or collection of cells associated with a variable.
The Concept of Binding
- Binding is an association between an entity and an attribute.
- Binding time is the time at which a binding takes place.
- Language design time involves binding operator symbols to operations.
- Language implementation time involves binding floating-point types to a representation.
- Compile time involves binding a variable to a type in C or Java.
- Load time involves binding a C or C++ static variable to a memory cell.
- Runtime involves binding a nonstatic local variable to a memory cell.
- A binding is static if it occurs before runtime and remains unchanged throughout program execution.
- A binding is dynamic if it first occurs during execution or can change during execution.
Type Binding
- Static types may be specified by explicit or implicit declarations.
- Explicit declaration is a program statement used for declaring the types of variables.
- Advantage of explicit declaration is reliability.
- Implicit declaration is a default mechanism for specifying types of variables through default conventions.
- Writability is an advantage of implicit declaration.
- Reliability is a disadvantage of implicit declaration.
- In C#, a variable can be declared with
var
and an initial value, where the initial value sets the type through type inferencing. - Visual BASIC 9.0+, ML, Haskell, F#, and Go use type inferencing to determine types of variables based on context.
- Dynamic type binding, used in JavaScript, Python, Ruby, PHP, and C#, flexibility is achieved through assignment statements.
- Dynamic type binding disadvantage is high cost due to dynamic type checking and interpretation, also type error detection by the compiler is difficult.
Variable Attributes
- Allocation is getting a cell from some pool of available cells for storage bindings and lifetime.
- Deallocation is putting a cell back into the pool of available cells for storage bindings and lifetime.
- The lifetime of a variable concerns storage bindings and is defined as the time during which it is bound to a particular memory cell.
- Static variables are bound to memory cells before execution begins and remain bound to the same memory cell throughout execution (e.g., C and C++ static variables in functions).
- Static variable advantages include efficiency (direct addressing) and history-sensitive subprogram support.
- Static variable disadvantages include lack of flexibility (no recursion).
- Stack-dynamic variables have storage bindings created when their declaration statements are elaborated, elaborated means code is only executed upon declaration.
- Stack-dynamic advantages include recursion and conserved storage.
- Stack-dynamic disadvantages include overhead of allocation and deallocation, also inefficient references from the indirect addresing.
- Explicit heap-dynamic variables are allocated and deallocated by explicit directives, specified by the programmer, and take effect during execution.
- Explicit heap-dynamic, can only be referenced only through pointers or references, e.g., dynamic objects in C++ (via new and delete) and all objects in Java.
- Explicit heap-dynamic advantages include enables dynamic storage management.
- Explicit heap-dynamic disadvantages include inefficiency and unreliability.
- Implicit heap-dynamic variable allocation and deallocation are caused by assignment statements.
- Implicit heap-dynamic variables are all variables in APL and all strings/arrays in Perl, JavaScript, and PHP.
- Implicit heap-dynamic advantages include flexibility (generic code).
- Implicit heap-dynamic disadvantages include inefficiency because all attributes are dynamic.
Variable Scope
- Scope is the range of statements over which it is visible.
- Local variables belong to a program unit if they are declared in that Unit.
- Nonlocal variables in a program unit are those that are visible in the unit but not declared there.
- Global variables are a special category of nonlocal variables.
- The scope rules of a language determine how references to names are associated with variables.
- Static scoping is based on program text.
- You connect a name/variable reference within the declaration.
- Search process: search declarations first locally, then look at larger enclosing scopes until one is found for the given name.
- Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent.
- Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F#, and Python).
- Variables can be hidden from a unit by having a "closer" variable with the same name.
- Ada allows access to these "hidden" variables, for example via
unit.name
. - Blocks are a method of creating static scopes inside program units (originating from ALGOL 60).
- Using C:
void sub() { int count; while (...) { int count; count++; ... } }
- The above example, while legal in C and C++, is not permitted in Java and C# due to its error-prone nature.
- C99, C++, Java, and C# allow variable declarations to appear anywhere a statement can appear.
- In C99, C++, and Java scopes of local variables run from declaration of end block.
- The declaration order in C# shows that scope of variable in block contains declaration.
- However, variables must be declared before they are used.
- In C++, Java, and C#, variables can be declared in for statements.
- The scope of such variables is restricted to the for construct.
- C, C++, PHP, and Python support a program structure that consists of a sequence of function definitions in a file.
- These languages allow variable declarations to appear outside function definitions.
- C and C++ have both declarations (just attributes) and definitions (attributes and storage).
- A declaration outside a function definition specifies that it is defined in another file.
- With PHP, programs are embedded in HTML markup documents, in any number of fragments, some statements and some function definitions.
- The scope of a variable declared in a function is local to the function in PHP.
- The scope of a variable declared outside functions is from the declaration to the end of the program, but skips over any intervening functions.
- Global variables in PHP can be accessed in a function through the
$GLOBALS
array or by declaring itglobal
. - In Python, a global variable can be referenced in functions, but can be assigned in a function only if it has been declared to be
global
in the function. - Static scoping has issues in programs related to how often initial structures change/get reworked.
- Often static scoping access is too permissible on structures and sub programs.
- Dynamic scope is based on calling sequences of program units, not their textual layout (temporal versus spatial).
- References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.
Example of Scope
function big() {
function sub1() {
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}
- In the above Javascript example,
big
callssub1
, which callssub2
; andsub2
usesx
. - Under static scoping, the reference to
x
insub2
relates tobig
'sx
. - Using dynamic scoping, the reference to
x
insub2
relates tosub1
'sx
. - Dynamic scoping has an advantage of convenience.
- Dynamic scoping has numerous disadvantages.
- Disadvantage of dynamic scoping is that variables in subprograms are visisble to all subprograms it calls
- Disadvantage of dynamic scoping is that statically type checking is impossible.
- Disadvantage of dynamic scoping is poor readability due to being unable to statically determine variable details.
- Lifetime of a variable concerns the time a variable is bound to a specific memory location.
- Scope and lifetime are sometimes associated, are different concepts.
- Scope and lifetime can be best contrasted when considering
static
variables in aC++
function. - The referencing environment of a statement is the collection of all names that are visible in the statement.
- In a static-scoped language, it includes local variables plus all visible variables in all enclosing scopes.
- A subprogram is active if its execution has begun but has not yet terminated and a variable has been declared.
- With dynamic scoping that has a language, the variable is defined locally and is referenced.
Named constants
- With named constants, the a variable is bound in storage to one value.
- Advantages of named constants are readability and modifiability.
- Named constants are used to to parameterize programs.
- Manifest constants or dynamic named constants allows values to be changed Languages vary in the implementation of explicit constants.
- Ada, C++, and Java named constants are expressions of any kind, dynamically bound.
- C# has two levels of constants readonly and const.
- When const is specified, the named constant is bound at compile time.
- When readonly is specified, the named constant is bound dynamically.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.