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

Practice Exam 1-Spring 2008

I. Method Tracing (Sure way to cure insomnia, reading code before bed.)

Use the following method to answer questions 1 - 4:
1 public boolean isEligibleAge(int age){
2   if ((age > 12) && (age < 20))
3     return true;
4   return false;
5 }

1. What line contains the method header?
     a. REMOVEDe 1
     b. REMOVEDe 2
     c. REMOVEDe 3
     d. REMOVEDe 4

2. Label each portion of line 1 with one of the following words: parameter, return type, method name, visibility or instance variable
     a. public
     b. boolean
     c. isEligibleAge
     d. int age

3. For what range of age will isEligibleAge return false?


4. For what range of age will isEligibleAge return true?


5. You friend asks you to help her with this strange ArrayIndexOutOfBoundsException she keeps getting. Without even looking at her code what can you already tell her about the Exception (in terms when it occurs) and what she should probably look at in her code?

Ask questions and post answers at Questions-PracticeExam1-MethodTracing-Spring08.

II. Short Answers (aka no essays please!)

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



2. What is the point of a constructor? How do you call the parent's constructor?



3. What two advantages of the array data structure? What are two disadvantages?


Ask questions and post answers at Questions-PracticeExam1-ShortAnswer-Spring08.

III. True/False (You have a 50-50 chance of getting each one of these, I think)

Answer true or false for the following statements. If the statement is false, correct the underlined portion so that the statement actually yields true.
1. Only the current class can have direct access to protected variables and methods.


2. this always refers to the current class and super always refer to the parent class.


3. Methods such as FileChooser.getMediaPath and Math.cos are known as final methods, because both are accessible without creating an instance of the class that houses them.


4. void methods return something upon completion.


5. A class can extend more than one class at a time e.g. myClass extends class1, class2.


6. Method overloading occurs when a child class has the exact method signature (name, order and type of parameters) as a parent method. (Hint: think about toString().)


7. Every class in Java is either directly or indirectly a child of Object.


8. Object is the only class in Java which does not have a parent.


Ask questions and post answers at Questions-PracticeExam1-TrueFalse-Spring08.

IV. Turtle Method Writing (Only in CS can you try to spell words with Turtles…)

Assume you have the following functions within LetterTurtle:
public void drawJ(int x, int y){
  setHeading(0);    	
  penUp();    	
  moveTo(x+30,y);
  penDown();    	
  turn(180);    	
  forward(90);    	
  turn(45);    	
  forward(20);    	
  turn(45);    	
  forward(30);    	
  turn(45);    	
  forward(20);
}

public void drawA(int x, int y) {
  setHeading(0);
  penUp();
  moveTo(x,y+100);
  penDown();
  moveTo(x+25,y);
  moveTo(x+50,y+100);
  penUp();
  moveTo(x+10,y+50);
  penDown();
  moveTo(x+40,y+50); 
}

public void drawV(int x, int y) {
  setHeading(0);
  penUp();
  moveTo(x,y);
  penDown();
  moveTo(x+25,y+100);
  moveTo(x+50,y);
}

Write the main method for writing the word "JAVA" across a World. You may assume that each letter is 50 pixels across and 100 pixels tall. Also the method will start drawing from the top left corner of the 50 by 100 area. Be sure to include a 20 pixel buffer room between each letter and do not forget to show the World at the end.


Ask questions and post answers at Questions-PracticeExam1-TrueMethodWriting-Spring08.

V. Turtle Method Tracing (Ambiguity is not a good thing.)

Answer the following questions about provided the code:
  
1 public class VagueTurtle extends Turtle {
2   public VagueTurtle (World w) {
3     super(w);
4     setPenWidth(5);
5   }
6 
7   public void method431(int num1, int num2, int num3) {
8     penUp();
9     moveTo(num1, num2);
10    penDown();
11    for(int i = 1; i <= num3; i++){
12      forward(i * 5);
13      turn(45);
14    }
15  }
16 }


A CS1316 student has not heeded the TAs’ advice about using more descriptive and self-documenting names for his methods and parameters. Since you did so well previously, the TAs are once again asking for your help. Explain what each parameter (num1, num2, and num3) is for and what method431 actually does. Draw a picture to help explain.













What would be better names for this method and its parameters?




Name one advantage to having self-documenting names.



