1. What is a block?
2. What differences are there between a procedure block and a module block?
3. When must/must not a module have a block body?
4. Define scope.
5. Which of (a) a Procedure's name (b) its parameters (c) its local variables are part of its scope?
6. Under what circumstances might a procedure's name not be visible in its body?
7. Define the terms local and global as they pertain to procedures.
8. Why is it important to avoid using many global variables?
9. Suppose the declaration VAR z : BOOLEAN were contained in a program module, again in a procedure, and yet again in another procedure inside this one. Does an assignment to z in the innermost procedure change the value of the variable in the main program? in the first procedure? in some other procedure declared in the main program? in some other procedure defined inside the first one? What if yet another procedure were declared inside the one innermost above, this one with no BOOLEANs declared in it, but also with an assignment to z. At what level(s) in the above hierarchy is z affected?
10. What does visibility mean?
11 Define the term side effect.
12. Under what circumstances are side effects good? bad?
13. Under what circumstances might a program have many variables with the same name, and how does the compiler distinguish among them?
14. Describe the differences between the scope rules for nested procedures and those for nested modules.
15. What is meant by "the outermost scope level?"
16. How do modules import two items of the same name into the same scope?
17. Can two modules EXPORT items of the same name into the same scope?
18. What is the difference between a variable that is EXPORTed and one that is EXPORTed as QUALIFIED? Illustrate with examples.
19. A program module cannot have an EXPORT list. Why?
20. What is the difference between a standard identifier and a reserved word?
21. What is a dynamic module?
22. How may the visibility of identifiers inside a dynamic module be different from that in the scope immediately surrounding it?
23. What is a Fibonacci sequence?
24. What is the difference between an opaque type and a transparent type?
25. What is a procedure type?
26. Show how to define procedure types in each case that could have the following assigned to it.
(a) SeqFile.OpenWrite
(b) STextIO.WriteString
(c) RealIO.WriteFixed
(d) Strings.Append
(e) LongMath.ln
27. When should you use or not use the LOOP .. EXIT construction?
28. What two Modula-2 commands cause the program to unconditionally transfer control to some other line of code?
29. Which two commands can cause a program to terminate?
30. In what other way, besides the issuing of a direct command, might a program be caused to terminate?
31. How is a termination event detected?
32. How does one determine whether a program is in normal or exceptional execution state?
33. What is an exception?
34. How is an exception trapped?
35. What are the three strategies that can be followed in an exception handler?
36. What specific tools are necessary, and how are they used, to declare and detect exceptions in user-written code?
37 How do the exceptions facilities prevent the raising of exceptions by code that does not contain the definition of those exceptions in its own scope?
38. How many exception handlers may a program module contain?
39. How many exception handlers may the body of a module itself have attached?
40. What is the output of the following program?
MODULE Scramble; FROM SWholeIO IMPORT WriteCard; FROM STextIO IMPORT WriteLn; VAR w, x, y, z : CARDINAL; PROCEDURE Scram (x, y : CARDINAL); VAR z : CARDINAL; BEGIN w := 7; x := 8; y := 9; z := 10; END Scram; BEGIN (* Main *) w := 1; x := 2; y := 3; z := 4; WriteCard (w, 6); WriteCard (x, 6); WriteCard (y, 6); WriteCard (z, 6); WriteLn; Scram (x, y); WriteCard (w, 6); WriteCard (x, 6); WriteCard (y, 6); WriteCard (z, 6); WriteLn; END Scramble.
41. Make a list of the variables visible at the places marked point 1, point 2, etc. If some are visible provided that they are written as qualified identifiers, please indicate this also. This question of course assumes a multi pass compiler. Why?
MODULE Exercise; VAR outA, outB : REAL; MODULE Inner1; EXPORT inner1C, days; TYPE days = (Sat, Sun); VAR inner1C, inner1D : REAL; (* point 1 *) END Inner1; MODULE Inner2; EXPORT QUALIFIED inner2E, Inner3; VAR inner2E, inner2F : REAL; (* point 2 *) MODULE Inner3; EXPORT inner3H; VAR inner3G, inner3H : REAL; END Inner3; (* point 3 *) END Inner2; (* point 4 *) END Exercise.
42. Give an example of a program (not the same as in the text) in which unexpected results are obtained because a procedure modifies a variable global to it.
43. Give an example of a program (not the same as in the text) in which unexpected results are obtained because a procedure has a variable parameter when a value parameter would do.
44. Expand upon the bar graph program in section 10.2 as follows: Write a module that allows a user to enter data in several (1 - 10) subcategories of some main category and that then prints out an appropriately scaled bar graph that pictures the data. Make the program module general, but test it on the following data:
The number of students at TWU taking each of the following courses are in parentheses: Chemistry (120), Physics (70), Mathematics (200), Biology (130), and Computer Science (200).
45. Write a program module to examine the contents of Modula-2 program files and count the number of occurrences of the various reserved words and standard identifiers. Use a LOOP in the routine that reads the file.
46. Select a program from chapter 8 that employed StreamFile and add to it a FINALLY clause that closes any channels still open.
47. Select a program from chapter 8 that employed SeqFile and add to it a FINALLY clause that closes any channels still open.
48. Select a program from chapter 8 that employed RndFile and add to it a FINALLY clause that closes any channels still open.
49. Revise the module PointToPoint in chapter 6 to add a procedure to compute the slope (using the slope formula) of a segment joining two points. Forbid vertical segments with an appropriate precondition, but add an exception to cover cases where client software ignores the rules. Encapsulate the whole thing in a library module and test the result.
50. Revise the module Fractions in this chapter to raise a different exception indeterminate in cases where both the numerator and denominator are zero. Do a complete test of the result.
51. Add exceptions as appropriate to the module Stats in chapter 7, and test the result.
52. Write a program module to demonstrate (by catching and handling the exception and printing the message) that an ISO library module that tries to close a channel it did not open causes an exception.
53. Write a program module to demonstrate (by catching and handling the exception and printing the message) that a program that attempts to access an array element with too large an index causes an exception.
54. Write a program module to demonstrate (by catching and handling the exception and printing the message) that a program that attempts to obtain the error number of an exception but provides the wrong source identifier causes an exception.
55. Write a program module to demonstrate (by catching and handling the exception and printing the message) that a program that attempts to use RndFile.NewPos to compute a file position past the end of the file causes an exception.
56. Modify the program in #15 to print the bar graphs with vertical bars instead of horizontal ones.
57. Write a new version of the ISO library module Strings with the same content as in the standard, but with exceptions added to cover all those cases where attempts are made to overfill strings. That is, require the user of the module to employ such procedures as CanDeleteAll and CanInsertAll, before Delete and Insert are used, by removing the silent truncation semantics from the latter and replacing them with the raising of exceptions when the limits are violated.