Thu May 6 14:45:00 EST 1999

CSCI A202

Introduction to Programming II

Final Exam


Each of the multiple choice questions in this test has five alternatives, one of which is the right answer, and the other four being wrong. As announced in class, for the multiple choice questions you can mark more than one answer, if you are unsure. Each correct answer counts as 1 and each incorrect answer counts as -0.25. Your score for a question is the sum of the scores for the answers you have marked. This way you can get partial credit and your score for a multiple-choice question will range in the interval [-n, n] for a question that weights n points. (Highest payoff is when you mark only one answer, the one that is correct).

Work as fast as you can, without being careless. Good luck and do well!


1. (3 points) What is the result of attempting to compile and run this code?
class Alpha { 
  public static void main(String[] args) {
    Beta f = new Beta(); 
  } 
  Alpha(int i) {  } 
}
 
class Beta extends Alpha { }

The code does not compile because Beta does not define a no-args constructor.
The code does not compile because Beta does not define any constructors whatsoever.
The code compiles and runs succesfully, with no output.
The code does not compile because Alpha does not define a no-args constructor.
None of the above.

2. (3 points) What is the result of attempting to compile and run this code?

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

The code does not compile because Beta's constructor is empty.
The code does not compile because Alpha does not define a no-args constructor.
The code compiles and runs succesfully.
The code does not compile because Beta does not define a no-args constructor.
None of the above.

3. (3 points) What is the result of attempting to compile and run this code?

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()      { } 
  Beta(int i) { } 
}

The code does not compile because Beta's constructors are empty.
The code compiles and runs succesfully, but there is no output.
The code compiles and runs succesfully, and outputs 0.
The code compiles and runs succesfully, and outputs 3.
None of the above.

4. (3 points) What is the result of attempting to compile and run this code?

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) {
    this(); 
  } 
}

The code compiles and runs succesfully, and outputs 0.
The code compiles and runs succesfully, and outputs 3.
The code compiles and runs succesfully, and outputs 6.
The code does not compile because at least one of Beta's constructors is not defined legally.
None of the above.

5. (3 points) What is the result of attempting to compile and run this code?

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

The code compiles and runs succesfully, and outputs 0.
The code compiles and runs succesfully, and outputs 3.
The code compiles and runs succesfully, and outputs 6.
The code does not compile because at least one of Beta's constructors is not defined legally.
None of the above.

6. (3 points) What is the result of attempting to compile and run this code?

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(12); 
  } 
  Alpha() { 
    System.out.println(0); 
  } 
  Alpha(int i) { 
    System.out.println(i); 
  }   
}
 
class Beta extends Alpha {
  Beta() {
      
  } 
  Beta(int i) {
    this(); 
    System.out.println(3);
  } 
}
Write your answer here:

7. (3 points) What is the result of attempting to compile and run this code?

class Alpha {
  public static void main(String[] args) {
    Alpha f = new Beta(); 
    System.out.println(f.test(3));  
  } 
  String test(int i) {
    return (i + 2) + " "; 
  } 
} 
 
class Beta extends Alpha {
  String test(int i) {
    return (i + 1) + " "; 
  } 
  String test(long i) {
    return i + " "; 
  } 
}

The code compiles and runs succesfully, and outputs 3.
The code compiles and runs succesfully, and outputs 4.
The code compiles and runs succesfully, and outputs 5.
The code does not compile.
None of the above.

8. (3 points) What is the result of attempting to compile and run this code?

class Alpha {
  public static void main(String[] args) {
    Beta f = new Alpha(); 
    System.out.println(f.test(3));  
  } 
  String test(int i) {
    return (i + 2) + " "; 
  } 
} 
 
class Beta extends Alpha {
  String test(int i) {
    return (i + 1) + " "; 
  } 
}

The code compiles and runs succesfully, and outputs 3.
The code compiles and runs succesfully, and outputs 4.
The code compiles and runs succesfully, and outputs 5.
The code does not compile.
None of the above.

9. (3 points) What is the result of attempting to compile and run this code?

class Alpha {
  public static void main(String[] args) {
    Alpha f = new Alpha(); 
    System.out.println(f.test(3));  
  } 
  String test(int i) {
    return (i + 2) + " "; 
  } 
} 
 
class Beta extends Alpha {
  String test(int i) {
    return (i + 1) + " "; 
  } 
}

