You are on page 1of 4

322 Chapter 6 Data Types

42. Why is Java not strongly typed?


43. What is the syntax of array references?
44. What languages have no type coercions?
45. Why are the unions in C and C++ called free unions?
46. What is name type equivalence?
47. What is a dangling pointer?
48. What is the primary advantage of name type equivalence?
49. What is the primary disadvantage to structure type equivalence?
50.
51.
For what types does C use structure type equivalence?
What set operation models C’s struct data type?

pesirscledt
binany
p r o B l e m S e t EroageofralMwexpmenead n

lstonsere
'
deimaludye . iuo

… P hh精 存
tatesob
1. What are the arguments for and against representing Boolean values as
single bits in memory?
2. How does a decimal value waste memory space? one dig
v 7 tabyt
tesopesereatio
sforese
take onorf mas
'

Tx
3. VAX minicomputers use a format for floating-point numbers that is
not the same as the IEEE standard. What is this format, and why was
least decnral
4u bit

wigie
code
it chosen by the designers of the VAX computers? A reference for VAX
floating-point representations is Sebesta (1991).
4. Compare the tombstone and lock-and-key methods of avoiding dangling
pointers, from the points of view of safety and implementation cost.
僅發⽣在某些上⽂中時 0
5. What disadvantages are there in implicit dereferencing of pointers, but
降低language 可素性 only in certain contexts?
6. Explain all of the differences between Ada’s subtypes and derived types.
⽤於訪問指向结構 or ←ψ 7. What significant justification is there for the -> operator in C and C++?
類指針的成員 使程式 c 8.,
vWhat are all of the differences between the enumeration types of C++
and those of Java?

