REVIEW QUESTIONS
1. What is a descriptor?
A descriptor is the collection of the attributes of a variable. In an implementation, a descriptor is an area of memory that stores the attributes of a variable.
2. What are the advantages and disadvantages of decimal data type?
Advantage: accuracy of decimal values.
Disadvantages: limited range since no exponents are allowed, and its representation wastes memory.
4. Describe the three string length options.
- Static length string, is the string length option which its length can be static and set when the string is created.
- Limited dynamic length strings, is the string length option which allow strings to have varying length up to a declared and fixed maximum set by variable’s definition.
- Dynamic length strings, is the string length option which allow strings to have varying length with no maximum, as in JavaScript, Perl, and the standard C++ library.This option requires the overhead of dynamic storage allocation and deallocation but provides maximum flexibility.
5. Define ordinal enumeration and 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 Java, for example, the primitive ordinal types are integer, char, and boolean.
- An enumeration type is one in which all of the possible values, which are named constants, are provided in the definition. Enumeration types provide a way of defining and grouping collections of name constants, which are called enumeration constants. An example in C#:
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; - A subrange type is a contiguous subsequence of an ordinal type. For example, p. 264
12 .. 14 is a subrange of integer 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 allocation 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?
10. What happens when a nonexistent element of an array is referenced in Perl?
A reference to a nonexistent element in Perl yields undef, but no error is reported.
12. What languages support negative subscripts?
C++, Ruby, and Lua support negative subscripts.
21. What is the purpose of level numbers in COBOL records?
The level numbers in COBOL records are used to establish a hierarchical structure of related records.
32. What are the design issues for unions?
- Should type checking be required? Note that any such type checking must be dynamic.
- Should unions be embedded in records?
36. What are the two common problems with pointers?
One common problem with pointers is the dangling pointer, or dangling reference which is a pointer that contains the address of a heap-dynamic variable that has been deallocated. The common problem is a lost heap-dynamic variable which is an allocated heap-dynamic variable that is not longer accessible to the user program, i.e. it hasn’t a pointer. Such variables are called garbage because they are unusable.
38. What is a C++ reference type, and what is its common use?
C++ includes a special kind of pointer type, called a reference type. It is used primarily for the formal parameters in function definitions. A C++ reference type variable is a constant pointer that is always implicitly dereferenced.
39. Why are reference variables in C++ better than pointers for formal parameters?
Because a C++ reference type variable is a constant, it must be initialized with the address of some variable in its definition, and after initialization a reference type variable can never be set to reference any other variable.
48. What languages have no type coercions?
Languages that have no type coercions are ML and F#.
50. What is name type equivalence?
Name type equivalence 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.
PROBLEM SET
2. How are negative integers stored in memory?
A negative integer could be stored in sign-magnitude notation, in which the sign bit is set to indicate negative and the remainder of the bit string represents the absolute value of the number. Sign-magnitude notation, however, does not lend itself to computer arithmetic. Most computers now use a notation called twos complement to store negative integers, which is convenient for addition and subtraction.
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).
8. What are the differences between the reference type variable of C++ and those of Java?
A C++ reference type variable is a constant pointer that is always implicitly dereferenced. Because a C++ reference type variable is a constant, it must be initialized with the address of some variable in its definition, and after initialization a reference type variable can never be set to reference any other variable. Unlike C++ reference variables, Java reference variables can be assigned to refer to different class instances; they are not constants.
19. Any type with typedef is type equivalent to its parent type. How does the use of typedef differ in C and C++?
Typedef in C and C++ does not introduce a new type; it simply defines a new name for an existing type. So, any type defined with typedef is type equivalent to its parent type. One exception to C using name type equivalence for structures, enumerations, and unions is if two structures, enumerations, or unions are defined in different files, in which case structural type equivalence is used. This is a loophole in the name type equivalence rule to allow equivalence of structures, enumerations, and unions that are defined in different files. C++ is like C except there is no exception for structures and unions defined in different files.
21. In what way is dynamic type checking better than static type checking?
Dynamic type checking is better than static type checking when user want to make sure (at runtime) that the pointers are of the correct type. With dynamic_cast, if the pointer is not of the right type, at runtime, it will return 0 instead.