Sample Data Structures Questions
Chapter 2
Abstract Data Types and Java Classes

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 2 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.) At the moment there are 13 short answer questions and 12 multiple choice questions in this file.

Short Answers

    Short Answers
    Section 2.1 - 2.2
    Introduction to Classes
  1. Here is part of the Throttle declaration:
    
        public class Throttle
        {
           public Throttle(int size)
           ...
            
           public double getFlow()
           ...        
           
           public void shift(int amount)
           ...
        }
    
    Write several lines of Java code to create a Throttle called quiz with 100 positions, activate the method that shifts quiz's flow to the halfway point, and then print the current flow from quiz.

  2. Part of the Throttle implementation from Chapter 2 is shown below. Mark each method method as follows: Mark C for any constructor; mark M for any modification method. Mark A for any accessor method.
    
        public class Throttle
        {
           public Throttle(int size)...
           
           public double getFlow( )...
            
           public boolean isOn( )...
            
           public void shift(int amount)...
            
           public void shutOff( )...
        }
    

  3. What is a no-arguments constructor? In what situation does Java automatically provide a no-arguments constructor? What does the automatic no-arguments constructor do?

  4. How many Throttles are created by this code, and what is the output of the printlns?
       Throttle x;
       Throttle y;
       
       x = new Throttle(100);
       x.shift(25);
       y = x;
       y.shift(25);
       System.out.println(x.getFlow( ));
       System.out.println(y.getFlow( ));
    

    Short Answers
    Section 2.3
    Packages

  5. Suppose that the Throttle class is implemented in a package called edu.colorado.simulations. Write the import statment that is needed for a program to use the Throttle class.

  6. Where should you place the .class file for the Throttle class from the previous question?

    Short Answers
    Section 2.4
    Parameters, Equals, Clones

  7. When is it appropriate to implement a static method? Give a small example as part of your answer.

  8. Here is a method with a parameter named spot, which is a reference to a Location object:
       public static foo(Location spot)
       {
          spot.shift(2,0);
       }
    
    Now, suppose that s is a reference to a Location with s.getX( ) equal to 40. Then the method foo(s) is activated. What is the value of s.getX( ) after this activation?

  9. Here is a method with a parameter named spot, which is an integer:
       public static foo(int spot)
       {
          spot += 2;
       }
    
    Now, suppose that s is an integer with a value of 40. Then the method foo(s) is activated. What is the value of s after this activation?

  10. Consider this code that creates some Location objects with coordinates x=10 and y=20:
       Location a, b, c;
       a = new Location(10,20);
       b = new Location(10,20);
       c = b;
       ...
    
    After this code executes, what are the values of these boolean expressions?
       a==b
       a.equals(b)
       a==c
       a.equals(c)
       b==c
       b.equals(c)
    
    Also, write two clear sentences telling me the difference between == and the equals method for the Location class.

  11. Consider this code that creates some Location objects:
       Location a, b, c;
       a = new Location(10,20);
       b = (Location) a.clone( );
       b.shift(3,0);
       ...
    
    After this code executes, what are the x-coordinates of a and b? Also, write two clear sentences telling me the difference between = and the clone method for the Location class.

  12. Suppose I implement a class with a clone method. How should I modify the class head to inform the Java compiler that you plan to implement the clone method.

  13. How does the implementation begin for a typical clone method?

Multiple Choice

    Multiple Choice
    Section 2.1 - 2.2
    Introduction to Classes
  1. Suppose I create two Throttles using the class from Chapter 2:
    
        Throttle mower = new Throttle(...);
        Throttle copter = new Throttle(...);
    
    Which statement best describes the instance variable called top for these two Throttles:
    • A. mower.top and copter.top will always be two separate instance variables.
    • B. mower.top and copter.top will only be separate if the two throttles are created with a different number of positions.
    • C. mower.top and copter.top will never be two separate instance variables.

  2. Is it possible for a method of a class to activate another method of the same class?
    • A. No.
    • B. Yes, but only public methods.
    • C. Yes, but only private methodss.
    • D. Yes, both public and private methods can be activated within another method.

  3. Can two different classes contain methods with the same name?
    • A. No.
    • B. Yes, but only if the two classes have the same name.
    • C. Yes, but only if the main program does not create objects of both kinds.
    • D. Yes, this is always allowed.

  4. What is the common pattern of class definitions that is used in Chapter 2?
    • A. Methods and instance variables are both private.
    • B. Methods are private, and instance variables are public.
    • C. Methods are public, and instance variables are private.
    • D. Methods and instance variables are both public.

  5. What is the primary purpose of a constructor?
    • A. To allow multiple classes to be used in a single program.
    • B. To copy an actual argument to a method's parameter.
    • C. To initialize each object as it is declared.
    • D. To maintain a count of how many objects of a class have been created.

    Multiple Choice
    Section 2.3
    Packages

  6. Suppose that a package edu.colorado.foo has two classes Goo and Hoo. Which statement will import both these classes?
    • A. import edu.colorado.*
    • B. import edu.colorado.all
    • C. import edu.colorado.Goo-Hoo
    • D. import edu.colorado.Goo;Hoo
    • E. None of the above.

    Multiple Choice
    Section 2.4
    Parameters, Equals, Clones

  7. Suppose that the Foo class does not have a clone method. What happens when an assignment x=y; is given for two Foo objects?
    • A. x is set to refer to a copy of y's object
    • B. x is set to refer to the same object that y refers to
    • C. Compiler error
    • D. Run-time error

  8. Suppose that the Foo class has a clone method. What typically happens when an call x=(Foo) y.clone( ); is given for two Foo objects?
    • A. x is set to refer to a copy of y's object
    • B. x is set to refer to the same object that y refers to
    • C. Compiler error
    • D. Run-time error

  9. Consider this class definition:
    
        public class Quiz
        {
           private double score;
           
           public int f( )...
           public static int g( )...
        }
    
    Where could the assignment score=1.0; appear for the private instance variable score?
    • A. Both f and g can carry out the assignment.
    • B. f can carry out the assignment, but not g.
    • C. g can carry out the assignment, but not f.
    • D. Neither f nor g can carry out the assignment.

  10. Here is a small method implementation, using the Location type from Chapter 2:
    
        public static void f(int i, Location k)
        {    
            i += 1;         
            k.shift(2, 0);
        }                      
    
    Suppose that a main program has an integer variables a (equal to zero), and a Location object b (with b.getX( ) equal to zero). Then the main program calls f(a,b); What are the values of a and b.getX( ) after the method f finishes?
    • A. Both a and b.getX( ) are still 0.
    • B. a is now 1, but b.getX( ) is still 0.
    • C. a is still 0, but b.getX( ) is now 2.
    • D. a is now 1, and b.getX( ) is now 2.

  11. Suppose that the Foo class does not have an equals method. What happens when an expression x==y; is evaluated for two Foo objects?
    • A. The expression is true if x and y refer to the same object
    • B. The expression is true if x and y refer to objects with the same values
    • C. Compiler error
    • D. Run-time error

  12. Suppose that the Foo class has a typical equals method. What happens when an expression x.equals(y); is evaluated for two Foo objects?
    • A. The expression is true if x and y refer to the same object
    • B. The expression is true if x and y refer to objects with the same values
    • C. Compiler error
    • D. Run-time error



Michael Main (main@colorado.edu)