Thu May 6 14:45:00 EST 1999
Introduction to Programming II
Final Exam with Answers
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); } }
0 3
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?
The object reference is just a variable, a name for an object.
Overloaded methods are distinguished by having their own signature, which includes the number, order, and type of the parameters. The return type is not part of the distinguishing signature.
When a child class overrides the definition of a parent's method, two versions of that method exist. If a polymorphic reference is used to invoke the method, the specific version of the method that gets invoked is determined by the type of the object being referred to, not by the type of the reference variable. (25. "How is overriding related to polymorphism?")
A class can be instantiated; an abstract class cannot. Abstract classes often contain abstract methods that do not have implementations.
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
It would be fair enough to say that an identifier is an alphabetic character or an alphabetic character followed by an identifier. This could be refined in a number of ways and be made more precise to allow for alphanumeric characters inside the identifier but not as the leading character.
Frame
import java.awt.*; import java.awt.event.*; class Nineteen extends Frame implements ActionListener, WindowListener { Button b = new Button("Move"); Circle c = new Circle(100, 100); public static void main(String[] args) { Frame f = new Nineteen(); f.setSize(200, 200); f.setVisible(true); } public void paint(Graphics g) { c.render(g) } public void actionPerformed(ActionEvent a) { c.move(); repaint(); } Nineteen() { FlowLayout f = new FlowLayout(); setLayout(f); add(b); b.addActionListener(this); addWindowListener(this); } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { System.exit(0); } 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 storage = new int[n]; for (int i = 0; i < storage.length; i++) { storage[i] = (int)(Math.random()*10) + 1; } } void show() { for (int i = 0; i < storage.length; i++) { System.out.print(storage[i] + " "); } System.out.println(); } void sort() { // sorts the storage array int aux; boolean sorted; do { sorted = true; for (int i = 0; i < storage.length - 1; i++) { if (storage[i] < storage[i+1]) { aux = storage[i]; storage[i] = storage[i+1]; storage[i+1] = aux; sorted = false; } } } while (! sorted); } 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 CokeMachine { int rootBeer; int grapeSoda; CokeMachine() { rootBeer = 20; grapeSoda = 20; show(); } void rootBeer() { if (rootBeer > 0) { rootBeer -= 1; show(); } else { System.out.println("Out of rootBeer. Sorry"); } } void grapeSoda() { if (grapeSoda > 0) { grapeSoda -= 1; show(); } else { System.out.println("Out of rootBeer. Sorry"); } } void refill() { grapeSoda = 20; rootBeer = 20; show(); } void show() { System.out.println("grapeSoda: " + grapeSoda); System.out.println("rootBeer: " + rootBeer); System.out.println("-----------------------"); } }
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]).
[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).
snoc()
[2]
22.3 (3 points) What's the output of this program when you compile and run it? 10 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? function() sums up the values in a list, so sum() would be a good name for it. snoc() appends a cell at the end of a list, so one good name for it would be append()
10
function()
function() sums up the values in a list, so sum() would be a good name for it. snoc() appends a cell at the end of a list, so one good name for it would be append()
sum()
snoc() appends a cell at the end of a list, so one good name for it would be append()
append()
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? Class Beta's only constructor class Beta extends Alpha { Beta (String msg) { message = msg; } } actually has a super(); inserted as the first line (by Java) due to constructor chaining. And that requires a no-arg constructor in Alpha(). But Alpha has only one constructor, that takes a String as an argument. 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. How about this: class Beta extends Alpha { Beta (String msg) { super(msg); } // message = msg; } }
Class Beta's only constructor class Beta extends Alpha { Beta (String msg) { message = msg; } } actually has a super(); inserted as the first line (by Java) due to constructor chaining. And that requires a no-arg constructor in Alpha(). But Alpha has only one constructor, that takes a String as an argument.
class Beta extends Alpha { Beta (String msg) { message = msg; } }
super();
Alpha()
String
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
How about this:
class Beta extends Alpha { Beta (String msg) { super(msg); } // message = msg; } }