April 7, 2013

KBP chapter 6


Rickvian Aldi / 02PCT / 1601253441
Assignment By Mr. Tri Djoko Wahjono


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 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. For example, the number 0.1 (in decimal) can be exactly
represented in a decimal type, but not in a floating-point type,

-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.
Decimal types are stored very much like character strings, using binary
codes for the decimal digits. These representations are called binary coded
decimal (BCD). In some cases, they are stored one digit per byte, but in

others, they are packed two digits per byte. Either way, they take more storage

than binary representations. It takes at least four bits to code a decimal

digit. Therefore, to store a six-digit coded decimal number requires 24 bits of

memory. However, it takes only 20 bits to store the same number in binary.



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.

-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++.

-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.



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.
There are two user-defined
ordinal types that have been supported by programming languages: enumeration
and subrange.

-An enumeration type is one in which all of the possible values, which are
named constants, are provided, or enumerated, in the definition. Enumeration
types provide a way of defining and grouping collections of named constants,
which are called enumeration constants.

-A subrange type is a contiguous subsequence of an ordinal type. For example,
12..14 is a subrange of integer type.



6. What are the advantages of user-defined enumeration types?

-Readability is enhanced very directly: Named values are easily recognized,
whereas coded values are not.
In the area of reliability, the enumeration types of Ada, C#, F#, and Java
5.0 provide two advantages: (1) No arithmetic operations are legal on

enumeration types; this prevents adding days of the week, for example, and
(2) second, no enumeration variable can be assigned a value outside its defined

range.4 If the colors enumeration type has 10 enumeration constants and uses

0..9 as its internal values, no number greater than 9 can be assigned to a

colors type variable.


12. What languages support negative subscripts?

-Ruby and Lua.


15. What is an aggregate constant?

-consider the following:
List : array (1..5) of Integer := (1, 3, 5, 7, 9);
Bunch : array (1..5) of Integer := (1 => 17, 3 => 34,
others => 0);
In the first statement, all the elements of the array List have initializing

values, which are assigned to the array element locations in the order in which

they appear. In the second, the first and third array elements are initialized

using direct assignment, and the others clause is used to initialize the

remaining elements. As with Fortran, these parenthesized lists of values are

called aggregate values.

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.
For example, if the matrix had the values

3 4 7
6 2 5
1 3 8

it would be stored in row major order as
3, 4, 7, 6, 2, 5, 1, 3, 8

-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.
If the example matrix were stored in column major order, it would have the

following order in memory:
3, 6, 1, 4, 2, 3, 7, 5, 8

21. What is the purpose of level numbers in COBOL records?

-indicate by their relative values the hierarchical structure of the record.

Any line that is followed by a line with a higher-level number is itself a

record.


23. What is the primary difference between a record and a tuple?

-Tuples are similar to records, but do not have names for their constituent
parts.


24. Are the tuples of Python mutable?

-Python’s tuples are closely related to its lists, except that tuples are
immutable. A tuple is created by assigning a tuple literal, as in the following
example:

myTuple = (3, 5.8, 'apple')

Notice that the elements of a tuple need not be of the same type.
The elements of a tuple can be referenced with indexing in brackets, as in
the following:

myTuple[1]

This references the first element of the tuple, because tuple indexing begins

at 1.


26. In what primarily imperative language do lists serve as arrays?

-Python.


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.

-free unions
are unions that allowed complete freedom from type checking in their use.

-Type checking of unions requires that each union construct include a type
indicator. Such indicator is called tag or discriminant, and a union with
a discriminant is called a discriminated union.


36. What are the two common problems with pointers?

-Pointers have some inherent dangers: Dangling pointers are difficult
to avoid, and memory leakage can occur.



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.

46. Why is Java not strongly typed?

-expressions are strongly typed in Java.
However,
an arithmetic operator with one floating-point operand and one integer operand
is legal. The value of the integer operand is coerced to floating-point, and
a floating-point operation takes place. This is what is usually intended by the
programmer. However, the coercion also results in a loss of one of the benefits
of strong typing—error detection.


50. What is name type equivalence?

-Type equivalence is a strict form
of type compatibility—compatibility without coercion.


51. What is structure type equivalence?

-Structure type equivalence
means that two variables have equivalent types if their types have identical
structures.

PROBLEM SET

1. What are the arguments for and against representing Boolean values as
single bits in memory?

ANSWER : 

A Boolean value could be represented by a single bit, but because a single
bit of memory cannot be accessed efficiently on many machines, they are often
stored in the smallest efficiently addressable cell of memory, typically a

byte.



2. How does a decimal value waste memory space?

ANSWER :

-Their representation in memory is mildly wasteful, for reasons discussed in

the following paragraph.
-Decimal types are stored very much like character strings, using binary
codes for the decimal digits. These representations are called binary coded
decimal (BCD). In some cases, they are stored one digit per byte, but in

others,
they are packed two digits per byte. Either way, they take more storage than
binary representations. It takes at least four bits to code a decimal digit.

Therefore,
to store a six-digit coded decimal number requires 24 bits of memory.
However, it takes only 20 bits to store the same number in binary.



7. What significant justification is there for the -> operator in C and C++?

ANSWER :

-In C and C++, there are two ways a pointer to a record can be used to

reference a field in that record. If a pointer variable p points to a record

with a field named age, (*p).age can be used to refer to that field. The

operator ->, when used between a pointer to a record and a field of that

record, comfbines dereferencing and field reference. For example, the

expression p -> age is equivalent to (*p).age. In Ada, p.age
can be used, because such uses of pointers are implicitly dereferenced.


9. The unions in C and C++ are separate from the records of those languages,
rather than combined as they are in Ada. What are the advantages
and disadvantages to these two choices?

ANSWER : 

-Ada :

Advantage:
Unconstrained variant records in Ada allow the values of their variants to

change types during execution.

disadvantage:
However, the type of the variant can be changed only by
assigning the entire record, including the discriminant. This disallows

inconsistent records because if the newly assigned record is a constant data

aggregate, the value of the tag and the type of the variant can be statically

checked for consistency.

13. Analyze and write a comparison of using C++ pointers and Java reference
variables to refer to fixed heap-dynamic variables. Use safety and convenience
as the primary considerations in the comparison.

ANSWER:

-In c and C++ pointer can be use the same way as addresses. This design offer

no solutions to the dangling pointer or lost heap-dynamic variable problems.

-Reference types, such as those in Java and C#, provide heap management
without the dangers of pointers.

so its clear that Java has more safety for the heap-dynamic variables.


21. In what way is static type checking better than dynamic type checking?

ANSWER :
It allows many type errors to be caught early in the development cycle. Static

type checkers evaluate only the type information that can be determined at

compile time, but are able to verify that the checked conditions hold for all

possible executions of the program, which eliminates the need to repeat type

checks every time the program is executed