CPSC 120, Fall 2002
Second Test, October 23


This is the second test from CPSC 120: Principles of Computer Science, Fall 2002.
Answers given here for essay-type questions are sample answers only.
Other answers can also receive full credit.


Question 1: Define each of the following terms, as they relate to this course:
           a) variable
           b) device driver
           c) bus
           d) precondition

Answer:

           a) A variable is a memory location that has been given a name and that can store a value. Once a variable has been declared, its name can be used to refer to the memory location or, sometimes, to the value stored in the memory location.

           b) A device driver is software that the CPU can use to communicate with and control a device such as a network interface card, a hard disk drive, or a keyboard. A device driver generally contains subroutines and interrupt handlers. Without a device driver for a particular device, the device is useless.

           c) A bus is a bundle of wires that connect devices in the computer so that they can communicate. A new device can be added to the system by plugging it into the bus. (Most of the communication is between the CPU and other devices on the bus.)

           d) A precondition is a statement that must be true at a certain point in a program, in order for the program to continue on from that point without error. For example, a precondition of the statement "sum := sum + 1/count" is that count is not zero, since it is an error to divide by zero.


Question 2: The terms syntax and semantics are used to describe two aspects of language. Explain these terms and what they have to do with programming languages. Include some examples in your discussion.

Answer:   Syntax refers to the grammar of a language, while semantics refers to meaning. Syntax rules say what statements or sentences are legally part of the language. For example, "x = 3" is not a legal statement in the xTurtle language because the syntax of an assignment statement requires the ":=" operator, not "=". The semantics of the legal statement "x := 3" is that after this statement is executed, the value in the variable x will be 3. If a program contains a syntax error, the computer will recognize that there is an error and refuse to run the program. A semantics error, on the other hand, means that the program will run but that it won't give the correct answer. The computer cannot find semantics errors because computers don't deal with meaning. They can only mechanically do exactly what they are told.


Question 3: What was the ENIAC? What role did it play in the history of computing?

Answer:   The ENIAC was the first electronic programmable computer. It was designed during World War II to compute firing tables, but was completed too late to be used during the war. It proved that it was possible to build a complex computer from extremely fast electronic components. However, although it was programmable, it did not use a stored program. So, it had to be physically re-wired to change its program.


Question 4: Write a short xTurtle program that will draw two parallel lines, like these:

parallel lines

Answer:   Here is one possible answer:

             forward(8)
             turn(90)
             PenUp
             forward(3)
             PenDown
             turn(90)
             forward(8)

Question 5: Write an xTurtle program that draws the following picture. There are 7 lines radiating out from the center. The angle between one line and the next is 30 degrees. Use a loop to draw the picture.

lines

Answer:   Here is one possible answer:

             DECLARE count
             count := 0
             LOOP
                forward(5)
                turn(90)
                forward(1)
                back(2)
                forward(1)
                turn(-90)
                back(5)
                count := count + 1
                EXIT IF count = 7
                turn(30)
             END LOOP

Question 6: Draw the picture that is produced by the following xTurtle program.

             DECLARE size
             size := 1
             LOOP
               forward(size)
               turn(120)
               forward(size)
               turn(120)
               forward(size)
               turn(120)
               EXIT IF size = 7
               back(1)
               size := size + 2
             END LOOP

Answer:   This draws four triangles of size 1, 3, 5, and 7 nested inside one another like this:

triangles


Question 7: The following xTurtle program uses random numbers, so it will draw a different picture each time it is run. However, all the pictures are similar. Describe the pictures that this program produces, and make a drawing that gives the basic idea of what they look like.

             DECLARE deg,len,count
               count := 0
               LOOP
               deg := randomInt(360)
               len := randomInt(10)
               face(deg)
               forward(len)
               back(len)
               count := count + 1
               EXIT IF count = 100
             END LOOP

Answer:   This draws 100 lines radiating out from a center point. The length of each line is chosen at random to be one of the numbers 1, 2, 3, ..., 10. The turtle faces a random direction between 1 and 360 before drawing the the line, so that each line points in a random direction. Note that the lines "forward(len) back(len)" return the turtle to its starting point, so each line starts from the same point. Each picture is different, but here are two actual pictures drawn by the program:

first sample output second sample output


Question 8: One of the main themes of the film "Giant Brains" is that computers are universal machines. According to the film, what does this mean, and how do computers compare to other machines in this regard? Give some examples. Why is universality important?

Answer:   Most machines are built for a single purpose. A car is for driving. Attempts to make a car that can fly have been (usually rather comic) failures. However, computers are programmable, so that they can be adapted to an indefinite number of different tasks simply by loading a new program into memory. This makes computers "universal" in the sense that they can be programmed to do any computational task. The same machine can be used for games, writing papers, architectural drawings, and scientific modeling. There is also a deeper sense in which computers are universal machines: All computers are equivalent in the tasks that they can be programmed to perform, except for differences in speed and amount of memory. Essentially, the first time a computer was built, it was capable -- given enough time and memory -- of doing anything that any possible computer will ever be able to do.


Question 9: Do you believe that computers are making truly fundamental changes in society? Defend your answer.

Answer:   (This is too open ended to give a difinite answer! It can be argued both ways. A good answer should mention both positive and negative change, and it should show an understanding of the difference between truly fundamental change and merely making some things easier or faster.)


David Eck, eck@hws.edu