CSc 10: Introduction to Computing

6 Weeks Exam: Fall 1996 (modified for Fall 2001, using Java)

 

1. Which of the following were Agreat ideas@ of Babbage=s Analytical Engine that anticipated modern computers?  Write a short explanation for each one, explaining why or why not.

_____ It was a general purpose device.

 

_____ It was compact.

 

_____ It had a separate Amill@ and Astore@.

 

_____ It was electro-mechanical.

 

_____ It had a branch instruction.

 

 

2. Below is a Knobby=s World program that adds 2 + 3.


//Knobby adder program, by Robert Barnes and Glenn Blank

//Add 2+3 as two strings of marks: XX XXX => XXXXX

 

define right as

{ //Turn Knobby's to the right

  left left left

}

define getAMark as

{ //read a mark ('X') from next corner to the right

  right move read

}

define goToBlank as

{ //go to blank past first two marks

  move move

}

define fillItIn as

{ //put a mark in blank between strings of marks

  write

}

define fillBlank as

{ //get a mark and put it in the space between strings of marks

  getAMark goToBlank fillItIn

}

define goToEnd as

{ //go the end of the second string of three marks

  move move move move

}

define getABlank as

{ //read a blank (empty corner) and turn around

  read left left

}

define erase as

{ //erase a mark (with a blank)

  move write

}

define eraseEnd as

{ //erase mark at the end of concatenated string of marks

  goToEnd getABlank erase

}

define goHome as

{ //go back to starting position

  move  move  move  move  move move right

}

define main as

{ //2+3 (XX XXX) == 5 (XXXXX)

  fillBlank           //put a mark between the XX and the XXX

  eraseEnd         //erase the mark at the end of the XXX

  goHome          //tada!

}

A) What problem solving strategy did I probably use to design my solution?  Why do you think so?

 

 

 

 

B) What problem solving strategy would you use to write a Knobby program that adds 1+2?  Why?

 

 

 

 

C) Modify the Knobby program above so that it adds 1+2.  Comment your code!

 

D) In what way could this program be made more general?  What construct of the Knobby=s World programming language might you use to achieve this generality?


 

 

 


3. Write a Java definition for pi (about 3.1416).  What does this example illustrate about invariants? How can we avoid writing this definition in Java (i.e., using code that already exists somewhere)? 

 

 

 

 

 

4. Suppose you want to use the method Input.getInt() in a Java program.

a) Write the code needed to get the Java compiler to recognize this method.   

 

 

b) What will the Java compiler do with the information supplied by the above code?

 

 

 

c) When will the Java linker combine this code with your program?

 

 

 

5.  What would the following Java expression do?  Show partial results, for example:   (15)

1 + 3 - 2

     4

 2

a) 2 + 7 % 3          b)  sqrt(pow(3,2)) / -2       c) -3.1 * 5 % 2 + 7

 

 

 

 

 

6. Given the following class interface:

class Circle

{  public Circle(double radius);    //create a Circle with radius in inches

   public double perimeter();       //computes perimeter in inches

         public double area();            //computes area of circle in inches

         private double _radius;          //radius is half diameter, in inches

}; //Circle

a) Write a snippet of Java code that creates a Circle object, c, whose radius is 2 inches,

then displays its area on the monitor.

 

 

 

 

b) What would happen if you tried to write a snippet of code that displayed the _radius of c? Why?

 

 

Are the following statements true or false?  Explain why or why not.

___ a) Java operators can mean different things depending on the types of their operands.

 

 

____b) Java comments cannot contain any Knobby=s World code.

 

 

___ c) Compiled code runs faster than equivalent interpreted code.

 

 

(See end of review questions at end of each chapter in the book for more of these questions. 

Answers supplied in http://www.cse.lehigh.edu/~glennb/um/book/exsolnew.htm.)