Monday, April 8, 2013

Concepts of Programming Languages - Chapter 7


Review Questions

2. What is a ternary operator?
A ternary operator is an operator that has three operands.

3. What is a prefix operator?
A prefix operator is an operator that precedes the operands.

4. What operator usually has right associativity?
Exponentiation operator.

5. What is a nonassociative operator?
A nonassociative operator is an operator that needs to be parenthesized to show the desired order to become legal.

8. Define functional side effect.
Functional side effect is a side effect that occurs when the function changes either one of its parameters or a global variable.

11. What is an overloaded operator?
An overloaded operator is an arithmetic operator that is often used for more than one purpose.

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

28. What is a cast?
Explicit type conversion.


Problem Set

1. When might you want the compiler to ignore type differences in an expression?
Suppose Type1 is a subrange of Integer. It may be useful for the difference between Type1 and Integer to be ignored by the compiler in an expression.

3. Do you think the elimination of overloaded operators in your favorite language would be beneficial? Why or why not?
Yes. Because eliminating overloaded operators will increase readability and prevent compiler confusion in choosing the correct meaning of the operator.

7. Describe a situation in which the add operator in a programming language would not be commutative.
An expression such as i + fun(j)

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?

a. left to right: sum1 is 46; sum2 is 48
b. right to left: sum1 is 48; sum2 is 46

15. Explain why it is difficult to eliminate functional side effects in C.
All functions in C are subprograms and each of them return only one value.To return more that one value, we have to use parameters and if the parameters are pointers it may change the value of variables. Another problem is if the function changes the value of a global variable, it may affect the whole program.

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 values 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


Sunday, April 7, 2013

Concepts of Programming Languages - Chapter 6


Review Questions

1. What is a descriptor?
A descriptor is the collection of the attributes of a variable.

3. What are the design issues for character sting types?
- Should strings be simply a special kind of character array or a primitive type?
- Should strings have static or dynamic length?

4. 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 length strings, allow strings to have varying length with no maximum.

11.How does JavaScript support sparse arrays?
JavaScript sparse arrays means that the subscript values need not be contiguous.

12. What languages support negative subscripts?
Perl.

13. What languages support array slices with stepsizes?
Phyton, Perl, and Ruby.

23. What is the primary difference between a record and a tuple?
The elements of a record are named while the elements of a tuple are not named.

24. Are the tuples of Python mutable?
No.

27. What is the action of the Scheme function CAR?
The function CAR returns the first element of its list parameter.

28. What is the action of the F# function tl?
The function tl returns all elements of its list parameter except the first one.

32. What are the design issues for unions?
- Should type checking be required? Note that any such type checking must be dynamic.
- Should unions be embedded in records?

33. Are the unions of Ada always type checked?
Yes.

34. Are the unions of F# discriminated?
Yes.

43. What is a compatible type?
A compatible type is one that either is legal for the operator or is allowed under language rules to be implicitly converted by compiler-generated code to a legal type.


Problem Set

7. Compare the pointer and reference type variable in C++.
A C++ reference type variable is a constant pointer that’s always implicitly dereferenced.

9. C provides two derived data types both for name and structure type equivalence: struct and union. Makes a study on when to use struct type variables and union type variables.
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.

21. In what way is dynamic type checking better than static type checking?
Dynamic type checking detects error at compile time and it is better than detecting error at run time.

Concepts of Programming Languages - Chapter 5


Review Questions

2. What is the potential danger of case-sensitive names?
It affects the writability of a program because the need to remember specific case usage makes it more difficult to write correct programs.

3. In what way are reserved words better that keywords?
Reserved words are better that keywords because the ability to redefine keywords are confusing.

4. What is an alias?
Alias is a variable name that can be used to access the same memory loaction of another variable name.

6. What is the l-value of a variable? What is the r-value?
R-value is the address of a variable.
L-value is the variable's value.

7. Define binding and  binding time.
Binding is an association between an attribute and an entity.
Binding time is the time at which a binding takes place.

8. After language design and implementation [what are the four times binding can take place in a program?]
Type bindings, static type bindings, dynamic type bindings, storage bindings.

18. What is a block?
A block is a section of code which variables storage is allocated when the section is entered and deallocated when the section is exited.

23. What are the advantages of named constants?
To improve readability, to parameterize a program.


Problem Set

1. Decide which of the following identifier names is valid in C language. Support your decision.
_Student
Student
student123
In C language, underscore (_) and uppercase/lowercase letter are allowed to start identifier names, digits and symbols aren't allowed. "int" is a reserved word for integer so it's not allowed.


2. What is l-value? Write a statement in C language which gives the compile time error “l-value required”.
The address of a variable is called its l-value.
Example:
int i = 10, j;
j = 9--; //error C2105: '--' needs l-value


4. Why is the type declaration of a variable necessary? What is the value range of the int type variable in Java?
Because the type declaration of a variable determines the range of values the variable can store and the set of operations that are defined for values of the type.
-2147483648 to 2147483647.

5. Describe a situation each where static and dynamic type binding is required.
Static binding is required if we need the bindings to occur first before the run time begins and remains unchanged throughout program execution.
Dynamic binding is required if we need the binding to occur first during run time or can change in the course of program execution.