Pages

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.

13.
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?
Constructors.

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?
Initialize.

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?
Pass-by-assignment.

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

15.
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.