Questions for Programming Languages, Chapter 4, Names
1. Which is longer: a variable’s scope, lifetime, or visibility?
2. What are some ways that languages denote the difference between l
Left or right side of equal
mark, obviously. Also, de
referencing symbols like “ref” “*”
3. What does “variable collision” mean?
Having the same name for multiple variables
4. What is the relationship between scope and lifetime?
Sometimes they are the same. Most often, lif
etime is bigger than scope.
5. What does the code snippet effectively do?
x = *p;
*p = y;
What is the difference between disjoint and nested scope?
In disjoint scope, having the same name for different variables is fine, th
interference. In nested scope, when you have the same name for different variables
you have to have a way of telling which is which. Sometimes one is hidden.
Name a way in which Java allows forward
referencing and a way in which it does
You cannot forward
reference variables, but you can forward
and instance variables.
What kind of data structure is a “dictionary”?
What is the relationship between a variable’s scope and whether or not it is
f it is visible it is in scope. If it is hidden, it may or may not be in scope.
10. What is the relationship between having dynamic memory allocation for
variables and recursion?
With static allocation for variables there can be no recursion. To do recu
need a dynamic memory management system.
What does it mean for a variable’s memory to be statically allocated?
It is allocated for the entire life of the program, and never forgets its value.
In Java, how can you force a variable to have a lifetime that is bigger than its
Declare it to be “static”
What data structure would you use in Java to resolve variable identifiers?
A Stack of HashMaps
Give an example of using a variable t
hat is not in its local scope.
Easiest is using a variable in a loop that was defined before the loop.
Give an example of how to hide a variable in Java.
Naming a local variable the same as an instance variable.
In Java, do the words “public” and “
private” refer to the variable’s scope, lifetime,
1. Why might it be a bad idea to allow pre
defined identifiers in a programming
There can be confusion on the original meaning of the word and the user
. (Example: in Pascal you can redefine “true” to mean “false”)
2. A variable is bound to several things. Name 2.
A name, a memory address, a type, a value, and a lifetime
How do you denote a “block” of code in Java?
In Java, you don’t have t
o declare all the variables at the beginning of a scope, but
in some languages you do. Does this mean that Java allows forward
variables? Why or why not?
No. You must declare a variable before you use it still.
In Java, does the key
word “static” refer to static memory allocation, static scoping,
or static variable bindings?
Give an example of something in Java that can be a r
value but not an l
Why do we need to keep a stack of symbol tables?
Each table on the stack is one scope. This allows scopes to be nested.
What type of scoping does Java use?
If a variable name has bindings that are dependent on the runtime history of the
program then what type of scoping is being used?
Give a reason that static scoping is preferred.
Harder to do type checking, error
prone, security issues, access
variables takes more time.
How was the lifetime of variables defined for early programming languages like
COBOL and FORTRAN?
Lifetime of a variable was the entire running of the program.
Name an advantage of overloading
Easy to unders
tand, don’t have to think of convoluted names for related things
Give an example of operator overloading in Java
+ for strings
1. What is the main difference between the terms “static” and “dynamic” when it
comes to bindings?
Static = happe
ns at compile time, doesn’t change
Dynamic = happens at runtime, on
2. Do most common languages use static or dynamic scoping?
3. What is the “lifetime” of a variable?
The time in which a variable is in memory
4. What is an “identifier”?
he name of an entity like a function, variable, or type
5. What does it mean to be case
Upper and lower case make a difference
6. Name some Java keywords
7. What does a variable’s l
value refer to?
Its memory location
8. What does a
value refer to?
9. What is a variable’s scope?
The lines of code that are allowed to refer to a variable
What is a symbol table?
A data structure that keeps up with what each name is bound to.
What is it called when one na
me can mean several different things?
What is a variable’s lifetime?
The time in which it is allocated memory.