Jumat, 28 Juni 2013

Chapter 9

Chapter 9 


Review Questions

1.    What are the three general characteristics of subprograms?



  • Each subprogram has a single entry point, excluding co-routine.
  • The calling program is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time.
  • Control always returns to the caller when the subprogram execution terminates.
  • They provide flexibility to the subprogram
  • The storage of local variables in an active subprogram can be shared with the local variables in all inactive subprograms.
  • They efficiently used when computer has small memory (Faster Access).
  • Cost of the time required to allocate
  • Access to dynamic local variable must be indirect
  • The stack dynamic local variables, subprograms cannot be history sensitive



4.     What are formal parameters? What are actual parameters?
The parameters in the subprogram header are called formal parameters.
Subprogram call statements must include the name of the subprogram and alist of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters.
5.     What are the advantages and disadvantages of keyword parameters?
The advantage of keyword parameter is that they can appear in any order in the actual parameter list.
The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.
7. What is a parameter profile? What is a subprogram protocol?Answer: Parameter profile is the number, order, and types of its formal parameters.
Subprogram protocol is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol.

8. What are formal parameters? What are actual parameters?Answer: Formal parameters are the parameters in the subprogram header.
Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram which must be included with the name of the subprogram by the subprogram call statements.

10 . What are the differences between a function and a procedure ?
- Functions return values while procedure does not .
- Procedure defines new statements, while function define new user-defined operators.
17.  What is parametric polymorphism?
Parametric polymorphism is provided by a subprogram that takes a generic parameter that is used in a type expression that describes the types of the parameters of the subprogram. Both Ada and C++ provides a kind of compile-time parametric polymorphism.

18.  What causes a C++ template function to be instantiated?
C++ template functions are instantiated implicitly either when the function is named in a call or when its address is taken with the & processor.

25. What is ad hoc binding?
Ad hoc binding is when the environment of the call statement that passed the subprogram as an actual parameter.
26. What is multicast delegate?
All of the methods stored in a delegate instance are called in the order in which they were placed in the instance. This is called a multicast delegate.

34. What is a closure?Closure is a nested subprogram ant its referencing environment, which together  allow the subprogram to be called from anywhare in a program

37. In what way coroutine different from conventional subprogram? coroutine control mechanism is often called symmetric unit control model, in the otherhand conventional subprogram have a master-slave relationship between a caller and a called subprogram.
coroutine can have multiple entry point which are controlled by the coroutine themselves.

Problem Set

3. Argue in support of the template functions of C++. How is it different from the template functions in other languages?Answer: C++ templated classes are instantiated to become typed classes at compile time. For example, a templated Count class, can be created with the following declaration:
Count<int> doCount;


5.Consider the following program writen in C syntax:void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 1, list[5] = {2, 4, 6, 8, 10};
swap(value, list[0]);
swap(list[0], list [1]);
swap(value, list[value]);
}

For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap ?
a. Passed by Value
value =1 , list[5] = {2,4,6,8,10}
b. Passed by reference
value =6, list[5] ={4,1,2,8,10}
c. Passed by value-result
value =6, list[5] ={4,1,2,8,10}

6 . Compare and contrast PHP’s parameter passing with that of C#.
PHP’s parameter passing is similar to that of C#, except that either the actual parameter or the formal parameter can specify pass-by-reference. Passby- reference is specified by preceding one or both of the parameters with an ampersand

15. How is the problem of passing multidimensional arrays handles by Ada?
Ada compilers are able to determine the defined size of the dimensions of all arrays that are used as parameters at the time subprograms are compiled. In Ada, unconstrained array types can be formal parameters. An unconstrained array type is one in which the index ranges are not given in the array type definition. Definitions of variables of unconstrained array types must include index ranges. The code in a subprogram that is passed an unconstrained array can obtain the index range information of the actual parameter associated with such parameters

Chapter 8

Chapter 8


Review Questions

2. What did Bohm and Jocopini prove about flowcharts?
It was proven that all algorithms that can be expressed by flowcharts can be coded in a programming languages with only two control statements: one for choosing between two control flow paths and one for logically controlled iterations.

