Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
ADVANCE
PROGRAMMING
LANGUAGES
By: Farwa Ansari
(Chapter 5)
Page | 1
Chapter 5: Names, Bindings and Scopes
Review Questions
Question 1:
What are the design issues for names?
Solution...
Page | 2
Question 7:
Define binding and binding time.
Solution:
A binding is an association between an attribute and an en...
Page | 3
Question 12:
Define static, stack-dynamic, explicit heap-dynamic, and implicit heap dynamic variables.
What are t...
Page | 4
Solution:
I. It allows more access to both variables and subprograms than is necessary.
II. It allows more access...
Page | 5
end;
While in C variable declaration is as follows:
int number = 1;
Question 21:
Describe the encapsulation of an...
Page | 6
Problem Set
Question 6:
Consider the following JavaScript skeletal program:
// The main program
var x;
function s...
Page | 7
a = 19;
b = 21;
z = 23;
. . .
. . .
List all the variables, along with the program units where they are declared,...
Page | 8
2 int a,b,c in definition 1, and int b,c,d in definition 2, and int c, d, e in
definition 3
3 int a,b,c in defini...
Page | 9
var x, y, z;
function sub1() {
var a, y, z;
. . .
}
function sub2() {
var a, b, z;
. . .
}
function sub3() {
var ...
Upcoming SlideShare
Loading in …5
×

Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)

3,519 views

Published on

Concept of Programming Languages Tenth Edition (Robert W. Sebesta)

Published in: Education
  • Hi there! I just wanted to share a list of sites that helped me a lot during my studies: .................................................................................................................................... www.EssayWrite.best - Write an essay .................................................................................................................................... www.LitReview.xyz - Summary of books .................................................................................................................................... www.Coursework.best - Online coursework .................................................................................................................................... www.Dissertations.me - proquest dissertations .................................................................................................................................... www.ReMovie.club - Movies reviews .................................................................................................................................... www.WebSlides.vip - Best powerpoint presentations .................................................................................................................................... www.WritePaper.info - Write a research paper .................................................................................................................................... www.EddyHelp.com - Homework help online .................................................................................................................................... www.MyResumeHelp.net - Professional resume writing service .................................................................................................................................. www.HelpWriting.net - Help with writing any papers ......................................................................................................................................... Save so as not to lose
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • You can hardly find a student who enjoys writing a college papers. Among all the other tasks they get assigned in college, writing essays is one of the most difficult assignments. Fortunately for students, there are many offers nowadays which help to make this process easier. The best service which can help you is HelpWriting.net
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)

  1. 1. ADVANCE PROGRAMMING LANGUAGES By: Farwa Ansari (Chapter 5)
  2. 2. Page | 1 Chapter 5: Names, Bindings and Scopes Review Questions Question 1: What are the design issues for names? Solution: The following are the primary design issues for names: I. Are names case sensitive? II. Are the special words of the language reserved words or keywords? Question 2: What is the potential danger of case-sensitive names? Solution: Potential issue of case sensitive names is Readability because if names will not be case sensitive then multiple names can be of same name effective the readability of the program. Question 3: In what way are reserved words better than keywords? Solution: A reserved word is a special word that cannot be used as a user-defined name. A keyword is a word that is special only in certain contexts and can be used as a user-defined name. Question 4: What is an alias? Solution: When more than one variable can be used to access the same memory location, then the variables are called aliases. Question 5: Which category of C++ reference variables is always aliases? Solution: The variables that belong to the category of Union typed variables. Union is a type whose variables may store different type values at different times during program execution. Two pointer variables are also aliases when they point to the same memory location. Question 6: What is the l-value of a variable? What is the r-value? Solution: The l-value of a variable is its address where its value is stored while the r-value of a variable is its value.
  3. 3. Page | 2 Question 7: Define binding and binding time. Solution: A binding is an association between an attribute and an entity, or between an operation and a symbol. Binding time is the time at which binding takes place. Binding time can be late or early that is at run time or compile time respectively. Question 8: After language design and implementation what are the four times bindings can take place in a program? Solution: I. Language design time — bind operator symbols to operations II. Language implementation time– bind floating point type to a representation III. Compile time — bind a variable to a type in C or Java or else IV. Load time — bind a C or C++ static variable to a memory cell V. Runtime — bind a non-static local variable to a memory cell Question 9: Define static binding and dynamic binding. Solution: A static binding occurs before run time that is at compile time and remains unchanged throughout program execution while a dynamic binding occurs during execution or at run time and can change during execution of the program. Question 10: What are the advantages and disadvantages of implicit declarations? Solution: Advantages Disadvantages Convenient to programmers Can be detrimental to reliability because they prevent the compilation process from detecting some typographical and programmer errors. Writability Question 11: What are the advantages and disadvantages of dynamic type binding? Solution: Advantages: Flexibility means creation of generic program units Disadvantages: Add to the cost of implementation and type error detection by the compiler is difficult.
  4. 4. Page | 3 Question 12: Define static, stack-dynamic, explicit heap-dynamic, and implicit heap dynamic variables. What are their advantages and disadvantages? Solution: I. Static – bound to memory cells before execution begins and remains bound to the same memory cell throughout the execution. II. Stack-dynamic – storage bindings are created for variables when their declaration statements are elaborated. III. Explicit heap-dynamic – allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. IV. Implicit heap-dynamic variables – Allocation and deallocation caused by assignment statements. Question 13: Define lifetime, scope, static scope, and dynamic scope. Solution: I. Lifetime – A time during which the variable is bound to a specific memory location. The lifetime begins when it is bound to a specific cell and ends when it is unbound from that cell or the time in which the variable is in use. II. Scope – The range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement. III. Static scope – is based on program text and to connect a name reference to a variable, you or the compiler must find the declaration. IV. Dynamic scope – Based on calling sequences of program units, not their textual layout. References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point. Question 14: How is a reference to a nonlocal variable in a static-scoped program connected to its definition? Solution: Suppose a reference is made to a variable x in subprogram sub1. The correct declaration is found by first searching the declarations of subprogram sub1. If no declaration is found for the variable there, the search continues in the declarations of the subprogram that declared subprogram sub1, which is called its static parent. If a declaration of x is not found there, the search continues to the next-larger enclosing unit (the unit that declared sub1’s parent), and so forth, until a declaration for x is found or the largest unit’s declarations have been searched without success. Question 15: What is the general problem with static scoping?
  5. 5. Page | 4 Solution: I. It allows more access to both variables and subprograms than is necessary. II. It allows more access to both variables and subprograms than is necessary. Question 16: What is the referencing environment of a statement? Solution: The referencing environment of a statement is the collection of all variables that are visible in the statement. Question 17: What is a static ancestor of a subprogram? What is a dynamic ancestor of a subprogram? Solution: Static ancestors are the static parents of the static subprogram. The static programs that holds the static subprograms are the static ancestors of the static subprograms. While the dynamic ancestors of a dynamic subprogram are all the procedures called before dynamic subprogram during the execution of a program, that have not yet finished executing. Question 18: What is a block? Solution: Block is a section of code in a program. Question 19: What is the purpose of the let constructs in functional languages? Solution: "let" introduces a new variable scope, and allows user to bind variables to values for that scope. It is often read as "let x be the [value] in …" Question 20: What is the difference between the names defined in an ML let construct from the variables declared in a C block? Solution: In ML, the form of a let construct is as follows: let val name 1 = expression 1 . . . val name n = expression n in expression
  6. 6. Page | 5 end; While in C variable declaration is as follows: int number = 1; Question 21: Describe the encapsulation of an F# let inside a function and outside all functions. Solution: Question 22: What are the advantages and disadvantages of dynamic scoping? Solution: Advantage Disadvantage The only advantage of dynamic scoping is writability. It is more convenient and it provides more flexibility than static scoping. For example, in some cases, some parameters passed from one subprogram to another are variables that are defined in the caller. None of these need to be passed in a dynamic scoped language, because they are implicitly visible in the called subprogram. Inability to statically check for references to nonlocal variables. Dynamics scoping also makes program difficult to read because the calling sequence of subprograms must be known to determine the meaning of references to nonlocal variables. Question 23: What are the advantages of named constants? Solution: The advantages are readability and modifiability.
  7. 7. Page | 6 Problem Set Question 6: 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. 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 b. Repeat part a, but assume dynamic scoping. Solution: a. Considering the recent declaration of x in sub1, static scoping of x in sub1 will remain in sub1 while for sub2 and sub3 static scope will be in sub1 and main respectively. b. As in the dynamic scoping the compiler or interpreter finds the correct instance of variable in use at its actual location so the dynamic scoping for a reference to x in sub1, sub2, sub3 will lie in sub1. Question 9: Consider the following Python program: x = 1; y = 3; z = 5; def sub1(): a = 7; y = 9; z = 11; . . . def sub2(): global x; a = 13; x = 15; w = 17; . . . def sub3(): nonlocal a;
  8. 8. Page | 7 a = 19; b = 21; z = 23; . . . . . . 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. Solution: Functions Variable’s values Variables x y z a b w Before sub1 when program starts execution from main 1 3 5 Sub1 9 11 7 Sub2 15 13 17 Sub3 23 19 21 Final Values 15 9 23 19 21 17 Question 10: Consider the following C program: void fun(void) { int a, b, c; /* definition 1 */ . . . while (. . .) { int b, c, d; /*definition 2 */ . . . 1 while (. . .) { int c, d, e; /* definition 3 */ . . . 2 } . . . 3 } . . . 4 } For each of the four marked points in this function, list each visible variable, along with the number of the definition statement that defines it. Solution: Marked Points Visible variables along with their definition statement numbers 1 int a,b,c in definition 1, and int b,c,d in definition 2
  9. 9. Page | 8 2 int a,b,c in definition 1, and int b,c,d in definition 2, and int c, d, e in definition 3 3 int a,b,c in definition 1, and int b,c,d in definition 2 4 int a,b,c in definition 1 because definition 2’s and 3’s scope ends before definition 1’s Question 11: Consider the following skeletal C program: void fun1(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void); /* prototype */ void main() { int a, b, c; . . . } void fun1(void) { int b, c, d; . . . } void fun2(void) { int c, d, e; . . . } void fun3(void) { int d, e, f; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was defined. a. main calls fun1; fun1 calls fun2; fun2 calls fun3. b. main calls fun1; fun1 calls fun3. c. main calls fun2; fun2 calls fun3; fun3 calls fun1. d. main calls fun3; fun3 calls fun1. e. main calls fun1; fun1 calls fun3; fun3 calls fun2. f. main calls fun3; fun3 calls fun2; fun2 calls fun1. Solution: a. d, e, f in fun3. c in fun2. b in fun1 b. d, e, f in fun3. b, c in fun1 c. b, c, d in fun1. e, f in fun3. c in fun2. d. b, c, d in fun1. e , f in fun3. e. c, d, e in fun2. f in fun3. b in fun1 f. b, c, d in fun1. e in fun2. f in fun3 Question 12: Consider the following program, written in JavaScript-like syntax: // main program
  10. 10. Page | 9 var x, y, z; function sub1() { var a, y, z; . . . } function sub2() { var a, b, z; . . . } function sub3() { var a, x, w; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last subprogram activated? Include with each visible variable the name of the unit where it is declared. a. main calls sub1; sub1 calls sub2; sub2 calls sub3. b. main calls sub1; sub1 calls sub3. c. main calls sub2; sub2 calls sub3; sub3 calls sub1. d. main calls sub3; sub3 calls sub1. e. main calls sub1; sub1 calls sub3; sub3 calls sub2. f. main calls sub3; sub3 calls sub2; sub2 calls sub1. Solution: a. a, x, w in sub3. b, z in sub2, y in sub1. b. a, x, w in sub3. y, z in sub1. c. a, y, z in sub1. x, w in sub3. b in sub2. d. a, y, z in sub1. x, w in sub3. e. a, b, z in sub2. x, w in sub3. y in sub1. f. a, y, z in sub1. b in sub2. x w in sub3.

×