Programming Languages Chapter 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

1.20 Give one example of a logic error in a program, using the language of your choice.

The Java loop header for (int i = 0; i < array.length - 1; i++) would produce a logic error if the programmer intended the loop to visit every position in the array.

1.11 List three reasons one would package code in a procedure or function to solve a problem.

(1) A procedure or function allows the programmer to refer to a complex algorithm by name, thus simplifying the use of an algorithm. (2) A procedure or function allows the programmer to write an algorithm in one place but use it in many other places, thus reducing the amount of code in a program and simplifying program maintenance. (3) A procedure or function allows the programmer to decompose a problem into smaller subprograms that can be solved incrementally.

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

(1) An array data structure, which represents a sequence of data items accessible by index position, allows the programmer to treat a collection of many data items as one thing. (2) The function sqrt(x) allows the programmer to name a complex algorithm for computing the square root of a number without listing all of the code for that algorithm.

1.3 List at least three ways in which the use of assembly language represented an improvement for programmers over machine language.

(1) Binary opcodes are replaced by mnemonic symbols. (2) Binary address fields are replaced by mnemonic symbols. (3) The assembler checks the program for errors, such as undeclared data labels.

1.2 State a difficulty that machine language programmers faced when (a) translating their ideas into machine code, and (b) loading their code by hand into computer memory.

(a) The algorithms for solving many problems are most easily discovered, represented, verified, and communicated to other people using a high-level notation, such as that of arithmetic expressions in mathematics. However, high-level notations do not look anything like machine codes, which must be linear sequences of bit strings. Moreover, many of the instructions that these strings represent involve machine manipulations that allow the algorithm to execute effectively on hardware but have little to do with the description of the problem being solved by the algorithm. (b) Loading machine code by hand requires the operator to enter each bit in a bit string and all of the bit strings in a program. The potential for error during these entries is enormous. Moreover, it is impossible to insert or remove an instruction without shifting (and thus re-entering) many of the other instructions

1.6 The languages Scheme, C++, Java, and Python have an integer data type and a string data type. Explain how values of these types are abstractions of more complex data elements, using at least one of these languages as an example.

A value of the integer type int in Java is represented internally as a sequence of 32 bits, usually using twos complement notation. Values of the String data type in Java are objects that include a sequence of Unicode character values (16 bits each) and a field for the length of the string.

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 provided a notation for expressing algorithms. ALGOL compilers translated this notation to the machine codes for different types of machines. In this sense, ALGOL programs were machineindependent, because they could be written once and translated many times to run on several different types of machines. However, each of these types of machines had a von Neumann architecture. Moreover, ALGOL's use of the assignment statement reflected this model of computation. Thus, ALGOL was not independent of the von Neumann model of computation

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.

An assembly language branch instruction forces the programmer to express the flow of control in terms of the physical locations of the instructions in memory. The programmer must declare and use instruction labels correctly and understand several different types of branch instructions. The potential for logic errors is quite high, and the code for even a simple loop or two-way if statement can be very complex to read and difficult to modify. High-level if statements and for loops express control information in terms of the logic of a problem, not in terms of instruction locations.

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.

An iterator allows the programmer to visit each data element in an array without keeping track of its position. An iterator-based for loop on an array of integers is thus simpler to write and read, as the next two Java code segments show:

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

Assignment statements reflect the underlying modification of storage locations in a von Neumann machine. Selction and loop statement reflect the transfer of control from the current instruction location to a location that is not the next one in memory.

1.15 Which three properties characterize imperative programming languages?

Imperative languages include constructs for assignment, selection, and loops.

1.1 Explain why von Neumann's idea of storing a program in computer memory represented an advance for the operators of computers

Lacking stored programs, early computer operators had to literally rewire a computer to get it to solve different problems. With stored programs, the computer operator could load a different program for each problem, thus getting the computer to solve different problems without rewiring the hardware. Problem solving thus became faster and less error-prone.

1.12 What role do parameters play in the definition and use of procedures and functions?

Parameters allow the programmer to specify and vary the data that procedures or functions use. Thus, they allow the programmer to define algorithms as general methods that can be applied in different situations.

1.13 In what sense does recursion provide additional abstraction capability to function definitions? Give an example to support your answer.

Recursion allows the programmer to decompose a complex problem into simpler subproblems of exactly the same form. An example is the factorial of a given number, which is defined in terms of the factorial of a smaller number and the factorial of the number 1. Recursion also allows the programmer to dispense with the assignment statement and the loop statement.

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

The Java condition if x == 0 would generate a syntax error, because it omits the parentheses that should enclose the comparison. The Java statement return x would generate a syntax error, because it omits the trailing semicolon.

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

The Java expression x / y would generate an exception if x and y are integers and y equals 0. The Java expression array[i] would generate an exception if the value of i is less than 0 or greater than or equal to array.length.

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

The map function expects two arguments, another function and a list. The map function applies this function to each data value in the list, and builds a list of the results. The programmer could write the code to do this, using a for loop over the list and building a new list of the results, but the map function automates this common pattern of list processing.

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

The symbol @ is not a lexical item in Java, so its presence in a program would generate an unrecognized symbol error. The Java sequence of characters "This is an incomplete string omits the closing ", so it would generate an error to the effect that a string must end at the end of a line of code.

1.21 Java and Python programs are translated to byte code that runs on a virtual machine. Discuss the advantages and disadvantages of this implementation strategy, as opposed to that of C++, whose programs translate to machine code.

The translation of a source program to byte code can make programs easier to port to different types of hardware, as long as byte code interpreters exist for that hardware. Also, byte code plugins for Web browsers allow Java programs to run as applets on the Web. The main disadvantage of a byte code interpreter is that it runs code more slowly than native machine code execution.

1.7 Explain the difference between a data structure and an abstract data type (ADT), using at least two examples.

Two types of data structures are arrays and linked nodes. These are used to organize the data for abstract data types, such as lists, queues, and stacks. Users of these ADTs need only learn the names of their operations and their performance characteristics, and are not concerned with the underlying data structures.


Kaugnay na mga set ng pag-aaral

Florida State Insurance Law with Definitions

View Set

Reading 45- Market Organization & Structure

View Set

The CE Shop Real Estate - Contracts and Regulations Section

View Set

Med Surg Chapter 14: Altered Immune Responses, and Transplantation

View Set

Сессия Педагогика 200 вопросов

View Set