3. What is the definition of block?
In Ruby, block is a sequence of code, delimited by either breves or the do and and reserved words.

4. What is/are the design issue(s) for all selection and iteration control statements?
There is only one design issue that is relevant to all of the selection and iteration control statements: Should the control structure have multiple entries?

7. Under what circumstances must an F# selector have an else clause?
An F# selector have an “else” clause if  the “if” expression does return a value.

9. What are the design issues for multiple-selection statements?
What is the form and type of the expression that controls the selection?
How are the selectable segments specified?
How are the case values specified?
How should unrepresented selector expression values be handled, if at all?
Is execution flow through the structure restricted to include just a single selectable segment?

14.  What are the design issues for all iterative control statements?
How is the iteration controlled?
Where should the control mechanism appear in the loop statement?

15.  What are the design issues for counter-controlled loop statements?
What are the type and scope of the loop variable?
Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect loop control?
Should the loop parameters be evaluated only once, or once for every iteration?

21.  What are the design issues for logically controlled loop statements?
Should the control be pretest or post-test?
Should the logically controlled loop be a special form of a counting loop or a separate statement?

23.  What are the design issues for user-located loop control mechanisms?
Should the conditional mechanism be an integral part of the exit?
Should only one loop body be exited, or can enclosing loops also be exited?

Problem Set


1. What design issues should be considered for two-way selection statements?
The design issues for two-way selectors can be summarized as follows:
• What is the form and type of the expression that controls the selection?
• How are the then and else clauses specified?
• How should the meaning of nested selectors be specified?

4. What are the limitations of implementing a multiple selector from two-way selectors and gotos?
A multiple selector can be built from two-way selectors and gotos, but the resulting
structures are cumbersome, unreliable, and difficult to write and read.

5. What are the arguments pro and con, for Java’s approach to specify compound statements in control statements?
• Compound statements are required in control statements when the body of the if
or else clause requires multiple statements.
• Java uses braces to form compound statements, which serve as the bodies of if
and else clauses.

9. Boolean expressions are necessary in the control statements in Java, as opposed to also allowing arithmetic expressions, as in C++. Give a conditional statement that is correct in C++ but not in Java.
int i=10;
if( i )
{
// block of statements
}
This block of statement is valid in C++ but not in Java.

11. Explain the advantages and disadvantages of the Java switch statement, compared to C++’s switch statement.
The Java variable in the argument of a switch statement can be of type integral ( byte,
short etc.), char and String( JDK 1.7 onwards), whereas in C++ the argument can be int
or char.

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.


Selasa, 26 Maret 2013

Chapter 3

Review Questions

1. Define syntax and semantics!
Syntax is the form of its expressions, statements, and program units. Semantics is the meaning of those expressions, statements, and program units.


2.Who are language descriptions for ?
Potential users and Programming Language Implementors


3. Describe the operation of a general language generator.
A general language generator is a device that can be used to generate the sentences of the language. It generates unpredictable sentences which makes a generator seems to be a device of limited usefulness as language descriptor.



5. What is the difference between a sentence and a sentential form?
A sentence is a sentential form that has only terminal symbols. A sentence form is every string of symbols in the derivation.

7. What three extensions are common to most EBNFs ?
Three extensions are commonly included in the various versions of EBNF. The first extension denotes an optional part of an RHS, which is delimited by brackets. The second extension is the use of the brackets in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether. And the third extension deals with multiple-choice options.


9. What purpose do predicates serve in an attribute grammar ?
Predicates in attribute grammar states the static semantic rules a language needs to follow. A false value in predicates indicates that there is an invalid syntax or static semantics and determines whether an action is allowed or not.


10. What is the diference between a synthesized and an inherited atribute ?
The attributes are divided into two groups: synthesizedattributes and inherited attributes. The synthesized attributes are the result of the attribute evaluation rules, and may also use the values of the inherited attributes. The inherited attributes are passed down from parent nodes.


12. What is the primary use of attribute grammar?
Attribute grammar is primary used to provide complete descriptions of the syntax and static semantics of programming languages.


