Rabu, 26 November 2014

CHAPTER 7

Nama : Aditya Isnugraha
Class   : LM01
NIM    : 1801419606

Assignment from Tri Djoko Wahjono
CHAPTER 7

- REVIEW QUESTION

11. What is an overloaded operator? 
Answer :
 Operator that has different implementation depending on its arguments.


12. Define narrowing and widening conversions. 
Answer :
 Widening conversion is a conversion that converts a value to a type that can include least approximations of all values of the original type. Narrowing conversion is a conversion of data that might cause a loss of precision



13. In JavaScript, what is the difference between == and ===? 
Answer :
“==” operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It’s much similar Java’s equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other.


14. What is a mixed-mode expression? 
Answer :
 Mixed-mode expression: The expressions that allow them to design decisions concerning arithmetic expressions is whether an operator can have operands of different types.


15. What is referential transparency?
 Answer :
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







 - 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. 
Answer :
 <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. 
Answer :



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?
Answer :
(a) (left -> right)

sum 1 = 46
sum 2 = 48

(b) (right -> left)

sum 1 = 48
sum 2 = 46


14. What is your primary argument against (or for) the operator precedence rules of APL? 
Answer :
 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.
Answer :
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.












Tidak ada komentar:

Posting Komentar