This is your Lab Assignment Nine. It is optional. 

It also serves as practice for the Midterm Exam. 

1. You compile and run this program. 

class One {
  int a = 0;
  int b = 0;
  void fun() {
    b += 1;
    a += 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    alpha.fun();
    alpha.fun();
    alpha.fun();
  }
}

What is the output that it produces? Why?

2. You compile and run this program. 

class One {
  int a = 0;
  int b = 0;
  void fun() {
    b += 1;
    a += 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
  }
}

What is the output that it produces? Why? 

3. You compile and run this program. 

class One {
  int a = 0;
  static int b = 0;
  void fun() {
    b += 1;
    a += 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    alpha.fun();
    beta.fun();
    alpha.fun();
  }
}

What is the output that it produces? Why? 

4. What is the result of attempting to compile and run the following code?

class One {
  int value;
  One () {
    value = 3;
  }
  public static void main(String[] args) {
    One alpha = new One(3);
    One beta = new One();
    System.out.println(alpha.value + " " + beta.value);
  }
}

Here are some possibilities: 

(a) The program does not compile. 
(b) The program compiles and runs and outputs 0 0 
(c) The program compiles and runs and outputs 3 0 
(d) The program compiles and runs and outputs 0 3 
(e) The program compiles and runs and outputs 3 3
(f) None of the above. 

Please explain. 

5. What is the result of attempting to compile and run the following code?

class One {
  int value;
  One (int v) {
    value = 0;
  }
  One () {
    value = 3;
  }
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One(3);
    System.out.println(alpha.value + " " + beta.value);
  }
}

Here are some possibilities: 

(a) The program does not compile. 
(b) The program compiles and runs and outputs 0 0 
(c) The program compiles and runs and outputs 3 0 
(d) The program compiles and runs and outputs 0 3 
(e) The program compiles and runs and outputs 3 3
(f) None of the above.

Please explain. 

6. What is the result of attempting to compile and run the following code?

class One {
  static int value;
  One (int v) {
    value = v;
  }
  public static void main(String[] args) {
    One alpha = new One(0);
    One beta = new One(3);
    System.out.println(alpha.value + " " + beta.value);
  }
}

Here are some possibilities: 

(a) The program does not compile. 
(b) The program compiles and runs and outputs 0 0 
(c) The program compiles and runs and outputs 3 0 
(d) The program compiles and runs and outputs 0 3 
(e) The program compiles and runs and outputs 3 3
(f) None of the above. 

Please explain. 

7. What is the result of attempting to compile and run the following code?

class One {
  static int value;
  public static void main(String[] args) {
    One alpha = new One();
    One beta = new One();
    System.out.println(alpha.value + " " + beta.value);
  }
}

Here are some possibilities: 

(a) The program does not compile. 
(b) The program compiles and runs and outputs 0 0 
(c) The program compiles and runs and outputs 3 0 
(d) The program compiles and runs and outputs 0 3 
(e) The program compiles and runs and outputs 3 3
(f) None of the above.

Please explain. 

--