22. Give an example of an ambiguous grammar.
<assign> -> <id> = <expr>

<id> -> A | B | C

<expr> -> <expr> + <expr>

| <expr> * <expr>

|  (  <expr>  )

| <id>



24. Give an unambiguous grammar for if-then-else.
<if_stmt> -> if <logic_expr> then <stmt>
if <logic_expr> then <stmt> else <stmt>


25. What is the problem with using a software pure interpreter for operational semantics?
The detailed characteristics of the particular computer would make actions difficult to understand. Such a semantic definition would be machine-dependent.

27. What is loop invariant? Explain with an example.
A loop invariant is a condition that is necessarily true immediately before and immediately after each iteration of a loop. Example:
int j = 9;
for(int i=0; i<10; i++)
j–;
In this example it is true (for every iteration) that i + j == 9. A weaker invariant that is also true is that
i >= 0 && i < 10 (because this is the termination condition) or that j <= 9 && j >= 0.




Problem Set

3. Rewrite the BNF of Example 3.4 to represent operator – and operator / instead of operator + and operator *.
<assign> → <id> = <expr>
<id> → A | B | C
<expr> → <expr> – <term>
| <term>
<term> → <term> / <factor>
| <factor>
<factor> → ( <expr> )
| <id>

4. Rewrite the BNF of Example 3.4 to add the ++ and — unary operators of Java.
<assign>
→ <id> = <expr>
<id>
→ A | B | C
<expr>
→ <expr> + <term> class  <Unary – condition- statement>
| (++/–) <term>
<term>
→ <factor> + <term>
| <factor>
<factor>
→ ( <expr> )
| (++/–) <id>


9. Modify the grammar of Example 3.4 to add a unary minus operator that has higher precedence than either + or *.
<assign> -> <id> = <expr>
<id>  ->  A | B | C
<expr> -> <expr> + <term>
| <term>
<term> -> <term> * <factor>
| <factor>
<factor> -> ( <expr> )
| +<id> | -<id>


11. Consider the following grammar:
<S> → <A> a <B> b
<A> → <A> b | b
<B> → a <B> | a
Which of the following sentences are in the language generated by this grammar?
a. bbaabb
b. bbaba
c. bbaaaabb
d. abaabb


a. bbaabb and c.bbaaaabb

15.   Convert the BNF of Example 3.1 to EBNF.
<program>  ->  begin <stmt_list> end
<stmt_list> -> <stmt>
| <stmt> ; <stmt_list>
<stmt> -> <var> = <expression>
<var> -> A | B | C
<expression> -> <var> {(+|-) <var>}


16. Convert the BNF of Example 3.3 to EBNF.
<assign> -> <id> = <expr>
<id> -> A | B | C
<expr> -> <expr> { ( + | * ) <expr> }
| ( <expr> )
| <id>


17. Convert the following EBNF to BNF:
S → A{bA}
A → a[b]A
S → A | A B
B → b A | b A B
A → a A | a b A

18. What is a fully attributed parse tree?
A fully attributed parse tree is a condition when all the attributed values in parse tree have been computed.


23. Compute the weakest precondition for each of the following assignment statements and postconditions:
a. a = 2 * (b – 1) – 1 (a > 0)
b. b = ( c + 10)  / 3 (b > 6)
c. a = a + 2 * b – 1 ( a > 1)
d. x = 2 * y + x – 1 (x > 11)

(a) ->   a = 2 * (b – 1) – 1 {a > 0}
2 * (b – 1) – 1 > 0
2 * b – 2 – 1 > 0
2 * b > 3
b > 3 / 2
(b) ->   b = (c + 10) / 3 {b > 6}
(c + 10) / 3 > 6
c + 10 > 18
c > 8
(c) ->   a = a + 2 * b – 1 {a > 1}
a + 2 * b – 1 > 1
2 * b > 2 – a
b > 1 – a / 2
(d) ->   x = 2 * y + x – 1 {x > 11}
2 * y + x – 1 > 11
2 * y + x > 12