final_sample_1

# final_sample_1 - CSE 142 Sample Final Exam #1 (based on...

This preview shows pages 1–5. Sign up to view the full content.

1 of 12 CSE 142 Sample Final Exam #1 (based on Spring 2005's final; 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 int[] a1 = {7}; arrayMystery(a1); int[] a2 = {4, 3, 6}; arrayMystery(a2); int[] a3 = {7, 4, 8, 6, 2}; arrayMystery(a3); int[] a4 = {10, 2, 5, 10}; arrayMystery(a4); int[] a5 = {2, 4, -1, 6, -2, 8}; arrayMystery(a5); _____________________________ _____________________________ _____________________________ _____________________________ _____________________________

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 of 12 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(); addToXTwice(a, p1); System.out.println(a + " " + b + " " + p1.x + " " + p2.x); addToXTwice(b, p2); System.out.println(a + " " + b + " " + p1.x + " " + p2.x); } public static void addToXTwice(int a, BasicPoint p1) { a = a + a; p1.x = a; System.out.println(a + " " + p1.x); } }
3 of 12 3. Inheritance Mystery Assume that the following classes have been defined: public class A extends B { public void method2() { System.out.print("a 2 "); method1(); } } public class B extends C { public String toString() { return "b"; } public void method2() { System.out.print("b 2 "); super.method2(); } } public class C { public String toString() { return "c"; } public void method1() { System.out.print("c 1 "); } public void method2() { System.out.print("c 2 "); } } 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(); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 of 12 4. File Processing Write a static method named printStrings that takes as a parameter a Scanner holding a sequence of integer/string pairs and that prints to System.out one line of output for each pair with the given String repeated the given number of times. For example if the Scanner contains the following data: 6 fun. 3 hello 10 <> 2 25 4 wow! your method should produce the following output:
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/15/2011 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

### Page1 / 12

final_sample_1 - CSE 142 Sample Final Exam #1 (based on...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online