CSC204 Chapter 5: Variables and Scope

VividExpressionism avatar
VividExpressionism
·
·
Download

Start Quiz

Study Flashcards

38 Questions

What are the characteristics of variables in programming languages?

Type, scope, lifetime, type checking, initialization, and type compatibility

What is the maximum length of a name in FORTRAN I?

6

Which of the following languages does not impose a limit on the length of a name?

All of the above

What is the disadvantage of case sensitivity in names?

Readability

Which of the following languages has mixed case predefined names?

All of the above

What is a keyword in programming languages?

A word that is special only in certain contexts

What is a reserved word in programming languages?

A special word that cannot be used as a user-defined name

What is a potential problem with reserved words?

Too many collisions occur

What is a variable characterized by?

Name, Address, Value, Type, Lifetime, and Scope

What is the memory address associated with a variable?

Address

What is the purpose of a variable's type?

Both A and B

What is the r-value of a variable?

The value of the variable

What is an alias in programming?

Two variable names that access the same memory location

What is the abstract memory cell of a variable?

The size required by the variable in memory

Why are aliases harmful to readability?

Because readers must remember all of them

What is created via pointers, reference variables, or the union of C and C++?

Aliases

What is a binding in the context of programming languages?

A binding is an association between an attribute and an entity

What happens during language design time in terms of binding?

Binding operator symbols to operations

Which of the following languages provides implicit declarations?

Perl

What is the advantage of implicit declarations?

Writability

What type of binding occurs before run time and remains unchanged throughout program execution?

Static binding

Which of the following languages use dynamic binding?

Python, Ruby, JavaScript, and PHP

What is the disadvantage of implicit declarations?

Reliability

What occurs during load time in terms of binding?

Binding a C or C++ static variable to a memory cell

What is the main characteristic of explicit heap-dynamic variables?

They are allocated and deallocated by explicit directives specified by the programmer.

What is the advantage of implicit heap-dynamic variables?

They allow for flexible and generic code.

What is the scope of a variable?

The range of statements over which it is visible.

What is a local variable?

A variable that is declared within a program unit or block.

What are the disadvantages of implicit heap-dynamic variables?

They are inefficient and lose error detection.

What determines how references to names are associated with variables?

The scope rules of a language.

In C, C++, PHP, JavaScript, and Python, where can variable definitions appear?

Anywhere in the program

What is the purpose of declarations in programming languages?

To specify types and attributes, but not allocate storage

What is an advantage of static scoping?

It provides a method of nonlocal access

What is a disadvantage of dynamic scoping?

It is difficult to read and inability to type checking references

In dynamic scoping, what is the scope of a variable?

Based on the calling sequence of subprograms

What is an alternative to static scoping?

Encapsulation

What is true about definitions in programming languages?

They specify attributes and allocate storage

What is a characteristic of dynamic scoping?

Variables are scoped based on the calling sequence of subprograms

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

This quiz covers the concepts of variables, scope, and lifetime in programming languages, including properties, attributes, and type checking.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Chapter 5.2
33 questions

Chapter 5.2

HilariousSagacity avatar
HilariousSagacity
Names, Bindings, and Scopes Lecture Quiz
5 questions
CSC204 Chapter 5: Variables and Scope
38 questions
Use Quizgecko on...
Browser
Browser