CSC204 Chapter 5: Variables and Scope
38 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What are the characteristics of variables in programming languages?

  • Type, scope, lifetime and initialization
  • Scope, lifetime, and type compatibility
  • Type, scope, lifetime, type checking, initialization, and type compatibility (correct)
  • Type, scope, and lifetime
  • What is the maximum length of a name in FORTRAN I?

  • 31
  • 6 (correct)
  • 5
  • 30
  • Which of the following languages does not impose a limit on the length of a name?

  • Ada
  • C
  • C++
  • All of the above (correct)
  • What is the disadvantage of case sensitivity in names?

    <p>Readability</p> Signup and view all the answers

    Which of the following languages has mixed case predefined names?

    <p>All of the above</p> Signup and view all the answers

    What is a keyword in programming languages?

    <p>A word that is special only in certain contexts</p> Signup and view all the answers

    What is a reserved word in programming languages?

    <p>A special word that cannot be used as a user-defined name</p> Signup and view all the answers

    What is a potential problem with reserved words?

    <p>Too many collisions occur</p> Signup and view all the answers

    What is a variable characterized by?

    <p>Name, Address, Value, Type, Lifetime, and Scope</p> Signup and view all the answers

    What is the memory address associated with a variable?

    <p>Address</p> Signup and view all the answers

    What is the purpose of a variable's type?

    <p>Both A and B</p> Signup and view all the answers

    What is the r-value of a variable?

    <p>The value of the variable</p> Signup and view all the answers

    What is an alias in programming?

    <p>Two variable names that access the same memory location</p> Signup and view all the answers

    What is the abstract memory cell of a variable?

    <p>The size required by the variable in memory</p> Signup and view all the answers

    Why are aliases harmful to readability?

    <p>Because readers must remember all of them</p> Signup and view all the answers

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

    <p>Aliases</p> Signup and view all the answers

    What is a binding in the context of programming languages?

    <p>A binding is an association between an attribute and an entity</p> Signup and view all the answers

    What happens during language design time in terms of binding?

    <p>Binding operator symbols to operations</p> Signup and view all the answers

    Which of the following languages provides implicit declarations?

    <p>Perl</p> Signup and view all the answers

    What is the advantage of implicit declarations?

    <p>Writability</p> Signup and view all the answers

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

    <p>Static binding</p> Signup and view all the answers

    Which of the following languages use dynamic binding?

    <p>Python, Ruby, JavaScript, and PHP</p> Signup and view all the answers

    What is the disadvantage of implicit declarations?

    <p>Reliability</p> Signup and view all the answers

    What occurs during load time in terms of binding?

    <p>Binding a C or C++ static variable to a memory cell</p> Signup and view all the answers

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

    <p>They are allocated and deallocated by explicit directives specified by the programmer.</p> Signup and view all the answers

    What is the advantage of implicit heap-dynamic variables?

    <p>They allow for flexible and generic code.</p> Signup and view all the answers

    What is the scope of a variable?

    <p>The range of statements over which it is visible.</p> Signup and view all the answers

    What is a local variable?

    <p>A variable that is declared within a program unit or block.</p> Signup and view all the answers

    What are the disadvantages of implicit heap-dynamic variables?

    <p>They are inefficient and lose error detection.</p> Signup and view all the answers

    What determines how references to names are associated with variables?

    <p>The scope rules of a language.</p> Signup and view all the answers

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

    <p>Anywhere in the program</p> Signup and view all the answers

    What is the purpose of declarations in programming languages?

    <p>To specify types and attributes, but not allocate storage</p> Signup and view all the answers

    What is an advantage of static scoping?

    <p>It provides a method of nonlocal access</p> Signup and view all the answers

    What is a disadvantage of dynamic scoping?

    <p>It is difficult to read and inability to type checking references</p> Signup and view all the answers

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

    <p>Based on the calling sequence of subprograms</p> Signup and view all the answers

    What is an alternative to static scoping?

    <p>Encapsulation</p> Signup and view all the answers

    What is true about definitions in programming languages?

    <p>They specify attributes and allocate storage</p> Signup and view all the answers

    What is a characteristic of dynamic scoping?

    <p>Variables are scoped based on the calling sequence of subprograms</p> 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.

    Quiz Team

    Description

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

    More Like This

    Use Quizgecko on...
    Browser
    Browser