Wednesday, June 26, 2013

Concepts of Programming Languages ---- Chapter 14 Exception Handling and Event Handling



Concepts of Programming Languages ---- Chapter 14 Exception Handling and Event Handling

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 14 Exception Handling and Event Handling



Review Question

1.   Define functional form, simple list, bound variable, and referential transparency.
A higher-order function, or functional form, is one that either takes one or more functions as parameters or yields a function as its result, or both. A simple list is the problem of membership of a given atom in a given list that does not include sublists. A bound variable is a variable that never changes in the expression after being bound to an actual parameter value at the time evaluation of the lambda expression begins. A referential transparency is the execution of a function always produces the same result when given the same parameters
2.   When is an exception thrown or raised?
An exception is raised when its associated event occurs.
6.   Explain in which the arithmetic processing capabilities of LISP and Prolog are similar.
      Arithmetic processing is simple to implement in LISP and Prolog.
7.   What does the abbreviation REPL stand for?
      REPL stand for read-evaluate-print loop
9.   What is the scope of exception handlers in Ada?
      Exception handlers can be included in blocks or in the bodies of subprograms, packages, or tasks.
10. What are the four exceptions defined in the Standard package of Ada?
There are four exceptions that are defined in the default package, Standard:
·         Constraint_Error
·         Program_Error
·         Storage_Error
·         Tasking_Error
11. are they any predefined exceptions in Ada?
      Yes, they are.
14. What is the name of all C++ exception handlers?
      Try clause.
16. Which standard libraries define and throw the exception overflow_error in C++?
      The exception overflow_error is thrown by math library functions.
30. In which version were assertions added to Java?
      Assertions were added to Java in version 1.4.
31. What is the use of the assert statement?
The assert statement is used for defensive programming. A program may be written with many assert statements, which ensure that the program’s computation is on track to produce correct results.
33. What is the purpose of a Java JFrame?
The JFrame class defines the data and methods that are needed for frames. So, a class that uses a frame can be a subclass of JFrame. A JFrame has several layers, called panes.
34. What are the different forms of assert statement?
      There are two possible forms of the assert statement:
             ·         assert condition;
             ·         assert condition : expression;

Problem Set
1.   What mechanism did early programming languages provide to detect or attempt to deal with errors?
Early programming languages were designed and implemented in such a way that the user program could neither detect nor attempt to deal with such errors. In these languages, the occurrence of such an error simply causes the program to be terminated and control to be transferred to the operating system.
2.   Describe the approach for the detection of subscript range errors used in C and Java.
In C subscript ranges are not checked. Java compilers usually generate code to check the correctness of every subscript expression. If any exception generates, then an unchecked exception is thrown.
6.   In languages without exception-handling facilities, it is common to have most subprograms include an “error” parameter, which can be set to some value representing “OK” or some other value representing “error in procedure”. What advantage does a linguistic exception-handling facility like that of Ada have over this method?
There are several advantages of a linguistic mechanism for handling exceptions, such as that found in Ada, over simply using a flag error parameter in all subprograms. One advantage is that the code to test the flag after every call is eliminated. Such testing makes programs longer and harder to read. Another advantage is that exceptions can be propagated farther than one level of control in a uniform and implicit way. Finally, there is the advantage that all programs use a uniform method for dealing with unusual circumstances, leading to enhanced readability.
7.   In languages without exception-handling facilities, we could send an error-handling procedure as parameter to each procedure that can detect errors than must be handled. What disadvantage are there to this method?
There are several disadvantages of sending error handling subprograms to other subprograms. One is that it may be necessary to send several error handlers to some subprograms, greatly complicating both the writing and execution of calls. Another is that there is no method of propagating exceptions, meaning that they must all be handled locally. This complicates exception handling, because it requires more attention to handling in more places.
 



Concepts of Programming Languages ---- Chapter 15 Functional Programming Languages

Concepts of Programming Languages ---- Chapter 15 Functional Programming Languages
Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 15 Functional Programming Languages


Review Questions
2.   What does a lambda expression specify?
The predicate function is often given as a lambda expression, which in ML is defined exactly like a function, except with the fn reserved word, instead of fun, and of course the lambda expression is nameless.
3.   What data types were parts of the original LISP?
      Atoms and lists.
6.   What is a simple list?
      A list which membership of a given atom in a given list that does not include sub lists.
7.   What does the abbreviation REPL stand for?
      REPL stand for read-evaluate-print loop
11. What are the two forms of DEFINE?
DEFINE takes two lists as parameters. The first parameter is the prototype of a function call, with the function name followed by the formal parameters, together in a list. The second list contains an expression to which the name is to be bound.
18. What is tail recursion? Why is it important to define functions that use recursion to specify repetition to be tail recursive?
A function is tail recursive if its recursive call is the last operation in the function. This means that the return value of the recursive call is the return value of the non-recursive call to the function. It is important to specify repetition to be tail recursive because it is more efficient(increase the efficiency).
19. Why were imperative features added to most dialects of LISP?
LISP began as a pure functional language but soon acquired some important imperative features to increased its execution efficiency.
27. What is the use of the fn reserved word in ML?
The predicate function is often given as a lambda expression, which in ML is defined exactly like a function, except with the fn reserved word, instead of fun, and of course the lambda expression is nameless.
29. What is a curried function?
      Curried function let new functions can be constructed from them by partial evaluation.
30. What does partial evaluation mean?
Partial evaluation means that the function is evaluated with actual parameters for one or more of the leftmost formal parameters.
32. What is the use of the evaluation environment table?
A table called the evaluation environment stores the names of all implicitly and explicitly declared identifiers in a program, along with their types. This is like a run-time symbol table.
33. Explain the process of currying.
The process of currying replaces a function with more than one parameter with a function with one parameter that returns a function that takes the other parameters of the initial function.

Problem Set
4.   Refer to a book on Haskell programming and discuss the features of Haskell.
Haskell features lazy evaluation, pattern matching, list comprehension, type classes, and type polymorphism.
7.   What features make F# unique when compared to other languages?
F# has a full-featured IDE, an extensive library of utilities that supports imperative, object-oriented, and functional programming, and has interoperability with a collection of nonfunctional languages. F# includes a variety of data types. Among these are tuples, like those of Python and the functional languages ML and Haskell, lists, discriminated unions, an expansion of ML’s unions, and records, like those of ML, which are like tuples except the components are named. F# has both mutable and immutable arrays.
8.   How is the functional operator pipeline ( |> ) used in F#?
The pipeline operator is a binary operator that sends the value of its left operand, which is an expression, to the last parameter of the function call, which is the right operand. It is used to chain together function calls while flowing the data being processed to each call.

Concepts of Programming Languages ---- Chapter 13 Concurrency

Concepts of Programming Languages ---- Chapter 13 Concurrency

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 13 Concurrency


Review Questions
1.   What are the three possible levels of concurrency in programs?
·         Instruction level (executing two or more machine instructions simultaneously)
·         Statement level (executing two or more high-level language statements simultaneously)
·         Unit level (executing two or more subprogram units simultaneously)
4.   What level of program concurrency is best supported by SIMD computers?
Statement-level concurrency
5.   What level of program concurrency is best supported by MIMD computers?
      Unit-level concurrency is best supported by MIMD computers.
