Chapter 6 Problems



Chapter 6 Review Questions

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

2. What are the advantages and disadvantages of decimal data types?
- Decimal types have the advantage of being able to precisely store decimal values, at least those within a restricted range, which cannot be done with floating-point. The disadvantages of decimal types are that the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful, for reasons discussed in the following paragraph.

3. What are the design issues for character string types?
- The two most important design issues that are specific to character string types are the following:
• Should strings be simply a special kind of character array or a primitive type?
• Should strings have static or dynamic length?

4. Describe the three string length options.
- First, the length can be static and set when the string is created. Such a string is called a static length string. This is the choice for the strings of Python, the immutable objects of Java’s String class, as well as similar classes in the C++ standard class library, Ruby’s built-in String class, and the .NET class library available to C# and F#. The second option is to allow strings to have varying length up to a declared and fixed maximum set by the variable’s definition, as exemplified by the strings in C and the C-style strings of C++.

5. Define ordinal, enumeration, and subrange types.
- A subrange type is a contiguous subsequence of an ordinal type. An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers. An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated, in the definition. The third option is to allow strings to have varying length with no maximum, as in JavaScript, Perl, and the standard C++ library. These are called dynamic length strings. This option requires the overhead of dynamic storage allocation and deallocation but provides maximum flexibility.

7. In what ways are the user-defined enumeration types of C# more reliable than those of C++?
- C# includes both the references of Java and the pointers of C++. However, the use of pointers is strongly discouraged. In fact, any subprogram that uses pointers must include the unsafe modifier. Note that although objects pointed to by references are implicitly deallocated, that is not true for objects pointed to by pointers. Pointers were included in C# primarily to allow C# programs to interoperate with C and C++ code.

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.
- In 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. If the array is a matrix, it is stored by rows. In column major order, the elements of an 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. If the array is a matrix, it is stored by columns.

18. What is an access function for an array?
- The access function for a multidimensional array is the mapping of its base address and a set of index values to the address in memory of the element specified by the index values.

20. What is the structure of
- An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys. In the case of non-associative arrays, the indices never need to be stored (because of their regularity). In an associative array, however, the user-defined keys must be stored in the structure. So each element of an associative array is in fact a pair of entities, a key and a value. We use Perl’s design of associative arrays to illustrate this data structure.an associative array?

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. The unions in these languages are called free unions, because programmers are allowed complete freedom from type checking in their use. Type checking of unions requires that each union construct include a type indicator. Such an indicator is called a tag, or discriminant, and a union with a discriminant is called a discriminated union.

32. What are the design issues for unions?
- So, the primary design issues that are particular to union types are the following:
• Should type checking be required? Note that any such type checking must
be dynamic.
• Should unions be embedded in records?

44. Define type error.
- A type error is the application of an operator to an operand of an inappropriate type. For example, in the original version of C, if an int value was passed to a function that expected a float value, a type error would occur (because compilers for that language did not check the types of parameters).

45. Define strongly typed.
- A programming language is strongly typed if type errors are always detected. This requires that the types of all operands can be determined, either at compile time or at run time. The importance of strong typing lies in its ability to detect all misuses of variables that result in type errors. A strongly typed language also allows the detection, at run time, of uses of the incorrect type values in variables that can store values of more than one type.

Problem Set

2. How are negative integers stored in memory?
- The CPU doesn't care whether a byte holds -1 or 15 when it moves it from one place to another. There's no such thing as a "signed move" (to a location of the same size - there is a signed move for larger or smaller destinations). The CPU only cares about the representation when it does arithmetic on the byte. The CPU knows whether to do signed or unsigned arithmetic according to the op-code that you (or the compiler on your behalf) chose.

6. Compare the use of Boolean types in C++ and Java. Give emphasis on their use in conditional statements and conditional loops.
- The Boolean or logical data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid-19th century. The Boolean data type is the primary result of conditional statements, which allow different actions and change control flow depending on whether a programmer-specified boolean condition evaluates to true or false. C++ has a separate Boolean data type ('bool'), but with automatic conversions from scalar and pointer values that are very similar to those of C. This approach was adopted also by many later languages, especially by some scripting ones such as AWK. One problem with this approach is that the tests if(t==TRUE){...} and if(t) are not equivalent. Python has a related situation, where the Boolean type, bool is a subtype of the integer type, int, and Booleans False and True act as 0 and 1, respectively, in arithmetic contexts.

7. Compare the pointer and reference type variable in C++/
- A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. A pointer can point to NULL while reference can never point to NULL. You can't take the address of a reference like you can with pointers. There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

16. What are the arguments for the inclusion of strong type checking in Java, although they were not included in C or C++?
- C and C++ are not strongly typed languages because both include union types, which are not type checked. ML is strongly typed, even though the types of some function parameters may not be known at compile time. F# is strongly typed. Java and C#, although they are based on C++, are strongly typed in the same sense as Ada. Types can be explicitly cast, which could result in a type error. However, there are no implicit ways type errors can go undetected.

21. In what way is dynamic type checking better than static type checking.
- Static type checking means that type checking occurs at compile time. No type information is used at runtime in that case. Dynamic type checking occurs when type information is used at runtime. C++ uses a mechanism called RTTI (runtime type information) to implement this. The most common example where RTTI is used is the dynamic_cast operator which allows downcasting of polymorphic types. Dynamic typing can deal with reflection, whereas static typing, although it can be made to work with specific reflective features, in general cannot. This is because the ability of a program to change itself makes it thoroughly unpredictable, hence hard to analyze by a static checker.