Review Questions
1. Define operator precedence and
operator associativity.
-The operator precedence rules for
expression evaluation partially define
the order in which the operators of
different precedence levels are evaluated.
-An operator can have either left or
right associativity, meaning that when
there are two adjacent operators with
the same precedence, the left operator is
evaluated first or the nright operator
is evaluated first, respectively.
2. What is a ternary operator?
-ternary, meaning it has three
operands.
3. What is a prefix operator?
-means they precede their operands.
8. Define functional side effect.
-A side effect of a function, naturally
called a functional side effect, occurs
when the function changes either one of
its parameters or a global variable. (A
global variable is declared outside the
function but is accessible in the
function.)
10. What is a conditional expression?
-is a if-then-else that has more
convinient form :
expression_1 ? expression_2 :
expression_3
where expression_1 is interpreted as a
Boolean expression.
If expression_1 evaluates to true, the
value of the whole expression is the
value of expression_2;
otherwise, it is the value of
expression_3.
13. In JavaScript, what is the
difference between == and ===?
== is equality relational operator
=== is similiar to == but prevent their
operands from being coerced.
14. What is a mixed-mode expression?
-language that allow expressions is
whether an operator can have operands of
different types.
15. What is referential transparency?
-A program has the property of
referential transparency if any two
expressions in the program that have
the same value can be substituted for one
another anywhere in the program,
without affecting the action of the program.
20. How does C support relational and
Boolean expressions?
relational :
-for example, inequality, the C-based
languages use !=
boolean :
-Highest postfix ++, --
unary +, -, prefix ++, --, !
*, /, %
binary +, -
<, >, <=, >=
=, !=
&&
Lowest ||
21. What is the purpose of a compound
assignment operator?
-A compound assignment operator is a
shorthand method of specifying a
commonly needed form of assignment. The
form of assignment that can be
abbreviated with this technique has the
destination variable also appearing as
the first operand in the expression on
the right side.
28. What is a cast?
-In the C-based languages, explicit
type conversions are called casts.
Problem Set
1. When might you want the compiler to
ignore type differences in an
expression?
ANSWER :
-When it's overflow or underflow.
3. Do you think the elimination of
overloaded operators in your favorite
language would be beneficial? Why or
why not?
ANSWER:
-no.
overloaded operators can give more
advantages, if it's eliminated, it will only
have one function.
the benefit in the following will be no
more exist, which harden the coding
process.
For example, if + and * are overloaded
for a matrix abstract data type and A,
B,
C, and D are variables of that type,
then
A * B + C * D
can be used instead of
MatrixAdd(MatrixMult(A, B),
MatrixMult(C, D))
9. Assume the following rules of
associativity and precedence for
expressions:
Precedence Highest *, /, not
+, –, &, mod
– (unary)
=, /=, < , <=, >=, >
and
Lowest or, xor
Associativity Left to right
Show the order of evaluation of the
following expressions by parenthesizing
all subexpressions and placing a
superscript on the right parenthesis
to indicate order. For example, for the
expression
a + b * c + d
the order of evaluation would be
represented as
((a + (b * c)1)2 + d)3
a. a * b - 1 + c
b. a * (b - 1) / c mod d
c. (a - b) / c & (d * e / a - 3)
d. -a or c = d and e
e. a > b xor c or d <= 17
f. -a + b
ANSWER :
a. (((a * b)1 - 1)2
+ c)3
b. ( (a * (b - 1)1)3
/ (c mod d)2 )4
c. ( ((a - b)1 / c)2
& ((d * e)3 / (a – 3)4)5 )6
d. ((-a)1 or ((c = d)2
and e)3 )4
e. ((a > b)1 xor (c or (d
<= 17)2)3)4
f. ((-a)1 + b)2
21. Why does Java specify that operands
in expressions are all evaluated in
left-to-right order?
ANSWER:
-because left-to-right order is a
common used.
22. Explain how the coercion rules of a
language affect its error detection
ANSWER:
-As a simple illustration of the
problem, consider the following Java code:
int a;
float b, c, d;
. . .
d = b * a;
Assume that the second operand of the
multiplication operator was supposed
to be c, but because of a keying error
it was typed as a. Because mixed-mode
expressions are legal in Java, the
compiler would not detect this as an error.
It would simply insert code to coerce
the value of the int operand, a, to
float. If mixed-mode expressions were
not legal in Java, this keying error
would have been detected by the
compiler as a type error.