Tuesday, April 9, 2013

Concepts of Programming Languages ---- Chapter 7 Expressions and Assignment Statements

Concepts of Programming Languages ---- Chapter 7 Expressions and Assignment Statements

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


Review Questions
1.      What is a coercion?
Coercion is defined as an implicit type conversion that initiated by the compiler.

2.      What is an overloaded operator?
Overloaded operator is arithmetic operators which used for more than one purpose.

3.      Define narrowing and widening conversions.
A narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type.
Widening conversion converts a value to a type that can include at least approximations of all of the values of the original type.

4.      In JavaScript, what is the difference between == and ===?
== is used for equivalence and === is used to prevent their operands from being coerced and similar to their relatives == and !=.

5.      What is a mixed-mode expression?
One of the design decisions concerning arithmetic expressions is whether an operator can have operands of different types. Languages that allow such expressional is called mixed-mode expression

6.      What is referential transparency?
The concept of referential transparency is related to and affected by functional side effects.

7.      What are the advantages of referential transparency?
The most important of these is that the semantics programs is much easier to understand than the semantics of programs that are not referentially transparent

8.      What is short-circuit evaluation?
A short-circuit evaluation of an expression in one in which the result is determined without evaluating all of the operands and /or operantors.

9.      What is the purpose of a compound assignment operator?
A compound assignment operator is a shorthand method of specifying a commonly needed form of assignment.


10.  What two languages include multiple assignments?
Perl and Ruby.

Problem Set
1.      Assume the following rules of associativity and precedence for expressions:
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.   a * b - 1 + c  à (((a * b)1  - 1)2  + c)3
B.   a * (b - 1) / c mod d à (((a * (b - 1)1 )2  / c)3  mod d)4
            C.  (a - b) / c & (d * e / a - 3) à ( ( ( a - b )1 / c )2 & ( ( ( d * e )3 / a )4 - 3 )5 )6
D.  –a or c =d and e à ( ( ( - a )1 or ( c = d )2 )3 and e )4
E.  a > b xor c or d <= 17 à ( ( a > b )1 xor ( c or ( d <= 17 )2 )3 )4
F.  –a + b à ( - ( a + b )1 )2
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.    a * b - 1 + c  à ( a * ( b - ( 1 + c )1 )2 )3
B.    a * (b - 1) / c mod d à ( a * ( ( b - 1 )2 / ( c mod d )1 )3 )4
C.    (a - b) / c & (d * e / a - 3) à ( ( a - b )5 / ( c & ( d * ( e / ( a - 3 )1 )2 )3 )4 )6
D.    –a or c =d and e à ( - ( a or ( c = ( d and e )1 )2 )3 )4
E.    a > b xor c or d <= 17 à ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
F.    –a + b à ( - ( a + b )! )2

