Podcast
Questions and Answers
What are the characteristics of variables in programming languages?
What are the characteristics of variables in programming languages?
What is the maximum length of a name in FORTRAN I?
What is the maximum length of a name in FORTRAN I?
Which of the following languages does not impose a limit on the length of a name?
Which of the following languages does not impose a limit on the length of a name?
What is the disadvantage of case sensitivity in names?
What is the disadvantage of case sensitivity in names?
Signup and view all the answers
Which of the following languages has mixed case predefined names?
Which of the following languages has mixed case predefined names?
Signup and view all the answers
What is a keyword in programming languages?
What is a keyword in programming languages?
Signup and view all the answers
What is a reserved word in programming languages?
What is a reserved word in programming languages?
Signup and view all the answers
What is a potential problem with reserved words?
What is a potential problem with reserved words?
Signup and view all the answers
What is a variable characterized by?
What is a variable characterized by?
Signup and view all the answers
What is the memory address associated with a variable?
What is the memory address associated with a variable?
Signup and view all the answers
What is the purpose of a variable's type?
What is the purpose of a variable's type?
Signup and view all the answers
What is the r-value of a variable?
What is the r-value of a variable?
Signup and view all the answers
What is an alias in programming?
What is an alias in programming?
Signup and view all the answers
What is the abstract memory cell of a variable?
What is the abstract memory cell of a variable?
Signup and view all the answers
Why are aliases harmful to readability?
Why are aliases harmful to readability?
Signup and view all the answers
What is created via pointers, reference variables, or the union of C and C++?
What is created via pointers, reference variables, or the union of C and C++?
Signup and view all the answers
What is a binding in the context of programming languages?
What is a binding in the context of programming languages?
Signup and view all the answers
What happens during language design time in terms of binding?
What happens during language design time in terms of binding?
Signup and view all the answers
Which of the following languages provides implicit declarations?
Which of the following languages provides implicit declarations?
Signup and view all the answers
What is the advantage of implicit declarations?
What is the advantage of implicit declarations?
Signup and view all the answers
What type of binding occurs before run time and remains unchanged throughout program execution?
What type of binding occurs before run time and remains unchanged throughout program execution?
Signup and view all the answers
Which of the following languages use dynamic binding?
Which of the following languages use dynamic binding?
Signup and view all the answers
What is the disadvantage of implicit declarations?
What is the disadvantage of implicit declarations?
Signup and view all the answers
What occurs during load time in terms of binding?
What occurs during load time in terms of binding?
Signup and view all the answers
What is the main characteristic of explicit heap-dynamic variables?
What is the main characteristic of explicit heap-dynamic variables?
Signup and view all the answers
What is the advantage of implicit heap-dynamic variables?
What is the advantage of implicit heap-dynamic variables?
Signup and view all the answers
What is the scope of a variable?
What is the scope of a variable?
Signup and view all the answers
What is a local variable?
What is a local variable?
Signup and view all the answers
What are the disadvantages of implicit heap-dynamic variables?
What are the disadvantages of implicit heap-dynamic variables?
Signup and view all the answers
What determines how references to names are associated with variables?
What determines how references to names are associated with variables?
Signup and view all the answers
In C, C++, PHP, JavaScript, and Python, where can variable definitions appear?
In C, C++, PHP, JavaScript, and Python, where can variable definitions appear?
Signup and view all the answers
What is the purpose of declarations in programming languages?
What is the purpose of declarations in programming languages?
Signup and view all the answers
What is an advantage of static scoping?
What is an advantage of static scoping?
Signup and view all the answers
What is a disadvantage of dynamic scoping?
What is a disadvantage of dynamic scoping?
Signup and view all the answers
In dynamic scoping, what is the scope of a variable?
In dynamic scoping, what is the scope of a variable?
Signup and view all the answers
What is an alternative to static scoping?
What is an alternative to static scoping?
Signup and view all the answers
What is true about definitions in programming languages?
What is true about definitions in programming languages?
Signup and view all the answers
What is a characteristic of dynamic scoping?
What is a characteristic of dynamic scoping?
Signup and view all the answers
Study Notes
Variables Characterized
- A variable is characterized by a collection of properties or attributes:
- Type
- Scope
- Lifetime
- Type checking
- Initialization
- Type compatibility
Names
- Length of names varies by language:
- FORTRAN I: maximum 6 characters
- FORTRAN 95: maximum 31 characters
- COBOL: maximum 30 characters
- Ada: no limit, and all are significant
- C++: no limit, but implementors often impose one
- C# and Java: no limit, and all are significant
- PHP: must begin with a $
- Perl: $, @, or % at the beginning means it specifies its type
- Case sensitivity:
- Disadvantage: readability (names that look alike are different)
- Names in C-based languages are case sensitive
- Names in other languages are not
- Worse in C++, Java, and C# because predefined names are mixed case
Special Words
- Keywords:
- A keyword is a word that is special only in certain contexts
- Example: Real in Fortran (data type followed by a name)
- Reserved words:
- A reserved word is a special word that cannot be used as a user-defined name
- Potential problem: If there are too many, many collisions occur (e.g., COBOL has 300 reserved words!)
Variables
- A variable is an abstraction of a memory cell that can be characterized as follows:
- Name
- Address
- Value
- Type
- Lifetime
- Scope
Variables Attributes
- Name:
- Most variables have names as previously discussed
- Address:
- The memory address with which it is associated
- A variable may have different addresses at different times during execution
- A variable may have different addresses at different places in a program
- If two variable names can be used to access the same memory location, they are called aliases
- Type:
- Determines the range of values of variables and the set of operations that are defined for values of that type
- In the case of floating point, type also determines the precision
- Value:
- The contents of the location with which the variable is associated
- The l-value of a variable is its address
- The r-value of a variable is its value
- Abstract memory cell:
- It is the associated size required by the variable
- Example: Floating-point value may occupy four physical bytes while it is thought to occupy one abstract memory cell
The Concept of Binding
- A binding is an association between an attribute and an entity:
- Between a variable and its type or value
- Between an operation and a symbol
- Binding time:
- The time at which a binding takes place
- Possible binding times:
- Language design time
- Language implementation time
- Compile time
- Load time
- Runtime
- Link time
Static and Dynamic Binding
- A binding is static if it first occurs before run time and remains unchanged throughout program execution
- A binding is dynamic if it first occurs during execution or can change during execution of the program
- Languages that use dynamic binding:
- Python
- Ruby
- JavaScript
- PHP
Categories of Variables by Lifetime
- Explicit heap-dynamic variables:
- Allocated and deallocated by explicit directives
- Specified by the programmer which take effect during execution
- Referenced only through pointers or references
- Example: dynamic objects in C++ (via new and delete), all objects in Java
- Implicit heap-dynamic variables:
- Allocation and de-allocation caused by assignment statements
- Example: all variables in APL; all strings and arrays in Perl, JavaScript, and PHP
Scope
- The scope of a variable is the range of statements over which it is visible
- A variable is local in a program if it is declared there
- The nonlocal variables of a program unit or block are those that are visible within the block, but are not declared there
- Global variables:
- A special category of nonlocal variables
- The scope rules of a language determine how references to names are associated with variables
Global Scope
- C, C++, PHP, JavaScript, and Python allow variable definitions to appear outside the functions
- Declarations specify types and other attributes, but do not cause allocation of storage
- Definitions specify attributes and cause storage allocation
- C and C++ have both declarations and definitions of global data
Evaluation of Static Scoping
- Advantages: provides a method of nonlocal access
- Disadvantages:
- Subprograms end up being nested at the same level
- Use more global variables than required
Dynamic Scope
- Dynamic scoping:
- Is based on the calling sequence of subprograms
- The reference of a variable in a subprogram is the variable declared in its caller subprogram
- Example: JavaScript code snippet demonstrating dynamic scoping
Evaluation of Dynamic Scoping
- Advantages: parameters passed from one subprogram to another are implicitly visible in the called subprogram
- Disadvantages:
- Difficult to read and inability to type checking references
- Less reliable
- Execute slower than equivalent static-scoped programs
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the concepts of variables, scope, and lifetime in programming languages, including properties, attributes, and type checking.