0% found this document useful (0 votes)
123 views5 pages

Ch6 Problem Set

1. Boolean values can be represented as single bits to save memory, but this limits their use to Boolean operations. Representing them as bytes allows more general use but wastes space for single values. 2. Sign-magnitude and ones' complement representations of negative numbers are inconvenient for computer arithmetic. Twos' complement avoids this issue but still represents negative numbers in binary. 3. Enumeration types were added to C# after being omitted from early Java, providing more clarity and type safety compared to integer constants while avoiding implicit coercion issues.

Uploaded by

Khizra Asaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views5 pages

Ch6 Problem Set

1. Boolean values can be represented as single bits to save memory, but this limits their use to Boolean operations. Representing them as bytes allows more general use but wastes space for single values. 2. Sign-magnitude and ones' complement representations of negative numbers are inconvenient for computer arithmetic. Twos' complement avoids this issue but still represents negative numbers in binary. 3. Enumeration types were added to C# after being omitted from early Java, providing more clarity and type safety compared to integer constants while avoiding implicit coercion issues.

Uploaded by

Khizra Asaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 6

Problem Set

1. What are the arguments for and against representing Boolean values as single bits in
memory?
bytes (1 byte), short(2 bytes), integer (4 bytes), long (8 bytes). As a result, depending
on the domain of the variable required, data types are used.
2. How does a decimal value waste memory space?
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.
3. VAX minicomputers use a format for floating-point numbers that is not the same as the
IEEE standard. What is this format, and why was it chosen by the designers of the VAX
computers? A reference for VAX floating-point representations is Sebesta (1991).
The existing DEC VAX formats, inherited from the PDP-11, because The PDP-11 had several
uniquely innovative features, and was easier to program than its predecessors through the
additional general-purpose registers.
4. Compare the tombstone and lock-and-key methods of avoiding dangling pointers, from the
points of view of safety and implementation cost.
Tombstones take more memory, while lock-&-key requires add’l cpu time on each ptr
assignment to copy key as well as pointer. Pointer arithmetic could overwrite key in the heap.
5. What disadvantages are there in implicit dereferencing of pointers, but only in certain
contexts? For example, consider the implicit dereference of a pointer to a record in Ada when
it is used to reference a record field.
When implicit dereferencing of pointers occurs only in certain contexts, it makes the language
slightly less orthogonal. The context of the reference to the pointer determines its meaning.
This detracts from the readability of the language and makes it slightly more difficult to learn.

6. Explain all of the differences between Ada’s subtypes and derived types.
A derived type is a new type that is based on some previously defined type with which it is not
equivalent, although it may have identical structure. Derived types inherit all the properties of
their parent types. Consider the following example:
type Celsius is new Float;
type Fahrenheit is new Float;
The types of variables of these two derived types are not equivalent, although their structures
are identical. Furthermore, variables of both types are not type equivalent with any other
floating-point type. Derived types can also include range constraints on the parent type, while
still inheriting all of the parent’s operations.
On the other hand Ada subtype is a possibly range-constrained version of an existing type. A
subtype is type equivalent with its parent type.
Ada’s derived types are very different from Ada’s subrange types.
For example, consider the following type declarations:
type Derived_Small_Int is new Integer range 1..100; subtype Subrange_Small_Int is Integer
range 1..100;
Variables of both types, Derived_Small_Int and Subrange_Small_Int, have the same range of
legal values and both inherit the operations of Integer.
However, variables of type Derived_Small_Int are not compatible with any Integer type. On the
other hand, variables of type Subrange_Small_Int are compatible with variables and constants
of Integer type and any subtype of Integer.
7. What significant justification is there for the -> operator in C and C++?
The justification of –> operator in c and c++ is writability. In c ++ there are 2 ways a pointer
record can be used to reference a field in that record. It is slightly easier to write p –> q
than(*p).q.

8. What are all of the differences between the enumeration types of C++ and those of Java?
In Java they can include fields, constructors, and methods. The possible values of an
enumeration are the only possible instances of the class. All enumeration types inherit toString,
as well as a few other methods. An array of the instances of an enumeration type can be
fetched with the static method values. The internal numeric value of an enumeration variable
can be fetched with the ordinal method. No expression of any other type can be assigned to an
enumeration variable. Also, an enumeration variable is never coerced to any other type.

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?
The advantage is that Unconstrained variant records in Ada allow the values of their variants to
change types during execution. However the disadvantage is that 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.

