Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Pre-Quiz1-Spring 2008

I. Who is your grading TA? (Circle one)

II. Multiple Choice (Circle the best choice)
Consider the following code and use it to answer questions 1 - 5:
1 int[] array = {9, -7, 3, 6, -19, 14, -2};
2 int posTotal = 0, negTotal = 0;
3 for (int i = 0; i < array.length; i++){
4   if (array[i] > 0)
5     posTotal = posTotal + array[i];
6   else
7     negTotal = negTotal + array[i];
8 }

1. What is the length of array?
     a. 0
     b. 6
     c. 7
     d. 8

2. How many times will the loop iterate?
     a. 0
     b. 6
     c. 7
     d. 8

3. What is the value for posTotal after the for loop finishes completely?
     a. 4
     b. 9
     c. 18
     d. 32

4. What is the value for negTotal after the for loop finishes completely?
     a. 4
     b. -7
     c. -21
     d. -28

5. What will happen if we try to access array[array.length] ?
    a. We will get the last item in the array, -2.
    b. We will get an Exception.
    c. We will get the next to the last item in the array, 14.
    d. We will get the first item in the array, 9.

Ask questions at Student1009.

III. Picture Method
1 public void myMethod(){
2   Pixel[] pixels = getPixels();
3   for (int i = 0; i < pixels.length; i = i + 5){
4     Pixel pixel = pixels[i];
5     pixel.setGreen(0);
6   }
7 }
Apparently someone did not read the directions for homework one carefully enough and wrote a method with an ambiguous method name and no comments. The TAs have asked you for your help in figuring out what this method is supposed to do and what is a better name for it WITHOUT RUNNING THE CODE.

1. What is this method doing?

2. What is a better name for the method?

Ask questions at Pre-Quiz1-Picture Method-Spring 2008.

IV. Short Answer
1. What is one advantage to extending from a similar class?

2. Explain the difference between a void and a non-void method.

Ask questions at Pre-Quiz1-Short Answer-Spring 2008.

V. True/False
1. Java is case sensitive (is Java the same as java)?
2. Java is 1-indexed (all indices begin with 1).
3. this refers to the super class.
4. public and private are both visibility modifiers.
5. All classes in must be capitalized and all variables must be lowercase for the java file to compile.

Ask questions at Pre-Quiz1-True False-Spring 2008.

VI. The PolygonTurtle
Write a class PolygonTurtle that extends Turtle and is capable of drawing polygons having 3 or more sides.

Write a constructor for PolygonTurtle that takes in a World and calls the parent class’ constructor that also takes in a World.

isPolygon method
Write a method called isPolygon that will take in the number of sides of a figure and determine if it is a drawable polygon. The method will return true if the number of sides is greater than 2, false, otherwise.

public boolean isPolygon(int numOfSides)

drawPolygon method
Write a method called drawPolygon that takes in the number of sides and the length of each side the figure will have and draws it. Make sure the method only draws figures that have more than 2 sides.

public void drawPolygon(int numOfSides, int lengthOfSides)

drawSpiral method
Write a method called drawSpiral that draws a polygon repeatedly around in a circle based on specified x and y coordinates (startingXCoordinate, startingYCoordinate). The polygon drawn will be based on the inputted number of sides (numOfSides) and the length of the sides (lengthOfSides). The number of times the polygon is drawn is also based on inputted value (numOfSpirals).

public void drawSpiral(int startingXCoordinate, int startingYCoordinate, int numOfSides, int lengthOfSides, int numOfSpirals)

Main method
Write a main method to test the methods written above. You could also attempt to get exact picture shown above.

Ask questions at Pre-Quiz1-Polygon Turtle-Spring 2008.

Links to this Page