REVIEW QUESTION
2. What is a ternary operator?
Ternary operator is an operator that has three operands.
4. What operator usually has right associativity?
In Ruby and Ada the operator is **
In C-based languages ++, –, unary -, unary +
8. Define functional side effect
A functional side effect is a side effect of a function which occurs when the function changes either one of its parameters or a global variable.
11. What is an overloaded operator?
Overloaded operator is Arithmetic operators that are often used for more than one purpose. For example, + usually is used to specify integer addition and floating-point addition. Some languages—Java, for example—also use it for string catenation.
12. Define narrowing and widening conversions
Narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type. For example, converting a double to a float in Java is a narrowing conversion, because the range of double is much larger than that of float. Widening conversion converts a value to a type that can include at least approximations of all of the values of the original type. For example, converting an int to a float in Java is a widening conversion.
15. What is a referential transparency?
A referential transparency if a program has the property of 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.
25. What mixed-mode assignments are allowed in Ada?
Ada does not allow mixed-mode assignment.
PROBLEM SET
7. Describe a situation in which the add operator in a programming language would not be commutative.
When we use add operator to combined strings. Such as “abc” + “xyz” = “abcxyz” and “xyz” + “abc” = “xyzabc”. These example are obviously not commutative.
8. Describe a situation in which the add operator in a programming language would not be associative.
If the three numbers being added are -32768, 32767, and 1 (assuming 16 bit signed integers):
(-32768 + 32767) + 1 = (-1) + 1 = 0.
-32768 + (32767 + 1) = -32767 + <error overflow> = <error overflow>
So if associativity causes an overflow exception, the add operator is not associative.
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
( ( ( 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
15. Explain why it is difficult to eliminate functional side effects in C.
Because in C there is only function, which mean that all subprograms only return one value. One way to eliminate the side effect of two way parameter and still provide subprogram return more than one value, the values would need to be placed in a struct and the struct returned. Access to global in functions would also have to be disallowed.
20. 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. Ans: 7
b. operands are evaluated right to left. Ans: 12
22. Explain how the coercion rules of a language affect its error detection.
Coercion defined as an implicit type conversion that is initiated by the compiler. Language designers are not in agreement on the issue of coercions in arithmetic expressions. Those against a broad range of coercions are concerned with the reliability problems that can result from such coercions, because they reduce the benefits of type checking. Those who would rather include a wide range of coercions are more concerned with the loss in flexibility that results from restrictions.