7.   What is the difference between physical and logical concurrency?
·         Physical concurrency is several program units from the same program that literally execute simultaneously.
·         Logical concurrency is multiple processors providing actual concurrency, when in fact the actual execution of programs is taking place in interleaved fashion on a single processor.
8.   What is the work of a scheduler?
Scheduler manages the sharing of processors among the tasks.
12. What is a heavyweight task? What is a lightweight task?
Heavy weight task executes in its own address space. Lightweight task all run in the same address space.
16. What is a task descriptor?
Task descriptor is a data structure that stores all of the relevant information about the execution state of a task.
21. What is a binary semaphore? What is a counting semaphore?
A binary semaphore is  a semaphore that requires only a binary-valued counter. A counting semaphore is a synchronization object that can have an arbitrarily large number of states.
35. what does the Java yield method do?
The yield method, which takes no parameters, is a request from the running thread to surrender the  voluntarily. The thread is put immediately in the task-ready queue, making it ready to run. The scheduler then chooses the highest-priority thread from the task-ready queue. If there are no other ready threads with priority higher than the one that just yielded the processor, it may also be the next thread to get the processor.
36. What does the Java join method do?
Java forces a method to delay its execution until the run method of another thread has completed its execution.
37. What does the Java interrupt method do?
Interrupt becomes one way to communicate to a thread that it should stop.
42. What kind of Java object is a monitor?
In Java, a monitor can be implemented in a class designed as an abstract data type, with the shared data being the type. Accesses to objects of the class are controlled by adding the synchronized modifier to the access methods.
55. What is Concurrent ML?
ML is an extension to ML that includes a form of threads and a form of synchronous message passing to support concurrency.
56. What is the use of the spawn primitive of CML?
The use of Spawn primitive of CML is to create a thread.
60. What is the type of an F# heap-allocated mutatable variable?
A mutable heap-allocated variable is of type ref

Problem Set
1.   Explain clearly why a race condition can create problems for a system.
Because two or more tasks are racing to use the shared resource and the behavior of the program depends on which task arrives first (and wins the race). The importance of competition synchronization should now be clear.
2.   What are the different ways to handle deadlock?
·         Ignoring deadlock
·         Detection
·         Prevention
·         Avoidance
4.   In the producer-consumer example of Section 13.3, suppose that we incorrectly replaced the release(access) in the consumer process with wait(access). What woud be the result of this error on execution of the system?
      Deadlock would occur if the release(access) were replaced by a wait(access) in the 
       consumer process, because instead of relinquishing access control, the consumer 
       would wait for control that it already had.

Concepts of Programming Languages ---- Chapter 12 Support for Object-Oriented Programming

Concepts of Programming Languages ---- Chapter 12 Support for Object-Oriented Programming

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 12 Support for Object-Oriented Programming



Review Question
2.   Define abstract data type.
An abstract data type is a data structure that are 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.
6 .  Explain how information hiding is provided in Ada package.
Data type representations can appear in the package specification but be hidden from clients by putting them in the private clause of the package. The abstract type itself is defined to be private in the public part of the package specification. Private types have built-in operations for assignment and comparison for equality and inequality.
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.
8.   What is an abstract method? What is an abstract class?
An abstract method is an implemented method which all of descendant class should have and it is included in Building. An abstract class is  a class that includes at least one abstract method.
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.
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.
31. What is the root class in Objective-C?
The predefined root class named NS Object
33. What is the purpose of an Objective-C category?
The purpose of an Objective-C category is to add certain functionalities to different classes and also to provide some of the benefits of multiple inheritance, without the naming collisions that could occur if modules did not require module names on their functions.
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.
2.   In what ways can “compatible “ be defined for the relationship between an overridden method and the overriding method?
Every overriding method must have the same number of parameters as the overridden method and the types of the parameters and the return type must be compatible with those of the parent class.
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.
One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common  code amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be  refactored to move the common code up to a mutual super class. This also tends to result in a better organization of  code and smaller, simpler compilation units.
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.
Inheritance does.
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?
is no explicit deallocation operator. A finalize method is implicitly called when the garbage collector  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.
25. Study and explain private and public modifiers in C++. How do those modifiers differ in C#?
      C++ includes both classes and structs, which are nearly identical constructs. The only difference is
      that the default access modifier for class is private, whereas for structs it is public. C# also has structs,
      but they are very different from those of C++. In C#, structs are, in a sense, lightweight classes. They
      can have constructors, properties, methods, and data fields and can implement interfaces but do not
      support inheritance.