Session 4 : Data types(GSLC)

here is some task given from ms. Yanfi because GSLC

1. What are the advantages and disadvantages of decimal data types? 
Andvantages:
being able to precisely store decimal values, at least those within a restricted range, which cannot be done with floating-point.
Disadvantages:
the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful.

2. How does a decimal value waste memory space? 
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. In what way is static type checking better than dynamic type checking? 
The big benefit of static type checking is that it allows many type errors to be caught early in the development cycle. Static typing 
usually results in compiled code that executes more quickly because when the compiler knows the exact data types that are in use, it can 
produce optimized machine code (i.e. faster and/or using less memory). 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.
A static type-checker will quickly detect type errors in rarely used code paths. Without static type checking, even code coverage tests 
with 100% coverage may be unable to find such type errors.

4. What is coercion in programming languages? 
A compatible type is one that either is legal for
the operator or is allowed under language rules to be implicitly converted by compiler-generated code (or the interpreter) to a legal type. 
This automatic conversion is called a coercion. For example, if an int variable and a float variable are added in Java, the value of the int 
variable is coerced to float and a floating-point add is done.

5. Explain how coercion rules can weaken the beneficial effect of strong typing? 
The coercion results in a loss of one of the benefits of strong typing—error detection. For example, suppose a program had the int variables
a and b and the float variable d. Now, if a programmer meant to type a + b, but mistakenly typed a + d, the error would not be detected by the 
compiler. The value of a would simply be coerced to float. So, the value of strong typing is weakened by coercion.

Komentar