Exam Review Questions


  1. Write a loop that will print out the first eight multiples of 7.



  2. Solve #1 again with a different technique.



  3. This loop never ends, due to a subtle typing error. Where's the problem? Why would that make the loop get stuck? for (int i=0; i< 5; i++){
    for (int j=0; j< 7; i++){
    System.out.println("Help! I'm stuck in an infinite loop!");
    }
    }



  4. Explain the difference between the assignment operator (=) and the equality operator (==).



  5. What is the purpose of the "void" keyword?



  6. Questions 6-9 go together
  7. Declare a variable of type String and assign it the value "Poodle."


  8. Declare a variable of type TextField and use the class constructor to initialize the value of your variable. No arguments are needed for the class constructor.


  9. Now take your text field variable and use it to call the setText method that is found in class Text Field in the API. Send in your string variable as an argument for the setText method.


  10. Here is the signature of another method from class TextField in the API:
    public String getText(){
    Show how to call that method and assign its results to a new string variable.



  11. Write a method of your own that accepts two integers and returns the greater of the two.



  12. Write a chunk of code that will add up all of the integers from 1 to 100 and will print the result in the console window.



  13. Here is an example of a method call in the middle of some program:
    String inputString = nameTextField.getText();
    myPet.setName(inputString);
    Which variables are being declared in these lines of code? Which variables must have been declared earlier? How many different methods are being called? What is the signature of those methods? Guess as much as you can about what's going on in this program, just from what you see here.



  14. Here is the code for a simple applet. It doesn't compile, due to two errors in the code. These errors are based on essentials of java-- you don't need to know anything about class Applet, class Label, etc. in order to find them. Find both errors and explain why they are a mistake. Then say how you would change the code to fix the errors.

    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Label;

    public class ExamApplet1 extends Applet {
    boolean ready = false;
    Label questionLabel = new Label("What is this thing?");
    questionLabel.setSize(25,40);

    public void init(){
    setBackground(Color.black);
    }

    public void start(){
    ready = true;
    this.add(questionLabel);
    g = this.getGraphics();
    this.decorate(g);
    }

    public void decorate(Graphics g){
    g.clearRect(0,0,250,400);
    g.setColor(Color.red);
    g.drawString("this is an applet", 30, 240);
    }

    public void paint(Graphics g){
    if (ready = true){
    this.decorate(g);
    }
    }
    }



  15. Here is another class, Zucchini. I made it up-there isn't really a package called "food" available from Sun, but pretend there is.

    import java.food.*;

    public class Zucchini extends Vegetable{
    public void prepare(){
    if (this.testFreshness()== true){
    this.wash();
    this.peel();
    this.slice("Julienne");
    }else {
    System.out.println("Cannot prepare");
    }
    }

    public void saute(FryingPan thePan){
    Spatula theSpatula = Utensil.getSpatula();
    thePan.setHeat("medium");
    thePan.addOil(.5);
    for (int i = 0; i< 12; i++){
    theSpatula.flip(this);
    }
    }

    public void serve(){
    Cheese topping = new Cheese("romano");
    topping.grate();
    topping.sprinkle(this);
    }
    }

    This code compiles and runs without exceptions, so there are no errors. If you were to look up class Vegetable in the API, what methods should appear there? What would be the signature of those methods?



  16. The Zucchini class refers to several other classes. How many classes do you see? List them. How do you know that they are classes?



  17. Identify all of the methods that belong to the other classes.



  18. Where is a class constructor being called? How can you tell?



  19. Which method is static? How can you tell?



  20. import java.food.*;

    public class Ratatouille extends Stew{
    int cookTime = 45;
    Vegetable[] veggies = new Vegetable[5];

    public void setIngredients(){
    veggie[1]= new Onion();
    veggie[2]= new Zucchini();
    veggie[3]= new Eggplant();
    veggie[4]= new Tomato();
    veggie[5]= new Garlic();
    }

    public void makeStew(){
    for (int i = 0; i< 5; i++){
    veggie[i].prepare();
    veggie[i].saute(Utensil.getFryingPan());
    this.add(veggie[i]);
    }
    }

    public void simmer(StewPot thePot){
    Spoon theSpoon = Utensil.getSpoon();
    thePot.setHeat("low");
    for (int i = 0; i< 15; i++){
    theSpoon.stir(this);
    }
    }
    }



    For the next five questions, use class Ratatouille to find each example. For each example, say how you knew it was appropriate-what features did you look at?


  21. Give an example of using a class constructor.



  22. Give an example of a method call, where the method being called belongs to class Ratatouille.



  23. Give an example of a method call, where the method being called does not belong to class Ratatouille.



  24. Give an example of using the results of a method call to assign a variable.



  25. Give an example of using the results of a method call as the argument for another method call.



  26. Class Ratatouille doesn't compile. There are five errors, where it says it cannot resolve symbol for variable veggie. What's wrong? Fix it!



  27. I fixed that mistake and now class Ratatouille compiles, but then when it runs, it says "Array Index Out of Bounds." Why? Fix that too.



  28. Create an example of your own to explain the difference between a method call and a method definition.



  29. Here is the signature of a method:
    public String spellNumber(int i)
    Is the code below a correct call of that method? Say why or why not.
    theLabel.setText(spellNumber("one"));



  30. Would this code below be a correct call of that spellNumber method? Again, explain why or why not.
    theLabel.setText(spellNumber(int i));



  31. Here is part of the code for a different applet:

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Button;
    import java.awt.event.ActionListener;
    public class ExamApplet2 extends Applet implements ActionListener{
    Button button1 = new Button("a");
    Button button2 = new Button("b");
    Button button3 = new Button("c");
    Button button4 = new Button("d");
    Button button5 = new Button("e");
    Button button6 = new Button("f");
    Button button7 = new Button("g");

    public void init(){
    setBackground(Color.black);
    button1.setBackground(new Color(170,170,0));
    button2.setBackground(new Color(170,170,0));
    button3.setBackground(new Color(170,170,0));
    button4.setBackground(new Color(170,170,0));
    button5.setBackground(new Color(170,170,0));
    button6.setBackground(new Color(170,170,0));
    button7.setBackground(new Color(170,170,0));
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    add(button1);
    add(button2);
    add(button3);
    add(button4);
    add(button5);
    add(button6);
    add(button7);
    }
    ... }

    As you can see, the author this code seems to enjoy copying and pasting. Your task is to rewrite the code using arrays and loops, to avoid the unnecessary repetition.





Back to main page