Sample Data Structures Questions
Chapter 5
Linked Lists

Data Structures and Other Objects Using Java (Third Edition)
by Michael Main
ISBN 0-321-37525-4


The Purpose of These Questions

These are typical exam questions from Chapter 5 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) There are 13 short answer questions and 11 multiple choice questions in this file.

Short Answers

    Short Answers
    Sections 5.1
    Java's Object Type
  1. Consider this code from the start of a Java method:
       Object obj;
       String good = new String("Study hard!");
       String bad  = new String("Party hard!");
    
       obj = good;
       good = bad;
       ...
    
    Write one more assignment statement that can appear after these to reset the good String back to its original value. Is your assignment a widening conversion or a narrowing conversion?

  2. Will the following code compile correctly? Will it run without errors?
       Object obj;
       Integer i;
       String s = new String("forty-two");
    
       obj = s;
       i = (Integer) obj;
    

  3. Write some lines of code that declares an Integer object, using the Integer wrapper class. Assign the value 42 to this object, then copy this value from the Integer object to an ordinary int variable.

  4. Suppose that i and j are both Integer objects (using the Integer wrapper class). Write a statement that will print the sum of i and j.

  5. Suppose that b and c are Integer objects. A typical use of the clone method looks like this:
       b = (Integer) c.clone( );
    
    Write a short clear explanation of why the (Integer) type cast is required in this typical example.

    Short Answers
    Section 5.2
    A Bag of Objects

  6. Here is some code to count the number of occurrences of a specific target integer in an array of integers:
       int i;
       int answer = 0;
    
       for (i = 0; i < data.length; i++)
          if (data[i] == target)
             answer++;
    
    At the end of the code, the value of answer indicates how many times target appears in the array. Rewrite the code so that it works correctly when the data array is an array of objects and target is a non-null reference to an object with an equals method.

  7. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null reference.

  8. Consider this code using the ArrayBag from Section 5.2:
       Integer i = new Integer(42);
       Integer j = new Integer(43);
       ArrayBag b = new ArrayBag( );
    
       // Add two i's and one j:
       b.add(i);
       b.add(i);
       b.add(j);
    
    Draw a diagram showing the reference variables i and j with their pointers to Integer objects, and also show b's data array with its pointers after these add statements.

    Short Answers
    Section 5.3
    JCL Collections and
    Nodes of Objects

  9. Suppose that the variable head is a reference to the head node of a linked list of objects. Each node has an instance variable called link (which is a reference to the next node) and another instance variable called data (which is an Object that's stored in the node). Write a few lines of code to count the number of occurrences of a specific non-null target Object on the list. At the end of your code, a variable called answer should indicate how many times the target appears in the array. Use target.equals to test for equality.

  10. Revise your answer from the previous question so that the code also works correctly for the situation where the target might be the null reference.

    Short Answer
    Section 5.4
    Iterators

  11. Suppose that x is an Object variable. Write a small piece of Java code that will print the message "Leopold is cool" provided that x is actually a reference to an object of the Integer wrapper class.

  12. Suppose that i is an Iterator object. Write a small piece of Java code that uses i.hasNext and i.next to print all the Objects of i to System.out.

  13. Describe one primary advantage of an external iterator versus an internal iterator.

Multiple Choice

    Multiple Choice
    Sections 4.1
    Java's Object Type
  1. Suppose that obj is an Object variable and s is a String variable. Which of the following statements is a correctly-compiling widening conversion? Don't worry about possible run-time exceptions.
    • A. obj = s;
    • B. s = obj;
    • C. s = (String) obj;
    • D. Two or more answers are correct.

  2. Suppose that obj is an Object variable and s is a String variable. Which of the following statements is a correctly-compiling widening conversion? Don't worry about possible run-time exceptions.
    • A. obj = s;
    • B. s = obj;
    • C. s = (String) obj;
    • D. Two or more answers are correct.

  3. Suppose that obj is an Object variable and that it refers to an Integer object. If s is a String variable, then which statement is correct about the assignment "s = (String) obj;"?
    • A. The statement will not compile.
    • B. The statement will compile, but there will be a run-time exception.
    • C. The statement will compile and run with no exception.

  4. Suppose that obj is an Object variable, and consider these two possible assignments:
       obj = new Integer(42);
       obj = new Double(42.0);
    
    Both assignments compile correctly. Select the true statement about what happens at run time:
    • A. Both assignments will run with no errors, regardless of which one occurs first.
    • B. Both assignments will run with no errors, but only if the Integer assignment occurs first.
    • C. Both assignments will run with no errors, but only if the Double assignment occurs first.
    • D. A run-time exception occurs with either statement.

    Multiple Choice
    Section 5.2
    A Bag of Objects

  5. Consider this code using the ArrayBag of Section 5.2 and the Location class from Chapter 2. What is the output?
       Location i = new Location(0, 3);
       Location j = new Location(0, 3);
    
       b.add(i);
       b.add(j);
    
       System.out.println(b.countOccurrences(i));   
    
    • A. 0
    • B. 1
    • C. 2
    • D. 3

  6. Consider this code using the ArrayBag of Section 5.2 and the Location class from Chapter 2. What is the output?
       Location i = new Location(0, 3);
    
       b.add(i);
       b.add(i);
    
       System.out.println(b.countOccurrences(i));   
    
    • A. 0
    • B. 1
    • C. 2
    • D. 3

  7. Consider this code using the ArrayBag of Section 5.2 and the Location class from Chapter 2. What is the output?
       Location i = new Location(0, 3);
       Location j = new Location(0, 3);
    
       b.add(i);
       b.add(j);
    
       // Change the Location j:
       j.shift(1, 0);
    
       System.out.println(b.countOccurrences(i));   
    
    • A. 0
    • B. 1
    • C. 2
    • D. 3

  8. Suppose that b is a BagArray variable from Section 5.2, and consider these two possible statements:
       b.add(new Integer(42));
       b.add(new Double(42.0));
    
    Both statements compile correctly. Select the true statement about what happens at run time:
    • A. Both statements will run with no errors, regardless of which one occurs first.
    • B. The first statement will run okay, but after adding an Integer object to the bag, you can no longer add a Double object.
    • C. A run-time exception occurs with either statement.

    Multiple Choice
    Section 5.3
    JCL Collections and
    Nodes of Objects

  9. What is a primary difference between an array and a Vector from Java's Class Libraries:
    • A. Vectors can have more than one index.
    • B. Vectors can have negative integers as indexes.
    • C. Vectors can have positive double numbers as indexes.
    • D. Vectors grow automatically as needed.

  10. Suppose that x and y are reference variables and a program activates x.equals(y). What occurs if x is the null reference?
    • A. A NullPointerException occurs
    • B. It always returns true.
    • C. It always returns false.
    • D. It returns true if y is also a null reference; otherwise it returns false.

    Multiple Choice
    Section 5.4
    Iterators

  11. What is the primary purpose of an Iterator object?
    • A. To add new objects to a collection.
    • B. To step through the objects of a collection one at a time.
    • C. To play an audio clip.
    • D. To display a graphical object.



Michael Main (main@colorado.edu)