Chapter 7-Concepts of Programming Languages(Robert W. Sebesta)-Mr. Tri Djoko Wahjono, Ir, M.Sc.

REVIEW QUESTION

2. What is a ternary operator?

Ternary operator is an operator that has three operands.

 

4. What operator usually has right associativity?

In Ruby and Ada the operator is **

In C-based languages ++, –, unary -, unary +

 

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

 

11. What is an overloaded operator?

Overloaded operator is Arithmetic operators that are often used for more than one purpose. For example, + usually is used to specify integer addition and floating-point addition. Some languages—Java, for example—also use it for string catenation.

 

12. Define narrowing and widening conversions

Narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type. For example, converting a double to a float in Java is a narrowing conversion, because the range of double is much larger than that of float. Widening conversion converts a value to a type that can include at least approximations of all of the values of the original type. For example, converting an int to a float in Java is a widening conversion.

 

15. What is a referential transparency?

A referential transparency if a program has the property of any two expressions in the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program.

 

25. What mixed-mode assignments are allowed in Ada?

Ada does not allow mixed-mode assignment.

 

PROBLEM SET

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

When we use add operator to combined strings. Such as “abc” + “xyz” = “abcxyz” and “xyz” + “abc” = “xyzabc”. These example are obviously not commutative.

 

8. Describe a situation in which the add operator in a programming language would not be associative.

If the three numbers being added are -32768, 32767, and 1 (assuming 16 bit signed integers):

(-32768 + 32767) + 1 = (-1) + 1 = 0.

-32768 + (32767 + 1) = -32767 + <error overflow> = <error overflow>

So if associativity causes an overflow exception, the add operator is not associative.

 

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

 

15. Explain why it is difficult to eliminate functional side effects in C.

Because in C there is only function, which mean that all subprograms only return one value. One way to eliminate the side effect of two way parameter and still provide subprogram return more than one value, the values would need to be placed in a struct and the struct returned. Access to global in functions would also have to be disallowed.

 

20. Consider the following C program:

int fun(int *i) {

*i += 5;

return 4;

}

void main() {

int x = 3;

x = x + fun(&x);

}

What is the value of x after the assignment statement in main, assuming

a. operands are evaluated left to right. Ans: 7

b. operands are evaluated right to left. Ans: 12

 

22. Explain how the coercion rules of a language affect its error detection.

Coercion defined as an implicit type conversion that is initiated by the compiler. Language designers are not in agreement on the issue of coercions in arithmetic expressions. Those against a broad range of coercions are concerned with the reliability problems that can result from such coercions, because they reduce the benefits of type checking. Those who would rather include a wide range of coercions are more concerned with the loss in flexibility that results from restrictions.

 

Chapter 6-Concepts of Programming Languages(Robert W. Sebesta)-Mr. Tri Djoko Wahjono, Ir, M.Sc.

REVIEW QUESTION

1. What is a descriptor?

A descriptor is the collection of the attributes of a variable.

 

2. What are the advantages and the disadvantages of decimal data types

Decimal types have the advantage of being able to precisely store decimal values, at least those within a restricted range, which cannot be done with floating-point. For example, the number 0.1 (in decimal) can be exactly represented in a decimal type, but not in a floating-point type. The disadvantages of decimal types are that the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful.

 

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

 

8. What are the design issues for array?

-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 multi-dimensioned arrays allowed, or both?

-Can arrays be initialized when they have their storage allocated?

-What kinds of slices are allowed, if any?

 

17. Define row major order and column major order

Row major order is where the elements of the array that have as their first subscript the lower bound value of that subscript are stored first, followed by the elements of the second value of the first subscript, and so forth. If the array is a matrix, it is stored by rows. Column major order is where the elements of an array that have as their last subscript the lower bound value of that subscript are stored first, followed by the elements of the second value of the last subscript, and so forth. If the array is a matrix, it is stored by columns.

 

22. Define fully qualified and elliptical references to fields in records.

A fully qualified reference to a record field is one in which all intermediate record names, from the largest enclosing record to the specific field, are named in the reference. Both the COBOL and the Ada example field references above are fully qualified. In an elliptical reference, the field is named, but any or all of the enclosing record names can be omitted, as long as the resulting reference is unambiguous in the referencing environment.

 

24. Are the tuple of Phyton mutable?

Python includes an immutable tuple type. If a tuple needs to be changed, it can be converted to an array with the list function. After the change, it can be converted back to a tuple with the tuple function.

 

32. What are the design issues for union?

-Should type checking be required? Note that any such type checking must be dynamic.

-Should unions be embedded in records?

 

35. What are the design issues for pointer types?

What are the scope and lifetime of a pointer variable?

-What is the lifetime of a heap-dynamic variable (the value a pointer references)?

-Are pointers restricted as to the type of value to which they can point?

-Are pointers used for dynamic storage management, indirect addressing, or both?

-Should the language support pointer types, reference types, or both?

 

36. What are the two common problems with pointers?

