Tuesday, April 9, 2013

Concepts of Programming Languages ---- Chapter 7 Expressions and Assignment Statements

Concepts of Programming Languages ---- Chapter 7 Expressions and Assignment Statements

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 7 Expressions and Assignment Statements


Review Questions
1.      What is a coercion?
Coercion is defined as an implicit type conversion that initiated by the compiler.

2.      What is an overloaded operator?
Overloaded operator is arithmetic operators which used for more than one purpose.

3.      Define narrowing and widening conversions.
A narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type.
Widening conversion converts a value to a type that can include at least approximations of all of the values of the original type.

4.      In JavaScript, what is the difference between == and ===?
== is used for equivalence and === is used to prevent their operands from being coerced and similar to their relatives == and !=.

5.      What is a mixed-mode expression?
One of the design decisions concerning arithmetic expressions is whether an operator can have operands of different types. Languages that allow such expressional is called mixed-mode expression

6.      What is referential transparency?
The concept of referential transparency is related to and affected by functional side effects.

7.      What are the advantages of referential transparency?
The most important of these is that the semantics programs is much easier to understand than the semantics of programs that are not referentially transparent

8.      What is short-circuit evaluation?
A short-circuit evaluation of an expression in one in which the result is determined without evaluating all of the operands and /or operantors.

9.      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.


10.  What two languages include multiple assignments?
Perl and Ruby.

Problem Set
1.      Assume the following rules of associativity and precedence for expressions:
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  à (((a * b)1  - 1)2  + c)3
B.   a * (b - 1) / c mod d à (((a * (b - 1)1 )2  / c)3  mod d)4
            C.  (a - b) / c & (d * e / a - 3) à ( ( ( a - b )1 / c )2 & ( ( ( d * e )3 / a )4 - 3 )5 )6
D.  –a or c =d and e à ( ( ( - a )1 or ( c = d )2 )3 and e )4
E.  a > b xor c or d <= 17 à ( ( a > b )1 xor ( c or ( d <= 17 )2 )3 )4
F.  –a + b à ( - ( a + b )1 )2
2.      Show the order of evaluation of the expressions of problem 9, assuming that there are no precedence rules and all operators associate right to left.
A.    a * b - 1 + c  à ( a * ( b - ( 1 + c )1 )2 )3
B.    a * (b - 1) / c mod d à ( a * ( ( b - 1 )2 / ( c mod d )1 )3 )4
C.    (a - b) / c & (d * e / a - 3) à ( ( a - b )5 / ( c & ( d * ( e / ( a - 3 )1 )2 )3 )4 )6
D.    –a or c =d and e à ( - ( a or ( c = ( d and e )1 )2 )3 )4
E.    a > b xor c or d <= 17 à ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
F.    –a + b à ( - ( a + b )! )2

3.      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)+ (i/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. sum1 is 46, sum2 is 48.
b. sum1 is 48, sum2 is 46.

4.      Explain why it is difficult to eliminate functional side effects in C.
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.

5.      Consider the following C program:
int fun(int *i)
{
            *I +=5;
            return 4;
}
void main()
{
            int x = 3;
            x = x + fun(&x);
}
What is the value of x after the assignment statement in main, assuming
a. operands are evaluated left to right
b. operands are evaluated right to left

a. 7
b. 12

No comments:

Post a Comment