Selamat darang di blog saya ini. Blog ini ditulis atas permintaan dosen terkeren saya Tri Djoko Wahjono, yang mengajar mata kuliah Konsep Bahasa Pemrograman di Binus University di kampus yang keren dan eksklusif ini Alam Sutera (banyak kupu-kupu berterbangan, banyak burung gereja minta makan, #nofilter) – Jakarta.
Chap 6:
Review Questions
- What are the advantages of user-defined enumeration types?
*User-defined enumeration types are most useful when a data type can take one of a small, discrete set of values, each of which have some meaning that is not a number. A favorite example in textbooks is a type for the suit of a playing card. There are four options: SPADE, CLUB, HEART, DIAMOND, and so we would make an enumeration type with these four entries. The main advantages are efficiency in storage (compared to e.g. storing these values as strings) and more readable code.
- In what ways are the user-defined enumeration types of C# more reliable than those of C++?
*Since C# enumeration types are not coerced into integer types, as the C++ does so, it’s more reliable.
- What are the design issues for arrays?
*-What types are legal for subscripts?
-Are subscripting expressions in elemnet refrence range checked?
-When are subscript ranges bound?
-When does allocation take place?
-What is the maximum number of subscripts?
-Can array objects be initialized?
-Are any kind of slices supported?
- Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic, and heap-dynamic arrays. What are the advantages of each?
*Static array is an array which the subscript ranges are statically bound and storage allocation is static (before run-time). The advantage is efficiency (no dynamic allocation).
Fixed stack-dynamic array is an array which the subscript ranges are statically bound, but the allocation is done at declaration time. The advantage is space efficiency
Stack-dynamic array is an array which the subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time). The advantage is flexibility (the size of an array need not be known until the array is to be used).
Fixed heap-dynamic array is similar to fixed stack-dynamic, storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack). The advantage of fixed heap-dynamic array is flexibility (the array’s size always fits the problem).
Heap-dynamic array is an array which the binding of subscript ranges and storage allocation is dynamic and can change any number of times. The advantage is flexibility (arrays can grow or shrink during program execution).
- What happens when a nonexistent element of an array is referenced in Perl?
*If an r-value is required, undef is returned. If an l-value is required, the array is extended, then the newly created but undefined element is returned. No error is reported.
Problem Set
- Explain all of the differences between Ada’s subtypes and derived types.
*Ada’s subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. While Ada’s derived type is a completely separate type that has the same characteristics as its base type. We can’t mix operands of a derived type with operands of the base type.
- What significant justification is there for the -> operator in C and C++?
*The only justification for -> operator in C and C++ is writability. It is slightly easier to write p->q than (*p).q.
- What are all of the differences between the enumeration types of C++ and those of Java?
*In C++, an enumeration is just a set of named, integral constants. Also, C++ will implicitly convert enum values to their integral equivalent. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration. Java will explicitly convert enum values to their integral equivalent.
- The unions in C and C++ are separate from the records of those languages, rather than combined as they are in Ada. What are the advantages and disadvantages to these two choices?
*
Union separated from records | Union combined with records | |
Advantages | -Reduce developer overhead (less checking)
-Goes with the C++ design (C++ is not a weak strong typing language) |
-Support type checking
-Goes with the Ada design (Ada is strong typing language) |
Disadvantages | -No type checking of references
-May end with wrong or unexpected results |
-Increase developer overhead
-Using more functions to resolve type compatibility |
- Multidimensional arrays can be stored in row major order, as in C++, or in column major order, as in Fortran. Develop the access functions for both of these arrangements for three-dimensional arrays.
*Let the subscript ranges of the three dimensions be named min(1), min(2), min(3), max(1), max(2), and max(3). Let the sizes of the subscript ranges be size(1), size(2), and size(3). Assume the element size is 1.
Row Major Order:
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((i-min(1))*size(3) + (j-min(2)))*size(2) + (k-min(3))
Column Major Order:
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) + ((k-min(3))*size(1) + (j-min(2)))*size(2) + (i-min(1))
Chapter 7:
Review Questions
- What associativity rules are used by APL?
*In APL, all operators have the same level of precedence. Thus, the order of evaluation of operators in APL expressions is determined entirely by the associativity rule, which is right to left for all operators.
- What is the difference between the way operators are implemented in C++ and Ruby?
*The difference is in Ruby, all the arithmetic, relational, and assignment operator, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods. While in C, they aren’t implemented as methods.
- Define functional side effect.
*A functional side effect is a side effect of a function which occurs when the function changes either one of its parameters or a global variable.
- What is a coercion?
*A coercion is a process by which a compiler automatically converts a value of one type into a value of another type when that second type is required by the surrounding context.
- What is a conditional expression?
*A conditional expression is an expression that returns value A or value B depending on whether a Boolean value is true or false. A conditional expression lets you write a single assignment statement that has the same effect as the following:
if condition:
x = true_value
else:
x = false_value
Problem Set
- Should C’s single-operand assignment forms (for example, ++count) be included in other languages (that do not already have them)? Why or why not?
*Yes C should, because those assigning operations make operation simpler and easy to know if that operation is assigning operation. And it will ease the increment or even decrement while we use in looping.
- Describe a situation in which the add operator in a programming language would not be commutative.
*If the add operator for a language is also used to concatenate strings, it’s quite apparent that it would not be commutative.
For example:
“abc” + “def” = “abcdef”
“def” + “abc” = “defabc”
These two strings are obviously not equal, so the addition operator is not commutative.
- Describe a situation in which the add operator in a programming language would not be associative.
*It is not associative when it includes the other operator with higher precedence like the multiplication and division.
- Assume the following rules of associativity and precedence for expressions:
Precedence
Highest *, /, not
+, –, &, mod
– (unary)
=, /=, <, <=, >=, >
And
Lowest or, xor
Associativity Left to right
Show the order of evaluation of the following expressions by parenthesizing all subexpressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression
a + b * c + d
the order of evaluation would be represented as
((a + (b * c)1)2 + d)3
- a * b – 1 + c ((( a * b )1 – 1)2 + c )3
- a * (b – 1) / c mod d ((( a * ( b – 1 )1 )2 / c )3 mod d )4
- (a – b) / c & (d * e / a – 3) (((a – b)1 / c)2 & ((d * e)3 / a)4 – 3)5)6
- -a or c = d and e (( -a )1 or ( ( c = d )2 and e )3 )4
- a > b xor c or d <= 17 (((a > b)1 xor c)3 or (d <= 17)2 )4
- -a + b (–( a + b )1 )2
- Show the order of evaluation of the expressions of Problem 9, assuming that there are no precedence rules and all operators associate right to left.
*
- ( a * ( b – ( 1 + c )1 )2 )3
- ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
- ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
- ( – ( a or ( c = ( d and e )1 )2 )3 )4
- ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
f. ( – ( a + b )1 )2
Chapter 8:
Review Questions
- What is unusual about Python’s design of compound statements?
*Python uses indentation to specify compound statements. For example,
if x > y :
x = y
print “case 1″
equally indent statements are grouped as one compound statement.
- Under what circumstances must an F# selector have an else clause?
*If the expression returns a value, it must have an else clause.
- What are the common solutions to the nesting problem for two-way selectors?
*The common solution is to force an alternative semantics, by using compound statements.
- What are the design issues for multiple-selection statements?
*-What is the form and type of the control statement?
-How are the selectable segments specified?
-Is execution flow through the structure restricted to include just a single selectable segment?
-How are case values specified?
-What is done about unrepresented expression values?
- Between what two language characteristics is a trade-off made when deciding whether more than one selectable segment is executed in one execution of a multiple selection statement?
*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 selector. If there is no default, an unrepresented value causes the whole statement to be skipped.
Problem Set
- Analyze the potential readability problems with using closure reserved words for control statements that are the reverse of the corresponding initial reserved words, such as the case-esac reserved words of ALGOL 68. For example, consider common typing errors such as transposing characters.
*The potential readability problem is the typing errors. It’s very possible to occur if we don’t type the code carefully.
- Use the Science Citation Index to find an article that refers to Knuth (1974). Read the article and Knuth’s paper and write a paper that summarizes both sides of the goto issue.
*An alternative viewpoint is presented in Donald Knuth’s Structured Programming with go to Statements, which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.[7] In their quasi-standard book on the C programming language, Dennis Ritchie and Brian Kernighan warn that goto is “infinitely abusable”, but also suggest that it could be used for end-of-function error handlers and for multi-level breaks from loops.
- In his paper on the goto issue, Knuth (1974) suggests a loop control statement that allows multiple exits. Read the paper and write an operational semantics description of the statement.
*Operational semantics are a category of formal programming language semantics in which certain desired properties of a program, such as correctness, safety or security, are verified by constructing proofs from logical statements about its execution and procedures, rather than by attaching mathematical meanings to its terms (denotational semantics).
- What are the arguments both for and against the exclusive use of Boolean expressions in the control statements in Java (as opposed to also allowing arithmetic expressions, as in C++)?
*The primary argument for using Boolean expressions exclusively as control expressions is the reliability that results from disallowing a wide range of types for this use. In C, for example, an expression of any type can appear as a control expression, so typing errors that result in references to variables of incorrect types are not detected by the compiler as errors. No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.
- 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 selector. 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++)?
*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.
Chapter 9:
6.What is a Ruby array formal parameter?
There are two distinct categories of subprograms—procedures and functions— both of which can be viewed as approaches to extending the language. All sub- programs are collections of statements that define parameterized computations. Functions return values and procedures do not. Procedures can produce results in the calling program unit by two methods: (1) If there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them; and (2) if the procedure has formal parameters that allow the transfer of data to the caller, those parameters can be changed. However Functions are called by appearances of their names in expressions, along with the required actual parameters. And functions define new user-defined operators.
Problem Sets :
7. Consider the following program written in C syntax:
void fun (int first, int second) {
first += first;
second += second;
}
void main() {
int list[2] = {1, 3};
fun(list[0], list[1]);
}
For each of the following parameter-passing methods, what are the values of the list array after execution?
Passed by value
Passed by reference
Passed by value-result
Passed by value : list[2] = { 3 , 5 }
Passed by reference : list[2] = { 6 , 10 }
Passed by value-result : list[2] = { 6 , 10 }
6. What is the difference between an activation record and an activation record instance?
Static depth is depth of the nesting for each enclosing static scope.
Nesting depth is the difference between the static depth of the reference and that of the scope where it was declared.
Chain offset is same as nesting depth.
Problem Set :
\emph{Answer}:\\
procedure Main\_2 is\\
\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}At position 1 in procedure Sub1, the reference is to the local variable,
A, not to the nonlocal variable A from Bigsub. This reference to A has the
chain\_offset/local\_offset pair (0, 3). The reference to B is to the nonlocal B
from Bigsub. It can be represented by the pair (1, 4). The local\_offset is 4,
because a 3 offset would be the first local variable (Bigsub has no parameters). Notice that if the dynamic link were used to do a simple search for
an activation record instance with a declaration for the variable B, it would
find the variable B declared in Sub2, which would be incorrect. If the (1, 4)
pair were used with the dynamic chain, the variable E from Sub3 would be
used. The static link, however, points to the activation record for Bigsub,
which has the correct version of B . The variable B in Sub2 is not in the
referencing environment at this point and is (correctly) not accessible. The
reference to C at point 1 is to the C defined in Bigsub, which is represented
by the pair (1, 5).\\
\\
\noindent
Review Questions :
6. Explain how information hiding is provided in an Ada package.
There are two approaches to hiding the representation from clients in the package specification. The first one is to include two sections in the package specification, the second one is in which entities are visible to clients and one that hides its contents.
7. To what is the private part of an Ada package specification visible?
6. Discuss the advantages of C# properties, relative to writing accessor methods in C++ or Java.
One of the advantage of C# properties relative writing accessor methods in C++ or Java is it can control access to the fields.
7. Explain the dangers of C’s approach to encapsulation.
Review Questions :
6. Describe a situation where dynamic binding is a great advantage over its absence.
-Are Subclasses Subtypes? If so, derived objects can be legally used wherever a parent object could be used.
-Type Checking and Polymorphism
-Single and Multiple Inheritance. Inherit from 1 (or more than 1) parent.
-Object Allocation and Deallocation. Are objects allocated from heap or stack.
-Dynamic and Static Binding. When are messages bound to methods, before or during run-time?
-Nested Classes. Can a class be nested inside another class?
-Initialization of Objects. Are objs init’d when created? Implicit or explicit?10. What is a nesting class?
6. Compare the multiple inheritance of C++ with that provided by interfaces in Java.
The problems solved are reusability of code and “extensibility”. Reusability because one won’t have to copy/paste his code from one data type to another, allowing for a greater readability. Extensibility because a method can accept a certain class as an argument, and get a child class of this one. This will allow the user to have a wider set of functionality, but the method will still be able to know that the entities it relies on are present.
9. Describe the categories of changes that a subclass can make to its parent class.
Subclasses can add things (variables, methods). Subclass in C++ can effectively remove a method using “private” inheritance. Inherited methods can be overridden.
10. Explain one disadvantage of inheritance.
Language & implementation complexity. The shared inheritance problem of multiple inheritance. Subclass is dependent upon its base class (which might change over time).