Concepts of Programming Languages 10th Edition – Robert W. Sebesta
Lecturer : Tri Djoko Wahjono
Review Questions (Randomly 10 of 28)
- Define operator precedence and operator associativity. (Number 1)
– 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.
- What is a ternary operator? (Number 2)
– Operator with three operands.
- What is a prefix operator ? (Number 3)
– An operator which precede their operands.
- What operator usually has right associativity ? (Number 4)
– The “*” (pointer) operator.
- What is a nonassociative operator ? (Number 5)
– An operator that have no defined behavior when used in sequence in an expression.
- What associativity rules are used by APL? (Number 6)
– All operators have equal precedence and all operators associate right to left.
- Define functional side effect (Number 8)
– A condition where a function changes either one of its parameters or a global variable.
- What is an overloaded operator? (Number 11)
– The multiple use of an operator is called operator overloading.
- What is a mixed-mode expression? (Number 14)
– There are more than one type of operand in the expression.
- What is short-circuit evaluation? (Number 18)
– The result of expression is determined without all of the operands and/or operators.
Problem Sets (Randomly 7 of 22)
- When might you want the compiler to ignore type differences in an expression ? (Number 1)
– When you want to evaluate a string as a number and vice-versa.
- State your own arguments for and against allowing mixed-mode arithmetic expressions. (Number 2)
– FOR : A mixed mode arithmetic expression is needed in calculating expressions that might have decimal results. It is compulsory as it allows two different type of number data type such as float and integer to be summed without losing the precision of the float. Against : While it is compulsory to have mixed-mode expressions, it is more error prone when expressions made are more likely to have non-decimal results. A mixed mode might produce a decimal result even though the result wanted is a non-decimal.
- Do you think the elimination of overloaded operators in your favorite language would be beneficial ? why or why not ? (Number 3)
– No, it would not be beneficial. Overloading operator would be a helpful feature in developing a complex program with complex arithmetic operation as well. It allows developers to create a class whose function can replace countless lines of codes with an operator. This clearly will help a readability and writability of a program. Eliminating overloaded operators would null this advantage.
- Would it be a good idea to eliminate all operator precedence rules and require parentheses to show the desired precedence in expressions? Why or why not? (Number 4)
– No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.
- Should C’s assigning operations (for example, +=) be included in other languages (that do not already have them)? Why or why not? (Number 5)
– Yes, because it’s simpler than the normal <operand> = <operand> + <some values> form, and it can save much time.
- Should C’s single-operand assignment forms (for example, ++count) be included in other languages (that do not already have them)? Why or why not? (Number 6)
– Yes, because it’s simpler than the normal addition <operand> = <operand> + 1; and it can save much time.
- Describe a situation in which the ad operator in a programming language would not be communicative. (Number 7)
– An expression such as : x + fun(y), and fun(y) changes x.