Review Exercises (Set C1)

  1. Given the code below, what should one type to invoke the myMethod() method at the "?????????" in the main method given? (You may assume the Number class is visible to the main method.)

    public class Number {
           int num = 1;
           Number(int n) {
                 num = n;
           public void myMethod() {
    public class Main{
           public static void main(String[] args) {
                 Number n = new Number(3);
  2. What keyword references the implicit parameter?

  3. Explain the error in the code below:

    public class Square {
        private double sideLength_;
        public Square(double sideLength) {
            sideLength_ = sideLength;
        public double getArea() {
            return sideLength_*sideLength_;
    public class Main {
        public static void main(String[] args) {
            Square s = new Square(3.0);
  4. What single line of code should replace the comment below:

    public class Circle {
        private double radius;
        public Circle() {
            radius = 1.0;
        public boolean hasRadius(double radius) {
            //return true if the circle's radius equals
            //the one given, and false otherwise
  5. First, consider the following two classes:

    public class Paint {
        int red;
        int green;
        int blue;
        public Paint() {    
        public Paint(int red, int green, int blue) {
   = red;
   = green;
   = blue;
        public void setGreen(int green) {
   = green;
        public String toString() {
            return ( + "," + + "," +;
    public class ColorManipulator {
        public static void increaseGreen(int r, int g, int b) {
            g = 255;
        public static void main(String[] args) {
            Paint paintA = new Paint(255,0,0);
            Paint paintB = new Paint();
            paintB = paintA;
            int red = 255;
            int blue = 0; 
            int green = 0;
            increaseGreen(red, blue, green); 
            System.out.println(red + "," + blue + "," + green);
            int[] colA = {255,0,0};
            int[] colB = colA;
            colB[1] = 255;

    1. What four lines does the main method of the ColorManipulator class print?
    2. The constructor for the Paint class has been _____________. (Fill in the blank with a single appropriate word.)

  6. Constructors should have a _____________ visibility modifier if they are to be used in other, non-related classes, while instance fields should generally have a _____________ visibility modifier to allow for maximum flexibility in changing the the internal structure of the class in the future, without impacting other classes. (Fill in the blank.)

  7. What type of "special method" has no return type declared (not even void)?

  8. What does the following code print?

    public class CharWrapper {
        char c = 'a';
            CharWrapper(char c) {
                  this.c = c;
             public void setChar(char c) {
                 this.c = c;
             public char getChar() {
                 return this.c;
    public class WrapperTester {
           public static void main(String[] args) {
               CharWrapper c1 = new CharWrapper('b');
               CharWrapper c2 = new CharWrapper('c');
               c1 = c2;
  9. For questions #9-19, refer to the following code:

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

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

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

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

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

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

  16. 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.

  17. 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".).

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

  19. 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) {
  20. 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").

  21. 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

  22. 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() {
    	private int someMethod() {
    		return super.someMethod() + 1;
  23. 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

  24. 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

  25. 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)


  1. n.myMethod();

  2. this

  3. the getArea() method is not static, but was called in a static way

  4. return this.radius == radius;

    1. 0,0,0
    2. Overloaded
    1. public
    2. private
  5. a constructor does not declare a return type (as opposed to a method that declares a return type of void to indicate it won't return anything)

  6. d

    shape created at (5.0, 3.0)
    polygon created at given position
    square created at given position
    shape created
    polygon created
  9. Abstract classes can't be instantiated and Shape is abstract.
  10. Object, Polygon, Square, Colorable
    square polygon shape at 5.0, 3.0
    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.
  13. Overloads
    I am a polygon!
    public boolean equals (Object o) {
    return (o instanceof Circle) && this.radius_ == ((Circle) o).getRadius();
  16. Overrides
  17. AB
  18. 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.
  19. ACD
  20. ABC
  21. ABC