法 , 改值類数型範圈法 Y )9. Multidimensional arrays can be stored in row major order, as in C++, or
途代器⽀援订右
(

, in column major order, as in Fortran. Develop the access functions for


both of these arrangements for three-dimensional arrays.
10. In the Burroughs Extended ALGOL language, matrices are stored as a
delet
ew
管理em
性uufo
以酒
single-dimensioned array of pointers to the rows of the matrix, which are
.

。 更⾼memysafe
和默效 treated as single-dimensioned arrays of values. What are the advantages
0 型别安链⽩動虔理型别换 V X
and disadvantages of such a scheme?
0⽀摇星常虚理 11. Analyze and write a comparison of C’s malloc and free functions with
C++’s new and delete operators. Use safety as the primary consideration
malloc in the comparison.
⼿動管vet
uwsncnalou
安全
便利内存直接 antrol ,
⽅便零活
⼿动 需出錯
(t+
ctt
分配内建回收
:

Jaa 不仍⾏ mmy


,

机制 Programming Exercises 323


⾃鲜 memny管理更安全
SJmei 12. Analyze and write a comparison of using C++ pointers and Java reference
variables to refer to fixed heap-dynamic variables. Use safety and conve-
nience as the primary considerations in the comparison.
13. Write a short discussion of what was lost and what was gained in Java’s
designers’ decision to not include the pointers of C++.
14. What are the arguments for and against Java’s implicit heap stor-
age recovery, when compared with the explicit heap storage recovery
required in C++? Consider real-time systems.
15. What are the arguments for the inclusion of enumeration types in C#,
although they were not in the first few versions of Java?
16. What would you expect to be the level of use of pointers in C#? How
often will they be used when it is not absolutely necessary?
借⽀援不规助不规
⽂本管理圆管理 , 17. Make two lists of applications of matrices, one for those that require jag-
d
X
ged matrices and one for those that require rectangular matrices. Now,
更合直接⾼效则
H 算酷⼤值化
argue whether just jagged, just rectangular, or both should be included in
继你代波值 a programming language.
mafily
運算能⼒ 18. Compare the string manipulation capabilities of the class libraries of
bolh .
遍⽤多⽤途语⾔ C++, Java, and C#.
19. Look up the definition of strongly typed as given in Gehani (1983) and
compare it with the definition given in this chapter. How do they differ?
20. In what way is static type checking better than dynamic type checking?
21. Explain how coercion rules can weaken the beneficial effect of strong
typing.

p r o g r a m m i N g e x e r c i S e S

1. Design a set of simple test programs to determine the type compatibility


rules of a C compiler to which you have access. Write a report of your
findings.
2. Determine whether some C compiler to which you have access imple-
ments the free function.
3. Write a program that does matrix multiplication in some language that does
subscript range checking and for which you can obtain an assembly language
or machine language version from the compiler. Determine the number of
instructions required for the subscript range checking and compare it with
the total number of instructions for the matrix multiplication process.
4. If you have access to a compiler in which the user can specify whether
subscript range checking is desired, write a program that does a large
number of matrix accesses and time their execution. Run the program
with subscript range checking and without it, and compare the times.
Problem Set 385

18. In C, what is the significance of a for loop without the second


expression?
19. What does the range function in Python do?
20. What alternative is provided for goto in Java?
21. What are the design issues for logically controlled loop statements?
22. What is the main reason user-located loop control statements were
invented?
23. What predefined iterator for standard data structures is available
in Perl?
24. What is the use of a break statement in switch statements?
25. What are the differences between the break statement of C++ and that
of Java?
26. What are fatbars in guarded clauses?
27. What is the limitation of a guarded command?
28. How does a functional language implement repetition?
29. How are iterators implemented in Ruby?
30. What language predefines iterators that can be explicitly called to iterate
over its predefined data structures?
31. What common programming language borrows part of its design from
Dijkstra’s guarded commands?

P r o b l e m S e t
a .
Alse of values is to be addedto d SUM ,

but the loop if SUM be exited


is to
v
xceeds some presciked valle .
1. Describe three situations where a combined counting and logical looping
Alist otralue isto be read
statement is needed.
G Tmto an array ,
where

apresciibefdmber
the neading is to Terinate whey lither
of ialnes have blen nead
specpal value
2. Study the iterator feature of CLU in Liskov et al. (1981) and determine
or some

is found in the lise

ThvuermaeiodirofMidtnsF its advantages and disadvantages.


qosystesoergostl

ouintaiorgiifhrorrgi
uitbmghintkmshi
mid
bwr n ton

WiolMi nontptiflI
V
3. Compare the set of Ada control statements with those of C# and decide
mrbsihautooo 是
which are better and why.
4. What are the pros and cons of using unique closing reserved words on
compound statements?
ComTvol Statememe s the most
Cd
flexiby
ratonstqternent , RMaf 4 sple seleaton
5. What are the arguments, pros and cons, for Python’s use of indentation
woictabikty
#

to specify compound statements in control statements?


C

tuaatures is a greut boosttoc #

c
可讀簡得强制规節名
敏弛不适⽤被穿就讨播
6. Analyze the potential readability problems with using closure reserved
words for control statements that are the reverse of the corresponding initial
ue reserved words, such as the case-esac reserved words of ALGOL 68. For
example, consider common typing errors such as transposing characters.
7. Use the Science Citation Index to find an article that refers to Knuth
(1974). Read the article and Knuth’s paper and write a paper that sum-
marizes both sides of the goto issue.
386 Chapter 8 Statement-Level Control Structures

8. In his paper on the goto issue, Knuth (1974) suggests a loop control
statement that allows multiple exits. Read the paper and write an opera-
tional semantics description of the statement.
9. What are the arguments both for and against the exclusive use of Bool-
ean expressions in the control statements in Java (as opposed to also
allowing arithmetic expressions, as in C++)?
找⽬標榜 ← 10. Describe a programming situation in which the else clause in Python’s
for statement would be convenient.

显限迪国⾄少
exeante
*

11. Describe three specific programming situations that require a posttest loop.
12. Speculate as to the reason control can be transferred into a C loop statement.
资料虎理
P r o g r a m m i n g e x e r c i S e S

1. Rewrite the following pseudocode segment using a loop structure in the


specified languages:

k = (j + 13) / 27
loop:
if k > 10 then goto out
k = k + 1
i = 3 * k - 1
goto loop
out: . . .

a. C, C++, Java, or C#
b. Python
c. Ruby
Assume all variables are integer type. Discuss which language, for this
code, has the best writability, the best readability, and the best combin-
ation of the two.
2. Redo Programming Exercise 1, except this time make all the variables
and constants floating-point type, and change the statement

k = k + 1

to
k = k + 1.2

3. Rewrite the following code segment using a multiple-selection statement


in the following languages:

if ((k == 1) || (k == 2)) j = 2 * k - 1
if ((k == 3) || (k == 5)) j = 3 * k + 1
if (k == 4) j = 4 * k - 1
if ((k == 6) || (k == 7) || (k == 8)) j = k - 2

You might also like