Tuesday, April 16, 2013

Concepts of Programming Languages Chapter 7


Concepts of Programming Languages Chapter 7

Name            : Erland
NIM              : 1601218035
Lecturer       : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment  : Concept of programming languages Chapter 7

Review Questions


       1.  Define operator precedence and operator associativity.
             Answer :
-Operator precedence is a rule used to clarify which procedures should    be performed first in a given mathematical expression.-Operator associativity is a property that determines how operators of    the same precedence are grouped in the absence of parentheses

       2. What is ternary operator?
            Answer : An operator that have 3 operands.

       3. What is prefix operator?
             Answer : An operator which signifies function of one argument which 
                             argument immediately follows an occurrence of the operator

       4. what operator usually has right associativity?
             Answer : Increments(++) and decrements(–) operators in C languages

       5. What is nonassociative operator?
             Answer : Non-associative operators are operators that have no defined
                             behavior when used in sequence in an expression

        8. Define functional side effect.
             Answer:Functional side effect occurs when the function changes either one 
                           of its parameters or a global variable.

        9.  What is a coercion?
               Answer : Coercion is defined as an implicit type conversion that initiated 
                               by the compiler.

        10. What is overloaded operator?
             Answer :overloaded operator is a specific case of polymorphism, where 
                            different operators have different implementations depending on 
                            their arguments.


         12. Define narrowing and widening conversions.
               Answer : Narrowing conversion convert a value to a type that cannot 
                               store even aporiximations of all the value of the original type.
                               Widening conversion converts a value to a type that can include
                               at least approximations of all of the value of the original type.


Problem Sets

1. When might you want the compiler to ignore type differences in an expression?
     Answer:Suppose Type1 is a subrange of Integer. It may be useful for the difference 
                   between Type1 and Integer to be ignored by the compiler in an expression.
                            
5.  Should C’s assigning operations (for example, +=) be included in other languages 
     (that do not already have them) ? Why or why not ?
     Answer:No. C’s assigning operations should not be included in other languages. 
                   Because this assigning operations would be the different part of the code
                   if it is implemented on other code that might cause confusion of the 
                  programmer or even it might disrupt the semantics of a language’s syntax.

7. Describe a situation in which the add operator in a programming language 
    would not be commutative.
    Answer :
                   If the add operator for a language is also used to concatenate strings, 
                  it’s quite apparent that it would not be commutative.
     For example:
     “efg” + “hij” = “efghij”     “lkm” + “nop” = “lkmnop”
                 These two strings are obviously not equal, so the addition operator is not 
                commutative.

18.  Should an optimizing compiler for C or C++ be allowed to change the order of  
       subexpressions in a Boolean expression? Why or why not?
       Answer : No. Because of short-circuit evaluation, the order of subexpressions 
                       around an && is important.

21. Why does Java specify that operands in expressions are all evaluated in 
       left-to-right order?
       Answer: Most groups use left-to-right associativity, which means that in an 
                      expression with operators in the same precedence group, the operators 
                      are  applied in left-to-right order.