38 Questions
What is the main advantage of using dynamic variables over static variables?
Flexibility in recursion
What is a disadvantage of using implicit heap-dynamic variables?
Inefficient references
What is the main disadvantage of static variables?
Lack of flexibility
What is the main advantage of using stack-dynamic variables?
Allows recursion
What is a disadvantage of using explicit heap-dynamic variables?
Cost at execution time
What is the definition of a binding in programming languages?
A binding is an association between an operation and a symbol.
What is the main advantage of using static variables?
Efficiency in direct addressing
What is the binding time for associating operator symbols with operations?
Language design time
What is a disadvantage of using stack-dynamic variables?
Overhead of allocation and de-allocation
What type of binding occurs when a variable is declared with a data type?
Static binding
What is the lifetime of a variable?
The time during which the variable is bound to a particular memory cell
Which of the following languages use implicit declarations?
FORTRAN, BASIC, and Perl
What is the advantage of implicit declarations?
Writability
What type of binding occurs at runtime?
Dynamic binding
Which of the following languages use dynamic binding?
Python, Ruby, and JavaScript
What is the disadvantage of implicit declarations?
Reliability
What is a characteristic of a variable in programming languages?
Scope
Which programming language has a limit of 30 characters for a name?
COBOL
What is the disadvantage of case sensitivity in names?
Readability
Which programming languages have predefined names that are mixed case?
C++, Java, and C#
What is a keyword in programming languages?
A word that is special only in certain contexts
What is a potential problem with reserved words in programming languages?
Collisions may occur if there are too many
In which programming language does a name starting with a dollar sign ($) specify its type?
Perl
What is the characteristic of names in the C-based languages?
They are case sensitive
What is the primary advantage of explicit heap-dynamic variables?
Provides for dynamic storage management
What is a common disadvantage of implicit heap-dynamic variables?
Inefficient, because all attributes are dynamic
What determines how references to names are associated with variables?
Scope rules of a language
Which of the following is a characteristic of global variables?
They are a special category of nonlocal variables
What is the primary disadvantage of explicit heap-dynamic variables?
They are inefficient and unreliable
What is the scope of a variable?
The range of statements over which it is visible
What type of scoping is used in the given code snippet?
Static scoping
What is the reference of variable x in sub2?
The x declared in the procedure big
What is big in relation to sub2?
A static ancestor
What is the scope of the variable count in the while loop?
From the declaration to the end of the block
Why do Java and C# not allow declaring variables in the inner block?
Because it is error-prone
In which languages can variable declarations appear anywhere in a program unit?
C99, C++, Java, and C#
In C#, what is the scope of a variable declared in a block?
The whole block, regardless of the position of the declaration
In which languages can variables be declared in the for statements' initialization expressions?
C99, C++, Java, and C#
Study Notes
Introduction to Variables
- Variables have several attributes: type, scope, lifetime, type checking, initialization, and type compatibility.
Names
- Length of names varies across languages:
- FORTRAN I: maximum 6 characters
- FORTRAN 95: maximum 31 characters
- COBOL: maximum 30 characters
- Ada: no limit, all characters are significant
- C++ and C#: no limit, but implementors often impose one
- PHP: names must begin with a $
- Perl: $, @, or % at the beginning specifies the type
- Case sensitivity:
- Disadvantage: readability (names that look alike are different)
- C-based languages are case sensitive
- Other languages are not case sensitive
- Special words:
- Keywords: special words in certain contexts, e.g., in Fortran
- Reserved words: special words that cannot be used as user-defined names
- Potential problem: many reserved words can cause collisions, e.g., COBOL has 300 reserved words
The Concept of Binding
- Binding: association between an attribute and an entity
- Binding time: when the association takes place
- Possible binding times:
- Language design time
- Language implementation time
- Compile time
- Load time
- Runtime
- Link time
Static and Dynamic Binding
- Static binding: binding occurs before runtime and remains unchanged
- Advantages: reliability, efficiency
- Disadvantages: less flexibility
- Dynamic binding: binding occurs during execution or can change during execution
- Languages that use dynamic binding: Python, Ruby, JavaScript, and PHP
- Advantages: flexibility
- Disadvantages:
- Less reliable
- Cost at execution time
Storage Bindings and Lifetime
- Allocation: getting a memory cell from a pool
- Deallocation: putting a memory cell back into the pool
- Lifetime: time during which a variable is bound to a memory cell
- Types of storage binding based on lifetimes:
- Static variables
- Stack-dynamic variables
- Explicit heap-dynamic variables
- Implicit heap-dynamic variables
Categories of Variables by Lifetime
- Static variables:
- Bound to memory cells before execution
- Advantages: efficiency, history-sensitive subprogram support
- Disadvantage: lack of flexibility
- Stack-dynamic variables:
- Storage bindings created during execution
- Advantages: allows recursion, conserves storage
- Disadvantages:
- Overhead of allocation and deallocation
- Subprograms cannot be history sensitive
- Inefficient references
- Explicit heap-dynamic variables:
- Allocated and deallocated by explicit directives
- Advantages: provides dynamic storage management
- Disadvantage: inefficient and unreliable
- Implicit heap-dynamic variables:
- Allocation and deallocation caused by assignment statements
- Advantages: flexibility
- Disadvantages:
- Inefficient
- Loss of error detection
Scope
- Scope: range of statements over which a variable is visible
- Local variables: declared in a program unit
- Nonlocal variables: visible within a block, but not declared there
- Global variables: special category of nonlocal variables
- Scope rules: determine how references to names are associated with variables
Static Scope
- Static scoping: binding names to nonlocal variables
- The scope of a variable is determined by the block structure
Blocks
- Blocks create static scopes
- The reference to a variable inside a block is hidden from the same variable outside the block
Declaration Order
- C99, C++, Java, and C# allow variable declarations to appear anywhere in a program unit
- Declarations may create scopes that are not associated with subprograms
This quiz covers the basics of variables, including properties such as type, scope, lifetime, type checking, initialization, and type compatibility. Learn about the concept of binding, named constants, and more.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free