Nested Subprograms and Blocks Quiz

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 does the 'global' keyword do in Python?

  • Declares a variable as constant
  • Declares a variable as global within a function (correct)
  • Converts a local variable to global
  • Creates a new variable

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

  • By searching the entire program text
  • By random selection
  • By searching forward through subprogram calls
  • By searching back through subprogram calls (correct)

What is the difference between Dynamic Scope and Static Scope?

  • Dynamic Scope refers to the textual order, Static Scope refers to calling sequences
  • Static Scope uses temporal layout, Dynamic Scope uses spatial layout
  • Dynamic Scope uses temporal layout, Static Scope uses spatial layout (correct)
  • Static Scope has no relation to subprogram calls

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

<p>To show how global and local variables interact (D)</p> Signup and view all the answers

'UnboundLocalError' indicates a problem with accessing:

<p>'local' variables (D)</p> Signup and view all the answers

What is the scope of a variable?

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

In a program unit, what defines local variables?

<p>They are declared in that unit (A)</p> Signup and view all the answers

What category do global variables fall under?

<p>Special category of nonlocal variables (D)</p> Signup and view all the answers

How are names and variables associated in static scope?

<p>By finding the declaration through a search process (C)</p> Signup and view all the answers

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

<p>An error is reported (B)</p> Signup and view all the answers

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

<p>Static ancestor (C)</p> Signup and view all the answers

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

<p>Local to the function (B)</p> Signup and view all the answers

How can global variables be accessed in a PHP function?

<p>Through the $GLOBALS array (D)</p> Signup and view all the answers

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

<p>They cannot be accessed inside functions (B)</p> Signup and view all the answers

How are variable scopes handled in Python?

<p>Using the 'global' keyword (C)</p> Signup and view all the answers

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

<p>It is accessible from any part of the program (D)</p> Signup and view all the answers

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

<p>Fortran 2003+, F#, Python (C)</p> Signup and view all the answers

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

<p>Hiding variables from the unit (B)</p> Signup and view all the answers

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

<p>BEGIN ... END (D)</p> Signup and view all the answers

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

<p>Ada, Scheme, Fortran 2003+ (B)</p> Signup and view all the answers

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

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

Which languages allow variable declarations to appear outside function definitions?

<p>C, C++ (B)</p> Signup and view all the answers

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

<p>By the calling context (C)</p> Signup and view all the answers

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

<p>It sets 'a' to 1 (D)</p> Signup and view all the answers

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

<p>The most recent, compile-time binding (B)</p> Signup and view all the answers

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

<p>Providing implicit parameters (A)</p> Signup and view all the answers

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

<p>They are visible to all subprograms it calls (A)</p> Signup and view all the answers

Why is dynamic scoping criticized for poor readability?

<p>Because it makes it hard to determine the type of a variable statically (B)</p> Signup and view all the answers

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

<p>The 'day' variable is assigned 'Monday' before the first 'tester()' function is called. (B)</p> Signup and view all the answers

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

<p>UnboundLocalError (D)</p> Signup and view all the answers

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

<p>Static scope refers to variables being linked based on calling sequences. (C)</p> Signup and view all the answers

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

<p>Dynamic scope connects variables based on calling sequences. (C)</p> Signup and view all the answers

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

<p>'global' allows accessing global variables within a function. (B)</p> Signup and view all the answers

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

<p>'day' is limited in scope to only the second 'tester()' function. (B)</p> Signup and view all the answers

Flashcards are hidden until you start studying

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++.

Studying That Suits You

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

Quiz Team

More Like This

Nested Control Structures Quiz
6 questions
Nested Classes Flashcards
16 questions

Nested Classes Flashcards

BenevolentDramaticIrony avatar
BenevolentDramaticIrony
Use Quizgecko on...
Browser
Browser