Monday, 24 June 2013

Chapter 12

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (13 numbers, quite randomly in Review Qustion and 7 numbers in Problem Set) in Chapter 12 in "Concept of Programming Language" book.

Review Question

4. What is message protocol?
Message protocol is the entire collection of methods of an object.

5. What is an overriding method?
Overriding method is method that overrides the inherited method.

7. What is dynamic dispatch?
Dynamic dispatch is the third characteristic (after abstract data types and inheritance) of object-oriented programming language which is a kind of polymorhphism provided by the dynamic binding of messages to method definitions.

10. What is an inner class?
Inner classes are non-static classes that are nested directly in another class.

11. What is the message protocol of an object?
The message protocol of an objects are all the methods.

12. From where are Smalltalk objects allocated?
Smalltalk objects are allocated from the heap and are referenced through reference variables, which are implicitly dereferenced.

15. What kind of inheritance, single or multiple, does Smalltalk support?
Smalltalk supports single inheritance; it does not allow multiple inheritance.

18. From where can C++ objects be allocated?
The objects of C++ can be static, stack dynamic, or heap dynamic. Explicit deallocation using the delete operator is required for heap-dynamic objects, because C++ does not include implicit storage reclamation.

19. How are C++ heap-allocated objects deallocated?
C++ heap-allocated objects are deallocated using destructor.

29. Does Objective-C support multiple inheritance?
No Objective-C doesn’t support it. (It supports only single inheritance).

31. What is the root class in Objective-C?
The predefined root class named NSObject

38. What is boxing?
Boxing is primitive values in Java 5.0+ which is implicitly coerced when they are put in object context. This coercion converts the primitive value to an object of the wrapper class of the primitive value’s type.

39. How are Java objects deallocated?
By implicitly calling a finalize method when the garbage collector is about to reclaim the storage occupied by the object.

Problem Set
1 . What important part of support for inheritance is missing in Java?
Java does not support the private and protected derivations of C++. One can surmise that the Java designers believed that subclasses should be subtypes, which they are not when private and protected derivations are supported. Thus, they did not include them.

7. What is one programming situation where multiple inheritance has a significant disadvantage over interfaces?
A situation when there are two classes derived from a common parent and those two derived class has one child.

10. Explain one advantage of inheritance.
Inheritance offers a solution to both the modification problem posed by abstract data type reuse and the program organization problem. If a new abstract data type can inherit the data and functionality of some existing type, and is also allowed to modify some of those entities and add new entities, reuse and is also allowed to modify some of those entities and add new entities, reuse is greatly facilitated without requiring change to the reused abstract data type. Programmers can begin with an existing abstract data type and design a modified descendant of it to fit a new problem requirement. Furthermore, inheritance provides a framework for the definition of hierarchies of related classes that can reflect the descendant relationship in the problem space.
12. Compare inheritance and nested classes in C++. Which of these supports an is-a relationship?
Inheritance is where one class (child class) inherits the members of another class (parent class).Nested class is a class declared entirely within the body of another class or interface. Meanwhile, Inheritance does support it.

Describe the mechanism of dynamic dispatch with an example in Java. Is it possible to dynamically dispatch the data members?
In C++, a method must be defined as virtual to allow dynamic binding. In Java, all method calls are dynamically bound unless the called method has been defined as final, in which case it cannot be overridden and all bindings are static. Static binding is also used if the method is static or private, both of which disallow overriding.

17. What are the different options for object destruction in Java?
There is no explicit deallocation operator. A finalize method is implicitly called when the garbage collector is about to reclaim the storage occupied by the object.

