Sunteți pe pagina 1din 6

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 x = 5

If dynamic scoping is considered 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 Declared in
d, e, f fun3
c fun2
b fun1
a main

b. main calls fun1; fun1 calls fun3.

Variable Declared in
d, e, f fun3
b, c fun1
a main

c. main calls fun2; fun2 calls fun3; fun3 calls fun1.



Variable Declared in
b, c, d fun1
e, f fun3
a main


d. main calls fun3; fun3 calls fun1.

Variable Declared in
b, c, d fun1
e, f fun3
a main

e. main calls fun1; fun1 calls fun3; fun3 calls fun2.

Variable Declared in
c, d, e fun2
f fun3
b fun1
a main

f. main calls fun3; fun3 calls fun2; fun2 calls fun1.

Variable Declared in
b, c, d fun1
e fun2
f fun3
a 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 Union combined with records
Advantages
- Reduce developer overhead
(less checking)
- Goes with the C++ design (C++
is not a weak strong typing
language).
- Support type checking
- Goes with the Ada design (Ada
is strong typing language)
Disadvantages
- No type checking of references
- May end with wrong or
unexpected results
- 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)

C++ pointers Java reference
Safety
Hazardous ( pointers can point at
any variable anywhere in memory
because of manual allocation and
de-allocation of pointers)
Highly Safety (because java
references refer to class instances
and class instances are implicitly
de-allocated so that no dangling
references occurrence.
Convenience
Highly convenience because it
allow you to have more
management over variables.
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.

S-ar putea să vă placă și