Data Types, Expression and Assignment Statement

0

Written on Monday, May 05, 2008 by Ennah, the comsci student

Chapter 5

Part 1. Define the following:

1. Descriptor

2. Ordinal, enumeration, subrange types

3. Static array, fixed static-dynamic array, stack-dynamic array, dynamic array

4. Aggregate constant

5. Row major order, column major order

6. Fully qualified, elliptical references to fields in records

7. Union, free union, discriminated union

Part 2. Answer briefly.

1. Why does a decimal value waste memory space?

2. What are the design issues for character string types?

3. What are the design issues for pointer types?

Chapter 6

Part 1. Define the following:

1. Operator precedence, operator associativity

2. Functional side effect

3. Coercion

4. Conditional expression

5. Overloaded operator

6. Narrowing & widening conversions

Part 2. Answer briefly.

1. What is the purpose of a compound assignment operator?

2. Describe a situation in which the add operator in a programming language would not be associative.

Answers

Chapter 5
Part 1.
Define the following:
1.Descriptor

A descriptor is the collection of the attributes of a variable. In an implementation a descriptor is a collection of memory cells that store variable attributes. If attributes are all static, descriptors are required only at compile time. They are built by the compiler usually as a part of the symbol table, and are used during compilation. For dynamic attributes, however, part or all of the descriptor must be maintained during execution. In this case the descriptor is used by the run-time system.

2. Ordinal, enumeration, subrange types

An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers. In many languages, users can define two kinds of ordinal types: enumeration and subrange.

An enumeration type is one in which all of the possible values, which become symbolic constants, are enumerated in the definition. Enumeration types have advantages to readability and reliability.
Example: In Ada, type DAYS is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

A subrange type is a contiguous subsequence of an ordinal type. Subrange types were introduced by Pascal and are also included in Ada. There are no design issues that are specific to subrange types.
Example: 12 ... 14 is a sub range of integer type.

3. Static array, fixed static-dynamic array, stack-dynamic array, dynamic array

A static array is one in which the subscript ranges are statically bound and storage allocation is static (done before run time). The advantage of static array is efficiency: No dynamic allocation or deallocation is required.

A fixed stack-dynamic array is one in which the subscript ranges are statically bound, but the allocation is done at declaration elaboration time during execution. The advantage of fixed stack-dynamic arrays over static arrays is space efficiency. A large array in one procedure can use the same space as a large array in different procedure, as long as both procedures are not active at the same time.

A stack-dynamic array is one in which the subscript ranges are dynamically bound and the storage allocation is dynamic (done during run time). Once the subscript ranges are bound and the storage is allocated, however, they remain fixed during the lifetime of the variable. The advantage of stack-dynamic array over static and fixed-stack dynamic array is flexibility. The size of an array may need not to be known until the array is bound to be used.

A dynamic array is one in which the binding of subscript ranges and allocation is dynamic and can change any number of times during the array’s lifetime. The advantage here is flexibility: Arrays can grow and shrink during program execution as the need for space changes.

4. Aggregate constant

Aggregate constant are initializing values which are assigned to the array element locations in the order in which they appear or by directly assigning them to an index operator using direct assignment (=> operator). They are usually delimited by parentheses.

5. Row major order, column major order

Row major order is one way of mapping multidimensional array to one dimensional array in which 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 is one way of mapping multidimensional array to one dimensional array in which 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.

6. Fully qualified, elliptical references to fields in records

A fully qualified reference to a record field is one in which all intermediate record names, from the largest enclosing record to the specific field, are named in the reference.

An elliptical reference to a record field is one in which the field is named, but any or all of the enclosing record names can be omitted, as long as the resulting reference is unambiguous in the referencing environment.

7. Union, free union, discriminated union

A union is a type whose variables are allowed to store different type values at different times during execution.

A free union is a union for which there is no language support for type checking. Programmers are allowed complete freedom from type checking in their use. FORTRAN, C, and C++ have this kind of union.

A discriminated union has a field that indicates the type of the data in the union. This field is called a discriminant. Pascal and Ada have discriminated unions.

Part 2. Answer briefly.

1. Why does a decimal value waste memory space?

Decimal values are designed to be accurate to a given number of decimal places. Because of this, decimal takes more memory than binary representation because decimal values must be represented by using at least 4-bits per digit in the decimal number, so for instance:
12.69 is represented as: 0001 0010 0110 1001
1 => 0001
2 => 0010
6 => 0110
9 => 1001
Therefore, to store a six-digit decimal number requires 24 bits of memory. However, it takes only 20 bits to store the same number in binary.

2. What are the design issues for character string types?

Design issues for character string types:
a. Is character string a primitive type or just a special kind of array?
b. Is the length of character string objects static or dynamic?

3. What are the design issues for pointer types?

Design issues for pointer types:
a. What is the scope and lifetime of pointer variables?
b. What is the lifetime of heap-dynamic variables?
c. Are pointers restricted to pointing at a particular type?
d. Are pointers used for dynamic storage management, indirect addressing, or both?
e. Should a language support pointer and reference types?

Chapter 6
Part 1. Define the following:
1. Operator precedence, operator associativity

Operator precedence are rules for expression evaluation that define the order in which the operators of different precedence levels are evaluated.

Operator associativity is the rules of the language which determine, when there are two adjacent occurrences of operators of the same level of precedence, which operator is evaluated first.

2. Functional side effect

Functional side effect is the side effect of a function that occurs when the function changes either one of its parameters or a global variable.
Example: A + FUN(A) (This works only if FUN changes A, then there is an effect)
3. Coercion

Coercion is the implicit type conversion that occurs when two operands of an operator are not of the same type (and that’s legal in the language) and the compiler must choose one of them to be coerced and supply the proper code for the coercion.

4. Conditional expression

Conditional expression is an assignment statement of the general form

expression_1 ? expression_2: expression_3 (if-then-else)

where expression_1 is interpreted as Boolean expression. If expression_1 is evaluated as true, the value of the whole expression is the value of expression_2; otherwise, it is the value of expression_3

5. Overloaded operator

Overloaded operator is an operator that has multiple purposes assigned to it, i.e. , in Java, + is used both for numeric additon and for string concatenation

6. Narrowing & widening conversions

A narrowing conversion converts a value to a type that cannot store even approximations of all the values of the original type, for example, converting a double to a float in Java (the range of double is much larger than that of float).

A widening conversion converts a value to a type that can include at least approximations of all the values of the original type, for example, converting an int to a float in Java. Widening conversion are nearly always safe.

Part 2. Answer briefly.
1. 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. The form of assignment that can be abbreviated with this technique has the destination variable also appearing as the first operand on the first side, as in: a = a + b.

2. Describe a situation in which the add operator in a programming language would not be associative.

Consider the equation A + B + C where A and B are large positive numbers C is a negative number with a very large absolute value. If we were to associate the equation as follows: A + (B + C) then the equation will evaluate correctly, as B + C will not cause overflow. However, the situation is not associative as we could associate the equation as follows: (A + B) + C and have an overflow situation which would not yield the same result as the first associations evaluation.

If you enjoyed this post Subscribe to our feed

No Comment

Post a Comment