Saturday, April 6, 2013

Concepts of Programming Languages 10th Edition : Chapter 7


Created By : Robert W. Sebesta
Lecturer : Mr. Tri Djoko Wahjono, Ir. M.Sc
Answered by : Shirley Halim Ng
NIM : 1601233805
Class : 02PCT

 Review questions

 2.    What is a ternary operator?
        => A ternary operator is an operator with three operands.

 3.    What is a prefix operator?
          => A prefix operator is an operator that precede their operands

 9.    What is a coercion?
        => A coercion is the implicit type convertion.

10. What is a conditional expression?
        => Conditional expression is a statement that uses if-then-else statements.

11. What is an overloaded operator?
        => An overloaded operator is the multiple uses of an operator.

15. What is referential transparency?
        => Referential transparency is 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.

28. What is a cast?
        => A cast is an operator to convert a data type into another data types.


Problem Set

 3.    Do you think the elimination of overloaded operators in your favorite language would be beneficial? Why or why not?
=> I think it is not beneficial because it can be ambigious if we want to use that operator with the same type.


5.    Should C’s assigning operations (for example, +=) be included in other languages (that do not already have them)? Why or why not?
=> Yes, because we can make the operator simpler than before. We can just write a+=b instead a=a+b.

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.
b. operands are evaluated right to left.

=> a. 12
     b. 7

21. Why does Java specify that operands in expressions are all evaluated in left-to-right order?
=> Java specify that operands in expressions are all evaluated in left-to-right order because Java is an associative language because associative in common language is evaluated from left to right.


Concepts of Programming Languages 10th Edition : Chapter 6


Created By : Robert W. Sebesta
Lecturer : Mr. Tri Djoko Wahjono, Ir. M.Sc
Answered by : Shirley Halim Ng
NIM : 1601233805
Class : 02PCT


Review Questions
1. What is a descriptor?
=> descriptor is the collection of the attributes of a variable. In an implementation, a descriptor is an area of memory that stores the attributes of a variable.

2. What are the advantages and 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. 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, for reasons discussed in the following paragraph.

5.    Define ordinal, enumeration, and subrange types.
=>           - An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers.
                       - An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated, in the definition.
                       - A subrange type is a contiguous subsequence of an ordinal type.

 8.    What are the design issues for arrays?
=> The primary design issues specific to arrays are the following:
                    - 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?

12. What languages support negative subscripts?
=> Language that supports negative subscripts is Perl.

15. What is an aggregate constant?
=> Aggregate constant is the parenthesized lists of values in Fortran language.

17. Define row major order and column major order.
=> In Row major order is 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. In column major order, 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.

32. What are the design issues for unions?
=> The design issues for unions are:
                    - 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?
=> The design issues for pointer types are:
                   - 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?

43.  What is a compatible type?
=> 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 (or the interpreter) to a legal type.

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.

52.  What is the primary advantage of name type equivalence?
=> The primary advantage of name type equivalence is easy to implement.

53. What is the primary disadvantage to structure type equivalence?
=> The primary disadvantage to structure type equivalence is difficult to implement.


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.

7.    Compare the pointer and reference type variable in C++.
=> C++ pointers can point at any variable, regardless of where it is allocated. A C++ reference type variable is a constant pointer that is always implicitly dereferenced.

8.    What are the differences between the reference type variable in C++ and those of Java?
=> C++ reference type variable is a constant, it must be initialized with the address of some variable in it definition, and after initialization a reference type variable can never be set to reference any other variable, besides Java reference variables can be assigned to refer to different class instances, they are not constants.

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.
=>  With a union, we're only supposed to use one of the elements, because they're all stored at the same spot. This makes it useful when we want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they all can be used at once.

21. In what way is dynamic type checking better than static type checking?
=> Dynamic type checking better than static type checking is when we want to provide more programming flexibility.

Concepts of Programming Languages 10th Edition : Chapter 5


Created By : Robert W. Sebesta
Lecturer : Mr. Tri Djoko Wahjono, Ir. M.Sc
Answered by : Shirley Halim Ng
NIM : 1601233805
Class : 02PCT

Review questions
1. What are the design issues for names?
=> The design issues for names are :
               - Are names case sensitive?
               - Are the special words of the language reserved words or keywords?

4. What is an alias?
=> Alias is the variables of names that can be used to access the same memory location.

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

 9.  Define static binding and dynamic binding.
=> A binding is static if it first occurs before run time begins and remains unchanged throughout program execution. If the binding first occurs during run time or can change in the course of program execution, it is called dynamic.