20. Compare the way Smalltalk provides dynamic binding with that of C++.
In C++, the programmer can specify whether static binding or dynamic binding is to be used. Because static binding is faster, this is an advantage for those situations where dynamic binding is not necessary. Furthermore, even the dynamic binding in C++ is fast when compared with that of Smalltalk. Binding a virtual member function call in C++ to a function definition has a fixed cost, regardless of how distant in the inheritance hierarchy the definition appears. Calls to virtual functions require only five more memory references than statically bound calls (Stroustrup, 1988). In Smalltalk, however, messages are always dynamically bound to methods, and the farther away in the inheritance hierarchy the correct method is, the longer it takes. The disadvantage of allowing the user to decide which bindings are static and which are dynamic is that the original design must include these decisions, which may have to be changed later.

Chapter 11

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (12 numbers, quite randomly in Review Qustion and 5 numbers in Problem Set) in Chapter 11 in "Concept of Programming Language" book.

Review Question

1. What are the two kinds of abstraction in programming languages?
The two fundamental kinds of abstraction in contemporary programming languages are process abstraction and data abstraction.

8. What is the difference between private and limited private types in Ada?
Limited private is more restricted form and objects of a type that is declared limited private have no built-in operations.

10. What is the use of the Ada with clause?
With clause makes the names defined in external packages visible; in this case Ada. Text_IO, which provides functions for input of text.

11. What is the use of the Ada use clause?
Use clause eliminates the need for explicit qualification of the references to entities from the named package.

12. What is the fundamental difference between a C++ class and an Ada package?
Ada packages are more generalize encapsulations that can define any number of types.

15. What is the purpose of a C++ destructor?
The purpose of a C++ desctructor is as a debugging aid, in which case they simply display or print the values of some or all of the object’s data members before those members are deallocated.
16. What are the legal return types of a desctructor?
Destructor has no return types and doesn’t use return statements.

21. What are initializers in Objective-C?

22. What is the use of @private and @public directives?
The use is to specify the access levels of the instance variables in a class definition.

27. Where are all Java methods defined?
All Java methods are defined in a class.

30. What is a friend function? What is a friend class?
a “friend” of a given class is allowed access to public, private, or protected data in that class. Normally, function that is defined outside of a class cannot access such information.
Class that can access the private and protected members of the class in which it is declared as a friend. On declaration of friend class all member function of the friend class become friends of the class in which the friend class was declared.

37. What is the name of all Ruby constructors?

Problem Set
9. What happens if the constructor is absent in Java and C++?
It will be made automatically by the built-up in.

10. Which two conditions make data type “abstract”?
The representation of objects of the type is hidden from the program units that use the type, so the only direct operations possible on those objects are those provided in the type’s definition.
The declarations of the type and the protocols of the operations on objects of the type, which provide the type’s interface, are contained in a single syntactic unit. The type’s interface does not depend on the representation of the objects or the implementation of the operations. Also, other program units are allowed to create variables of the defined type.

11. Why is the destructor of C# rarely used?
Although C# allows destructors to be defined, because it uses garbage collection for most of its heap objects, destructors are rarely used.

12. How are classes in Ruby made dynamic?
Classes in Ruby are dynamic in the sense that members can be added at any time. This is done by simply including additional class definitions that specify the new members. Moreover, even predefined classes of the language, such as String, can be extended.

17. The namespace of the C# standard library, System, is not implicitly available to C# programs. Do you think this is a good idea? Defend your answer.
I think it is not, because it reduces its efficiency.

Sunday, 23 June 2013

Chapter 10

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (5 numbers, quite randomly in Review Qustion and 4 numbers in Problem Set) in Chapter 10 in "Concept of Programming Language" book.

Review Question

4. What are the two steps in locating a nonlocal variable in a static-scoped language with stack-dynamic local variables and nested subprograms?
  • Find the correct activation record instance
  • Determine the correct offset within that activation record instance
6. What are the two potential problems with the static chain methods?
  • A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large
  • Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes
7. What is display?
One alternative to static chain is to use a display, for this approach, the static links are collected in a single array called a display. Display uses a pointer array to store the activation records along the static chain.

11. Describe the deep access method of implementing dynamic scoping?
Deep Access – nonlocal references are found by searching the activation record instances on the dynamic chain. Length of chain cannot be statically determined every activation record instance must have variable names

