Exam 1 Study Guide

Picture ID is REQUIRED for all exams

Use the list of questions below as a guide when studying for Exam 1. It is by no means a comprehensive list of questions. Also, these are not the exact questions that will be on the exam. The form of the questions on the exam may also be different from those found here.

You are responsible for all material presented in lecture. You are also responsible for all material covered in lab and the concepts taught by any programming assignments.

Answering the applicable “self-test” questions in each chapter of the recommended text (by Savitch) is also a good way to review. The answers to the self-test questions are found at the end of each chapter.

General Coding

  1. Write Java code to output the sentence “I am the Greatest” to the standard output device, one word per line.
  2. True/False
    1. Variables can be declared almost anywhere in the code.
    2. In a for loop, a variable can be declared at the same time it is initialized, as in for (int i = 0; i < 42; i++).
    3. Java supports the boolean data type.
  3. Describe the Java garbage collection process.
  4. Why is method overloading possible in Java?
  5. What is a method's signature?
  6. Why is a method's return type not part of its signature in Java?
  7. Describe the potential problem when using both nextInt() and next() when performing user input. How is this problem addressed?
  8. You have been asked to write a set of four methods that find the smallest element in an array of ints, an array of doubles, an array of chars, and an array of strings. You decide to use method overloading. Write the method headers for these four overloaded methods.
  9. Explain how the compiler decides which method to call when given several methods with the same name.
  10. Define preconditon and postcondition as used in method header comments required by our coding standards.
  11. Define class invariant as used in a class' header comment required by our coding standards.
  12. What do we mean when we say that parameters and variables declared inside a method are “local” variables.
  13. What is the scope of a variable declared in a for loop control statement? (e.g. for (int i = 0; i < size; i++)).
  14. Given the declaration String aString = "the quick brown fox jumped over the lazy dog"; write syntactically correct Java code to
    1. Output the number of characters in aString to the standard output stream.
    2. Output the string backwards to the standard output stream.
    3. Instantiate a String object named anotherString as a copy of aString.
  15. Write the variable declaration for an array of four strings named sentence in which each string is initialized to “I love Java”.
  16. Draw a picture of memory that results from the code snippet below. Dog is a user-defined class with one instance variable: String name;. Be sure to differentiate between stack memory and heap memory.
    Dog[] dogs = new Dog[5];
    dogs[0] = new Dog("Fido");
    dogs[1] = new Dog("Lassie");
    dogs[2] = new Dog("Ralph");
        
  17. In Java, arrays are considered to be objects. In what ways is this true?
  18. Explain why the comparison (equality) operator == cannot be used to test the equality of objects in Java.
  19. Explain why the assignment operator = cannot be used to assign one object to another in Java.
  20. True/False — Arrays may be passed to a method.
  21. True/False — Methods can return arrays.

Object Oriented Programming

  1. Define the following terms:

    class, object, method, reference variable, primitive type, instance variable, public method, public instance variable, private method, private instance variable, constructor, copy constructor, method overrloading, class invariant, visibility modifier, instance, instantiation, class scope, mutator, accessor, method signature, immutable class/object, privacy leak

  2. Explain the difference between a class and an object. Use a “real life” entity as an example.
  3. A constructor is a “special” kind of method. Explain why this is so.
  4. Explain the OO design technique of composition (also known as aggregation). Explain how composition promotes code reuse.
  5. Why is it not a violation of the OOP data hiding principle to provide an accessor and mutator for all instance variables?
  6. Why is it a violation of the OOP data hiding principle to provide public instance variables?
  7. Why is returning a reference to a private instance variable considered a violation of OOP principles?
  8. What issues must be considered when implementing a copy constructor for a user-defined class?
  9. Define immutable object. How can you tell if an instance of a class will be immutable?
  10. The following questions refer to the following class definition.
    public class Aquarium
    {
        private int       volume;
        private int       numFish;
        private int	      temperature;
        private boolean   clean;
    
        public Aquarium()
        {
            // code intentionally missing
        }
        public Aquarium(int volume, int numFish, int temp)
        {
            // code intentionally missing	
        }
        public void cleanAquarium()                   { clean = true; }
        public int getTemperature()                   { return temperature; }
        public int getVolume()                        { return volume; }
        public int getNumFish()                       { return fish; }
        public void setTemperature(int temperature)   { this.temperature = temperature; }
        
    }
    
    1. Write the code to implement Aquarium's constructors.
    2. In general, returning a reference to an instance variable is ill-advised. Why is doing so acceptable in the getTemperature() method?
    3. Write the declarations and code to instantiate an Aquarium object that holds 6 fish in 6 gallons of 70 degree water.
    4. Under what circumstances can a user of Aquarium directly access clean?
    5. Identify the use of “composition” (if any) in the definition of Aquarium. If there is no usage, describe a situation that can be easily idenified as using composition.
    6. Explain how the Java features of private instance variables and public methods help achieve the OO objectives of encapsulation and data hiding.
    7. Define class invariant. What method(s) of a class have responsibilty for maintaining the class invariant?
    8. What is the usual purpose of a private method?
  11. Given the following class definitions of a Point and Circle in a coordinate plane.
    public class Point
    {          
        private int x, y;
    
        public Point(int x, int y)                  { this.x = x; this.y = y; }
        public int getX()                           { return x; }
        public int getY()                           { return y; }
        public void move(int deltaX, int deltaY)    { x += deltax; y += deltaY; }
    }
    
    public class Circle
    {
        private Point center;
        private int radius;
    
        public Circle(Point p, int radius)          { center = p; this.radius = radius; }
        public Point getCenter()                    { /* code missing */ }
        public int getRadius()                      { return radius; }
        public void alter(Point newCenter)          { /* code missing */ }
        public void alter(int radius)               { this.radius = radius; }
    }
    
    and the declarations
    Point point = new Point(3, 6);
    Circle circle1 = new Circle(point, 5);
    Circle circle2 = new Circle(new Point (1, 2), 3);
    
    1. Write a class invariant for Circle.
    2. Write the implementation of Circle's alter(Point newCenter) method.
    3. Explain the relationship of Circle's alter() methods in the context of composition.
    4. Write the implementation of Circle's getCenter()method.
    5. Write one line of code to output the x- and y- coordinates of circle2's center.
    6. Write one line of code to make circle2 and circle1 have the same center.
    7. Find the errors in the following statements (if any)
      1. Point p = point;
      2. Point p2 = new Point(3, 5);
      3. Circle c = new Circle();
      4. System.out.println(circle1.getX());
      5. circle1 = circle2;
    8. Write the Point's toString method that formats the Point as ( x, y ).