Pengajar : Tri Djoko Wahjono
Review Question :
11. What is an overloaded operator?
=> An overloaded operator if use of an operator for more than one
purpose.
12. Define narrowing and widening conversions.
=>Narrowing conversion is one that converts an object to a type that
cannot include all of the values of the original type, example: float
to int. Widening conversions is one in which an object is converted
to a type that can include at least approximations to all of the
values of the original type, example: int to float.
13. In JavaScript, what is the difference between == and ===?
=> “==” means equal,
But “===” means same type and equal.
14. What is a mixed-mode expression?
=>Mixed-mode expression is one that has operands of different types.
15. What is referential transparency?
=>Referential transparency is a property whereby an expression
can be replaced by its value without affecting the program.
Problem Set :
11. Write a BNF description of the precedence and associativity rules
defined for the expressions in Problem 9. Assume the only operands are
the names a,b,c,d, and e.
=> Assume the only operands are the names a, b, c, d, and e.
<expr> → <expr> or <e1> | <expr> xor <e1> | <e1>
<e1> → <e1> and <e2> | <e2>
<e2> → <e2> = <e3> | <e2> /= <e3> | <e2> < <e3>
| <e2> <= <e3> | <e2> > <e3> | <e2> >= <e3> | <e3>
<e3> → <e4> <e4> → <e4> + <e5> | <e4> – <e5> | <e4> & <e5> | <e4> mod <e5> | <e5>
<e5> → <e5> * <e6> | <e5> / <e6> | not <e5> | <e6>
<e6> → a | b | c | d | e | const | ( <expr> )
12. Using the grammar of Problem 11, draw parse trees for the expressions of Problem 9.
=>
13. Let the function fun be defined as
int fun(int *k) {
*k += 4;
return 3 * (*k) - 1;
}
Suppose fun is used in a program as follows:
void main() {
int i = 10, j = 10, sum1, sum2;
sum1 = (i / 2) + fun(&i);
sum2 = fun(&j) + (j / 2);
}
What are the values of sum1 and sum2
a. if the operands in the expressions are evaluated left to right?
b. if the operands in the expressions are evaluated right to left?
=> (a) (left -> right) sum1 is 46; sum2 is 48
(b) (right -> left) sum1 is 48; sum2 is 46
14. What is your primary argument against (or for) the operator precedence rules of APL?
=> I think the operator precedence rules of the common imperative
languages are nearly all the same, because they are based on those
of mathematics.
15. Explain why it is difficult to eliminate functional side effects in C.
=> There’s one reason functional side effects would be difficult to
remove from C is that all of C’s subprograms are functions,
providing the ability of returning only a single data value (though
it could be an array). The problem is that in many cases it is
necessary (or at least convenient) to return more than one data
value, which is done through the use of pointer actual parameters,
which are a means of creating functional side effects