Reverse Bee Q&A

jaspersugarlandΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 11 μήνες)

89 εμφανίσεις

Questions for Programming Languages, Chapter 4, Names

Hard

1. Which is longer: a variable’s scope, lifetime, or visibility?

Lifetime

2. What are some ways that languages denote the difference between l
-
values and
r
-
values?

Left or right side of equal
mark, obviously. Also, de
-
referencing symbols like “ref” “*”
or “!”

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?

int x,y;

int *p;

x = *p;

*p = y;

Sets x=y.

6.

What is the difference between disjoint and nested scope?

In disjoint scope, having the same name for different variables is fine, th
ere’s no
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.

7.

Name a way in which Java allows forward
-
referencing and a way in which it does
not.

You cannot forward
-
reference variables, but you can forward
-
reference methods
and instance variables.

8.


What kind of data structure is a “dictionary”?

A hashmap

9
.

What is the relationship between a variable’s scope and whether or not it is
visible?

I
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
rsion you
need a dynamic memory management system.

11.

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.

12.

In Java, how can you force a variable to have a lifetime that is bigger than its
scope?

Declare it to be “static”

13.

What data structure would you use in Java to resolve variable identifiers?

A Stack of HashMaps

14.

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.

15.

Give an example of how to hide a variable in Java.

Naming a local variable the same as an instance variable.

16.

In Java, do the words “public” and “
private” refer to the variable’s scope, lifetime,
or visibility?

Scope

Medium

1. Why might it be a bad idea to allow pre
-
defined identifiers in a programming
language?

There can be confusion on the original meaning of the word and the user
-
defined
meaning
. (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

3.

How do you denote a “block” of code in Java?

{}

4.

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
-
referencing for
variables? Why or why not?

No. You must declare a variable before you use it still.


5.

In Java, does the key
word “static” refer to static memory allocation, static scoping,
or static variable bindings?

Memory allocation

6.

Give an example of something in Java that can be a r
-
value but not an l
-
value.

Any literal.

7.

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.

8.

What type of scoping does Java use?

Static

9.

If a variable name has bindings that are dependent on the runtime history of the
program then what type of scoping is being used?

Dynamic

10.

Give a reason that static scoping is preferred.

Harder to do type checking, error
-
prone, security issues, access
ing nonlocal
variables takes more time.

11.

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.

12.

Name an advantage of overloading

Easy to unders
tand, don’t have to think of convoluted names for related things

13.

Give an example of operator overloading in Java

+ for strings




Easy

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
-
the
-
fly

2. Do most common languages use static or dynamic scoping?

Static

3. What is the “lifetime” of a variable?

The time in which a variable is in memory

4. What is an “identifier”?

T
he name of an entity like a function, variable, or type

5. What does it mean to be case
-
sensitive?

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
variable’s r
-
value refer to?

Its value

9. What is a variable’s scope?

The lines of code that are allowed to refer to a variable

10.

What is a symbol table?

A data structure that keeps up with what each name is bound to.

11.

What is it called when one na
me can mean several different things?

Overloading

12.

What is a variable’s lifetime?

The time in which it is allocated memory.