,TestBank For Building Java Programs: A Back to Basics Approach 5th Edition
Final 1
Sample Final Exam #1
(Spring 2005; thanks to Stuart Reges)
1. Array Mystery
Consider the following method:
public static void arrayMystery(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = i + a[i - 1] - a[i];
}
}
Indicate in the right-hand column what values would be stored in the array after the method arrayMystery executes
if the integer array in the left-hand column is passed as a parameter to it.
Original Contents of Array Final Contents of Array
,TestBank For Building Java Programs: A Back to Basics Approach 5th Edition
Final 1
2. Reference Semantics Mystery
The following program produces 4 lines of output. Write the output below, as it would appear on the console.
public class BasicPoint {
int x;
int y;
public BasicPoint() {
x = 2;
y = 2;
}
}
public class ReferenceMystery {
public static void main(String[] args) {
int a = 7;
int b = 9;
BasicPoint p1 = new BasicPoint();
BasicPoint p2 = new BasicPoint();
public static void addToXTwice(int a, BasicPoint p1) {
a = a + a;
p1.x = a;
System.out.println(a + " " + p1.x);
}
}
2
, TestBank For Building Java Programs: A Back to Basics Approach 5th Edition
Final 1
3. Inheritance Mystery
Assume that the following classes have been defined:
public class A extends B { public class C {
public void method2() { public String toString() {
System.out.print("a 2 "); return "c";
method1(); }
}
} public void method1() {
System.out.print("c 1 ");
public class B extends C { }
public String toString() {
return "b"; public void method2() {
} System.out.print("c 2 ");
}
public void method2() { }
System.out.print("b 2 ");
super.method2(); public class D extends B {
} public void method1() {
} System.out.print("d 1 ");
method2();
}
}
Given the classes above, what output is produced by the following code?
C[] elements = {new A(), new B(), new C(), new D()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
System.out.println();
elements[i].method2();
System.out.println();
System.out.println();
}
3
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller zusukihihi. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $49.99. You're not tied to anything after your purchase.