11. What are the advantages and disadvantages of dynamic type binding?
=> The advantages are:
       - Dynamic type binding allows any variable to be assigned a value of any type.
       - It provides more programming flexibility.
        The disadvantages are:
       -  It causes programs to be less reliable, because the error-detection capability of the compiler is diminished relative to a compiler for a language with static type bindings.
        - The cost of implementing dynamic attribute binding is considerable, particularly in execution time.

18. What is a block?
=> A block is a section of codes.


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 identifiers are: _Student, Student, and Student123. It’s because an identifier in C language has its own rule. There are:
             - An identifier can’t be started with number. The number is valid as long as it is not in the front of an identifier.
               - An identifier can’t include any data types.
               - An identifier can’t be started with symbol.
               - There are not allowed if the name of identifiers if same.

 4.  Why is the type declaration of a variable necessary? What is the value range of the int type variable in Java?
=> The type declaration of a variable is necessary because, in a program it documented information about its data, which provides clues about the program’s behavior. The value range of the int type variable in Java is 32 bits (4 bytes).

10. Consider the following C program:
void fun(void) {
int a, b, c; /* definition 1 */
. . .
while (. . .) {
int b, c, d; /*definition 2 */
. . . <-1 p="">
while (. . .) {
int c, d, e; /* definition 3 */
. . . <-2 p="">
}
. . . <-3 p="">
}
. . . <-4 p="">
}
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.
=>  - In 1 and 3 : the visible variables are a, b(both), c(both), and d from definition 1 and 2.
      - In 2: the visible variables are a, b(both), c(both), d(both), and e from definition 1, 2, and 3.
      - In 4: the visible variables are a, b, and c from the definition 1.

Tuesday, March 26, 2013

Concepts of Programming Languages 10th Edition : Chapter 3



Created By : Robert W. Sebesta
Lecturer : Mr. Tri Djoko Wahjono, Ir. M.Sc
Answered by : Shirley Halim Ng
NIM : 1601233805
Class : 02PCT


Review Questions

1. Define syntax and semantic.
=> The syntax of a programming language is the form of its expressions, statements, and program units. Semantics is the meaning of those expressions, statements, and program units.

5. What is the difference between a sentence and a sentential form?
=> Sentence is the strings of a language and a sentential form is the start symbol S of a grammar or any string in (V union T)* that can be derived from S. 

7. What three extensions are common to most EBNFs?
=> The three extensions are common to most EBNFs are brackets, braces, and multiple-choice options.

8. Distinguish between static and dynamic semantics.
=> The static semantics of a language is only indirectly related to the meaning of programs (syntax rather than semantics) and the dynamic semantics of a language is the meaning of the expression, statements, and program units of a programming language.

10. What is the difference between a synthesized and an inherited attribute?
=> Synthesizes attributes are used to pass semantic information up a parse tree, while inherited attributes pass semantic information down and across a tree.

15. Describe the two levels of uses of operational semantics.
=> The two levels of uses of operational semantics are natural operational semantics which is the highest level that interest in the final result of the execution of a complete program and the lowest level is structural operational semantics which operational semantics can be used to determine the precise meaning of a program through an examination of the complete sequence of state changes that occur when the program is executed.

17. What is stored in the state of a program for denotational semantics?
=> The state of a program for denotational semantics is the value of all its current variable.

18. Which semantics approach is most widely known?
=> The Denotational semantics is the most widely known semantics approach.

22. Give an example of an ambiguous grammar.
=> <assign>   -><id>< expr >
< id > -> A|B|C
< expr >-> < expr >+< expr>
|< expr >*< expr >
| (< expr >)
< id >

28. What is the use of the wp function? Why it is called a predicate transformer?
=> wp function is used to treat the process of producing a weakest precondition. It is called a predicate transformer because it takes a predicate, or assertion, as a parameter and returns another predicate.

Problem Set

1. Syntax error and semantics error are two types of compilation error. Explain the difference between the two in a program with examples.
=> Syntax Error: error due to missing colon, semicolon, parenthesis, etc. Syntax is the way in which we construct sentences by following principles and rules. 

Example: In C++, it would be a syntax error to say 

int x = "five";

This will not compile because it does not follow the syntax of the language and does not make any sense to the compiler. 


Semantic Error: it is a logical error. it is due to wrong logical statements. Semantics is the interpretations of and meanings derived from the sentence transmission and understanding of the message. Semantics errors are Logical, while Syntax errors are code errors. 

Example: A semantic error would compile, but be incorrect logically:

const int pi = 12345;

