W1: OOP

Please submit your answers to the questions as comments in a W1.txt file you’ll be writing in this lecture.

Grading rubric and submission

When you are done, submit your W1.txt file to BB.

You will be graded on the following:

|Item | Points | |Answers are correct | 100 |

Questions

1

Which of the following is a reference type and which is a primitive type?

int a;
double b;
int c[] = {1, 2, 3};
String s = "Hello World";

Answer

  • In the first two lines, int and double are basic types.
  • In the last two lines, array and String are both stored on the heap; technically, only String is a proper object from the base class Object.

2

Four part question:

  1. What is a static method in Java?

  2. Why does the main method need to be a static method?

public class Hello {
    public static void main(String[] args) {
        System.out.println("hello, world");
    }
}
  1. What is a static field in java?

  2. What is the relationship between static fields and static methods, versus non-static fields and non-static methods?

Answer

  • Static methods are called directly, or statically, without the need of an object instance.
  • main methods need to be static because it needs to be called without instantiating an object of the class.
  • A static field is a single value/memory reference shared by all objects of that class. It exists before any constructor is ever called.
  • A static method can only access static fields of the class (and any locally-defined variables). Regular, non-static methods can access both static and non-static fields (and methods).

3

What is the output of the following programs?

/* Program 1 */
public static void main(final String args[]) {
    String choice = new String("A");
    if (choice == "A") {
        System.out.println("Correct");
    }
    else {
        System.out.println("Wrong");
    }
}
/* Program 2 */
public static void main(final String args[]) {
    String choice = new String("A");
    if (choice.equals("A")) {
        System.out.println("Correct");
    }
    else {
        System.out.println("Wrong");
    }
}

Answer

  • Program 1’s output is Wrong and Program 2’s output is Correct.
  • Similar to the case in C, String is an object type where String choice will make choice have the address of the string on the heap. Thus, in Program 1, the if statement is comparing a memory address to a string; while in Program 2, the .equals() method will compare the content choice is pointing at to the string "A".

4

Does the below program change the season? Why, or why not?

static void change_season(String str) {
    str = "Spring";
}

public static void main(final String args[]) {
    String season = "Winter";
    change_season(season);
    System.out.println("The current season is: " + season);
}

Answer

The program does not change the season, because the original season variable, which is a pointer to Winter is reassigned to a new string, Spring, inside the method. That breaks the link with the original season in main.

5

What is the this keyword in Java?

Answer

The this keyword stores the memory address of the current object being used with a constructor/method. It is available inside the method as a variable, typically to refer to the object’s fields and disambiguate them from local variables with the same name.

6

What is the output of the main method below? Please explain.

public class Point {
    double x = 0;
    double y = 0;

    public Point(double x, double y) {
        x = x;
        y = y;
    }
}
public static void main(final String args[]) {
    Point point = new Point(1, 2);
    System.out.println("X: " + point.x + " Y: " + point.y);
}

Answer

The output of the main method is X: 0.0 Y: 0.0. The reason is in the Point class, the constructor does not pass the parameters x and y onto x and y of the Point class.

7

In the Point class below, how does Java choose between the two constructors?

public class Point {

   private double x, y; 
   
   public Point(double x, double y) {
        this.x = x;
        this.y = y;
   }

   public Point(Point other) {
       this.x = other.getX();
       this.y = other.getY();
   }

}

Answer

Java chooses between the two constructors by checking the type of the argument passed in. When a new Point is called with two doubles, it will use the constructor public Point(double x, double y); in contrast, when a new Point is called with a Point passed in, it will use the constructor public Point(Point other).