Monday, April 8, 2013

KBP - Chapter 7

Review Question

1. Define operator precedence and operator associativity.
    - Operator precedence : the value of an expression depends at least in part on the order of evaluation of the operators in the expression.
    - Operator associativity : associates from left to right in common, but for exponentiation operator sometimes associates from right to left.

2. What is the ternary operator?
   It means the operator has three operands.

3. What is the prefix operator?
   It means the operator precede their operands.

8. Define functional side effect.
   It occurs when the function changes either one of its parameter or a global variable.

9. What is coercion?
   It is defined as an implicit type conversion that is initiated by the compiler.

18. What is short-circuit evaluation?
    It's one in which the result is determined without evaluating all of the operands "and" / "or" operators.

24. What two languages include multiple assignments?
    Perl, Ruby.

___________________________________________________
Problem Set

7. Describe a situation in which the add operator in a programming language would not be commutative.

   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:

        "abc" + "def" = "abcdef" 
        "def" + "abc" = "defabc"

    These two strings are obviously not equal, so the addition operator is not commutative.

9. Show the order of evaluation of the following expressions by parenthe-sizing all subexpressions and placing a superscript on the right parenthe-sis to indicate order.
     a). a*b-1+c
         (a*b) --> (a*b)-1 --> (((a*b)-1)+3)
     b). a*(b-1)/c mod d
         (b-1) --> a*(b-1) --> (a*(b-1)/c) --> ((a*(b-1)/c)mod d)
     c). (a-b)/c&(d*e/a-3)
         (a-b) --> ((a-b)/c) --> ...(d*e) --> ...((d*e)/a) --> ...(((d*e)/a)-3) --> (((a-b)/c)&(((d*e)/a)-3))
     d). -a or c=d and e
         (-a) --> ...(c=d) --> ...((c=d)and e) --> ((-a) or ((c=d)and e))
     e). a>b xor c or d<=17
         (a>b) --> (a>b)...(d<=17) --> ((a>b)xor c)...(d<=17) --> (((a>b)xor c) or (d<=17))
     f). -a+b
         (-a) --> ((-a)+b)

15. Explain why it is difficult to elimintae functional side effects in C?
    Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function.
    Being first-class also means that it is possible to define and manipulate functions from within other functions.
    Special attention needs to be given to functions that reference local variables from their scope. If such a function escapes their block after being returned from it, the local variables must be retained in memory, as they might be needed later when the function is called.
    Often it is difficult to determine statically when those resources can be released, so it is necessary to use automatic memory management.

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?
    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 lest-to-right order?
    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.

KBP - Chapter 6

Review Question

1. What is the descriptor?
   Descriptor is the collection of the attributes of a variable.

4. Describe the three string length options.
    - A static length string is the length that can be static and set when the string is created.
    - A limited dynamic strings is the option that allow strings to have varying length up to a declared and fixed maximum set by the variable's definition.
    - A dynamic length strings is the option that allow strings to have varying length with no maximum.

5. Define ordinal, enumeration, and subrange types.
    - An ordinaltype is one in which the range of possible values can be easily associated with the set of positive integers.
    - An enumeration is one in which all of the possible values, which are named constant, are provided, or enumbered, in the definition.
    - A subrange type is a contiguous subsequence of an ordinal type.

8. What are the design issues for arrays?
    - What types are legal for subscripts?
    - Are subscripting expressions in element references range checked?
    - When are subscript ranges bound?
    - when does array allocarion take place?
    - Are ragged or rectangular multidimensioned arrays allowed, or both?
    - Can arrays be initialized when they have their storage allocated?
    - What kinds of slices are allowed, if any?

15. What is an aggregate constant?
    An aggregate constant is a nonscalar constant which value never change or are not changed during execution of the program.

17. Define row major order and column major order.
     - Row major order : the elements of the array that have as their first subscript the lower bound value of that subscript are stored first, followed by the elements of the second value of the first subscript, and so forth.
     - Column major order : the elements of the array that have as their last subscript the lower bound value of that subscript are stored first, followed by the elements of the second value of the last subscript, and so forth.

