You are on page 1of 2

Chapter 7 Homework

7.1 Consider the following sequence ofC/C++ statements, which are syntactically valid ut
have no reasonale semantic interpretation !assuming that i and" have een declared as i nt
variales#$
j = 0;
i = 3 / j;
for( i = 1; i > -1; i ++)
i --;
How are these situations handled when e%ecuted y your C/C++ system&
'n the statement$ for ! i(1 )i*+1 )i++#
this will e an infinite loop since i will never ecome less than +1 ecause of incrementation
i(,/" will e an invalid statement since it will ecome infinity
7.- .ive other kinds !eyond those illustrated in the previous question# of CIC++ statements
that are syntactically valid ut whose meaning cannot e reasonaly defined in the semantics of
a programming language.
7., !a# How does /ava define the numerical idea of infinity& !0ou should look at the Java
Language Specification l.osling et ai., 11123 for the details.#
4he /ava 5anguage 6pecification defines the idea of infinity as a floating point numer. 4he
numer is the result of the operation$
1.7f/7.7f
4he aove represents postive infinity while negative infinity would e the same
equation with a negative 1.7f instead. 8hile this numer is truly infinite !as no digitally
recorded numer could e# it has many properties which make it useful as a representation of
infinity. 9ne fairly important property is that negative infinity will always e evaluated as less
than any other floating point numer and likewise, positive infinity will evaluate as greater than
any floating point numer.
!# 5ooking at the specifications in the Java Language Definition :;rnold and .osling, 111<3,
can you e%plain in plain =nglish the meaning of the statement i ( 3fj; for all possile values of j,
including 7&
' am going to assume that " is an identifier of a primitive type variale ecause otherwise it
would not compile. 4here are four rules which must e checked in order, much like an if+else if
statement, to evaluate the aove statement. 4he first rule is that if either operand of the
arithmetic e%pression is a doule, convert the other to a doule as well and compute a doule
result. 'n this case, since , is not a doule, if " did represent a doule value then the value of i
would e the precise !to an e%tent# result of ,.7 divided y the value of ". 4he second rule is "ust
like the first ut regarding floats. 'f " represented a float value then the resulting value of i
would e a float equal to precisely !to an e%tent again# ,.7 divided y the value of ". 4he third
rule is again the same ut with longs. 'f " represented a long value then the resulting value of i
would e equal to , divided y the value of "...truncated to the ne%t lower integer !mathematical
integer, not "ava#. 4he last rule states that in all other cases, meaning neither operand is a
doule, float or long, convert oth operands to ints. 4hen i would e the result of , divided y ",
again truncated to the ne%t lower integer.
4he value that is stored in i would also depend on the type of i. 'f i did not type+match
with " then the value of , / " may e >widended> iff i?s type is a superset of "?s type.
'f " is 7 then the statement will not compile ecause division y @ero is not accepted.
7.A Consider the e%pression X + y/2 in the language C. How many different meanings does this
e%pression have, depending on the types of % and y&
B ;CD 0 E94H ;F= G59;4 B +0/-(H.H77777
7.H Fewrite the factorial program of Gigure 7.- in /ava so that the the initial value of n is read as
input, and the final value of f is written to the screen. Cow run the program for the values -,
,, ... until the value of f overflows the si@e of an int value. 8hat is the value of n for which this
occurs& How is this error treated y /ava&
7.2 Gor your CIC++ compiler, verify whether or not the cast of the integer -H7 to a char results
in an error.
7.7 Gor your /ava compiler/interpreter, verify whether or not the cast of the integer 2HH,7 to a
char results in an error.
'n "ava casting integer to character is not possile. 't will throw an error like elow >possile
loss of precision> this is all ecause of the si@e of charater is too small to accomodate the
integer type variale.
7.< Gor oth C/C++ and /ava, write a program to compute -
,7
+ 2HH,A. 4hen convert this
integer !or long int) to floating point y adding 1.7. Ce%t convert the floating point numer
ack to an integer and sutract 1. Do you get ack the same integer you started with& Do not
use doule precision floating point.
7.1, Ising 8e+ased sources, e%amine the C++ an d;da e%ception handling models in more
detail.
!a# How does the C++ model for e%ception handling differ from the /ava model discussed in
this chapter&
!# How does the ;da model for e%ception handling differ from the /ava model&

You might also like