16. Describe the deep-access method of implementing dynamic scoping.
The dynamic chain links together all subprogram activation records instances in the reverse of the order in which they were activated. Therefore, the dynamic chain is exactly what is needed to reference nonlocal variables in a dynamic-scoped language.

Problem Set
6. Although local variables in Java methods are dynamically allocated at the beginning of each activation, under what circumstances could the value of a local variable in a particular activation retain the value of previous activation ?
If the variable is declared as static. Static modifier is a modifier that makes a variable history – sensitive.

8. Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access?
Following the hint stated with the question, the target of every goto in a program could be represented as an address and a nesting_depth, where the nesting_depth is the difference between the nesting level of the procedure that contains the goto and that of the procedure containing the target. Then, when a goto is executed, the static chain is followed by the number of links indicated in the nesting_depth of the goto target. The stack top pointer is reset to the top of the activation record at the end of the chain.

9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and nonlocal references?
Including two static links would reduce the access time to nonlocals that are defined in scopes two steps away to be equal to that for nonlocals that are one step away. Overall, because most nonlocal references are relatively close, this could significantly increase the execution efficiency of many programs.

11. If a compiler uses the static chain approach to implementing blocks, which of the entries in the activation records for subprograms are needed in the activation records for blocks?
Blocks are treated as parameterless subprograms that are always called from the same place in the program.

Chapter 9

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (9 numbers, quite randomly in Review Qustion and 4 numbers in Problem Set) in Chapter 9 in "Concept of Programming Language" book.

Review Question

1. What are the three general characteristics of subprograms?

  • Each subprogram has a single entry point.
  • The calling program unit 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.

2. What does it mean for a subprogram to be active?
A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution.

4. What characteristic of Phyton subprogram sets them apart from those of other language?
One characteristic of Phyton functions that sets them apart from  the functions of other common programming languages is that function def statements are executable.

7. What is a parameter profile? What is a subprogram protocol?
A parameter profile of a subprogram contains the number, order, and types of its formal parameters.
A protocol of a subprogram is its parameter profile plus, if it is a function, its return type.

11. What are the design issues for subprograms?

  • Are local variables statically or dynamically allocated?
  • Can sub program definitions appear in another subprogram definitions?
  • What parameter-passing method or methods are used?
  • Are the types of the actual parameters checked against the types of the formal parameters?
  • If subprograms can be passed as parameters and subprograms can be nested, what is the referencing environment of a passed subprogram?
  • Can subprograms be overloaded?
  • Can subprograms be generic?
  • If the language allows nested subprograms, are the closures supported?

14. What languages allow subprogram definitions to be nested?
JavaScript, Phyton, Ruby, Lua, Algol 60, Algol 68, Pascal, and Ada.

15. What are the three semantic models of parameter passing?

  • In mode.
  • Out mode.
  • Inout mode.

20. What is the parameter-passing method of Phyton and Ruby called?

25. What is ad hoc binding?
The environment of the call statement that passed the subprogram as an actual parameter.

Problem Set
5. Consider the following program written 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
b. Passed by reference
c. Passed by value-result

a. value =1 , list[5] = {2,4,6,8,10}
b. value =6, list[5] ={4,1,2,8,10}
c. 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. Pass-by reference is specified by preceding one or both of the parameters with an ampersand.

