Here are the answers to the exam administered on Wed Apr 10, 2013:

April 10, 2013 

Name or username: _______________________________ 

C212/A592 Exam

For each exercise below determine if the code compiles or not, and 
if not explain why. If it compiles explain if it runs or not and if 
not explain why. If the program runs explain what it prints and why.

------( 1)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 1: The following three classes in a file Exam.java

class Horse {
  void talk() {
    System.out.println("Howdy.");
  }
}

class Unicorn extends Horse {
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}

class Example {
  public static void main(String[] args) {
    Horse a;
    Unicorn b;
    a = new Horse();
    a.talk();
    b = new Unicorn();
    b.talk();
    b.sing();
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Example     
Howdy.                                  <-------( 1 )----------------
Howdy.
That's me in the (uni)corner...
-bash-4.1$

------( 2)------------------------------------------------------

-bash-4.1$ ls
Exam.java
-bash-4.1$ cat Exam.java
class Horse {
  void talk() {
    System.out.println("Howdy.");
  }
}

class Unicorn extends Horse {
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}

// Problem 2: With Horse, Unicorn as defined above, the Example class defined below.

class Example {
  public static void main(String[] args) {
    Horse a, c;
    Unicorn b;
    a = new Horse();
    a.talk();
    b = new Unicorn();
    b.talk();
    b.sing();
    c = new Unicorn();
    c.talk();
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Example
Howdy.                                          <--------( 2 )--------------------
Howdy.
That's me in the (uni)corner...
Howdy.
-bash-4.1$

------( 3)------------------------------------------------------

-bash-4.1$ ls -l
total 4
-rw-r--r-- 1 dgerman www 388 Apr 13 15:08 Exam.java
-bash-4.1$ cat Exam.java
class Horse {
  void talk() {
    System.out.println("Howdy.");
  }
}

class Unicorn extends Horse {
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}

// Problem 3: With Horse, Unicorn as defined above, the Example class defined below.

class Example {
  public static void main(String[] args) {
    Horse a = new Unicorn();
    a.sing();
  }
}

-bash-4.1$ javac Exam.java
Exam.java:18: cannot find symbol
symbol  : method sing()
location: class Horse
    a.sing();                                                  <--------( 3 )--------------------
     ^
1 error
-bash-4.1$

------( 4)------------------------------------------------------

-bash-4.1$ cat Exam.java
class Horse {
  void talk() {
    System.out.println("Howdy.");
  }
}

class Unicorn extends Horse {
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}

// Problem 4: With Horse, Unicorn as defined above, the Example class defined below.

class Example {
  public static void main(String[] args) {
    Unicorn a;
    a = new Horse();
  }
}


-bash-4.1$ javac Exam.java
Exam.java:18: incompatible types
found   : Horse
required: Unicorn
    a = new Horse();                                           <--------( 4 )--------------------
        ^
1 error
-bash-4.1$

------( 5)------------------------------------------------------

-bash-4.1$ ls -l
total 4
-rw-r--r-- 1 dgerman www 706 Apr 13 15:13 Exam.java
-bash-4.1$ cat Exam.java
// Problem 5: The following three classes in a file Exam.java

class Horse {
  void talk() {
    System.out.println(this + "says: Howdy.");
  }
}

class Unicorn extends Horse {
  void talk() {
    System.out.println("Bonjour!");
  }
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}

class Example {
  public static void main(String[] args) {
    Horse a = new Horse();
    a.talk();
    System.out.println("------------------------");
    Unicorn b = new Unicorn();
    b.talk();
    b.sing();
    System.out.println("------------------------");
    Unicorn c = new Unicorn();
    c.talk();
    c.sing();
    System.out.println("------------------------");
  }
}

-bash-4.1$ javac Exam.java
-bash-4.1$ java Example
Horse@39579371says: Howdy.                                             <--------( 5 )--------------------
------------------------
Bonjour!
That's me in the (uni)corner...
------------------------
Bonjour!
That's me in the (uni)corner...
------------------------
-bash-4.1$

------( 6)------------------------------------------------------

-bash-4.1$ ls -l
total 4
-rw-r--r-- 1 dgerman www 330 Apr 13 15:15 Exam.java
-bash-4.1$ cat Exam.java
// Problem 6: The following two classes in a file Exam.java

class Horse {
  String name;
  Horse(String name) {
    this.name = name;
  }
  void talk() {
    System.out.println(this + "says: Howdy.");
  }
}

class Unicorn extends Horse {
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}
-bash-4.1$ javac Exam.java
Exam.java:13: cannot find symbol
symbol  : constructor Horse()                                            <--------( 6 )--------------------
location: class Horse
class Unicorn extends Horse {
^
1 error
-bash-4.1$

------( 7)------------------------------------------------------

-bash-4.1$ ls -l
total 4
-rw-r--r-- 1 dgerman www 209 Apr 13 15:22 Exam.java
-bash-4.1$ cat Exam.java
// Problem 7: The folllowing two classes in a file Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta();
  }
  Alpha(int i) {

  }
}

class Beta extends Alpha {

}

-bash-4.1$ javac Exam.java
Exam.java:12: cannot find symbol
symbol  : constructor Alpha()
location: class Alpha
class Beta extends Alpha {                                           <--------( 7 )--------------------
^
1 error
-bash-4.1$

------( 8)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 8: The following two classes in a file Exam.java

class Horse {
  String name;
  Horse(String name) {
    this.name = name;
  }
  void talk() {
    System.out.println(this + "says: Howdy.");
  }
}

class Unicorn extends Horse {
  Unicorn(String name) {
    this.name = name;
  }
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}


-bash-4.1$ javac Exam.java
Exam.java:14: cannot find symbol
symbol  : constructor Horse()
location: class Horse
  Unicorn(String name) {                                         <--------( 8 )--------------------
                       ^
1 error
-bash-4.1$

------( 9)------------------------------------------------------

-bash-4.1$ cat One.java
// Problem 9: One.java as defined below

class One {
  int a = 0;
  void fun() {
    int b = 0;
    b += 1;
    a += 1;
    System.out.print(a + b);
  }
  public static void main(String[] args) {
    One m = new One();
    m.fun();
    m.fun();
    m.fun();
  }
}
-bash-4.1$ javac One.java
-bash-4.1$ java One
234-bash-4.1$                                         <--------( 9 )--------------------

------(10)------------------------------------------------------

-bash-4.1$ cat One.java
// Problem 10: One.java as defined below

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();
  }
}
-bash-4.1$ javac One.java
-bash-4.1$ java One
246-bash-4.1$                                         <--------( 10 )--------------------

------(11)------------------------------------------------------

-bash-4.1$ cat One.java
// Problem 11: One.java as defined below

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();
  }
}
-bash-4.1$ javac One.java
-bash-4.1$ java One
224-bash-4.1$                                         <--------( 11 )--------------------

------(12)------------------------------------------------------

-bash-4.1$ cat One.java
// Problem 12: One.java as defined below

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();
  }
}
-bash-4.1$ javac One.java
-bash-4.1$ java One
235-bash-4.1$                                         <--------( 12 )--------------------

