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 is the main advantage of using dynamic variables over static variables?

  • Efficiency in direct addressing
  • History-sensitive subprogram support
  • Flexibility in recursion (correct)
  • Conservation of storage
  • What is a disadvantage of using implicit heap-dynamic variables?

  • Error detection is diminished
  • Lack of history-sensitive subprogram support
  • Descriptor association and storage is used
  • Inefficient references (correct)
  • What is the main disadvantage of static variables?

  • Inefficient references
  • Error detection is diminished
  • Descriptor association and storage is used
  • Lack of flexibility (correct)
  • What is the main advantage of using stack-dynamic variables?

    <p>Allows recursion</p> Signup and view all the answers

    What is a disadvantage of using explicit heap-dynamic variables?

    <p>Cost at execution time</p> Signup and view all the answers

    What is the definition of a binding in programming languages?

    <p>A binding is an association between an operation and a symbol.</p> Signup and view all the answers

    What is the main advantage of using static variables?

    <p>Efficiency in direct addressing</p> Signup and view all the answers

    What is the binding time for associating operator symbols with operations?

    <p>Language design time</p> Signup and view all the answers

    What is a disadvantage of using stack-dynamic variables?

    <p>Overhead of allocation and de-allocation</p> Signup and view all the answers

    What type of binding occurs when a variable is declared with a data type?

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

    What is the lifetime of a variable?

    <p>The time during which the variable is bound to a particular memory cell</p> Signup and view all the answers

    Which of the following languages use implicit declarations?

    <p>FORTRAN, BASIC, and 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 at runtime?

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

    Which of the following languages use dynamic binding?

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

    What is the disadvantage of implicit declarations?

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

    What is a characteristic of a variable in programming languages?

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

    Which programming language has a limit of 30 characters for a name?

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

    What is the disadvantage of case sensitivity in names?

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

    Which programming languages have predefined names that are mixed case?

    <p>C++, Java, and C#</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 potential problem with reserved words in programming languages?

    <p>Collisions may occur if there are too many</p> Signup and view all the answers

    In which programming language does a name starting with a dollar sign ($) specify its type?

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

    What is the characteristic of names in the C-based languages?

    <p>They are case sensitive</p> Signup and view all the answers

    What is the primary advantage of explicit heap-dynamic variables?

    <p>Provides for dynamic storage management</p> Signup and view all the answers

    What is a common disadvantage of implicit heap-dynamic variables?

    <p>Inefficient, because all attributes are dynamic</p> Signup and view all the answers

    What determines how references to names are associated with variables?

    <p>Scope rules of a language</p> Signup and view all the answers

    Which of the following is a characteristic of global variables?

    <p>They are a special category of nonlocal variables</p> Signup and view all the answers

    What is the primary disadvantage of explicit heap-dynamic variables?

    <p>They are inefficient and unreliable</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 type of scoping is used in the given code snippet?

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

    What is the reference of variable x in sub2?

    <p>The x declared in the procedure big</p> Signup and view all the answers

    What is big in relation to sub2?

    <p>A static ancestor</p> Signup and view all the answers

    What is the scope of the variable count in the while loop?

    <p>From the declaration to the end of the block</p> Signup and view all the answers

    Why do Java and C# not allow declaring variables in the inner block?

    <p>Because it is error-prone</p> Signup and view all the answers

    In which languages can variable declarations appear anywhere in a program unit?

    <p>C99, C++, Java, and C#</p> Signup and view all the answers

    In C#, what is the scope of a variable declared in a block?

    <p>The whole block, regardless of the position of the declaration</p> Signup and view all the answers

    In which languages can variables be declared in the for statements' initialization expressions?

    <p>C99, C++, Java, and C#</p> Signup and view all the answers

    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

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    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.

    More Like This

    Chapter 5.2
    33 questions

    Chapter 5.2

    HilariousSagacity avatar
    HilariousSagacity
    CSC204 Chapter 5: Variables and Scope
    38 questions
    Use Quizgecko on...
    Browser
    Browser