Jumat, 28 Juni 2013

Chapter 11

Chapter 11

Review Questions

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.
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?
The initializers in Objective-C are 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.
43. What is a C++ namespace, what is its purpose?
In general, a namespace is a container for a set of identifiers and allows the disambiguation of homonym identifiers residing in different namespaces. The purpose is to help programs manage the problem of global namespace.


Problem Set


4. What are the advantages of the nonpointer concept in Java?
Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects. Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array. You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.
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, or definition, of the type and the operations are contained in a single syntactic unit
- The representation of objects of the type is hidden from the program units that use the type, so only direct operations possible on those objects are those provided in the type’s definition
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.
19. Compare Java’s packages with Ruby’s modules.
In Ruby, the require statement is used to import a package or a module. For example, the extensions package/module is imported as follows.
require ‘extensions’
External files may be included in a Ruby application by using load or require. For example, to include the external file catalog.rb, add the following require statement.
require “catalog.rb”
The difference between load and require is that load includes the specified Ruby file every time the method is executed and require includes the Ruby file only once.
In Java, the import statement is used to load a package. For example, a Java package java.sql is loaded as follows.
import java.sql.*;

Chapter 10

Chapter 10


Review Questions

1. What is the definition used in this chapter for “simple” subprograms ?-Subprograms cannot be nested and all local variables are static.
2. Which of the caller of callee saves execution status information ?
-Either can save the execution status
3. What must be stored for the linkage to a subprogram ?
- Execution status information
4. What is the task of a linker ?
- find files that contain the translated subprograms referenced in that program and load them into memory, set target addresses of all calls to those subprograms in the main program to the entry addresses of those subprograms.
8. What kind of machines often use registers to pass parameters ?
- RISC Machines
10. Define static chain, static_depth, nesting_depth,and chain_offset.- static chain : a chain of static links that connect certain activation record instances in the stack.
– static depth : integer associated with a static scope that indicates how deeply it is nested in the outermost scope.
– nesting_depth : difference between static_depth of a subprogram containing reference to x and the static_depth of a subprogram containing the declaration of x.
– chain_offset : number of links to the correct activation record instance
12. How are references to variables represented in the static-chain method?It is represented by static depth.

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 particularactivation retain the value of previous activation ?
- If the variable is declared as static. Static modifier is a modifier that makes a variable history – sensitive.
7. It is stated in this chapter that when nonlocal variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every nonlocal access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.One very simple alternative is to assign integer values to all variable names used in the program.  Then the integer values could be used in the activation records, and the comparisons would be between integer values, which are much faster than string comparisons.
8. Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access? Hint: Consider the way the correct activation record instance of the static parent of a newly enacted procedure is found (see Section 10.4.2).
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.

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.