RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

exam3-study-questions
exam3 study sheet

Exam3 will be given be during finals. The exam is (inherently) cumulative, but will stress material since exam2.

Study Sheet -- you can bring one page of notes to the exam: hand-written (no machine reproduction in any way), single-sided, 8.5″x11″


The material from Exam1 and Exam2 won't be especially tested, but due to the cumulative nature of the material, you should know all the topics from the exam1 study questions. and exam2 study questions.

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)).

For the class java.util.Scanner, you will need to know how to use its methods hasNext and next, as well as the related hasNextInt, nextInt, etc.. You should know that its constructor takes one argument -- where the Scanner reads its individual characters from (either the keyboard, System.in, or from a particular String. You won't need to know that a java.io.File is also a fine source of characters.)

You will not need to know/use “import”, static You will need to know when to use static!, or classes such as java.io.File.

For these practice questions, feel free to discuss answers on the WebCT discussion board(s). Problems tagged as “[Challenge]” or “Optional” aren't necessarily difficult, but might rely on remembering details or concepts which weren't stressed. Such problems would be extra credit if they were on the test at all.

When giving a java expression or statement, you should be able to type it into BlueJ's Code Panel without any error; this means knowing where you need parentheses, where you need squirly-braces, and where you need semicolons (though I'll give some slack on these). I particularly recommend the problems which require actual code or definitions.

New Topics for Final Exam

The final exam is cumulative. A significant portion of the final exam will focus on the topics that are new since the second exam, but most of the new material builds on previous material.

Review Questions on new topics

  1. Review all topics, questions, and problems from the lab assignments, homework assignments, and lecture notes.
  2. Review the Exam 2 study guide (including book exercises in Chpts 1-4).
  3. Review the exercises in Chapters 5 and 6.
  4. When should a field or method be declared static?
  5. Create linked lists and arrays that hold lists of:
  6. For the lists above, write For Each, While, and For loops to:
  7. Create instances of class Scanner to:
  8. For any for-each loop problem in the notes, write a total of three versions: (a) using a for-each loop, (b) using a while loop, and (c) using a for loop.
    Note that (b) and (c) are similar, just re-arranging certain parts (e.g. do you write the end-of-loop update at the end of the loop, or between the for's parentheses?) Note that the for-each loop does the same work as the other two, but is simpler to write (since Java is doing stuff for you: keeping track of where you are in the list, and updating the loop variable for you).
  9. 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?
  10. 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 ( Integer sc : gs ) {
        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.
  11. 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.
  12. What does the following method return and do? (The answer is a short, straightforward English sentence, with no Java.)
    LinkedList<Treasure> extract( LinkedList<Treasure> goods, double heavyThresh ) {
      LinkedList<Treasure> flotsam = new LinkedList<Treasure>();
    
      for ( Treasure t : goods ) {
        if (t.getWeight() > heavyThresh) { flotsam.add(t); } 
        }
    
      for ( Treasure h : flotsam ) {
        goods.remove(h);
        }
    
      return flotsam;
      }
    
  13. Write a for-each loop which determines a list's size without actually actually calling the size() method!.
    (Suppose that you have a variable lst which holds a LinkedList<Gewgaw>)
  14. What is the difference between the soFar1 and soFar2 in the following?
      String soFar1 = "";
      String soFar2 = "";
      for ( Treasure t : swag ) {
        soFar1 =  soFar1 + " " + d.getName();
        soFar2 =  d.getName() + " " + soFar2;
        }
    
    (Here, swag is a LinkedList<Treasure>.) Don't look at the solution key until you've worked through two small examples by hand. (You can also type this into BlueJ, and use the debugger to step through the loop.)
  15. We'll write some 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.
  16. 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.
  17. 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.
  18. 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
(More problems might get added on Saturday; please check back then.)

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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