HW3_Key - Chapter 5(Review Questions Pages 212 – 213 13...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 5: (Review Questions: Pages 212 – 213) 13) Define coercion, type error, type checking, and strong typing.(10 Points) • Coercion is the automatic conversion between compatible types and it is done by the compiler. • A Type error is the application of an operator to an operand of an inappropriate type. • Type checking is the activity of ensuring that the operands of an operator are of compatible types. • A Strong typing language is one in which each name in a program in the language has a single type associated with it, and that type is known at compile time. 16) Define lifetime, scope, static scope, and dynamic scope. (10 Points) • The lifetime of a variable is the time during which the variable is bound to a specific memory location. • The scope of a variable is the range of statements over which it is visible. • Static scope is binding names to non-local variables. • Dynamic scope is based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus the scope can be determined only at run time. 18) What is the general problem with static scoping? (10 Points) Static scoping, in general, can have those kind of problem: 1. A programmer could mistakenly call a subprogram that should not have been callable, which world not be detected as an error by the complier. 2. Too much data access and verifications. 3. Encourages the use of more global than are necessary and not preferable in programming. ١ Chapter 5: (Problem Set: Page 214-217) 9) Assume the following program was compiled and executed using static scoping rules. What value of x is printed in procedure sub1? Under dynamic scoping rules, what valued of x is printed in procedure sub1? (10 Points) program main; var x : integer: procedure sub1; begin { sub1 } writeln ('x = ' , x) end; { sub1 } procedure sub2; var x : integer; begin { sub2 } x := 10; sub1 end; { sub2 } begin { main } x := 5; sub2 end. { main } Answer: • If static scoping is considered • If dynamic scoping is considered x=5 x = 10 ٢ 11) Consider the following program: (10 Points) program main; var x, y, z : integer: procedure sub1; begin { sub1 } … end; { sub1 } procedure sub2; var a, x, w : integer; procedure sub3; var a, b, z : integer; begin { sub3 } … end; { sub3 } begin { sub2 } … end; { sub2 } begin { main } end. { main } Answer: As it is clear from the structure of the program: ٣ 13) Consider the following skeletal C program: (20 Points) 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 varied that name of the function is which is defined. a. main calls fun1; fun1 calls fun2; fun2 calls fun3. Variable d, e, f c b a Declared in fun3 fun2 fun1 main b. main calls fun1; fun1 calls fun3. Variable d, e, f b, c a Declared in fun3 fun1 main c. main calls fun2; fun2 calls fun3; fun3 calls fun1. Variable b, c, d e, f a Declared in fun1 fun3 main ٤ d. main calls fun3; fun3 calls fun1. Variable b, c, d e, f a Declared in fun1 fun3 main e. main calls fun1; fun1 calls fun3; fun3 calls fun2. Variable c, d, e f b a Declared in fun2 fun3 fun1 main f. main calls fun3; fun3 calls fun2; fun2 calls fun1. Variable b, c, d e f a Declared in fun1 fun2 fun3 main ٥ Chapter 6: (Problem Set: Page 277-279) 10) the unions in C and C++ are separate from the records of those languages, rather than combined as they are in Pascal and Ada. What are the advantages and disadvantages to these two choices? (10 Points) Union separated from records - Reduce developer overhead (less checking) - Goes with the C++ design (C++ Advantages is not a weak strong typing language). - No type checking of references Disadvantages - May end with wrong or unexpected results Union combined with records - Support type checking - Goes with the Ada design (Ada is strong typing language) - Increase developer overhead - using more functions to resolve type compatibility 20) Analyze and write a comparison of using C++ pointer and Java reference variables to refer to heap-dynamic variables. Use safety and convenience as the primary considerations in the comparison. (10 Points) Safety C++ pointers Hazardous ( pointers can point at any variable anywhere in memory because of manual allocation and de-allocation of pointers) Highly convenience because it Convenience allow you to have more management over variables. Java reference Highly Safety (because java references refer to class instances and class instances are implicitly de-allocated so that no dangling references occurrence. Lesser convenience since it restricts the way you manage the variables. 22) What are the arguments for and against Java's implicit heap storage recovery, when compared with explicit heap storage recovery required in C++?(10 Points) In Java implicit heap storage recovery will prevent dangling object problem since garbage collection is used and it is done automatically by the run-time system. While, explicit heap storage recovery, as in C++, creates dangling pointers because the deallocation statement names only one pointer to the heap-dynamic variable to be deallocated. So that any other pointers to the disposed heap-dynamic variable are made dangling by the process, because the run-time system did not determine that other such pointers exist. Hence Java's implicit heap storage recovery is significant and safer than the explicit heap storage recovery required in C++. Moreover, Java's implicit heap storage recovery reduce the programmer overhead and in the worst cases no lost of data can occurs since the run-time system is responsible for allocation and deallocation of variables. ٦ ...
View Full Document

This note was uploaded on 09/15/2010 for the course ICS ics103 taught by Professor Alvi during the Spring '07 term at King Fahd University of Petroleum & Minerals.

Ask a homework question - tutors are online