------(13)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 13: The two classes below in a file Exam.java

class Horse {
  String name;
  Horse(String name) {
    this.name = name;
  }
  void talk() {
    System.out.println(this + "says: Howdy.");
  }
}

class Unicorn extends Horse {
  Unicorn(String name) {
    super(name);
  }
  void sing() {
    System.out.println("That's me in the (uni)corner..."); // R.E.M.
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$

So the program compiles. That's the right answer.                                 <--------( 13 )--------------------

This program illustrates correct constructor chaining. 

------(14)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 14: The three classes below in a file Exam.java

abstract class Horse {
  String name;
  Horse(String name) {
    this.name = name;
  }
  void talk() {
    System.out.println("Howdy, I'm " + name);
  }
}

class Unicorn extends Horse {
  Unicorn(String name) {
    super(name);
  }
  void talk() {
    super.talk();
    System.out.println("I also speak a bit of French.");
  }
}

class Exam {
  public static void main(String[] args) {
    Horse a = new Unicorn("Oliver");
    a.talk();
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Exam
Howdy, I'm Oliver                                         <--------( 14 )--------------------
I also speak a bit of French.
-bash-4.1$

------(15)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 15: The code below in a file Exam.java

interface Horse {
  void talk();
}

class Unicorn implements Horse {
  String name;
  Unicorn(String name) {
    this.name = name;
  }
  public void talk() {
    System.out.println("Bonjour, je suis " + name);
  }
}

class Exam {
  public static void main(String[] args) {
    Horse a = new Unicorn("Francois");
    a.talk();
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Exam
Bonjour, je suis Francois                                          <--------( 15 )--------------------
-bash-4.1$

------(16)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 16: The code below in a file Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha (int i) {

  }
}

class Beta extends Alpha {
  Beta(int i) {

  }
}
-bash-4.1$ javac Exam.java
Exam.java:13: cannot find symbol
symbol  : constructor Alpha()
location: class Alpha
  Beta(int i) {                                         <--------( 16 )--------------------
              ^
1 error
-bash-4.1$

------(17)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 17: The code below in a file Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(6);
  }
  Alpha() {
    System.out.println(7);
  }
  Alpha(int i) {
    System.out.println(i);
  }
}

class Beta extends Alpha {
  Beta() {

  }
  Beta(int i) {

  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Alpha
7                                         <--------( 17 )--------------------
-bash-4.1$

------(18)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 18: The code below in a file Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(4);
  }
  Alpha() {
    System.out.println(5);
  }
  Alpha(int i) {
    System.out.println(i);
  }
}

class Beta extends Alpha {
  Beta() {
    super(6);
  }
  Beta(int i) {
    this();
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Alpha
6                                         <--------( 18 )--------------------
-bash-4.1$

------(19)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 19: Code below in Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(4);
  }
  Alpha() {
    System.out.println(5);
  }
  Alpha(int i) {
    System.out.println(i);
  }
}

class Beta extends Alpha {
  Beta() {
    super(6);
  }
  Beta(int i) {
    this(7);
  }
}
-bash-4.1$ javac Exam.java
Exam.java:19: recursive constructor invocation
  Beta(int i) {
  ^                                         <--------( 19 )--------------------
1 error
-bash-4.1$

------(20)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 20: Code below in Exam.java

class A { }

class B extends A { }

class C extends A { }

class D {
  public static void main(String[] args) {
    B a = new C();
  }
}
-bash-4.1$ javac Exam.java
Exam.java:11: incompatible types
found   : C
required: B
    B a = new C();                                         <--------( 20 )--------------------
          ^
1 error
-bash-4.1$

------(21)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 21: Code below in Exam.java

class A { }

class B extends A { }

class C extends A { }

class D {
  public static void main(String[] args) {
    B a = new A();
  }
}
-bash-4.1$ javac Exam.java
Exam.java:11: incompatible types
found   : A
required: B
    B a = new A();                                         <--------( 21 )--------------------
          ^
1 error
-bash-4.1$

------(22)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 22: Code below in Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(3);
  }
  Alpha() {
    System.out.println(0);
  }
  Alpha(int i) {
    System.out.println(i);
  }
}

class Beta extends Alpha {
  Beta() {
    super(6);
  }
  Beta(int i) {
    super(3);
    this();
  }
}
-bash-4.1$ javac Exam.java
Exam.java:21: call to this must be first statement in constructor
    this();                                         <--------( 22 )--------------------
        ^
1 error
-bash-4.1$

------(23)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 23: Code below in Exam.java

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(12);
  }
  Alpha() {
    System.out.print(13);
  }
  Alpha(int i) {
    System.out.print(i);
  }
}

class Beta extends Alpha {
  Beta() {

  }
  Beta(int i) {
    this();
    System.out.print(14);
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Alpha
1314-bash-4.1$                                         <--------( 23 )--------------------

------(24)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 24: Code below in Exam.java

abstract class Alpha {
  int value;
  Alpha (int value) {
    this.value = value;
    System.out.println(value);
  }
  public static void main(String[] args) {
    Beta f = new Beta(2013);
  }
}

class Beta extends Alpha {
  Beta (int value) {
    super(value);
    System.out.println("Howdy.");
  }
}
-bash-4.1$ javac Exam.java
-bash-4.1$ java Alpha
2013                                         <--------( 24 )--------------------
Howdy.
-bash-4.1$


------(25)------------------------------------------------------

-bash-4.1$ cat Exam.java
// Problem 25: Code below in Exam.java

abstract class Alpha {
  int value;
  Alpha (int value) {
    this.value = value;
    System.out.println(value);
  }
  public static void main(String[] args) {
    Beta f = new Beta(2013);
  }
}

class Beta extends Alpha {
  Beta (int value) {
    System.out.println("Howdy.");
    super(value);
  }
}
-bash-4.1$ javac Exam.java
Exam.java:15: cannot find symbol
symbol  : constructor Alpha()
location: class Alpha
  Beta (int value) {                                         <--------( 25 )--------------------
                   ^
Exam.java:17: call to super must be first statement in constructor
    super(value);
         ^
2 errors
-bash-4.1$

--