Monday 15 April 2013

Chapter 7 - Concept of Programming Languages Tenth Edition - Robert Sebesta

Review Question Chapter 7


5. What is a nonassocative operator?

A nonassociative operator means that the expression is illegal.


6. What associativity rules are used by APL?

all operators have equal precedence and all operators associate right to left.


8. Define functional side effect.

Functional side effect occurs when the function changes either one of its parameters or a global variable.


9. What is coercion?

A coercion is an implicit type conversion.


10. What is a conditional expression?

a conditional expression is a statement that contains if-then-else to perform a conditional expression assignment.

14. What is a mixed-mode expressions?

A mixed-mode expressions is a programming language that allows an operator to have operands of different types.


28. What is a cast

A cast in C-based languages, is an explicit type conversions.



Problem Set Chapter 7


1. When might you want the compiler to ignore type differences in an expressions?

When i wanted to do an operation, for example : char array with an integer (string + integer), however this is not possible and we must first convert the char array into integer(typecasting) and after that we can do the operation.


3. Do you think the elimination of overloaded operators in your favorite language would be beneficial? Why or why not?

Yes, I do think that the elimination of overloaded operators in my favorite language would be beneficial.

Elimination of overloaded operators would:

- Enhance and increase readability.

- Minimize the compiler overhead (choosing the correct operator meaning).


4. Would it be a good idea to eliminate all operator precedence rules and require parentheses to show the desired precedence in expressions? Why or why not?

I against this idea. The amount of parentheses needed will have an impact to readability, and it will also made the programmer have more work to write the parentheses down, when you can just type it the standard way. After all that's how the math's work


5. Should C’s assigning operations (for example, +=) be included in other languages (that do not already have them)? Why or why not?
Personally, i myself prefer to use x = x + y due to the readability, some people uses x += y. But it still okay to implement this type of operator in other languages.


7. Describe a situation in which the ad operator in a programming language would not be communicative

An expression such as x + fun(y).


21. Why does Java specify that operands in expressions are all evaluated in left-to-right order ?

Java has well defined rules for specifying order in which in expression are all evaluated in left-to-right order, this is done to make Java easier to code, as left-to-right order is how most of us learned it in our early school on the mathematics subject. left-to-right order is also one of the most used guidelines in order to evaluate operands in an expression. This is also done to prevent functional side effect from happening.

Chapter 6 - Concept of Programming Languages Tenth Edition - Robert Sebesta

Review Question chapter 6

15. What is an aggregate constant?

A parenthesized lists of values.



16. What array operations are provided specifically for single-dimensioned arrays in Ada?

catenation specified by the ampersand (&).


17. Define row major order and column major order.

Row major order (by rows) – used in most languages
Column major order (by columns) – used in Fortran


18. What is an access function for an array?
Access function maps subscript expressions to an address in the array


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


21. What is the purpose of level numbers in COBOL records?

COBOL uses level numbers to show nested records; others use recursive definition


22. 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 the reference is unambiguous.


23. What is the primary difference between a record and a tuple?

record is an aggregate of data elements in which the individual elements are identified by names and accessed through offsets from the beginning of the structure.
tuple is a data type that is similar to a record, except that the elements are not named.


24. Are the tuples of Python mutable?

Python includes an immutable tuple type


25. What is the purpose of an F# tuple pattern?

A tuple pattern is simply a sequence of names, one for each element of the tuple , with or without the delimiting parentheses.


26. In what primarily imperative language do lists serve as arrays?
Python


27. What is the action of the Scheme function CAR?

The CAR function returns the first element of its list parameter.


28. What is the action of the F# fuction tl?

it is the methods of the List class, as in List.hd[1; 3; 5; 7], which returns 1.


30. On what are Python’s list comprehensions based?

A list comprehension is an idea derived from set notaion.


Problem Set Chapter 6


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. How are negative integers stored in memory?
Negative integer is stored in memory, and in most modern computer, the one on the most left (the sign bit) usually determines the sign for that integer.


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

