Exam 1 Review Questions

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. The form of the questions on the exam may be different from those found here.

You are responsible for all material presented in lecture. You are also responsible for all associated reading assignments and material covered in lab.

Answering the applicable "self-test" questions in each chapter of the text is also a good way to review. The answers to the self-test questions are found at the end of each chapter.

The following material from the text was not covered in class and is NOT on the exam. Any thing not listed here may appear on the exam.


For all TRUE/FALSE questions below which are FALSE, explain why.
    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's 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?
  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, a array of doubles, a array of chars and a 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, PostCondition as used in method header comments required by our coding standards.
  11. What do we mean when we say that parameters and variables declared inside a method are "local" variables.
  12. What is the scope of a variable declared in a 'for' loop control statement?
    (i.e. for (int i = 0; i < size; i++)
  13. 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.
  14. Write the variable declaration for a array of four (4) Strings named sentence in which each string is initialized to "I love Java".
  15. Draw a picture of memory that results from the code snippet below. Be sure to differentiate between stack memory and heap memory
        Integer [ ] ints = new Integer[ 5 ];
        ints[0] = new Integer( 42 );
        ints[1] = new Integer( 57 );
        ints[2] = new Integer( 25 );
    
  16. In Java, arrays are considered to be objects. In what ways is this true?
  17. Explain why the equality operator, == cannot be used to test the equality of objects.
  18. Explain why the assignment operator, =, cannot be used to assign one object to another.
  19. True/False - Arrays may be passed to a method.
  20. True/False - Methods can return arrays.
  21. What are the primary issues when programming with a partially filled array?
  22. Write a code snippet to define and populate a 10 x 10 2-D array of ints with the values 1 - 100.
  23. Write a code snippet to define and populate a 10 x 10 2-D array of Integers with the values 1 - 100.
  24. Define wrapper classes, auto-boxing, auto-unboxing.
  25. What is/are the purpose(s) of wrapper classes?
  26. Object Oriented Programming
  27. Define the following terms
    class, object, method, reference variable, primitive type, instance variable, static method, static instance variable, public method/instance variable, privata method/instance variable, constructor, copy constructor, method overrloading, class invariant, visibility modifier, instance, instantiation, class scope, mutator, accessor, method signature, immutable class/object, shallow copy, deep copy, partially filled array, privacy leak.
  28. Explain the difference between a class and an object Use a "real life" entity as an example.
  29. A constructor is a "special" kind of method. Explain why this is so.
  30. Explain the OO design technique of "aggregation" (also known as "composition") Explain how aggregation promotes code reuse.
  31. Why is it not a violation of the OOP data hiding principle to provide an accessor and mutator for all instance variables?
  32. Why is it a violation of the OOP data hiding principle to provide public instance variables?
  33. Why is returning a reference to a private instance variable considered a violation of OOP principles?
  34. What issues must be considered when implementing a copy constructor for a user-defined class?
  35. Define immutable object. How can you tell if an instance of a class will be immutable?
  36. The following questions refer to the following class definition
      class Car
      {
      	public Car(  )
    	{
    		// code intentionally missing
    	}
    	public Car( int cylinders, int people, String color )
    	{
    		// code intentionally missing
    	}
    	public void start(  )				{ isMoving = true; }
    	public void stop(  )				{ isMoving = false; }
    	public String getColor (  )			{ return color; }
    	public int getCylinders(  )			{ return cylinders; }
    	public int getNrPassengers(  ) 			{ return passengers; }
    	public void setColor (String color)     	{ this.color = color; }
    	public void setCylinders (int cylinders )  	{ this.cylinders = cylinders; }
    	public void setPassengers (int passengers) 	{ this.passengers = passengers; }
    
    	private int    cylinders;
    	private int    passengers;
    	private String color;
    	private bool   isMoving;
    }
    
  37. Write the code to implement Car's constructors
  38. In general, returning a reference to an instance variable is ill-advised. Why is doing so acceptable in the getColor( ) method?
  39. Write the declarations and code to instantiate a Car object for a Black, 6-cylinder car that can hold two people.
  40. Under what circumstances can a user of Car directly access color?
  41. Identify the use of "aggregation" in the definition of Car (if any).
  42. Explain how the Java features of private instance variables and public methods help achieve the OO objectives of encapsulation and data hiding.
  43. Define class invariant. What method(s) of a class have responsibilty for maintaining the class invariant?
  44. What is the usual purpose of a private method?
  45. Given the following class definitions of a Point and Circle in a coordinate plane
     class Point
     {          
    	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; }
    	private int x, y;
    }
    
    class Circle
    {
    	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; }
    
       private Point center;
       private int   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 aggregation
    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 format's the Point as ( x, y ).
  46. True/False - Static instance variables of a class are shared among all objects of that class.
  47. True/False - Static methods of a class can only access static instance variables and other static methods of that class.
  48. True/False - Every method of a class can access the static data members of that class.
  49. True/False - Static data members are declared and initialized outside the class.
  50. True/False - Static data members may not be final.
  51. Inheritance
  52. Explain the differences among public, private, and protected member access specifiers. Be sure to mention their role in inheritance.
  53. Explain how inheritance promotes code reuse
  54. Explain the difference between the "is a" and "uses a" relationships.
  55. Explain how Java implements "is a" relationship
  56. Explain how Java implements "has a" relationship
  57. Describe the order in which constructors are called when using inheritance.
  58. Give a real life example (other than those used in class) for which inheritance would be appropriate
  59. For the following questions, assume that class D extends class B, that D has some of its own instance variables, some of its own instance methods, and overrides some of B's instance methods.
    1. Which of the two classes is the more general class?
    2. Which of the two classes is the more specific class?
    3. Can the toSting() method defined in D call the toString( ) method defined in B? If not, why not. If so, how?
  60. What does the keyword final mean in relation to inheritance.
  61. What is the Java class Object?
  62. What impact does the existance of Object have (if any) when implementing classes
  63. Explain the statement, "Private methods are effectively not inherited"
  64. What does the keyword super represent? Give one example of its use