The code compiles and runs succesfully, and outputs 3.
The code compiles and runs succesfully, and outputs 4.
The code compiles and runs succesfully, and outputs 5.
The code does not compile.
None of the above.

10. (3 points) What is the result of attempting to compile and run this code?

class Alpha {
  public static void main(String[] args) {
    Beta f = new Beta(); 
    System.out.println(test(3));  
  } 
  String test(int i) {
    return (i + 2) + " "; 
  } 
} 
 
class Beta extends Alpha {
  String test(int i) {
    return (i + 1) + " "; 
  } 
}

The code compiles and runs succesfully, and outputs 3.
The code compiles and runs succesfully, and outputs 4.
The code compiles and runs succesfully, and outputs 5.
The code does not compile.
None of the above.

11. (3 points) What is the result of attempting to compile and run this code?

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(1999); 
  } 
}
 
class Beta extends Alpha {
  Beta  (int value) { super(value); } 
}

The code will compile and 1999 will appear in the standard output.
The code will compile but nothing will appear in the standard output.
The code will not compile because Alpha is abstract.
The code will not compile because there is no default, no-args constructor for Alpha.
None of the above.

12. (3 points) What is the result of attempting to compile and run this code?

class Alpha {
  public static void main(String[] args) {
    System.out.println("... won't compile"); 
  } 
  public static void main() {
    System.out.println("... will not run"); 
  } 
}

The program does not compile because main is not defined correctly.
The program compiles but when you try to run it complains about main.
The program compiles and runs and prints ... won't compile
The program compiles and runs and prints ... will not run
None of the above.

13. (3 points) What is the difference between an object and an object reference?



14. (3 points) How are overloaded methods distinguished from each other?


15. (3 points) How are overridden methods distinguished from each other?

16. (3 points) What is the difference between an abstract class and a class?
 


17. (3 points) Consider the following program:
abstract class Alpha {
  abstract void complain(); 
} 
 
class Beta extends Alpha {
  void complain(String s) {
    System.out.println(s); 
  } 
}
 
class Tester {
  public static void main(String[] args) {
    Beta f = new Beta(); 
    f.complain("There's a tomato in every automaton."); 
  } 
}
What will happen if you try to compile and execute the Tester class?


The program will compile and run.
The code will not compile because Beta has no default no-arg constructor.
The code does not compile because Beta is abstract.
The code does not compile because Tester does not inherit from Alpha.
None of the above.

18. (2 points) An integer could be defined as a digit (0, 1, ..., 9), or a digit followed by an integer. Why is this a recursive definition? Could you come up with a recursive definition for a Java identifier?



19. (7 points) The code below is from an application that extends Frame, draws a circle, has one button and when the button gets pressed the circle moves (randomly) to the right, left, up, or down, for a distance of 10 pixels. Portions of the code have been erased. Complete the code to a working program or use the back of the exam to write your own program that accomplishes the implementation described.

import java.awt.*;
import java.awt.event.*; 
 
class Nineteen extends Frame __________ _______Listener, 
                                        ______________ {
  Button b = new Button("Move"); 
  Circle c = new Circle(100, 100); 
  public static void main(String[] args) {
    Frame f = new ________(); 
    f.setSize(200, 200); 
    f.setVisible(true); 
  } 
  public void _____(Graphics g) {
    ____________ 
  }
  public void _______________(ActionEvent a) {
    _________ 
    __________ 
  } 
  Nineteen() {
    FlowLayout f = new FlowLayout();
    setLayout(_); 
    add(_); 
    b.addActionListener(____); 
    addWindowListener(____); 
  }   
  public void windowActivated(WindowEvent e) { } 
  public void windowClosed(WindowEvent e) { } 
  public void windowClosing(WindowEvent e) { 
    _______________ 
  } 
  public void windowDeactivated(WindowEvent e) { } 
  public void windowDeiconified(WindowEvent e) { } 
  public void windowIconified(WindowEvent e) { } 
  public void windowOpened(WindowEvent e) { } 
}
 