C++ and Java boleaan usage is not different excpet in C++ the type is “bool” and in java it’s “boolean”, boolean data type only contains true or false, and this is usually used in a conditional statement or loops.


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

The relationship to C pointers is that, Reference type is less powerful but safer than the pointer type inherited from C. C++ References are different from pointer in some ways

  •     It is not possible to refer directly to a reference object after it is defined
  •     Once a reference is created, it cannot be later made to reference another object, it cannot be reseated.
  •     References cannot be NULL, every reference refers to some object although it may or may not valid.
  •     Reference cannot be Uninitialized

However, reference can still be invalid if the previously valid reference:

  •     referred to an object with automatic allocation which goes out of scope
  •     referred to an object inside a block of dynamic memory which has been freed.


9.  C provides two derived data types both for name and structure type equivalence: struct and union. Make a study on when to use struct type variables and union type variables
Union is used to save space. When we want to use one of the members of the union, rather than waste space for a lot of them. While in a struct, each data is located separated in memory address, therefor consuming more memories.


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

In Dynamic typing, we don’t need to initialize variables, Dynamic typing in practice, saves programmer from writing a few extra lines of codes.

Friday 12 April 2013

Chapter 5 - Programming Languages Concept - Tenth Edition - Robert Sebesta

Review chapter 5

1. What are the design issues for names ?
case sensitivity and special words of the language and keyboard

2. What is the potential danger of case-sensitive names ?
Case sensitivity causes a serious detriment to readability, and violates the design principle that language constructs, that look similar should have similar meaning

4. What is an alias ?
alias is an variable name that can be used to access same memory locations and variables

7. Define binding and binding time
Binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol
Binding time is time at which a binding takes place

9. Define static binding and dynamic binding
Static binding is the condition if the binding occurs before the run tume begins and remains unchanged throughout program execution
dynamic binding is the condition if the binding first occurs during run time or can change in the course of program execution

18. What is a block ?
a block are variables that typically stack dynamic, so their storage is allocated when the section is entered and deallocated when the section is exited


Problem set


1. Decide which of the following identifier names is valid in C language. Support your decision.
_Student
Int
123Student
Student123


Following Identifier
    a. _Student = Allowed, Rarely used, doesn’t really affect anything
    b. int = Not allowed, because it will be confusing to have a variable name as the type specifiers
    c. 123Student = Not allowed, the variable name look messy and can be confusing if used extensively.
    d. Student123 = Allowed, handy in case we want to separate a variable that we clearly know the purpose (For example we usually use x to measure length, we can type x1 for the first length, and x2 for the second length).

6.
Consider the following JavaScript skeletal program:
// The main program
Var x;
Function sub1() {
Var x;
Function sub2(){

}
}

Function sub3(){

}
Assume that the execution of this program is in the following unit order:
Main calls sub1
Sub1 calls sub2
Sub2 calls sub3
a. Assuming static coping, in the following, which declaration of x is the correct one for a reference to x?
i. Sub1
ii. Sub2
iii. Sub3

b. Repeat part , but assume dynamic scoping.

Scope

a. Static :

    1. Sub1 : sub1
    2. Sub2 : sub1
    3. Sub3: main

b. Dynamic :

    1. Sub1 : sub1
    2. Sub2 : sub1
    3. Sub3: sub1

7. 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();


Static Scope then x = 5, if Dynamic Scope then x = 10

10. Consider the following C program

void fun(void) {
int a, b, c; // definition 1

while ( …) {
int b, c, d; // definition 2
… <-1
while (…) {
int c, d, e; // definition 3
… <- 2
}
… <- 3
}
… <- 4
}

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

Points:

1) Variables :
    a = 1    b = 2
    c = 2    d = 2
2) Variables :
    a = 1    b = 2
    c = 3    d = 3
    e = 3
3) Variables :
    a = 1    b = 2
    c = 2    d = 2
4) Variables :
    a = 1
    b = 1
    c = 1