Selasa, 09 April 2013

Chapter 7

Review Questions

2. What is a ternary operator?
A ternary operator is an operator with three operands.

4. What operator usually has right associativity?
Exponentiation operator.

8. Define functional side effect.
Functional side effect is a side effect that occurs when the function changes either one of its parameters or a global variable.

10. What is a conditional expression?
Conditional expression is a statement that uses if-then-else statements.

11. What is an overloaded operator?
Operator Overloading is a specific case of polymorphism, where different operators have different implementations depending on their arguments.

24. What two languages include multiple assignments?
Ruby and Perl.

28. What is a cast?
A cast is an operator to convert a data type into another data types.


Problem Set

3.Do you think the elimination of overloaded operators in your favorite language would be beneficial? Why or why not?
It is not beneficial because it can be ambigious if we want to use that operator with the same type.


5.Should C’s assigning operations (for example, +=) be included in other languages (that do not already have them)? Why or why not?
Yes, because we can make the operator simpler than before. We can just write a+=b instead a=a+b.


15. Explain why it is difficult to eliminate functional side effects in C.
All functions in C are subprograms and each of them return only one value.To return more that one value, we have to use parameters and if the parameters are pointers it may change the value of variables. Another problem is if the function changes the value of a global variable, it may affect the whole program.


21. Why does Java specify that operands in expressions are all evaluated in left-to-right order?
Java has well defined rules for specifying order in which in expression are all evaluated in left-to-right order, this is done to make Java easier to code, as left-to-right order is how most of us learned it in our early school on the mathematics subject. left-to-right order is also one of the most used guidelines in order to evaluate operands in an expression. This is also done to prevent functional side effect from happening.







Chapter 5

Review Questions


2.What is the potential danger of case-sensitive names?To some people , this is a serious detriment to readability, because names that look very similar in fact denote different entities. In that sense, case sensitivity violates the design principle that the language constructs that looks similar should have similar meanings. But in languages whose variable names are case sensitive, although Rose and rose look similar, there is  no connection between the.


6. What is the l-value of a variable? What is the r-value?
R-value is the address of a variable.
L-value is the variable's value.


15.What is the general problem with static scoping?
-Allowing more access to both variables and subprograms than necessary
-The software used is highly dynamic, continually changes.



18. What is a block?A block is a section of code that allows to have its own local variables whose scope is    minimized and the variables are typically stack dynamic. Blocks provide the origin of the    phraseblock-structured language.

19.What is the purpose of the let constructs in functional languages?
A construct which first bind names to values, specified as expressions, and then uses the names defined in the first part.


23.  What are the advantages of normal constants?
A named constant allows for easier readability and writabillity, it’s also allows for easier error checking, where you only need to change the const variable in the program if it’s turns out to be wrong.


Problem Set

1. Decide which of the following identifier names is valid in C language. Support your decision
_Student -> valid, because it is started by a _. It is allowed in C.
int -> invalid, because it has the same name as one of a data type, hence might result in ambiguity.
Student -> valid, because it is started by an alphabet, regardless of the case.
123Student -> invalid, because identifier cannot have numbers as the start of the name.
Student123 -> valid, because although it contains numbers, the name starts with an alphabet.


2. What is l-value? Write a statement in C language which gives the compile time error “l-value required”.
The address of a variable is called its l-value.
Example:
int i = 10, j;
j = 9--; //error C2105: '--' needs l-value




 4.  Why is the type declaration of a variable necessary? What is the value range of the int type variable in Java?
 The type declaration of a variable is necessary because, in a program it documented information about its data, which provides clues about the program’s behavior. The value range of the int type variable in Java is 32 bits (4 bytes).


5. Describe a situation each where static and dynamic type binding is required.
Static binding is required if we need the bindings to occur first before the run time begins and remains unchanged throughout program execution.
Dynamic binding is required if we need the binding to occur first during run time or can change in the course of program execution.


Chapter 6

Review Questions



1. What is a descriptor?Descriptor  is the collection of the attributes of at variable. In an implementation, it is an area  of memory that stores the attributes of a variable.



5.Define ordinal, enumeration, dan subrange types.
Ordinal type is one in which the range of possible values can be easily associated with the set of positive integers.
enumeration type is one in which all of the possible values, which are named constants, are provided.



12. What languages support negative subscripts?
Language that supports negative subscripts is Perl.

15. What is an aggregate constant?
Aggregate constant is the parenthesized lists of values in Fortran language.

20. What is the structure of an associative array?
 The structure of an associative array is that each element of an associative array is in fact a pair of entities, a key, and a value.

21. What is the purpose of level numbers i n COBOL records?
The purpose of level numbers in COBOL records is to indicate by their relative values the hierarchical structure of a record.

27. What is the action of the Scheme function CAR?
The function CAR returns the first element of its list parameter.


28. What is the action of the F# function tl?
The function tl returns all elements of its list parameter except the first one.



32. What are the design issues for unions?
The design issues for unions are:
 - Should type checking be required? Note that any such type checking must be dynamic.
- Should unions be embedded in records?


34. Are the unions of F# discriminated?
Yes.



43.What is a compatible type?
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.



44.Define type error
Type type is the application of an operator to an operand of an inappropriate type.


45. Define Strongly typed!
Strongly type is a name for a programming language that type errors are always detected.


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.



53. What is the primary disadvantage to structure type equivalence?
The primary disadvantage to structure type equivalence is difficult to implement.


Problem Sets

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. In twos-complement notation, the representation of a negative integer is formed by taking the logical complement of the positive version of the number and adding one. Ones-complement notation is still used by some computers. In ones-complement notation, the negative of an integer is stored as the logical complement of its absolute value. Ones-complement notation has the disadvantage that it has two representations of zero.



5. What disadvantages are there in implicit deferencing of pointers, but only in certain contexts? For example , consider the implicit deference 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.


9.C provides two derived data types both for name and structure type equivalence: struct and union. Make a study on when to use struct type variables and union type variables.
 With a union, we're only supposed to use one of the elements, because they're all stored at the same spot. This makes it useful when we want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they all can be used at once.



21. In what way is dynamic type checking better than static type checking?
Dynamic type checking detects error at compile time and it is better than detecting error at run time.