class Circle {
  private int x, y; 
  Circle (int x, int y) {
    this.x = x; this.y = y; 
  } 
  public void render(Graphics g) {
    g.drawOval(x, y, 20, 20); 
  } 
  public void move() {
    switch((int)(Math.random() * 4)) { 
      case 0:  x += 10; break; 
      case 1:  x -= 10; break; 
      case 2:  y += 10; break; 
      default: y -= 10; break; 
    } 
  } 
}
20. (10 points) Write a Processor class that describes objects which upon instantiation fill an internal (instance, that is) array with a certain number of randomly chosen integers (the size of the array being specified as an argument to the constructor). The Processor objects should have a sort() method (defined as an instance method), that will sort the elements in the internal array in descending order. You can use any sorting algorithm that you're most comfortable with to implement sort().

Here's a template that you should feel free to use and finish:

import java.util.*; 
       
class Processor {
  int[] storage; 
  Processor(int n) { 
    // alocates storage and initializes it




  } 
  void show() { 
    for (int i = 0; i < storage.length; i++) {
      System.out.print(storage[i] + " "); 
    } 
    System.out.println(); 
  } 
  void sort() { // sorts the storage array













  } 
  public static void main(String[] args) {
    Processor b = new Processor(10); 
    System.out.println("Before: "); b.show(); 
    b.sort(); 
    System.out.println("After:  "); b.show(); 
  } 
}
and the result of running this class should be:
school.cs.indiana.edu%java Processor
Before: 
7 6 2 3 6 1 1 1 7 10 
After:  
10 7 7 6 6 3 2 1 1 1 
So you need to write a constructor and a method.
21. (10 points) Write a simulation (a class) for a coke machine. A coke machine can contain an arbitrary number of different types of soft drinks (in your example use rootBeer and grapeSoda and feel free to represent them as quantities, that is, ints). When you push the button for a certain drink the quantity of cans of that drink decreases by one (unless it's already zero). There is a certain message (or method invocation) that the coke machine will understand, which refills it (call it, in your implementation, refill()). The capacity of the coke machine is 20 cans for each soft drink. Here's a Tester class:
class Tester {
  public static void main(String[] args) {
    CokeMachine c = new CokeMachine(); 
    c.rootBeer(); 
    c.rootBeer(); 
    c.grapeSoda(); 
    c.refill(); 
    c.rootBeer(); 
  } 
}
and its output:
school.cs.indiana.edu%java Tester
grapeSoda: 20
rootBeer:  20          CokeMachine c = new CokeMachine(); 
-----------------------
grapeSoda: 20
rootBeer:  19          c.rootBeer(); 
-----------------------
grapeSoda: 20
rootBeer:  18          c.rootBeer();
-----------------------
grapeSoda: 19
rootBeer:  18          c.grapeSoda();
-----------------------
grapeSoda: 20
rootBeer:  20          c.refill(); 
-----------------------
grapeSoda: 20
rootBeer:  19          c.rootBeer(); 
-----------------------
Write your answer here:




















22. Consider the following class:
class Link {
  int value; 
  Link next;
  Link (int v, Link n) { value= v; next = n; } 
  public static void main(String[] args) {
    Link a = new Link(1, new Link(2, new Link(3, null))); // [1] 
    a.snoc(4);                                            // [2]  
    System.out.println(a.function()); 
  } 
  int function() {
    if (next == null) { return value; } 
    else { return value + next.function(); } 
    } 
  void snoc(int value) {
    if (next == null) { next = new Link(value, null); }
    else { next.snoc(value); } 
  } 
}
The first line in the main method creates a Link object and assigns it to the variable a.

22.1 (3 points) Draw a diagram that describes the structure to which a points after the assignment statement (first line in main, marked with [1]).


22.2 (3 points) Draw a diagram that describes the structure to which a points after snoc() is invoked on a (this line is marked with [2] in main).


22.3 (3 points) What's the output of this program when you compile and run it?


22.4 (5 points) What is a good name for the method function()? What is a good name for method snoc()? In other words, what do they do?


23. Consider the following code:
class Alpha {
  String message;
  Alpha (String msg) { message = msg; } 
}
 
class Beta extends Alpha {
  Beta (String msg)  { message = msg; } 
}
 
class Tester {
  public static void main(String[] args) {
    Beta f = new Beta("Greetings");
    System.out.println(f.message);  
  } 
}
You place the entire code in a file, called TwentyThree.java, and then try to compile and run it.

23.1 (3 points) This code will not compile. Why?


23.2 (3 points) Change only one line in the code above such that, after your change, the program compiles and runs, producing Greetings in the standard output.