Review Exercises (Set C2)

  1. Given the code below answer the questions that follow:
    public class Number {
         public int x;
         public Number(int x) {
            this.x = x;
         public static int cube(int x) {
            return x*x*x;
         public int squared() {
            return x*x;
    public class Main {
       public static void main(String[] args) {
          //Comment A
          //Comment B
          //Comment C
    1. What single statement can replace //Comment A above to create a new object of type Number named "myNumber" with an x-value of 4?
    2. What single statement can replace //Comment B above to invoke the method named squared for the object created in part (a) and print its output?
    3. What single statement can replace //Comment C above to invoke the method named cube to cube the value 5 and print its output?
  2. Overloaded methods can be distinguished by different _____________? (Fill in the blank)
  3. Identify the error in the following code:
    public class Cling {
       int a = 1;
       public static void main(String[] args) {
          int b = 2;
       public static void f(int b) {
          a = b;
  4. Name two distinguishing characteristics of constructors.
  5. What does the following print?
    public class Sandbox {
       private int lbsOfSand_;
       private int length_;
       private int width_;
       public Sandbox(int width, int length, int lbsOfSand) {
          width_ = width;
          length_ = length;
          lbsOfSand_ = lbsOfSand;
       public int[] getDimensions() {
          int[] dimensions = new int[2];
          dimensions[0] = width_;
          dimensions[1] = length_;
          return dimensions;
    public class SandboxFun {
       public static void main(String[] args) {
          Sandbox s1 = new Sandbox(3,4,5);
          Sandbox s2 = new Sandbox(6,7,8);
          s1 = s2;
          s2 = s1;

  6. For questions 6-18 below, refer to the following code:

  7. What is printed when line A is executed?

  8. What is printed when line B is executed?

  9. What is printed when line C is executed?

  10. What is printed when line D is executed?

    1. Assuming all of the classes are defined in the same package, what visibility modifiers can be used in place of public when the name_ and idNum_ instance variables of the Person class are declared, without causing an error? (Note: they are accessed directly in the FacultyMember class)

    2. Rather than using “public” or one of the modifiers mentioned in your answer to (a), what would be a better route to take to maximize the programmer's ability to modify the instance variables of this code in the future (without affecting any other code that might be dependent on this class)?

  12. What is printed when line E is executed?

  13. What is printed when lines F and G are executed (in order)?

  14. What could the word "Person" be replaced with in line H so that the three following lines do not create an error.

  15. What is printed when line I is executed?

  16. Which, if any, of the two lines J and K would produce an error if they were both uncommented? If either produces an error, describe it.

  17. Is it possible to create a subclass of StudentTutor that is abstract?

  18. If "public abstract String greeting();" was removed from the Person class, would an error be generated? If yes, describe it.

  19. The Person class is a subclass of what class?

  20. Describe the difference between overloaded methods and overridden methods.

  21. What symbol and/or text format is used to indicate a static method is protected in UML?

  22. MouseListener, ActionListener, and KeyListener are all examples of which of the following (circle all that apply)

    1. Classes
    2. Interfaces
    3. Packages
    4. Methods

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

    1. A final method is a method that can't be overridden
    2. A final class is a class that contains only final methods
    3. A class may implement multiple interfaces
    4. An abstract class may be extended by more than one class


    1. Number myNumber = new Number(4);
    2. System.out.print(myNumber.squared());
    3. System.out.printl(Number.cube(5));

  1. method signatures
  2. In the static method f, there is a reference to an instance variable a, but static variables are tied to the class, not any particular object (i.e., an instance of the class). Thus, the static method wouldn't know which object's value of a to use.

  3. Constructors have the same name as the class to which they apply. Also, in the declaration of a constructor no return type (not even "void") is specified.

  4. 6

    A person with a given name and id created
    A student named Bob Wu was created
    A student tutor was created


    person 102 named Bob Wu who is also a sophomore, and is a tutor in math


    A John Doe was created
    A student was created.


    person 102 named Bob Wu who is also a sophomore, and is a tutor in math


    1. The protected modifier and the default modifier (i.e., no modifier) can both be used.

    2. Make the instance variables private and use getters and setters to access or change their values.


    A John Doe was created
    A faculty member with name John Doe was created.



  12. Object


    I am a ...
    student tutor

  14. Lines J and K do not produce any errors

  15. yes

  16. No syntax error is produced. If the PeopleTester class is executed, no run-time error is produced. The only effect would be that if a Person reference was used to reference a FacultyMember object, one could no longer invoke the greeting() method without casting first to a FacultyMember.

  17. Object

  18. A method has been "overloaded" when the same name is used to define two (or more) methods, but each method has a different method signature (i.e., the input parameter(s) for each method are different). Overloaded methods can appear either in the same class, or across a class and one of its subclasses. A method has been "overridden", on the other hand, when it is defined in both a class and some subclass of that class, with an identical method signature (i.e., same name AND same input parameters), except these two methods do different things.

  19. A prefix of # indicates the method is protected, and the method name is underlined to indicate it is static.

  20. b.

  21. a,c,d