Thu May 6 14:45:00 EST 1999
Introduction to Programming II
Final Exam
1
-0.25
[-n, n]
n
Work as fast as you can, without being careless. Good luck and do well!
class Alpha { public static void main(String[] args) { Beta f = new Beta(); } Alpha(int i) { } } class Beta extends Alpha { }
Beta
Alpha
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) { } }
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) { } }
0
3
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(); } }
6
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(); } }
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); } }
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 + " "; } }
4
5
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) + " "; } }
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) + " "; } }
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) + " "; } }
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); } }
1999
abstract
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"); } }
main
... won't compile
... will not run
13. (3 points) What is the difference between an object and an object reference?
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."); } }
Tester
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?
9
Frame
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; } } }
Processor
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(); } }
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
rootBeer
grapeSoda
int
refill()
20
class Tester { public static void main(String[] args) { CokeMachine c = new CokeMachine(); c.rootBeer(); c.rootBeer(); c.grapeSoda(); c.refill(); c.rootBeer(); } }
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(); -----------------------
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); } } }
Link
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?
[1]
snoc()
[2]
function()
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); } }
TwentyThree.java
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.
Greetings