KBP Chapter 14

2.   When is an exception thrown or raised?

An exception is raised when its associated event occurs.

3.   What are the advantages of having support for exception handling builtin to language?

– First, without exception handling, the code required to detect error conditions can considerably clutter a program.

– Another advantage of language support for exception handling results from exception propagation. Exception propagation allows an exception raised in one program unit to be handled in some other unit in its dynamic or static ancestry.

• A language that supports exception handling encourages its users to consider all of the events that could occur during program execution and how they can be handled. This approach is far better than not considering such possibilities and simply hoping nothing will go wrong.

4. Give an example of hardware-detectable execution.

One of the example of hardware-detectable execution is division by zero which naturally results in error.

6. What is exception propagation in Ada ?

Exception propagation in Ada is a powerful tool for constructing a more reliable software systems.

8. Where does execution continue after an exception is handled in Ada ?

Control will continue after the exception clause.

12.   What is the use of Suppress pragma in Ada?
The suppress pragma is used to disable certain run-time checks that are parts of the built-in exceptions in Ada.

14. What is the name of all C++ exception handlers?
The name of all C++ exception handlers is Try clause.

15.  Which standard libraries define and throw the exception out_of_range in C++?

The exception out_of_range is thrown by library container classes.

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.

Problem set

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.

14.  Summarize the arguments in favor of the termination and resumption models of continuation.

The resumption model is useful when the exception is only an unusual condition, rather than an error. The termination model is useful when the exception is an error and it is highly unlikely that the error can be corrected so that execution could continue in some useful way.

Advertisements
Posted in Uncategorized | Tagged | Leave a comment

KBP Chapter 11 renew

1. What are the two kinds of abstractions in programming language?

Two kinds of abstractions in programming language is data abstraction(through ADT) and process abstraction.

2. Define abstract data type.

Data type that satisfies the following conditions:

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

9. What is in an Ada package specification? What about a body package?

An Ada package specification has interface of the encapsulation and even more. Meanwhile, body package has implementation of most, if not all, of the entities naemd in the associated package specification.

10. What is the use of the Ada with clause?

It makes the names defined in external packages visible.

11. What is the use of the Ada use clause?

It eliminates the need for explicit qualification of the references to entities from the named package.

15.   What is the purpose of a C++ destructor ?

The purpose of a C++ destructor is to deallocate heap space (memory) that the object or class used.

16.   What are the legal return types of a destructor?

A destructor has no return types.

20.  What is the use of limited private types?

Limited private types are useful when the usual predefined operations of assignment and comparison are not meaningful or useful.

21 What are initializers in Objective-C?

Intializers in Objective-C are constructors.

22. What is the use of @private and @public derivatives?

They are used to specify the access levels of the instance variables in a class definition. Similar to the reserved words private and public in C++.

23. When are constructors implicitly called in Objective-C?

Constructors in Objective-C are implicitly called after the object is created through the calling of alloc.

26. Why does Java not have destructors?

Because Java has an implicit garbage collection built in function which stores all objects which was supposed to be destroyed.

Problem set

9. What happens if the constructor is absent in Java and C++?

The compiler has no choice but to use the built-in constructor, so they can’t intialise the value of data.

11. Why is the destructor of C# rarely used?

Because C# also has its own garbage collection built in function, similar to those Java has.

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.

Posted in Uncategorized | Leave a comment

KBP Chapter 11

Posted in Uncategorized | Leave a comment

KBP Chapter 10

1.   What is the definition of control structure?

A control structure is a control statement and the collection of statements whose execution it controls.

2.   What did Böhm and Jocopini prove about flowcharts?

Böhm and Jacopini proved that all algorithms that can be expressed by flowcharts can be coded in a programming language with only two control statements: one for choosing between two control flow paths and one for logically controlled
iterations

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? All selection and iteration constructs control the execution of code segments, and the question is whether the execution of those code segments always begins with the first statement in the segment.

6. What is unusual about Python’s design of control statements?

Python uses indentation to specify control statements and using a colon instead of then for a then clause.

 

 

http://triwahjono.wordpress.com/page/2/

Posted in Uncategorized | Tagged | Leave a comment

KBP Chapter 9

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.

7. What is a parameter profile? What is a subprogram protocol?

The parameter profile of a subprogram is the container of the number, order, and types of its formal parameters. While a subprogram protocol is subprogram’s parameter profile as well as its return type if it’s a function.

8.   What are formal parameters? What are actual parameters?

Formal parameters are The parameters in the subprogram header. They are sometimes thought of as dummy variables because they are not variables in the usual sense.
Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram.

9.   What are the advantages and disadvantages of keyword parameters?

The advantage of keyword parameters 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.

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

The three semantic models of parameter passing are:

  • In mode: formal parameters can receive data from te corresponding actual parameter
  • Out mode: formal parameters can transmit data to the actual parameter
  • Inout mode: do both of In mode and Out mode.

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

The parameter-passing method of Python and Ruby is called passing-by-assignment. Because all data values are objects, every variable is a reference to an object. So, the actual parameter value is assigned to the formal parameter. Quite similar to pass-by reference.

23. What is automatic generalization?

Automatic generalization is the case in F# when for some functions F# infers a generic type for the parameters and the return value. This happens when the type inferencing system in F# cannot determine the type of parameters or the return tye of the function.

24. What is an overloaded subprogram?

An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment.

34. What is a closure?

A closure is a subprogram and the referencing environment where it was defined.

Problem set

3.   Argue in support of the templated functions of C++. How is it different from the templated functions of other languages?

It is different as C++ differentiates function based on overloading. It is not practical to make multiple function overloading in regard to writability and readability. Instead, creating a template allows a function to receive any datatype as long as the variation is based on the formal parameter definition.

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. Passed by value
b. Passed by reference
c. Passed by value-result

Answer:

a. Passed by value:  list[2] = {3,5}

b. Passed by reference:  list[2] = {6,10}

c. Passed by value-result: list[2] = {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 dimension of all arrays that are used as parameters at the time of subprograms are compiled.

http://triwahjono.wordpress.com/

Posted in Uncategorized | Tagged | Leave a comment

KBP Chapter 8

1.   What is the definition of control structure?

A control structure is a control statement and the collection of statements whose execution it controls.

2.   What did Böhm and Jocopini prove about flowcharts?

Böhm and Jacopini proved that all algorithms that can be expressed by flowcharts can be coded in a programming language with only two control statements: one for choosing between two control flow paths and one for logically controlled
iterations

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? All selection and iteration constructs control the execution of code segments, and the question is whether the execution of those code segments always begins with the first statement in the segment.

6. What is unusual about Python’s design of control statements?

Python uses indentation to specify control statements and using a colon instead of then for a then clause.

 

 

http://triwahjono.wordpress.com/page/2/

Posted in Uncategorized | Tagged | Leave a comment

KBP Chapter 7

1. Define operator precedence and operator associativity.

Operator precedence is the rule for expression evaluation that defines the order in which the operators of different precedence levels are evaluated. Example, multiplication is done first than addition.

Operator associativity is the rule which explains if there are two adjacent occurences of operators with the same level of precedence, which one is evaluated first. Example, a left associativity means the leftmost occurence is evaluated first.

2. What is a ternary operator?

A ternary operator is an operator that works for three operands.

For example ( a operator b ) c : d ;, this means if a operator b equals true, then do c, if false do d.

5. What is a nonassociative operator?

A nonassociative operator is an operator that has no associativity, means the operator should be parenthesized to show the order, otherwise it’s illegal.

10.  What is a conditional expression?

Conditional expression is a feature of a programming language which perform different computations or actions depending on whether a programmer specified boolean condition evaluates to true or false.

11.  What is an overloaded operator?

Arithmetic operators are often used for more than one purpose. This multiple use of an operator is called overloaded operator.

15.  What is referential transparency?

The concept of referential transparency is related to and affected by functional side effects. A program has the property of referential transparency if any two expressions in the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program.

18.  What is short-circuit evaluation?g

A short-circuit evaluation of an expression is one in which the result is determined without evaluating all of the operands and/or operators. For example, the value of the arithmetic expression (13 * a) * (b / 13 – 1) is independent of the value of (b / 13 – 1) if a is 0, because 0 * x = 0 for any x. So, when a is 0, there is no need to evaluate (b / 13 – 1) or perform the second multiplication.

Problem set

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 i think everyone who use programming language mostly start or know C, that’s why C is like the basic, and it’ll be really helpful if other programming language also use the same type of operator as C, just for the general functions.

7. Describe a situation in which the add operator in a programming language would not be commutativee.

   If the add operator for a language is also used to concatenate strings, it’s clear that it would not be commutative.
    For example:

        “abc” + “def” = “abcdef”  true
        “def” + “abc” = “defabc” false

    These two strings are obviously not equal, so the addition operator is not commutative.

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.

since the value of fun(&x) is always 4, the problem is the adding 5 to the i,

when the operands are evaluated left to right,  what happens is we save 3 in different memory, than we add the original x with 4, so it’ll become 7, then we add x with 5, but later we assign the value 7 to the x.

b. Operands are evaluated right to left.

but when we evaluate it from right to left, first we add the number 5 into x, so it’ll become 8, than we add the number 4(from return) to 8, which result is 12, then we store the value of 12 in x itself.

 http://triwahjono.wordpress.com/

Posted in Uncategorized | Tagged | Leave a comment