RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

exam03-study-questions
exam03 study sheet
2008.Fall

The exam will be in-class, on-paper, closed-computer. It will consist of questions like these questions, the homeworks, and labs. Some of the questions may be directly from those sources. The single best way to study for the exam is to sit down and re-work homework and lab questions from scratch.

For the class java.util.LinkedList<SomeType>, you will need to know the methods add(SomeType), get(int), and size() (but not remove(int) or remove(SomeType)).

You will need to know how to declare an array, call an array constructor, and fill an array with some value.

Here are some practice problems. (This is far longer than an exam would be.) You are encouraged to discuss these problems on the Blackboard discussion boards.

  1. Re-do programs from homework and lab. Re-do questions from previous exams. Some questions on the final might very similar or even the same as such previous questions.
  2. What is the syntax for calling a regular (non-static) method? What about for a static method?
  3. What is the syntax for a method's signature? What adjectives might come in front of the signature?
  4. Make a class Treasure which has a description (like "golden anvil" or "slightly-used gum") and a weight (in kg). Write a constructor, getters, and setters for this class. Write a method boolean isHeavierThan( Treasure other ). the solution key
  5. The following statement doesn't make sense (and won't compile). Why not? String m = System.out.println( "hi" );
  6. What is the value of new String("cat") + (newString("hay").equals( new String("cathay") ))? (Pay attention to non-standard the parentheses.) Confirm by typing this expression into Code Pad.
  7. Most of the problems on javabat.com are good practice (they tend to be a single method, and test cases are provided for you!).
    1. true or false?: In Java, Lists can start empty, and have values added later.
    2. true or false?: In Java, arrays can start empty, and have values added later.
    3. true or false?: For any java type, you can create a List of that type.
    4. true or false?: For any java type, you can create an array of that type.
  8. Create linked lists and arrays to hold:
  9. For the lists above, write while- and for-loops to:
  10. Skim the following classes, then answer the following questions.
    import java.util.LinkedList;
    
    class Country {
      private String name;
      private LinkedList<State> states;
      // public Getters omitted.
      }
    
    class State {
      private private String name;
      private private LinkedList<University> colleges;
      // public Getters omitted.
      }
    
    class University {
      private String name;
      private LinkedList<Student> roster;
      // public Getters omitted.
      }
    
    class Student {
      private String name;
      private LinkedList<String> grades;
      // public Getters omitted.
      }
        
    Suppose you have a variable albania of type Country. the solution key
  11. Read numbers from the keyboard (until there isn't a number to read next).
  12. Read words from the keyboard until there isn't a word to read next (that is, control-D is typed). Return the number of words entered.
  13. Suppose somebody gives you a static method String wordToPigLatin(String), which takes one word and converts it into pig latin.
    Write a method which takes an entire sentence (w/o punctuation), and converts it to pig latin.
    Hint: call the String method split(" "), and of course the method wordToPigLatin. Don't look at the solution key until you've answered: What task is being done once? When you've looked at half the items of the list, what accumulator (“so-far”) variable holds the result of the work you've done up to that point?
  14. What does the following loop do? (The answer is 10 words or less, and is something you could ask your non-programming friend to do to a list of numbers, and they'd understand perfectly.)
    void mystery( LinkedList<Integer> gs ) {
      if (gs.size() == 0) {
        System.err.println( "The list must contain at least one number." );
        return;
        }
    
      int lw = gs.get(0);
      for ( int i=0;  i<gs.size();  ++i ) {
        sc = gs.get(i);
        if (sc < lw) {
          lw = sc;
          }
        }
    
      gs.remove(lw);
      }
        
    Don't look at the solution key until you've worked through this code by hand for two sample inputs, each time keeping track of what each variable holds at each moment.
  15. What does the following loop do? (The answer is 10 words or less, and is something you could ask your non-programming friend to do to a sorted list of numbers, and they'd understand perfectly.)
    /**
     * @param a list of numbers *which is sorted* small-to-large.
     */
    void mystery2( LinkedList<Integer> gs, int nw ) {
      int i = 0;
      while ((i < gs.size()) && (nw <= gs.get(i)) ) {
        i = i+1;
        }
      // What is special about index i of the list, when we finish the loop?
    
      gs.add(nw, i);  // The "add" method adds nw to the list at index i,
                      // shoving existing numbers over by one to make room.
      }
    
    Don't look at the solution key until you've worked through this code by hand for two sample inputs, each time keeping track of what each variable holds at each moment.
  16. What does the following method return and do? (The answer is a short, straightforward English sentence, with no Java.) (For this example, class Treasure is some class which has a method getWeight; that's all we need to know.)
    LinkedList<Treasure> extract( LinkedList<Treasure> goods, double heavyThresh ) {
      LinkedList<Treasure> flotsam = new LinkedList<Treasure>();
    
      for ( int i=0;  i<good.size();  ++i )
        Treasure t = goods.get(i);
        if (t.getWeight() > heavyThresh) { flotsam.add(t); } 
        }
    
      for ( int i=0;  i<flotsam.size();  ++i )
        Treasure h = goods.get(i);
        goods.remove(h);
        }
    
      return flotsam;
      }
    
  17. What does the following do? class Doo { int foo( int n ) { return 3*n; } int goo( int n ) { return foo(n/2); } int hoo( int n ) { return goo(n+1) + goo(n+3); } } Suppose Doo d = new Doo();.
    1. What does d.hoo(5) return?
    2. What does (new Doo()).hoo(6) return?
  18. We'll write a series of three methods very similar to a list's toString. We give only an example, to explain the general method we're looking for. Suppose data is a list of Integers containing 6,3,5.
  19. Write method which takes in an array of objects, and returns a new array which has the same elements in reverse order.
    Hint -- test cases: Where should the object at index 0 of the input be, in your result? Where should the object at index 1 of the input be, in your result? Index 2? Index i? Don't look at the solution key until after you've done this problem by hand for an array of four items, and also for an array of five items. Make sure your program works for an array of zero items.
  20. [Harder than average:] Write method which takes in an array of objects, and reverses the elements of that array “in-place” (that is, without using a second array.) Don't look at the solution key until after you've done this problem by hand for an array of four items, and also for an array of five items. Make sure your program works for an array of zero items.

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Dec.15 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme