Midterm Exam Flashcards
To study chapters 1 to 8 and there concepts and definitions for the mid terms.
Dynamic type binding is closely related to implicit heap-dynamic variables. Explain this relationship.
Dynamic type binding is related to implicit heap-dynamic variables because both determine a variable’s type at runtime based on the assigned value.
Assuming static scoping, in the following, which declaration of x is the correct one for a reference to x ?
i. sub1
ii. sub2
iii. sub3
- For sub1, the correct declaration of x is from sub1 because it has a local variable x.
- For sub2, the correct declaration of x is from sub1 because sub2 is nested inside sub1 and follows static scoping.
- For sub3, the correct declaration of x is from the global scope because sub3 is not inside sub1 and does not have its own x.
Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1 ?
- Under static scoping, sub1 uses the x from the global scope where it was defined, so it prints x = 5.
- Under dynamic scoping, sub1 uses the x from sub2, where it was called, so it prints x = 10.
Consider the following JavaScript program:
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assuming static scoping is used.
- In sub1, the visible variables are {x, y, z, a} from the global scope and sub1 itself.
- In sub2, the visible variables are {x, y, z, a, b}, which include global variables, sub1’s variables, and sub2’s own variables (where z in sub2 shadows z from sub1).
- In sub3, the visible variables are {x, y, z, a, w}, including global variables and sub3’s own variables (where x in sub3 shadows the global x).