Programming Languages and Translation

Ed Smart
CSC 340
Homework #1

1.4. An abstraction allows programmers to say more with less in their code. Justify this statement with two examples.

One example to justify this statement would be the use of variables to hide locations in computer memory that contain data values, which is a basic abstraction. Another example is an array, which is a structured abstraction used to collect data into a sequence of individually indexed items.

1.5. ALGOL was one of the first programming languages to achieve machine independence, but not independence from the von Neumann model of computation. Explain how this is so.

ALGOL gained machine independence for program execution on computers by requiring that each type of hardware provide an ALGOL compiler, which translated standard ALGOL programs into the machine code of a particular machine. However, it still had to rely on the von Neumann model because this model consists of an area of memory where both programs and data are stored and a separate central processing unit that sequentially executes instructions fetched from memory, a computation model that is still followed by many modern programming languages.

1.9. Assembly language uses branch instructions to implement loops and selection statements. Explain why a for loop and an if statement in high-level languages represent an improvement on this assembly language technique.

A for loop and an if statement in high-level languages represent an improvement on this assembly language technique because they provide a reader with a much simpler code to work with.

1.10. What is the difference between the use of an index-based loop and the use of an iterator with an array? Give an example to support your answer.

The difference between the use of an index-based loop and the use of an iterator with an array is that an iterator with an array is managed code and will work regardless of whether or not the size of the array changes and an index-based loop is a branching instruction. An example of an index-based loop is the following:

for (int i = 0; i < thisArray.length; i++) {
System.out.println(thisArray[i]);
}

An example of an iterator loop would look like this:

for (int item : thisArray) {
System.out.println(item);
}

1.14. Explain what the map function does in a functional language. How does it provide additional abstraction capability in a programming language?

In a functional language, a map function expects another function and a list as arguments and builds and returns a list of the results of applying the argument function to each element in the argument list.

1.15. Which three properties characterize imperative programming languages?

The three properties that characterize imperative languages are the sequential execution of
instructions, the use of variables representing memory locations, and the use of assignment to change the values of variables.

1.16. How do the three properties in your answer to question 1.15 reflect the von Neumann model of computing?

These three properties reflect the von Neumann model of computing in the sense that typical characteristics of a language based on this model are variables that represent memory locations and assignment allowing a program to operate on these memory locations.

1.17. Give two examples of lexical errors in a program, using the language of your choice.

One example of a lexical error would be the following statement:
;int x;

There shouldn’t be a semicolon in front of the “int.” Another example would be:

int x = 3;
/Assigning a value of 3 to x

Comments within code begin with “//”, not “/”.

1.18. Give two examples of syntax errors in a program, using the language of your choice.

One example of a syntax error is:

int x

There is no semicolon at the end of this statement, which will result in an error. Another example would be:

in x;

The type int is not spelled correctly.

1.19. Give two examples of semantic errors in a program, using the language of your choice.

An example of a semantic error would be the following line:

int x = “word”;

Here, x is of type int, while “word” is a string. The types string and int are not compatible. Another example would be:

int x, y;
x = 1;
if (x > 0) {
x = y;
}

The variable y is not initialized here.

Leave a comment