Quiz J0-2 | CS 2113 Software Engineering - Spring 2021

Quiz J0-2

Submit your answers via this Google Form

Question 1

What is the output of the below program?

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);
    }

Question 2

For the rest of the quiz, when we reference Point refer to the class below:

public class Point{

    private double x;
    private double y;

    public static void set_to_origin(Point point){
        point.x = 0;
        point.y = 0;
    }

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

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

    public double getX(){
        return this.x;
    }
    public double getY(){
        return this.y;
    }
    public void setX(double X){
        this.x = X;
    }

    public void setY(double Y){
        this.y = Y;
    }
    
    public Point copy(){
        return new Point(this);
    }
}

Does the program below compile? If so what is its output?

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

Question 3

Select which definition(s) of set_to_origin below can be substituted into
Point using the current version of main() above such that it will compile and produce the output

X: 0 Y: 0

    //option 1
    public void set_to_origin(Point point){
        point.x = 0;
        point.y = 0;
    }
    
    
    //option 2
    public void set_to_origin(){
        x = 0;
        y = 0;
    }
    
    //option 3
    public static void set_to_origin(Point point){
        this.x = 0;
        this.y = 0;
    }
    
    
    //option 4
    private void set_to_origin(){
        x = 0;
        y = 0;
    }

Question 4

Consider the program below, what is its output?

    public static void main(final String args[]){
        Point[] points = new Point[10];
        points[0].setX(10);
        points[0].setY(10);

        int sum_x = 0;
        int sum_y = 0;
        for(Point p: points){
            p = points[0].copy();
            sum_x += p.getX();
            sum_y += p.getX();
        }
        System.out.println(sum_x+", "+sum_y);
    }

Question 5

How about now?

    public static void main(final String args[]){
        Point[] points = new Point[10];
        points[0].setX(10);
        points[0].setY(10);

        int sum_x = 0;
        int sum_y = 0;
        for (int i = 0; i < 10; i++){
            points[i] = points[0].copy();
            sum_x += points[i].getX();
            sum_y += points[i].getX();
        }
        System.out.println(sum_x+", "+sum_y);
    }