3.      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)+ (i/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?

a. sum1 is 46, sum2 is 48.
b. sum1 is 48, sum2 is 46.

4.      Explain why it is difficult to eliminate functional side effects in C.
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 (though it could be an array). The problem is that in many cases it is necessary (or at least convenient) to return more than one data value, which is done through the use of pointer actual parameters, which are a means of creating functional side effects.

5.      Consider the following C program:
int fun(int *i)
{
            *I +=5;
            return 4;
}
void main()
{
            int x = 3;
            x = x + fun(&x);
}
What is the value of x after the assignment statement in main, assuming
a. operands are evaluated left to right
b. operands are evaluated right to left

a. 7
b. 12

Concepts of Programming Languages ---- Chapter 6 Data Types

Concepts of Programming Languages ---- Chapter 6 Data Types

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


Review Questions
1.      What is a descriptor?
A descriptor is the collection of the attributes of a variable.
2.      What are the design issues for character string types?
-          Should strings be simply a special kind of character array or a primitive type?
-          Should strings have static or dynamic length?

3.      Describe the three string length options.
-          Static length string : the length can be static and set when the string is created
-          Limited Dynamic length strings : allow strings to have varying length up to a declared and fixed maximum set by the variable’s definition.
-          Dynamic lingth strings : allow strings to have varying length with no maximum.

4.      What are the design issues for arrays?
-          What types are legal for subscripts?
-          Are subscripting expressions in element references range checked?
-          When are subscript ranges bound?
-          When does array allocation take place?
-          Are ragged or rectangular multidimensioned arrays allowed or both?
-          Can arrays be initialized when they have their storage allocated?
-          What kinds of slices are allowed, if any?

5.       Define static, fixed stack-dynamic, stack-dynamic, fixed heap-dynamic, and heap-dynamic arrays. What are the advantages of each?
-          Static array : the subscript ranges are statically bound and storage allocation is static.
Advantage : efficiency(no dynamic allocation and deallocation is required).
-          Fixed stack-dynamic : the subscript ranges are statically bound, but the allocation is done at declaration elaboration time during execution
Advantage : space efficiency.
-          Stack dynamic array : both the subscript ranges and the storage allocation are dynamically bound at elaboration time.
Advantage : flexibility. (the size of an array need not be known until the array is to be used).
-          Fixed heap dynamic array : the subscript ranges and the storage binding are both fixed after storage is allocated.
Advantage : flexibility(the array’s size always fits the problem).
-          Heap dynamic array : the binding of subscript ranges and storage allocation is dynamic and can change an number of times during the array’s lifetime,
Advantage : flexibility (arrays can grow or shrink during program execution).

6.      What happens when a nonexistent element of an array is referenced in Perl?
When a nonexistent element of an array is referenced in perl, an array can be made to shrink to no elements by assigning it the empty list. So, the length of an array is defined to be the largest subscript plus one.

7.       What languages support array slices with stepsizes? 
-          Ruby 
-          Python
-          Perl

8.      What array initialization feature is available in Ada that is not available in other common imperative languages?
 array initialization feature is available in Ada is by directly assigning them to an index position using the => operator, which in Ada is called an arrow.

9.      What is an aggregate constant
Aggregate constant is the parenthesized list of constant value.

10.  What is the structure of an associative array?
An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys. User-defined keys must be stored in structure.

11.  What is the purpose of level numbers in COBOL records?
the purpose of level numbers in COBOL records is to divide of a COBOL program.
The example:
01                EMPLOYEE-RECORD
02        EMPLOYEE-NAME
            05 FIRST
            05 LAST
02        HOURLY-RATE

12.  Define fully qualified and elliptical references to fields in records.
-          Fully qualified references must include all record names.
-          Elliptical references allow leaving out record names as long as thereference is unambiguous.

13.  What the two common problems with pointers?
Dangling pointers (dangerous) and Lost heap-dynamic variable.
14.  What is a C++ reference type, and what is its common use?
C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters.

Problem set
1.      What are the arguments for and against four signed integer sizes in Java?

bytes (1 byte), short(2 bytes), integer (4 bytes), long (8 bytes). As a result,
depending on the domain of the variable required, data types are used.

2.      Compare the pointer and reference type variable in C++

A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. A C++ reference type variable is a constant pointer that is always implicitly dereferenced.

3.      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 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?

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.

4.      What are the arguments for and against Heap management’s single-size cells and variable-size cells?

Single-size Allocation Heap: all available cells are linked together using the pointers in the cells, forming a list of available space. Allocation is a simple matter of taking the required number of cells from this list when they are needed. Deallocation is a much more complex process. A heap-dynamic variable can be pointed to by more than one pointer, making it difficult to determine when the variable is no longer useful to the program. Simply because one pointer is disconnected from a cell.

Variable-size Allocation Heap: The initial setting of the indicators of all cells in the heap to indicate that they are garbage is difficult. Because the cells are different sizes, scanning them is a problem. One solution is to require each cell to have the cell size as its first field. Then the scanning can be done, although it takes slightly more space and somewhat more time than its counterpart for fixed-size cells.

5.      In what way is dynamic type checking better than static type checking?
Dynamic type checking is better than static because dynamic checking happens in compile time not in run time, Static checking reduces programmer flexibility.

Concepts of Programming Languages ---- Chapter 5 Names, Bindings, and Scopes


Concepts of Programming Languages ---- Chapter 5 Names, Bindings, and Scopes

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 5 Names, Bindings, and Scopes

Review Questions
1.       What are the design issues for names?
-          Are names case sensitive?
-          Are the special words of the language reserved words or keywords?

2.       What is the potential danger of case sensitive names?
Case sensitive violates the design principle that language constructs that look similar should have similar meanings. But in language whose variable names are case-sensitive, although for example rose and Rose look similar, there is no connection between them.

3.       In what way are reserved words better than keywords
As a language design choice, reserved words are better than keywords because the ability to redefine keywords can be confusing.

4.       Which category of C++ reference variables is always aliases?
Union type category of C++ reference variables is always aliases.

5.       What is the referencing environment of a statement?
The referencing environment of a statement is the collection of all variables that are visible in the statement.

6.       What is a block?
Block is a section of code which the section is entered and deallocated when the section is exited.

7.       What are the advantages and disadvantages of dynamic scoping?
Adavantages :
-          Easier to read
-          More reliable
-          Execute faster than equivalent programs in dynamic scoped languages.
Disadvantages :
-          There is no way to protect local variables from this accessibility.
-          Inability to type check references to nonlocals statically.
-          Dynamic scooping makes programs much more difficult to read, because the calling sequence of subprograms must be known to determine the meaning of references to nonlocal variables.

8.       What are the advantages of named constants?
The advantages of named constants are useful as aids to readability and program reliability.

Problem Set
1.       Decide which of the following identifier names in valid in C language. Support your decision
_Student
Int
Student
123Student
Student123
Answer : _Student, Student,Student123

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 sometimes called its I-value. The statement in C language which gives the compile time error is scanf(“%d”, value);

3.       Why is the type declaration of a variable necessary? What is the value range of the int type variable in Java?
Because the program have to know what data type of a variable and the value of the variable. The value range of the int type variable in Java is around -2147483648 to 2147483647.

4.       Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1?
Under dynamic-scoping rules, what value of x is displayed in sub1?
               var x;
               function sub1() {
               document.write("X="+ x + " <br />");
               }
               function sub2() {
               var x;
                x=10
               sub1();
               }
               x=5;
               sub2();

   Answer : Static scope: x=5 Dynamic scoping: x=10.