Review Exercises (Set C1)

Answer questions #1-10, by referring to the code given here.

  1. What is printed as a result of line A in the ShapeTester class?

  2. What is printed as a result of line B in the ShapeTester class?

  3. Describe the error that would result from uncommenting line C in the ShapeTester class.

  4. What other words could legally be used in place of "Shape" in line D in the ShapeTester class?

  5. What is printed as a result of lines E?

  6. What is printed as a result of line F?

  7. One of the following actions can be taken without altering the output of the program when it is run. The other three actions produce errors. Identify which three produce errors and explain them. For the remaining action, simply write "no error produced".

    1. adding the word "final" after the word "public" in the line: "public class Polygon extends Shape {" of the Polygon class

    2. removing the occurrence of “implements Colorable” from the Shape class:

    3. removing the first occurrence of the word "abstract" in the Shape class.

    4. removing the last two occurrences of the word "abstract" in the Shape class.

  8. The printWhoAmI(int ConfidenceLevel) method of the Square class ________________ the printWhoAmI() method of the Polygon class (fill in the blank with an appropriate word that starts with “o”.).

  9. What is printed to the console as a result of line G of the ShapeTester class?

  10. Complete the following code to be added to the Circle class that will return true only if o is of type Circle and has the same radius. (Note: it is possible to do this in a single line of code.)

    public boolean equals (Object o) {
    return 
       
    }
    
  11. The equals() method of the Circle class written above ____________ the equals() method of the Object class (fill in the blank with an appropriate word that starts with “o”).

  12. An instance variable or method that has the default level of visibility (i.e., has no visibility modifier specified) is accessible from which of the following? (Circle all that apply.)

    1. the same class
    2. the same package
    3. a subclass
    4. a different package
  13. Explain the error in the following code:

    public class SomeClass {
    	
    	private int num_;
    
    	public SomeClass() {
    		num_ = 10;
    	}
    	
    	public int someMethod() {
    		return num_;
    	}
    }
    
    public class SomeOtherClass extends SomeClass {
    
    	public SomeOtherClass() {
    		super();
    	}
    	
    	private int someMethod() {
    		return super.someMethod() + 1;
    	}
    }
    
  14. How are static methods indicated in UML?

  15. What symbol in UML indicates a method is private?

  16. Circle all of the following statements that are true. (There may or may not be more than one.)

    1. A class may implement more than one interface
    2. A class may extend more than one abstract class
    3. An abstract class may be extended by more than one class
    4. A subclass of a non-abstract superclass can be abstract
  17. Circle all of the following statements that are true. (There may or may not be more than one.)

    1. An inner class can reference instance variables of the outer class in which it is defined.
    2. An inner class can call methods of the outer class in which it is defined.
    3. An inner class can be anonymous
  18. Circle the methods below that are required of any class that implements a MouseListener interface.

    1. mousePressed(MouseEvent)
    2. mouseReleased(MouseEvent)
    3. mouseClicked(MouseEvent)
    4. mouseDragged(MouseEvent)
    5. mouseMoved(MouseEvent)

Answers

  1.  
    shape created at (5.0, 3.0)
    polygon created at given position
    square created at given position
    
  2.  
    shape created
    polygon created
    
  3. Abstract classes can’t be instantiated and Shape is abstract.
  4. Object, Polygon, Square, Colorable
  5.  
    square polygon shape at 5.0, 3.0
    
  6.  
    circle shape at 1.0, 2.0
    
    1. Polygon can no longer be subclassed – but Square attempts to do so
    2. No error produced
    3. The Shape class contains abstract methods and hence must be declared abstract.
    4. Non-abstract methods must have a body.
  7. Overloads
  8.  
    I am a polygon!
  9.  
    public boolean equals (Object o) {
    return (o instanceof Circle) && this.radius_ == ((Circle) o).getRadius();
       
    }
    
  10. Overrides
  11. AB
  12. The visibility of the method someMethod() of the SomeOtherClass class is less than that of the method of the same name in the super class SomeClass.
  13. They are underlined
  14. A dash (-)
  15. ACD
  16. ABC
  17. ABC