Ask questions and post answers at Questions-PracticeExam1-TurtleMethodTracing-Spring08.

VI. Inheritance (Cheezburger? Nom Nom Nom…)

Answer the questions about the following code:
Animal.java
1 public class Animal{
2   public void speak(){
3     System.out.println("Animal speak");
4   }
5
6   public void eat(){
7     System.out.println("Animal eat");
8   }
9
10  public void sleep(){
11    System.out.println("Animal sleep");
12  }
13}

Cat.java
1 public class Cat extends Animal{
2   public void speak(){
3     System.out.println("I can has cheezburger?");
4   }
5
6   public void eat(){
7     super.eat();
8     System.out.println("I is eating cheezburger.  " 
9                          + "NOM NOM NOM.");
10  }
11 }


Assume we have the following code in Dr. Java’s interactions pane. Write the resulting String that will be printed after each method is called.

> Cat c = new Cat();
> c.eat()
 



> c.speak()




> c.sleep()





Ask questions and post answers at Questions-PracticeExam1-Inheritance-Spring08.

VII. Picture Method Writing (Are we there yet!? )

The following code is provided for your reference:
>
  
  public void negate(){
    Pixel pixel = null;
    Pixel[] pixels = this.getPixels();

    for (int i = 0; i < pixels.length; i++) {
      pixel = pixels[i];
      pixel.setRed(255 - pixel.getRed());
      pixel.setGreen(255 - pixel.getGreen());
      pixel.setBlue(255 - pixel.getBlue());
    }
  }


Write a new method in the AdvancedPicture class called maximizeRGBThreeAtATime() that will look the pixels of the picture at a time. For first pixel maximize the Red value, the second, the Green value and the third, the Blue value. For example, if given the following picture, myPic with the following RGB values:

(234, 120, 9) (67, 12, 64) (38, 198, 43)
(92, 45, 23) (1, 62, 88) (179, 324, 90)

The result of myPic.maximizeRGBThreeAtATime() will yield:

(255, 120, 9) (67, 255, 64) (38, 198, 255)
(255, 45, 23) (1, 255, 88) (179, 324, 255)

Do not assume the dimension of the picture will be divisible by 3 (i.e. 40 by 70 picture). You must use a while loop in your solution.

public void maximizeRGBThreeAtATime()





Ask questions and post answers at Questions-PracticeExam1-PictureMethodWriting-Spring08.

VIII. More Turtle Method Tracing (Now with more array goodness!)

Answer the questions about the following the code:
import java.awt.*;
public class NeoFlowerTurtle extends Turtle{
  public NeoFlowerTurtle(World w){
    super(w);
  }
  
  public void drawPetal(int sideLength){
    for (int i = 0; i < 4; i++){
      this.forward(sideLength);
      this.turn(90);
    }
  }

  public static void main(String [] args){
    World w = new World();
    NeoFlowerTurtle [] turtles = new NeoFlowerTurtle[19];
    for (int i = 0; i < 19; i ++){
      turtles[i] = new NeoFlowerTurtle(w);
      turtles[i].setColor(Color.RED);
      turtles[i].setPenColor(Color.RED);
      if (i != 18){
        turtles[i].turn(i * 20);
        turtles[i].drawPetal(50);
      }
      else if (i == 18){
        turtles[i].setColor(Color.GREEN);
        turtles[i].setPenColor(Color.GREEN);
        turtles[i].turn(180);
        turtles[i].forward(200);
      }
    }
  }
}

What will be the resulting picture after NeoFlowerTurtle has been compiled and is run?








Ask questions and post answers at Questions-PracticeExam1-MoreTurtleMethodTracing-Spring08.

IX. Sounds (Can you hear me now?)

Answer the following questions about Sound.
>Sound guzdial = new Sound(FileChooser.getMediaPath("guzdial.wav"));
>Sound clap = new Sound(FileChooser.getMediaPath("clap-q.wav"));
>guzdial.play();
 clap.play();
1. What is the difference between play and blockingPlay? Based on your answer, what do you think will happen with the section of code provided above?







2. What happens when you want to add more data to a Sound that is already full? What property of arrays does this demonstrate? How would you work around this limitation?








Ask questions and post answers at Questions-PracticeExam1-sounds-Spring08.

Links to this Page