Dangling pointer, dangling pointer, or dangling reference, is a pointer that contains the address of a heap-dynamic variable that has been deallocated. Dangling pointers are dangerous for several reasons. First, the location being pointed to may have been reallocated to some new heap-dynamic variable. Furthermore, if the dangling pointer is used to change the heap-dynamic variable, the value of the new heap-dynamic variable will be destroyed. Finally, it is possible that the location now is being temporarily used by the storage management system, possibly as a pointer in a chain of available blocks of storage, thereby allowing a change to the location to cause the storage manager to fail.

Lost heap-dynamic, a lost heap-dynamic variable is an allocated heap-dynamic variable that is no longer accessible to the user program. Such variables are often called garbage, because they are not useful for their original purpose, and they also cannot be reallocated for some new use in the program.

 

44. Define type error

A type error is the application of an operator to an operand of an inappropriate type. For example, in the original version of C, if an int value was passed to a function that expected a float value, a type error would occur (because compilers for that language did not check the types of parameters).

 

44. Define strongly typed

A strongly typed is if type errors in a programming language are always detected.

 

45. Describe the three string length options

Static length string: the length can be static and set when the string is created.

Limited dynamic length string: allow strings to have varying length up to a declared and fixed maximum set by the variable’s definition, as exemplified by the strings in C and the C-style strings of C++.

Dynamic length strings: allow strings to have varying length with no maximum, as in JavaScript, Perl, and the standard C++ library.

 

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.

 

51. What is structure type equivalence?

Structure type equivalence means that two variables have equivalent types if their types have identical structures.

 

PROBLEM SET

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.

 

5. What disadvantages are there in implicit dereferencing of pointers, but only in certain contexts? For example, consider the implicit dereference of a pointer to a record in Ada when it is used to reference a record field.

When implicit de-referencing 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.

 

6. Compare the use of Boolean data types in C++ and Java. Give emphasis on their use in conditional statements and conditional loops.

The use of boolean in C++ and Java is almost the same. Boolean is a variable which has only 2 values : true and false. The only difference would be the primitive in C++ is bool while Java uses Boolean. On their use of conditional statements, they determine whether a loop or conditional statement is executed. The difference is that Boolean in Java does not accept integers as true / false value. They only accept the value either “true” or “false”. While in C++, it is possible to assign 1 as the value of a boolean which means true and 0 as false.

 

9. C provides twi derived data types both for name and structure type equivance: struct and union. Make a study on when to use struct type variables and union type variable.

If all data members of the variables are to be used at once then struct type variables are required, otherwise union type variables should be used.

 

11. In the Burroughs Extended ALGOL language, matrixes 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?

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

 

21. In what way is dynamic type checking is better than static type checking?

It is better to detect errors at compile time than at the run time, because the earlier correction is usually less costly.

Chapter 5-Concepts of Programming Languages(Robert W. Sebesta)-Mr. Tri Djoko Wahjono, Ir, M.Sc.

REVIEW QUESTION

 1. What are the design issues for names?

-are name case sensitive?

-are the special words of language reserved words or keywords?

4. What is an alias?

An alias is variables that has different name but share the same memory allocation.

7. Define binding and binding time

Binding is an association between an attribute and an entity, such as between a variable and its type of value, or between an operation and a symbol. Binding time is the time at which binding takes place. Binding and binding times are prominent concepts in semantics of programming language.

9. Define static binding and dynamic binding

A static binding is a binding that occurs before run time begins and remains unchanged throughout program execution. A dynamic binding is when the binding first occurs during run time or can change in the course of program execution.

18. What is a block?

A block is a section of code. Blocks provide origin of phrase block-structures language

23. What are the advantages of named constant?

-aids to readability and program reliability

-to parameterized a program

 

PROBLEM SET

1. Decide which of the following identifier names is valid in C language, support your decision.

_Student, int, Student, 123Student, Student123.

The valid identifier names are _Student, Student, Student123. Because the rules are that it should not start with any number. Only underscore (_) symbol used in declaration. And the name should not a keyword.

 

9. Consider the following Phyton program:

x=1;

y=3;

z=5;

def sub1():

a=7;

y=9;

z=11;

…….

def sub2():

global x;

a=13;

x=15;

w=17;

……..

def sub3():

nonlocal a;

a=19;

b=21;

z=23;

…….

…….

List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assuming static scoping is used.

Variable           Where Declared

In sub1:

a                     sub1

y                     sub1

z                     sub1

x                     main

In sub2:

a                     sub2

x                     sub2

w                    sub2

y                     main

z                     main

In sub3:

a                     sub3

b                     sub3

z                     sub3

w                    sub2

x                     sub2

y                     main

 

10. Consider the following C program:

Void fun(void){

int a,b,c;

……..

while(….){

int b,c,d;

………//1

while(….){

int c,d,e;

………//2

}

………//3

}

……….//4

}

For each of the four marked points in this function, list each visible variable, along with the number of the definition statements that defines it.

1: a from function fun, and b,c,d in the while

2: a from function fun, and c,d,e in the inner while, b from outer while

3: a from function fun, and b,c,d in the while

4: a, b from function fun