Nested Subprograms and Blocks Quiz

WittyTechnetium avatar
WittyTechnetium
·
·
Download

Start Quiz

Study Flashcards

34 Questions

What does the 'global' keyword do in Python?

Declares a variable as global within a function

In Dynamic Scope, how are references to variables connected to declarations?

By searching back through subprogram calls

What is the difference between Dynamic Scope and Static Scope?

Dynamic Scope uses temporal layout, Static Scope uses spatial layout

What is the purpose of the 'def tester()' function in the Python code snippets?

To show how global and local variables interact

'UnboundLocalError' indicates a problem with accessing:

'local' variables

What is the scope of a variable?

The range of statements over which it is visible

In a program unit, what defines local variables?

They are declared in that unit

What category do global variables fall under?

Special category of nonlocal variables

How are names and variables associated in static scope?

By finding the declaration through a search process

What happens if a declaration for a name cannot be found in static scope?

An error is reported

'Static parent' refers to the nearest what in static scope?

Static ancestor

In PHP, what is the scope of a variable declared within a function?

Local to the function

How can global variables be accessed in a PHP function?

Through the $GLOBALS array

What is a characteristic of JavaScript global variables that differentiates them from PHP global variables?

They cannot be accessed inside functions

How are variable scopes handled in Python?

Using the 'global' keyword

What does it mean when a variable in Python is declared as global?

It is accessible from any part of the program

In which programming languages can nested subprogram definitions create nested static scopes?

Fortran 2003+, F#, Python

What is the purpose of having a 'closer' variable with the same name as another variable in a unit?

Hiding variables from the unit

Which language syntax is used for creating static scopes inside program units since ALGOL 60?

BEGIN ... END

In which programming languages must all data declarations in a function appear at the beginning in C89?

Ada, Scheme, Fortran 2003+

What is the scope of local variables in C99, C++, and Java according to the text?

From the declaration to the end of the block

Which languages allow variable declarations to appear outside function definitions?

C, C++

In Pascal's dynamic scoping, how is the scope determined?

By the calling context

What effect does the first procedure have on variable 'a' in the given code snippet?

It sets 'a' to 1

According to static scope rules, which binding does a reference resolve to?

The most recent, compile-time binding

What is a common use of dynamic scope rules in subroutines?

Providing implicit parameters

What disadvantage is associated with variables during execution in dynamic scoping?

They are visible to all subprograms it calls

Why is dynamic scoping criticized for poor readability?

Because it makes it hard to determine the type of a variable statically

In the Python code snippet provided, why does the first 'tester()' function output 'The global day is: Monday'?

The 'day' variable is assigned 'Monday' before the first 'tester()' function is called.

What error occurs in the Python code snippet when the second 'tester()' function is called?

UnboundLocalError

In the context of static versus dynamic program scopes, which one of the following statements is true?

Static scope refers to variables being linked based on calling sequences.

What is the key difference between static and dynamic scopes in programming?

Dynamic scope connects variables based on calling sequences.

How does Python's 'global' keyword affect variable scope within functions?

'global' allows accessing global variables within a function.

What happens when the 'day' variable is declared as a local variable within the second 'tester()' function in the Python code snippet?

'day' is limited in scope to only the second 'tester()' function.

Study Notes

Variable Scope

  • A variable is an abstraction of a memory cell, characterized by a sextuple of attributes: name, address, value, type, lifetime, and scope.
  • The scope of a variable is the range of statements over which it is visible.
  • The local variables of a program unit are those that are declared in that unit.
  • The nonlocal variables of a program unit are those that are visible in the unit but not declared there.
  • Global variables are a special category of nonlocal variables.

Static Scope (Lexical Scope)

  • The scope of a variable can be statically determined prior to execution.
  • Used in many imperative and nonimperative languages.
  • To connect a name reference to a variable and its attributes, you (or the compiler) must find the declaration.
  • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name.
  • Terminology: Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent.

Blocks

  • A method of creating static scopes inside program units.
  • Syntax: BEGIN … END, {....}
  • Example: C program with nested blocks, showing the scope of variables x and y.

Declaration and Order

  • In C89, all data declarations in a function must appear in the beginning.
  • C99, C++, Java, and C# allow variable declarations to appear anywhere a statement can appear.
  • In C99, C++, and Java, the scope of all local variables is from the declaration to the end of the block.
  • In C# documentation, the scope of any variable declared in a block is the whole block, regardless of the position of the declaration in the block.

Declaration vs. Definition

  • In C and C++, there are both declarations (just attributes) and definitions (attributes and storage).
  • A declaration outside a function definition specifies that it is defined in another file.

Global Scope

  • C, C++, PHP, and Python support a program structure that consists of a sequence of function definitions in a file.
  • These languages allow variable declarations to appear outside function definitions.
  • Global variables can be accessed in a function through the $GLOBALS array or by declaring it global.

Dynamic Scope

  • Based on calling sequences of program units, not their textual layout (temporal versus spatial).
  • Referencing variables to declaration: References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.
  • Example: Static vs. Dynamic program scopes in Pascal.

Evaluation of Dynamic Scoping

  • Advantage: convenience.
  • Disadvantages:
    • While a subprogram is executing, its variables are visible to all subprograms it calls.
    • Impossible to statically type check.
    • Poor readability: it is not possible to statically determine the type of a variable.

Summary

  • Scope: Static and Dynamic.
  • Blocks and their Scope.
  • Declaration and their Order.
  • Declaration vs. Definition in C/C++.

Test your knowledge on nested subprogram definitions, nested static scopes, and creating static scopes inside program units. Understand concepts of hiding variables, and explore syntax examples from various programming languages like Ada, JavaScript, Python, and more.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser