Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 12
- REVIEW QUESTIONS
11. What is the message protocol of an object?
Answer :
12. From where are Smalltalk objects allocated?
Answer :
13. Explain how Smalltalk messages are bound to methods. When does this take place?
Answer :
14. What type checking is done in Smalltalk? When does it take place?
Answer :
15. What kind of inheritance, single or multiple, does Smalltalk support?
Answer :
- PROBLEM SET
11. Explain the advantages and disadvantages of having all values in a language be objects.
Answer :
12. What exactly does it mean for a subclass to have an is-a relationship with its parent class?
Answer :
13. Describe the issue of how closely the parameters of an overriding method must match those of the method it overrides.
Answer :
14. Explain type checking in Smalltalk.
Answer :
15. The designers of Java obviously thought it was not worth the additional efficiency of allowing any method to be statically bound, as is the case with C++. What are the arguments for and against the Java design?
Answer :
YNWA
Rabu, 14 Januari 2015
CHAPTER 11
Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 11
- REVIEW QUESTIONS
11. What is the use of the Ada use clause?
Answer :
12. What is the fundamental difference between a C++ class and an Ada package?
Answer :
13. From where are C++ objects allocated?
Answer :
14. In what different places can the definition of a C++ member function appear?
Answer :
15. What is the purpose of a C++ constructor?
Answer :
- PROBLEM SET
11. What are the arguments for and against the Objective-C design that method access cannot be restricted?
Answer :
12. Why are destructors rarely used in Java but essential in C++?
Answer :
13. What are the arguments for and against the C++ policy on inlining of methods?
Answer :
14. Describe a situation where a C# struct is preferable to a C# class.
Answer :
15. Explain why naming encapsulations are important for developing large programs.
Answer :
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 11
- REVIEW QUESTIONS
11. What is the use of the Ada use clause?
Answer :
12. What is the fundamental difference between a C++ class and an Ada package?
Answer :
13. From where are C++ objects allocated?
Answer :
14. In what different places can the definition of a C++ member function appear?
Answer :
15. What is the purpose of a C++ constructor?
Answer :
- PROBLEM SET
11. What are the arguments for and against the Objective-C design that method access cannot be restricted?
Answer :
12. Why are destructors rarely used in Java but essential in C++?
Answer :
13. What are the arguments for and against the C++ policy on inlining of methods?
Answer :
14. Describe a situation where a C# struct is preferable to a C# class.
Answer :
15. Explain why naming encapsulations are important for developing large programs.
Answer :
CHAPTER 10
Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER10
- REVIEW QUESTIONS
11. What is an EP, and what is its purpose?
Answer :
12. How are references to variables represented in the static-chain method?
Answer :
It is represented by static depth.
13. Name three widely used programming languages that do not allow nested subprograms.
Answer :
14. What are the two potential problems with the static-chain method
Answer :
1. It is difficult for a programmer working on a time-critical program to estimate the costs of nonlocal references, because the cost of each reference depends on the depth of nesting between the reference and the scope of declaration.
2. Subsequent code modifications may change nesting depths, thereby changing the timing of some references, both in the changed code and possibly in code far from the changes.
15. Explain the two methods of implementing blocks.
Answer :
- Blocks can be implemented by using the static-chain process for implementing nested subprograms. Blocks are treated as parameterless subprograms that are always called from the same place in the program. Therefore, every block has an activation record. An instance of its activation record is created every time the block is executed.
- Blocks can also be implemented in a different and somewhat simpler and more efficient way. The maximum amount of storage required for block variables at any time during the execution of a program can be statically determined, because blocks are entered and exited in strictly textual order. This amount of space can be allocated after the local variables in the activation record. Offsets for all block variables can be statically computed, so block variables can addressed exactly as if they were local variables.
- PROBLEM SET
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 par- ent of a newly enacted procedure is found (see Section 10.4.2).
Answer : 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?
Answer : 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.
10. Design a skeletal program and a calling sequence that results in an acti- vation record instance in which the static and dynamic links point to dif- ferent activation-recorded instances in the run-time stack.
Answer :
>\verb+ + X : Integer;\\
\verb+ +procedure Bigsub is\\
\verb+ +\verb+ + A, B, C : Integer;\\
\verb+ +\verb+ + procedure Sub1 is\\
\verb+ +\verb+ +\verb+ + A, D : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub1\\
\verb+ +\verb+ +\verb+ + A := B + C; $\longleftarrow$ 1\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ + end; — of Sub1\\
\verb+ + procedure Sub2(X : Integer) is\\
\verb+ +\verb+ + B, E : Integer;\\
\verb+ +\verb+ + procedure Sub3 is\\
\verb+ +\verb+ +\verb+ + C, E : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub3\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + Sub1;\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + E := B + A; $\longleftarrow$ 2\\
\verb+ +\verb+ + end; — of Sub3\\
\verb+ +\verb+ + begin — of Sub2\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub3;\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + A := D + E; $\longleftarrow$ 3\\
\verb+ + end; — of Sub2\\
\verb+ + begin — of Bigsub\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub2(7);\\
\verb+ +\verb+ + …\\
\verb+ + end; — of Bigsub\\
begin — of Main\_2\\
\verb+ + …\\
\verb+ + Bigsub;\\
\verb+ + …\\
end; — of Main\_2\\
\\
The sequence of procedure calls is:\\
Main\_2 calls Bigsub\\
Bigsub calls Sub2\\
Sub2 calls Sub3\\
Sub3 calls Sub1\\
\\
The activation records with static and dynamic links is as follows:\\
\begin{figure}
\centering
\includegraphics[scale=0.5]{ari}
\end{figure}
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?
Answer : There are two options for implementing blocks as parameterless subprograms: One way is to use the same activation record as a subprogram that has no parameters. This is the most simple way, because accesses to block variables will be exactly like accesses to local variables. Of course, the space for the static and dynamic links and the return address will be wasted. The alternative is to leave out the static and dynamic links and the return address, which saves space but makes accesses to block variables different from subprogram locals.
12. Examine the subprogram call instructions of three different architec- tures, including at least one CISC machine and one RISC machine, and write a short comparison of their capabilities. (The design of these instructions usually determines at least part of the compiler writer’s design of subprogram linkage.)
Answer :
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER10
- REVIEW QUESTIONS
11. What is an EP, and what is its purpose?
Answer :
12. How are references to variables represented in the static-chain method?
Answer :
It is represented by static depth.
13. Name three widely used programming languages that do not allow nested subprograms.
Answer :
14. What are the two potential problems with the static-chain method
Answer :
1. It is difficult for a programmer working on a time-critical program to estimate the costs of nonlocal references, because the cost of each reference depends on the depth of nesting between the reference and the scope of declaration.
2. Subsequent code modifications may change nesting depths, thereby changing the timing of some references, both in the changed code and possibly in code far from the changes.
15. Explain the two methods of implementing blocks.
Answer :
- Blocks can be implemented by using the static-chain process for implementing nested subprograms. Blocks are treated as parameterless subprograms that are always called from the same place in the program. Therefore, every block has an activation record. An instance of its activation record is created every time the block is executed.
- Blocks can also be implemented in a different and somewhat simpler and more efficient way. The maximum amount of storage required for block variables at any time during the execution of a program can be statically determined, because blocks are entered and exited in strictly textual order. This amount of space can be allocated after the local variables in the activation record. Offsets for all block variables can be statically computed, so block variables can addressed exactly as if they were local variables.
- PROBLEM SET
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 par- ent of a newly enacted procedure is found (see Section 10.4.2).
Answer : 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?
Answer : 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.
10. Design a skeletal program and a calling sequence that results in an acti- vation record instance in which the static and dynamic links point to dif- ferent activation-recorded instances in the run-time stack.
Answer :
>\verb+ + X : Integer;\\
\verb+ +procedure Bigsub is\\
\verb+ +\verb+ + A, B, C : Integer;\\
\verb+ +\verb+ + procedure Sub1 is\\
\verb+ +\verb+ +\verb+ + A, D : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub1\\
\verb+ +\verb+ +\verb+ + A := B + C; $\longleftarrow$ 1\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ + end; — of Sub1\\
\verb+ + procedure Sub2(X : Integer) is\\
\verb+ +\verb+ + B, E : Integer;\\
\verb+ +\verb+ + procedure Sub3 is\\
\verb+ +\verb+ +\verb+ + C, E : Integer;\\
\verb+ +\verb+ +\verb+ + begin — of Sub3\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + Sub1;\\
\verb+ +\verb+ +\verb+ + …\\
\verb+ +\verb+ +\verb+ + E := B + A; $\longleftarrow$ 2\\
\verb+ +\verb+ + end; — of Sub3\\
\verb+ +\verb+ + begin — of Sub2\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub3;\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + A := D + E; $\longleftarrow$ 3\\
\verb+ + end; — of Sub2\\
\verb+ + begin — of Bigsub\\
\verb+ +\verb+ + …\\
\verb+ +\verb+ + Sub2(7);\\
\verb+ +\verb+ + …\\
\verb+ + end; — of Bigsub\\
begin — of Main\_2\\
\verb+ + …\\
\verb+ + Bigsub;\\
\verb+ + …\\
end; — of Main\_2\\
\\
The sequence of procedure calls is:\\
Main\_2 calls Bigsub\\
Bigsub calls Sub2\\
Sub2 calls Sub3\\
Sub3 calls Sub1\\
\\
The activation records with static and dynamic links is as follows:\\
\begin{figure}
\centering
\includegraphics[scale=0.5]{ari}
\end{figure}
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?
Answer : There are two options for implementing blocks as parameterless subprograms: One way is to use the same activation record as a subprogram that has no parameters. This is the most simple way, because accesses to block variables will be exactly like accesses to local variables. Of course, the space for the static and dynamic links and the return address will be wasted. The alternative is to leave out the static and dynamic links and the return address, which saves space but makes accesses to block variables different from subprogram locals.
12. Examine the subprogram call instructions of three different architec- tures, including at least one CISC machine and one RISC machine, and write a short comparison of their capabilities. (The design of these instructions usually determines at least part of the compiler writer’s design of subprogram linkage.)
Answer :
Rabu, 17 Desember 2014
CHAPTER 9
Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 9
- REVIEW QUESTIONS
11. What are the design issues for subprograms?
Answer :
The design issues for subprograms are:
- Are local variables statically or dynamically allocated?
- Can subprogram definitions appear in other 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 closures supported?
12. What are the advantages and disadvantages of dynamic local variables?
Answer :
There are several advantages of stack-dynamic local variables, the primary one being the flexibility they provide to the subprogram. It is essential that recursive subprograms have stack-dynamic local variables. Another advantage of stack-dynamic locals is that the storage for local variables in an active subprogram can be shared with the local variables in all inactive subprograms.
The main disadvantages of stack-dynamic local variables are the following:
First, there is the cost of the time required to allocate, initialize (when necessary), and deallocate such variables for each call to the subprogram. Second, accesses to stack-dynamic local variables must be indirect, whereas accesses to static variables can be direct. This indirectness is required because the place in the stack where a particular local variable will reside can be determined only during execution. Finally, when all local variables are stack dynamic, subprograms cannot be history sensitive; that is, they cannot retain data values of local variables between calls.
13. What are the advantages and disadvantages of static local variables?
Answer :
The primary advantage of static local variables over stack-dynamic local variables is that they are slightly more efficient—they require no run-time overhead for allocation and deallocation. Also, if accessed directly, these accesses are obviously more efficient. And, of course, they allow subprograms to be history sensitive. The greatest disadvantage of static local variables is their inability to support recursion. Also, their storage cannot be shared with the local variables of other inactive subprograms.
14. What languages allow subprogram definitions to be nested?
Answer :
For a long time, the only languages that allowed nested subprograms were those directly descending from Algol 60, which were Algol 68, Pascal, and Ada. JavaScript, Python, Ruby, and Lua are also, most functional programming languages allow subprograms to be nested.
15. What are the three semantic models of parameter passing?
Answer :
Formal parameters are characterized by one of three distinct semantics models:
(1) They can receive data from the corresponding actual parameter; (2) they can transmit data to the actual parameter; or (3) they can do both. These models are called in mode, out mode, and inout mode, respectively.
- PROBLEM SET
11. C# supports out-mode parameters, but neither Java nor C++ does. Explain the difference.
Answer :
12. Research Jensen’s Device, which was a widely known use of pass-by- name parameters, and write a short description of what it is and how it can be used.
Answer :
Implementing a pass-by-name parameter requires a subprogram to be passed to the called subprogram to evaluate the address or value of the formal parameter. The referencing environment of the passed subprogram must also be passed. This subprogram/referencing environment is a closure. Pass-by-name parameters are both complex to implement and inefficient. They also add significant complexity to the program, thereby lowering its readability and reliability. Because pass-by-name is not part of any widely used language, it is not discussed further here. However, it is used at compile time by the macros in assembly languages and for the generic parameters of the generic subprograms in C++, Java 5.0, and C# 2005.
13. Study the iterator mechanisms of Ruby and CLU and list their similari- ties and differences.
Answer :
14. Speculate on the issue of allowing nested subprograms in programming languages—why are they not allowed in many contemporary languages?
Answer :
Because it is concerned to lead to ambiguity and some error compilation.
15. What are at least two arguments against the use of pass-by-name parameters?
Answer :
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
About these ads
Share this:
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 9
- REVIEW QUESTIONS
11. What are the design issues for subprograms?
Answer :
The design issues for subprograms are:
- Are local variables statically or dynamically allocated?
- Can subprogram definitions appear in other 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 closures supported?
12. What are the advantages and disadvantages of dynamic local variables?
Answer :
There are several advantages of stack-dynamic local variables, the primary one being the flexibility they provide to the subprogram. It is essential that recursive subprograms have stack-dynamic local variables. Another advantage of stack-dynamic locals is that the storage for local variables in an active subprogram can be shared with the local variables in all inactive subprograms.
The main disadvantages of stack-dynamic local variables are the following:
First, there is the cost of the time required to allocate, initialize (when necessary), and deallocate such variables for each call to the subprogram. Second, accesses to stack-dynamic local variables must be indirect, whereas accesses to static variables can be direct. This indirectness is required because the place in the stack where a particular local variable will reside can be determined only during execution. Finally, when all local variables are stack dynamic, subprograms cannot be history sensitive; that is, they cannot retain data values of local variables between calls.
13. What are the advantages and disadvantages of static local variables?
Answer :
The primary advantage of static local variables over stack-dynamic local variables is that they are slightly more efficient—they require no run-time overhead for allocation and deallocation. Also, if accessed directly, these accesses are obviously more efficient. And, of course, they allow subprograms to be history sensitive. The greatest disadvantage of static local variables is their inability to support recursion. Also, their storage cannot be shared with the local variables of other inactive subprograms.
14. What languages allow subprogram definitions to be nested?
Answer :
For a long time, the only languages that allowed nested subprograms were those directly descending from Algol 60, which were Algol 68, Pascal, and Ada. JavaScript, Python, Ruby, and Lua are also, most functional programming languages allow subprograms to be nested.
15. What are the three semantic models of parameter passing?
Answer :
Formal parameters are characterized by one of three distinct semantics models:
(1) They can receive data from the corresponding actual parameter; (2) they can transmit data to the actual parameter; or (3) they can do both. These models are called in mode, out mode, and inout mode, respectively.
- PROBLEM SET
11. C# supports out-mode parameters, but neither Java nor C++ does. Explain the difference.
Answer :
12. Research Jensen’s Device, which was a widely known use of pass-by- name parameters, and write a short description of what it is and how it can be used.
Answer :
Implementing a pass-by-name parameter requires a subprogram to be passed to the called subprogram to evaluate the address or value of the formal parameter. The referencing environment of the passed subprogram must also be passed. This subprogram/referencing environment is a closure. Pass-by-name parameters are both complex to implement and inefficient. They also add significant complexity to the program, thereby lowering its readability and reliability. Because pass-by-name is not part of any widely used language, it is not discussed further here. However, it is used at compile time by the macros in assembly languages and for the generic parameters of the generic subprograms in C++, Java 5.0, and C# 2005.
13. Study the iterator mechanisms of Ruby and CLU and list their similari- ties and differences.
Answer :
14. Speculate on the issue of allowing nested subprograms in programming languages—why are they not allowed in many contemporary languages?
Answer :
Because it is concerned to lead to ambiguity and some error compilation.
15. What are at least two arguments against the use of pass-by-name parameters?
Answer :
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
About these ads
Share this:
CHAPTER 8
Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 8
- REVIEW QUESTION
11. What is unusual about C’s multiple-selection statement?
Answer :
the C switch statement has virtually no restrictions on the placement of the case expressions, which are treated as if they were normal statement labels. This laxness can result in highly complex structure eithin the switch body.
12. On what previous language was C’s switch statement based?
Answer :
ALGOL
13. Explain how C#’s switch statement is safer than that of C.
Answer :
C# has a static semantics rule that disallows the implicit execution of more than one segment. Every segment must end with an explicit unconditional branch statement which transfer control out of the switch statement, or a goto, which can transfer control to one of the selectable segments
14. What are the design issues for all iterative control statements?
Answer :
The design issues are how the iteration is controlled and where the control mechanism should appear in the loop statement.
15. What are the design issues for counter-controlled loop statements?
Answer :
The design issues for counter-controlled loop statement are what the type and scope of the loop variable are, whether it should be legal for the loop variable or loop parameters to be changed in the loop and if so, whether the change affects loop control or not, and if the lop parameters should be evaluated only once or once for every iteration.
- PROBLEM SET
10. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selec- tor. If there is no default, an unrepresented value causes the whole statement to be skipped. What are the pros and cons of these two designs (Ada and C++)?
Answer :
Ada was designed for military grade software development. The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a "default" is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default.
11. Explain the advantages and disadvantages of the Java for statement, compared to Ada’s for.
Answer :
Java’s variable in the argument of a switch statement can be of integeral type (byte, short, int, etc), char, and String (JDK 1.7 and newer versions), but C++ can only be int or char.
12. Describe a programming situation in which the else clause in Python’s for statement would be convenient.
Answer :
13. Describe three specific programming situations that require a posttest loop.
Answer :
14. Speculate as to the reason control can be transferred into a C loop statement.
Answer :
goto statements can be used to have one cleanup sction in routine rather than multiple return statements, or used to exit out a nested loop that is very long
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 8
- REVIEW QUESTION
11. What is unusual about C’s multiple-selection statement?
Answer :
the C switch statement has virtually no restrictions on the placement of the case expressions, which are treated as if they were normal statement labels. This laxness can result in highly complex structure eithin the switch body.
12. On what previous language was C’s switch statement based?
Answer :
ALGOL
13. Explain how C#’s switch statement is safer than that of C.
Answer :
C# has a static semantics rule that disallows the implicit execution of more than one segment. Every segment must end with an explicit unconditional branch statement which transfer control out of the switch statement, or a goto, which can transfer control to one of the selectable segments
14. What are the design issues for all iterative control statements?
Answer :
The design issues are how the iteration is controlled and where the control mechanism should appear in the loop statement.
15. What are the design issues for counter-controlled loop statements?
Answer :
The design issues for counter-controlled loop statement are what the type and scope of the loop variable are, whether it should be legal for the loop variable or loop parameters to be changed in the loop and if so, whether the change affects loop control or not, and if the lop parameters should be evaluated only once or once for every iteration.
- PROBLEM SET
10. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selec- tor. If there is no default, an unrepresented value causes the whole statement to be skipped. What are the pros and cons of these two designs (Ada and C++)?
Answer :
Ada was designed for military grade software development. The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a "default" is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default.
11. Explain the advantages and disadvantages of the Java for statement, compared to Ada’s for.
Answer :
Java’s variable in the argument of a switch statement can be of integeral type (byte, short, int, etc), char, and String (JDK 1.7 and newer versions), but C++ can only be int or char.
12. Describe a programming situation in which the else clause in Python’s for statement would be convenient.
Answer :
13. Describe three specific programming situations that require a posttest loop.
Answer :
14. Speculate as to the reason control can be transferred into a C loop statement.
Answer :
goto statements can be used to have one cleanup sction in routine rather than multiple return statements, or used to exit out a nested loop that is very long
Rabu, 26 November 2014
CHAPTER 7
Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 7
- REVIEW QUESTION
11. What is an overloaded operator?
Answer :
Operator that has different implementation depending on its arguments.
12. Define narrowing and widening conversions.
Answer :
Widening conversion is a conversion that converts a value to a type that can include least approximations of all values of the original type. Narrowing conversion is a conversion of data that might cause a loss of precision
13. In JavaScript, what is the difference between == and ===?
Answer :
“==” operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It’s much similar Java’s equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other.
14. What is a mixed-mode expression?
Answer :
Mixed-mode expression: The expressions that allow them to design decisions concerning arithmetic expressions is whether an operator can have operands of different types.
15. What is referential transparency?
Answer :
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
- PROBLEM SET
11. Write a BNF description of the precedence and associativity rules defined for the expressions in Problem 9. Assume the only operands are the names a,b,c,d, and e.
Answer :
<expr> → <expr> or <e1> | <expr> xor <e1> | <e1>
<e1> → <e1> and <e2> | <e2>
<e2> → <e2> = <e3> | <e2> /= <e3> | <e2> < <e3>
| <e2> <= <e3> | <e2> > <e3> | <e2> >= <e3> | <e3>
<e3> → <e4>
<e4> → <e4> + <e5> | <e4> – <e5> | <e4> & <e5> | <e4> mod <e5> | <e5>
<e5> → <e5> * <e6> | <e5> / <e6> | not <e5> | <e6>
<e6> → a | b | c | d | e | const | ( <expr> )
12. Using the grammar of Problem 11, draw parse trees for the expressions of Problem 9.
Answer :
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 expressions are evaluated left to right?
b. if the operands in the expressions are evaluated right to left?
Answer :
(a) (left -> right)
sum 1 = 46
sum 2 = 48
(b) (right -> left)
sum 1 = 48
sum 2 = 46
14. What is your primary argument against (or for) the operator precedence rules of APL?
Answer :
The operator precedence rules of the common imperative languages are nearly all the same, because they are based on those of mathematics.
15. Explain why it is difficult to eliminate functional side effects in C.
Answer :
One reason functional side effects would be difficult to remove from C is that all of C’s subprograms are functions, providing the ability of returning only a single data value.
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 7
- REVIEW QUESTION
11. What is an overloaded operator?
Answer :
Operator that has different implementation depending on its arguments.
12. Define narrowing and widening conversions.
Answer :
Widening conversion is a conversion that converts a value to a type that can include least approximations of all values of the original type. Narrowing conversion is a conversion of data that might cause a loss of precision
13. In JavaScript, what is the difference between == and ===?
Answer :
“==” operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It’s much similar Java’s equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other.
14. What is a mixed-mode expression?
Answer :
Mixed-mode expression: The expressions that allow them to design decisions concerning arithmetic expressions is whether an operator can have operands of different types.
15. What is referential transparency?
Answer :
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
- PROBLEM SET
11. Write a BNF description of the precedence and associativity rules defined for the expressions in Problem 9. Assume the only operands are the names a,b,c,d, and e.
Answer :
<expr> → <expr> or <e1> | <expr> xor <e1> | <e1>
<e1> → <e1> and <e2> | <e2>
<e2> → <e2> = <e3> | <e2> /= <e3> | <e2> < <e3>
| <e2> <= <e3> | <e2> > <e3> | <e2> >= <e3> | <e3>
<e3> → <e4>
<e4> → <e4> + <e5> | <e4> – <e5> | <e4> & <e5> | <e4> mod <e5> | <e5>
<e5> → <e5> * <e6> | <e5> / <e6> | not <e5> | <e6>
<e6> → a | b | c | d | e | const | ( <expr> )
12. Using the grammar of Problem 11, draw parse trees for the expressions of Problem 9.
Answer :
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 expressions are evaluated left to right?
b. if the operands in the expressions are evaluated right to left?
Answer :
(a) (left -> right)
sum 1 = 46
sum 2 = 48
(b) (right -> left)
sum 1 = 48
sum 2 = 46
14. What is your primary argument against (or for) the operator precedence rules of APL?
Answer :
The operator precedence rules of the common imperative languages are nearly all the same, because they are based on those of mathematics.
15. Explain why it is difficult to eliminate functional side effects in C.
Answer :
One reason functional side effects would be difficult to remove from C is that all of C’s subprograms are functions, providing the ability of returning only a single data value.
Rabu, 05 November 2014
CHAPTER 6
Nama : Aditya Isnugraha
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 6
- REVIEW QUESTION
10. What happens when a nonexistent element of an array is referenced in Perl?
Answer :
If you try to append non-existent elements from an array to another one, the initial array will grow as needed, even though the elements to append do not exist.
11. How does JavaScript support sparse arrays?
Answer :
JavaScript objects are sparse, and arrays are just specialized objects with an auto-maintained length property (which is actually one larger than the largest index, not the number of defined elements) and some additional methods.
12. What languages support negative subscripts?
Answer : Ruby and Lua support negative subscripts.
13. What languages support array slices with Stepsizes?
Answer : Ruby, Python, Perl.
14. What array initialization feature is available in Ada that is not available in other common imperative languages?
Answer :
Ada provides two mechanisms for initializing arrays in the declarations statements: by listing them in the order in which they are to be stored, or by directly assigning them to an index position using the => operator, which in Ada is called an arrow.
15. What is an aggregate constant?
Answer : A parenthesized lists of values.
- PROBLEM SET
11. In the Burroughs Extended ALGOL language, matrices are stored as a single-dimensioned array of pointers to the rows of the matrix, which are treated as single-dimensioned arrays of values. What are the advantages and disadvantages of such a scheme?
Answer :
The advantage of this scheme is that accesses that are done in order of the rows can be made very fast; once the pointer to a row is gotten, all of the elements of the row can be fetched very quickly. If, however, the elements of a matrix must be accessed in column order, these accesses will be much slower; every access requires the fetch of a row pointer and an address computation from there. Note that this access technique was devised to allow multidimensional array rows to be segments in a virtual storage management technique. Using this method, multidimensional arrays could be stored and manipulated that are much larger than the physical memory of the computer.
12. Analyze and write a comparison of C’s malloc and free functions with C++’s new and delete operators. Use safety as the primary consider- ation in the comparison.
Answer : new and delete are type safe (no need for casts), malloc and free are not. Also malloc returns a void* which then has to be cast to the appropriate pointer type. new returns the correct pointer type itself; type safety. malloc requires you to tell the number of bytes to allocate, new figures it out itself.
13. Analyze and write a comparison of using C++ pointers and Java reference variables to refer to fixed heap-dynamic variables. Use safety and conve- nience as the primary considerations in the comparison.
Answer :
-In c and C++ pointer can be use the same way as addresses. This design offer
no solutions to the dangling pointer or lost heap-dynamic variable problems.
-Reference types, such as those in Java and C#, provide heap management
without the dangers of pointers.
so it's clear that Java has more safety for the heap-dynamic variables.
14. Write a short discussion of what was lost and what was gained in Java’s designers’ decision to not include the pointers of C++.
Answer :
There's a copious amount of documentation out there on this subject so there's no reason to write at length. I should also point out that pointers are a feature of both C and C++.
It's obvious that preventing the use of pointers significantly bolstered the amount of security that programmers could get 'for free' given that issues like stack overruns, memory corruption and inadequate/incorrect freeing of memory (though this is more an attribute of GC than lack of pointers) were largely nullified.
Given that programmers tend to be kind of ornery and resistant to change, preventing a means for direct memory access and easy manipulation of the data within rubbed a lot of programmers the wrong way. Many pointed out that the use of pointers was one of the ways that C/C++ were able to be such a high-level language while still maintaining considerable speed. While assembly language and machine code were still de rigeur amongst people that were looking to squeeze the most amount of speed and memory out of a given program, things like pointers and compilers that effectively optimized C and C++ code did a lot to win people over, not to mention that it's a hell of a lot easier to read.
With Java, however, a lot of those same programmers felt that the security that was gained was offset by the decreases in speed and the fact that a lot of the earlier JIT compilers were pretty pokey didn't help that. For all their faults, Sun's been pretty adamant about listening to the community so they've made a lot of improvements regarding in-place optimization, HotSpot, conditional compilation, etc.
Another problem that came about is the fact that removing pointers made existing C and C++ code hard to port. That's generally pretty true of any new or significantly changed language, however. Eventually people got a handle on how to best replicate pointer usage, tips and tricks got passed around and things improved..
15. What are the arguments for and against Java’s implicit heap stor- age recovery, when compared with the explicit heap storage recovery required in C++? Consider real-time systems.
Answer :
Implicit eliminates the creation of dangling pointers. Disadv: cpu-time to do recovery, sometimes when there’s plenty of heap storage so recovery isn’t necessary.
Class : LM01
NIM : 1801419606
Assignment from Tri Djoko Wahjono
CHAPTER 6
- REVIEW QUESTION
10. What happens when a nonexistent element of an array is referenced in Perl?
Answer :
If you try to append non-existent elements from an array to another one, the initial array will grow as needed, even though the elements to append do not exist.
11. How does JavaScript support sparse arrays?
Answer :
JavaScript objects are sparse, and arrays are just specialized objects with an auto-maintained length property (which is actually one larger than the largest index, not the number of defined elements) and some additional methods.
12. What languages support negative subscripts?
Answer : Ruby and Lua support negative subscripts.
13. What languages support array slices with Stepsizes?
Answer : Ruby, Python, Perl.
14. What array initialization feature is available in Ada that is not available in other common imperative languages?
Answer :
Ada provides two mechanisms for initializing arrays in the declarations statements: by listing them in the order in which they are to be stored, or by directly assigning them to an index position using the => operator, which in Ada is called an arrow.
15. What is an aggregate constant?
Answer : A parenthesized lists of values.
- PROBLEM SET
11. In the Burroughs Extended ALGOL language, matrices are stored as a single-dimensioned array of pointers to the rows of the matrix, which are treated as single-dimensioned arrays of values. What are the advantages and disadvantages of such a scheme?
Answer :
The advantage of this scheme is that accesses that are done in order of the rows can be made very fast; once the pointer to a row is gotten, all of the elements of the row can be fetched very quickly. If, however, the elements of a matrix must be accessed in column order, these accesses will be much slower; every access requires the fetch of a row pointer and an address computation from there. Note that this access technique was devised to allow multidimensional array rows to be segments in a virtual storage management technique. Using this method, multidimensional arrays could be stored and manipulated that are much larger than the physical memory of the computer.
12. Analyze and write a comparison of C’s malloc and free functions with C++’s new and delete operators. Use safety as the primary consider- ation in the comparison.
Answer : new and delete are type safe (no need for casts), malloc and free are not. Also malloc returns a void* which then has to be cast to the appropriate pointer type. new returns the correct pointer type itself; type safety. malloc requires you to tell the number of bytes to allocate, new figures it out itself.
13. Analyze and write a comparison of using C++ pointers and Java reference variables to refer to fixed heap-dynamic variables. Use safety and conve- nience as the primary considerations in the comparison.
Answer :
-In c and C++ pointer can be use the same way as addresses. This design offer
no solutions to the dangling pointer or lost heap-dynamic variable problems.
-Reference types, such as those in Java and C#, provide heap management
without the dangers of pointers.
so it's clear that Java has more safety for the heap-dynamic variables.
14. Write a short discussion of what was lost and what was gained in Java’s designers’ decision to not include the pointers of C++.
Answer :
There's a copious amount of documentation out there on this subject so there's no reason to write at length. I should also point out that pointers are a feature of both C and C++.
It's obvious that preventing the use of pointers significantly bolstered the amount of security that programmers could get 'for free' given that issues like stack overruns, memory corruption and inadequate/incorrect freeing of memory (though this is more an attribute of GC than lack of pointers) were largely nullified.
Given that programmers tend to be kind of ornery and resistant to change, preventing a means for direct memory access and easy manipulation of the data within rubbed a lot of programmers the wrong way. Many pointed out that the use of pointers was one of the ways that C/C++ were able to be such a high-level language while still maintaining considerable speed. While assembly language and machine code were still de rigeur amongst people that were looking to squeeze the most amount of speed and memory out of a given program, things like pointers and compilers that effectively optimized C and C++ code did a lot to win people over, not to mention that it's a hell of a lot easier to read.
With Java, however, a lot of those same programmers felt that the security that was gained was offset by the decreases in speed and the fact that a lot of the earlier JIT compilers were pretty pokey didn't help that. For all their faults, Sun's been pretty adamant about listening to the community so they've made a lot of improvements regarding in-place optimization, HotSpot, conditional compilation, etc.
Another problem that came about is the fact that removing pointers made existing C and C++ code hard to port. That's generally pretty true of any new or significantly changed language, however. Eventually people got a handle on how to best replicate pointer usage, tips and tricks got passed around and things improved..
15. What are the arguments for and against Java’s implicit heap stor- age recovery, when compared with the explicit heap storage recovery required in C++? Consider real-time systems.
Answer :
Implicit eliminates the creation of dangling pointers. Disadv: cpu-time to do recovery, sometimes when there’s plenty of heap storage so recovery isn’t necessary.
Langganan:
Postingan (Atom)