3.   Rewrite the BNF of Example 3.4 to represent operator -  and operator / instead of operator + and operator *.
=> <assign>   -><id>< expr >
< id > -> A|B|C

< expr >-> < expr >- < term >
 |< term >
< term > -> < term > / < factor >
                  |< factor >
< term > -> (< expr >)
 | < id >


4. Rewrite the BNF of Example 3.4 to add the += and *= operators of Java.
=><assign>   -><id>< expr >
< id > -> A|B|C

< expr >-> < expr >+=< term >
 |< term >
< term > -> < term > *= < factor >
                  |< factor >
< term > -> (< expr >)
 | < id >



 6. Using the grammar in Example 3.2, show a parse tree for each of the following statements:
a. A = A * (B * (C + A))
b. B = C * (A + C * B)
c. A = A + (B * (C))


7. Using the grammar in Example 3.4, show a parse tree for each of the following statements:
a. A = ( A * B ) + C
b. A = B * C + A
c. A = A + (B * C)
d. A = B * (C + (A *







10. Describe, in English, the language defined by the following grammar:
<S>-> <X> <Y>
<X>-> x < X >  | x
<Y>->y < Y > | y

=> <S>-> <X> <Y>   = S includes X and Y

<X>-> x < X >  | x                  = x is the element of X
<Y>->y < Y > | y                    = y is the element of Y

Monday, March 11, 2013

Concepts of Programming Languages 10th Edition : Chapter 2



Created By : Robert W. Sebesta
Lecturer : Mr. Tri Djoko Wahjono, Ir. M.Sc
Answered by : Shirley Halim Ng
NIM : 1601233805
Class : 02PCT

Review questions

3. What does Plankalkül mean?
=> Plankalkül means program calculus.

18. What two professional organizations together designed ALGOL 60?
=> The two professional organizations are European ALGOL Bulletin and Communications of the ACM.
22. On what language was COBOL based?
=> COBOL based on ALGOL 60 language.
23. In what year did the COBOL design process begin?
=> It began in 1959.
28.  PL/I was designed to replace what two languages?
=> PL/I was designed to replace FORTRAN and COBOL languages.
33. What language introduced the case statement?
=> A Language that introduced the case statement is ALGOL-W.
36. Why is Prolog called a nonprocedural language?
=> As we know, the name Prolog is from programming logic. Programming in logic programming languages is nonprocedural. Programs in such languages do not state exactly how a result is to be computed but rather describe the necessary form and/or characteristics of the result.
46. What is primary application for Objective-C?
=> The primary application for Objective-C is Objective-C 2.0.
47. What language designer worked on both C and Go?
=> The language designer that worked on both C and Go is Ken Thompson.
49. What was the first application for Java?
=> The first application for Java was Sun Microsystems.
53. What two languages was the original version of Perl meant to replace?
=> Perl language was originally a combination of sh and awk.
54. For what application area is JavaScript most widely used?
=> It’s most widely used in Web browser.
57. What data types does Java support?
=> Java supports int, float, string, double, char, etc.
64. What is primary platform on which C# is used?
=> The primary platform on which C# is used is .NET.
65. What is the input to an XSLT processor?
=> The input to an XSLT processor is an XML data document and an XSLT document.
66. What is the output to an XSLT processor?
=> The output to an XSLT processor is the transformation information.
69. Where is .jsp files executed?
=> It is executed on a Web server system.
Problem Set
    
4. As a research project, compare the features of C with those of the BASIC.
=> The syntax and operation is very similar, but visual basic isn't as powerful. Think of it as a watered down, easier version of c. With C, you can create anything from a simple calculator application to a full blown operating system. Visual basic can't make anything as extensive as an operating system, but it is much faster to code in visual basic when you want to make form based programs, or applications.

Features in C :

Low Level Features :
1.      C Programming provides low level features that are generally provided by the Lower level languages. C is Closely Related to Lower level Language such as “Assembly Language“.
2.      It is easier to write assembly language codes in C programming.
Portability :
1.      C Programs are portable i.e they can be run on any Compiler with Little or no Modification
2.      Compiler and Preprocessor make it Possible for C Program to run it on Different PC 
Powerfull :
1.      Provides Wide verity of ‘Data Types‘
2.      Provides Wide verity of ‘Functions’
3.      Provides useful Control & Loop Control Statements
Bit Manipulation :
1.      C Programs can be manipulated using bits. We can perform different operations at bit level. We can manage memry representation at bit level. [Eg. We can use Structure to manage Memory at Bit Level]
2.      It provides wide verity of bit manipulation Operators. We have bitwise operators to manage Data at bit level.
High Level Features :
1.      It is more User friendly as compare to Previous languages. Previous languages such as BCPL,Pascal and other programming languages never provide such great features to manage data.
2.      Previous languages have there pros and cons but C Programming collected all useful features of previous languages thus C become more effective language.
Modular Programming :
1.      Modular programming is a software design technique that increases the extent to which software is composed of separate parts, calledmodules
2.      C Program Consist of Different Modules that are integrated together to form complete program
Efficient Use of Pointers :
1.      Pointers has direct access to memory.
2.      C Supports efficient use of pointer .
More Efficient

Features in BASIC :

Arrays
Discusses making your code more compact and powerful by declaring and using arrays, which hold multiple related values.
Collection Initializers
Describes collection initializers, which enable you to create a collection and populate it with an initial set of values.
Constants and Enumerations
Discusses storing unchanging values for repeated use, including sets of related constant values.
Control Flow
Shows how to regulate the flow of your program's execution.
Data Types
Describes what kinds of data a programming element can hold and how that data is stored.
Declared Elements
Covers programming elements you can declare, their names and characteristics, and how the compiler resolves references to them.
Delegates
Provides an introduction to delegates and how they are used in Visual Basic.
Early and Late Binding (Visual Basic)
Describes binding, which is performed by the compiler when an object is assigned to an object variable, and the differences between early-bound and late-bound objects.
Error Types (Visual Basic)
Provides an overview of syntax errors, run-time errors, and logic errors.
Events
Shows how to declare and use events.
Interfaces
Describes what interfaces are and how you can use them in your applications.
LINQ
Provides links to topics that introduce Language-Integrated Query (LINQ) features and programming.
Objects and Classes
Provides an overview of objects and classes, how they are used, their relationships to each other, and the properties, methods, and events they expose.
Operators and Expressions
Describes the code elements that manipulate value-holding elements, how to use them efficiently, and how to combine them to yield new values.
Procedures
Describes Sub, Function, Property, and Operator procedures, as well as advanced topics such as recursive and overloaded procedures.
Statements
Describes declaration and executable statements.
Strings
Provides links to topics that describe the basic concepts about using strings in Visual Basic.
Variables
Introduces variables and describes how to use them in Visual Basic.
XML
Provides links to topics that describe how to use XML in Visual Basic.

6. Make an educated guess as to the most common syntax error in C programs.
=> The most common syntax error in C programs are missing the (;) semicolon at the end of statement, missing “&” when using scanf, send the wrong paramater when using function, missing specific library when using specific function, missing () or missing {}, etc.

10. Outline the major developments in ALGOL 60.
=>  - The concept of block structure was introduced. This allowed the programmer to localize parts of programs by introducing new data environments, or scopes.
  - Two different means of passing parameters to subprograms were allowed: pass by value and pass by name.
      - Procedures were allowed to be recursive. The ALGOL 58 description was unclear on this issue. Note that although this recursion was new for the imperative languages, LISP had already provided recursive functions in 1959.
      - Stack-dynamic arrays were allowed. A stack-dynamic array is one for which the subscript range or ranges are specified by variables, so that the size of the array is set at the time storage is allocated to the array, which happens when the declaration is reached during execution.

13. What is the primary reason why C became more widely used than Fortran?
  => The reasons why C became more widely use than Fortran:
      - Efficient compilers are universally available for all the main architechtures in use, and a good public compiler also exists (gcc). C compilers often come free with machines, while Fortran 90 compilers must be purchased, and often expensive.
      - C is very broad in scope, and is more powerful of Fortran 90 in some areas, such as pointers, and manipulations of strings of characters.
      - Acquired coding experience can be directly used outside the scientific world : C is very common in commercial world.

15. Are there any nonprocedural programming language other than prolog?
=> Yes, there are Fortran, C++, COBOL, Algol.

16. What is your opinion of the argument that languages that are too complex are too dangerous to use, and we should therefore keep all languages small and simple?
=> Languages are too complex are too dangerous to use because of its meaning itself. Ambiguous languages can cause trouble and misunderstanding among people.    So, we must keep it small and simple to avoid ambiguation and misunderstanding.

24. Why, in your opinion, do new scripting languages appear more frequently than new compiled languages?
=>New appear languages appear more frequently than new compiled languages because they are often smaller and simpler and focused on more narrow applications, which means their library need not to be nearly as large.

25. Give a brief general description of the Java servlet!
=> A servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web servers, so they can be thought of as Java Applets that run on servers instead of in web browsers. These kinds of servlets are the Java counterpart to non-Java dynamic Web content technologies such as PHP and ASP.NET.