Chapter 5 Flashcards
(6 cards)
Some programming languages are typeless. What are the obvious advantages and disadvantages of having no types in a language?
The advantage of a typeless language is flexibility; any variable can be used for any
type values. The disadvantage is poor reliability due to the ease with which type errors
can be made, coupled with the impossibility of type checking detecting them.
Dynamic type binding is closely related to implicit heap-dynamic vari
ables. Explain this relationship.
Implicit heap-dynamic variables acquire types only when assigned values, which must
be at runtime. Therefore, these variables are always dynamically bound to types.
Describe a situation when a history-sensitive variable in a subprogram is
useful.
Suppose that a Fortran subroutine is used to implement a data structure as an
abstraction. In this situation, it is essential that the structure persist between calls to the
managing subroutine.
Consider the following JavaScript skeletal program:
// The main program
var x;
function sub1() {
var x;
function sub2() {
. . .
}
}
function sub3() {
. . .
}
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
(a) i. sub1
ii. sub1
iii. main
(b) i. sub1
ii. sub1
iii. sub1
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?
var x;
function sub1() {
document.write(“x = “ + x + “<br></br>”);
}
function sub2() {
var x;
x = 10;
sub1();
}
x = 5;
sub2();
Static scoping: x is 5
Dynamic scoping: x is 10
Consider the following JavaScript program:
var x, y, z;
function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
. . .
}
. . .
}
function sub3() {
var a, x, w;
. . .
}
List all the variables, along with the program units where they are
declared, that are visible in the bodies of sub1, sub2, and sub3, assum
ing static scoping is used.