10. Multidimensional arrays can be stored in row major order, as in C++, or in column major
order, as in Fortran. Develop the access functions for both of these arrangements for three-
dimensional arrays.
Let the subscript ranges of the three dimensions be named min(1), min(2), min(3), max(1),
max(2), and max(3). Let the sizes of the subscript ranges be size(1), size(2), and size(3). Assume
the element size is 1.
Row Major Order:
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) +((i-min(1))*size(3) + (j-min(2)))*size(2) +
(k-min(3))

Column Major Order:


location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) +((k-min(3))*size(1) + (j-min(2)))*size(2)
+ (i-min(1))

11. In the Burroughs Extended ALGOL language, matrices are stored as a single-dimensioned
array of pointers to the rows of the matrix, which are treated as single-dimensioned arrays of
values. What are the advantages and disadvantages of such a scheme?
The advantage of this scheme is that accesses that are done in order of the rows can be made
very fast; once the pointer to a row is gotten, all of the elements of the row can be fetched very
quickly. If, however, the elements of a matrix must be accessed in column order, these accesses
will be much slower; every access requires the fetch of a row pointer and an address
computation from there. Note that this access technique was devised to allow
multidimensional array rows to be segments in a virtual storage management technique. Using
this method, multidimensional arrays could be stored and manipulated that are much larger
than the physical memory of the computer.

12. Analyze and write a comparison of C’s malloc and free functions with C++’s new and
delete operators. Use safety as the primary consideration in the comparison.
New and Delete are safe type. Malloc also return void which must then be thrown to the
appropriate pointer type. New returns the correct pointer type by itself. Malloc requires you to
tell the number of bytes that will be used to allocated. New will generate the numbers
themselves.

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.
In C and C ++, pointers can be used the same way as the address. This design offers no solution
to the problem pointers dangling or lost heap-dynamic variable. Reference type, such as Java
and C # provide heap management pointers without any problems.

14. Write a short discussion of what was lost and what was gained in Java’s designers’
decision to not include the pointers of C++.
It is clear that in order to prevent the use of pointers is significantly supported by the amount of
security which programmers can get a ‘free’ given that issues such as stack overruns, memory
corruption and memory errors (though this is more to the attributes of the GC rather than the
pointer) largely eliminated. Many who showed that the use of pointers is one way that the C / C
++ is able to be such a high-level language while maintaining enough speed. While assembly
language and machine code still become a necessity among people who are looking to get the
amount of speed and memory of a given program, things like pointers and compiler effectively
optimized code for C and C ++ not to mention that it is much easier to read.

15. What are the arguments for and against Java’s implicit heap storage recovery, when
compared with the explicit heap storage recovery required in C++? Consider real-time
systems.
Implicit eliminates dangling pointer manufacture. The disadvantage is that the CPU time
needed for recovery, sometimes when there are a lot of stack space so that recovery becomes
unimportant.

16. What are the arguments for the inclusion of enumeration types in C#, although they were
not in the first few versions of Java?
Java are strongly typed in the same sense as Ada. Types can be explicitly cast, which could
result in a type error. However, there are rules of a language have an important effect on the
value of 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.
17. What would you expect to be the level of use of pointers in C#? How often will they be
used when it is not absolutely necessary?
In C#, memory address pointers can only be used within blocks specifically marked as unsafe,
and programs with unsafe code need appropriate permissions to run.
18. Make two lists of applications of matrices, one for those that require jagged matrices and
one for those that require rectangular matrices. Now, argue whether just jagged, just
rectangular, or both should be included in a programming language.

19. Compare the string manipulation capabilities of the class libraries of C++, Java, and C#.
C does not allow a given typedef to appear more than once in the same scope. C++ handles
typedefs and type names differently than C, and allows redundant occurrences of a given
typedef within the same scope
20. Look up the definition of strongly typed as given in Gehani (1983) and compare it with the
definition given in this chapter. How do they differ?
A strongly-typed programming language is one in which each type of data (such as integer,
character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming
language and all constants or variables defined for a given program must be described with one
of the data types. Certain operations may be allowable only with certain data types. The
language compiler enforces the data typing and use compliance. An advantage of strong data
typing is that it imposes a rigorous set of rules on a programmer and thus guarantees a certain
consistency of results. A disadvantage is that it prevents the programmer from inventing a data
type not anticipated by the developers of the programming language and it limits how
“creative” one can be in using a given data type.
21. In what way is static type checking better than dynamic type checking?
The advantages of static checking are that potential errors can be identified earlier, the
program is better documented, more care is needed in the program design and
implementations can take advantage of the additional information to produce more efficient
programs with less runtime checking code.
22. Explain how coercion rules can weaken the beneficial effect of strong typing?
because They decrease the type error detection ability of the compiler.

You might also like