31. Define union, free union, and discriminated union.
     - A union is a type whose variables may store different type values at different times during program execution.
     - A free union is the union construct is used to specify union structures.
     - A discriminated union is a union with a discriminant.

44. Define type error.
    A type error is the application of an operator to an operand of an inappropriate type.

45. Define strongly type.
    A programming language is called strongly type if type errors are always detected.

47. What is a nonconverting cast?
    A nonconverting cast is a kind of conversion that there's no actual conversion takes place, it's merely a means of extracting the value of a variable of one type and using it as if it were of a different type.

49. Why are C and C++ not strongly typed?
    Because both include union types, which are not type checked.

50. What is name type equivalence?
    It means that two variables have equivalent types if they are defined either in the same declaration or in declarations that use the same type name.

51. What is structure type equivalence?
    It means that two variables have equivalent types if their types have identical structures.

___________________________________________________
Problem Set

2. How are negative integers stored in memory?
   To store negative integers, we can use a notation called twos-complement, which is convenient for addition and subtraction.
   By using this notation, the representation of a negative integer is formed by taking the logical complement of the positive version of the number and adding one.
   Ones-complement notation is still used by some computer. With this notation, the negative of an integer is stored as the logical complemenet of its absolute value.

7. Compare the pointer and reference type variable in C++.
    - Pointer : to implement algorithms and data structures.
    - Reference : to define attractive interfaces in function parameters and return types.

8. What are the differences between the reference type variable of C++ and those of Java?
    - C++ : Pointers, references, and pass-by-value are supported.
    - Java : Primitive and reference data type parameters are always passed by value.

19. Any type defined with typedef is type equivalent to its parent type. How does the use of typedef differ in C and C++?
    Because in C, there are two different namespaces of types : a namespace of struct/union/enum tag names and a namespace of typedef names.
    While in C++, there's only a  subtle difference. It's a holdover from C, in which it made a difference.

21. In what way is dynamic type checking is better than static type checking?
     - it's simpler languages
     - lack of compile time, quicker turnaround
     - can pass variables/objects between modules without declare their type

KBP - Chapter 5

Review Question

1. What are the design issues for names?
    - Are names case sensitive?
    - Are the special words of the language reserved words or keywords?

4. What is an alias?
   Alias is more than one variable name can be used to access the same memory location.

7. Define binding and bingding time.
   Binding is an association between an atribute and an entity, such as between a variable and its type or value, or between an operation and a symbol.
   Binding time is the time which a binding takes place is called.

9. Define static binding and dynamic binding.
   Static binding is a binding that first occurs before run time begins and remains unchanged throughout program execution.
   Dynamic binding is a binding that first occurs during run time or can change in the course of program execution.

13. Define lifetime, scope, static scope, and dynamic scope.
    Lifetime is the time during which the variable is bound to a specific memory location.
    Scope is the range of statements in which the variable is visible.
    Static scope is the method of binding names to nonlocal variables in ALGOL 60.
    Dynamic scope is the calling sequence of subprograms, not on their spatial relationship to each other.

18. What is a block?
    Block is a section of code.

___________________________________________________
Problem Set

1. Decide which of the following identifier names is valid in C language. Support your decision.
   
   _Student : valid.
   int : invalid. Because int is a data type, not an identifier.
   Student : valid.
   123Student : invalid. Because numbers can't be located at the front, can only located at the back after the identifier name.
   Student123 : valid.

4. Why is the type declaration of a variable necessary? What is the value range of the int type variable in Java?
    - Because it associates a type and an identifier with the variable, and allows the compiler to decide how musch storage space to allocate for storage of the value associated with the identifier.
    - Values range for int in Java : from -2,147,483,648 to 2,147,483,647.
    

5. Describe a situation each where static and dynamic type binding is required.
    - Static type binding : the binding to an object is optional, if a name is not bound to an object, the name is said to be null.
    - Dynamic type binding : every variable name is bound only to an object.