7. Consider the following program written in C syntax:
void fun (int first, int second){ first += first; second += second; 
void main() { int list [2] = {3,5}; fun(list[0], list[1]); }
For each of the following parameter-passing methods, what are the values of the list array after execution?
a. Passes by value
b. Passes by reference
c. Passes by value-result

a. 3, 5
b. 6, 10
c. 6, 10

How is the problem of passing multidimensional arrays handled 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.

Sunday, 14 April 2013

Chapter 8

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (8 numbers, quite randomly in Review Qustion and 3 or 4 numbers in Problem Set) in Chapter 8 in "Concept of Programming Language" book.

Review Question

1. What is the definition of control structure?
Control structure is a control statement and the collection of the statements whose execution it controls.

6. What is unusual about Phyton's design of compound statements?
Phyton uses indentation to specify compound statements.

7. Under what circumtances must an F# selector have an else clause?
If the "if" expression does return a value, it must have an else clause.

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?
  • Is execution flow through the structure restricted to include just a single selectable segement?
  • How are the case of values specified?
  • How should unrepresented selector expression values be handled, if at all?

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?

19. What does the range function in Phyton do?
range function is used for most simple counting loops in Phyton.

22. What is the main reason user-located loop control statements were invented?
In some situation, it is convenient for a programmer to choose a location for loop control other than the top bottom of the loop body.

Problem Set
2. Phyton uses indentation to specify compound statements. Give an example in support of this statement.
If x < y :
  x = y
  print "This is in compound statement"

6. In C language, a control statement can be implemented using a nested if else, as well as by using switch statement. Make a list of differences in the implementation of a nested if else and a switch statement. Also suggest under what circumstances the id else control stricture is used and in which condition the switch case is used.

If else structure
  • Used when the variable that checked have some range, like 0-50, 51-64, A-E, etc.
  • Used when the logic expression to compare the variable is many.
  • When there's only if statement without else, when the variable isn't valid at the logic expression there, the compound statements is skipped.
Switch case structure
  • Used when the variable that checked is exact value, like 1, 40, A, C, etc.
  • Used when the logic expression is just one but have a different output value each option.
  • When there's switch statement without default, when the variable isn't valid at any values there, the compound statements is skipped.

18. State one of the main legitimate needs for gotos.
Control flow transfers out of a loop where there's a large amount of lexical context that the loop required.

Saturday, 13 April 2013

Chapter 7

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (7 numbers, quite randomly in Review Qustion and 5 or 6 numbers in Problem Set) in Chapter 7 in "Concept of Programming Language" book.

Review Question
2. What is a ternary operator?
It means, the operator have three operands.

3. What is a prefix operator?
It means the operators precede their operands.

6. What associativity rules are used by APL?
Right to left for all operators.

7. What is the difference between the way operators are implemented in C++ and Ruby?
The difference is in Ruby, all the arithmetic, relational, and assignment operator, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods while in C, it doesn't.

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

18. What is short-circuit evaluation?
A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators.

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

Problem Set
1. When might you want the compiler to ignore type differences in an expression?
When I want to evaluate a string as a number and vice versa.

13. Let the function fun be defined as

int fun(int *k){
  *k += 4;
  return 3 * (*k) - 1;

Suppose fun is used in a program as follows:

void main(){
  int i = 10, j = 10, sum1, sum2;
  sum1 = (i/2) + fun (&i);
  sum2 = fun(&j) + (j/2);

What are the values of sum1 and sum2
a. if the operands in the expression are evaluated left to right?
b. if the operands in the expression are evaluated right to left?

a. sum1 = (10/2) + (3 * 10 - 1) = 5 + 29 = 34
    sum2 = (3 * 14 - 1) + (14/2) = 41 + 7 = 49

b. sum1 = (14/2) + (3 * 14 - 1) = 7 + 41 = 49
    sum2 = (3 * 10 - 1) + (10/2) = 29 + 5 = 34

15. Explain why it is difficult to eliminate functional side effects in C?
In C, it only have one functions, meaning that all subprograms return only one value. To eliminate the side effects and still provide subprograms that return more than one value, the values need to be placed in a struct and the struct returned. Access to global in functions would also have to be disallowed.

18. Should an optimizing compiler for C or C++ be allowed to change the order of subexpressions in a Boolean expression? Why or why not?
Because of short-circuit evaluation, the order of subexpressions is important.

20. Consider the following C program:

int fun(int *i){
  *i += 5;
  return 4;

void main(){
  int x = 3;
  x = x + fun(&x);

What is the value of x after the assignment statement in main, assuming
a. Operands are evaluated left to right.
b. Operands are evaluated right to left.

a. x = 3 + 4 = 7

b. x = 8 + 4 = 12

Sunday, 7 April 2013

Chapter 6

An assignment from Mr. Tri Djoko Wahyono (as always). As what the title said, it is the answer for some question (16 numbers, quite randomly in Review Qustion and 5 or maybe 6 numbers in Problem Set) in Chapter 3 in "Concept of Programming Language" book.

Review Question
1. What is a descriptor?
A descriptor is the collection of the attribute of a variable.

What are the advantages and disadvantages of decimal data types?
Decimal types have the advantages of being able to precisely store decimal values, at least those within a restricted range which cannot  be done with floating-point.
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.

3. What are the design issues for character string types?

  • Should strings be simply a special kind of character array or a primitive type?
  • Should strings have static or dynamic?

4. Describe the three string length  options.
The first option, when the length can be static and set when the string is created, it 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, which is called a limited dynamic length string.
The third option is to allow strings to have varying length with no maximum, which is called a dynamic length string.

5. Define ordinal, enumeration, and subrange types.

  • An ordinal type is one in which the range if possible values can be easily associated with the set of positive integers.
  • An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated, in definition.
  • A subrange type is a contiguous subsequence of an ordinal type.

8. What are the design issues for arrays?

  • What types are legal for subscripts?
  • Are subscripting expressions in element references range checked?
  • When are subscript ranges bound?
  • When does array allocation take place?
  • Are ragged or rectangular multidimensioned arrays allowed, or both?
  • Can arrays be intialized when they have their storage allocated?
  • What kinds of slices are allowed, if any?

13. What languages support array slices with stepsizes?
Phyton, Perl, and Ruby.
21. What is the purpose of level numbers in COBOL records?
Level numbers indicate by their relative values the hierarchical structure of the record.

24. Are the tuples of Phyton mutable?
No, Phyton includes an immutable tuple type.

35. What are the design issues for pointer types?

  • What are the scope and lifetime of a pointer variable?
  • What is the lifetime of a heap-dynamic variable (the  value a pointer references)?
  • Are pointers restricted as to the type of value to which they can point?
  • Are pointers used for dynamic storage management, indirect addressing, or both?
  • Should the language support pointer types, reference types, or both?

43. What is a compatible type?
A compatible type is one that either as 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.
A type error is the application of an operator to an operand of an inappropriate type.

45. Define strongly typed?
Strongly typed is a type of language which type errors are always detected.

49. Why are C and C++ bot strongly typed?
C and C++ are not strongly typed languages because both include union types, which are not type checked.

52. What is the primary advantage of name type equivalence?
Name type equivalence is easy to implement.

53. What is the primary disadvantage of structure type equivalence?
Structure type equivalent is more difficult to implement.

Problem Set
2. How are negative integers stored in memory?
A negative integer could be stored in a bit that indicate that it is a negative number, but not lend itself to computer arithmetic.

7. Compare the pointer and reference type variable in C++.
In C++, pointers can be used in the same ways as addresses are used in assembly languages. it is similar to a reference type, with one important and fundamental difference: A pointer refers to an object or a value in memory.

19. Any type defined with typedef is type equivalent to its parent type. How does the use of typedef differ in C and C++?
Any type defines with typedef is type equivalent to its parent type. One exception to C using name type equivalence for structures, enumeration, and files, in which case structural type equivalence is used. C++ is like C except there is no exception for structures and unions defined in different files.

21. In what way is dynamic type checking better than static type checking?
It is better to detect errors at compile time than at run time, because the earlier correction is usually less costly. The penalty for static checking is reduced programmer flexibility. Fewer shortcut and tricks are possible.

22. Explain how the dangling-pointer problem can be removed using the locks-and-keys approach.
The best solution to the dangling-pointer problem is to take deallocation of heap-dynamic variables out of the hands of programmers. If programs cannot explicitly deallocate heap-dynamic variables, there will be no dangling-pointers.