final_sample_7

# final_sample_7 - CSE 142 Sample Final Exam#7(based on...

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

1 of 17 CSE 142 Sample Final Exam #7 (based on Autumn 2008's final) 1. Array Mystery Consider the following method: public static void arrayMystery(int[] a) { for (int i = a.length - 2; i > 0; i--) { if (a[i - 1] < a[i + 1]) { a[i] += a[i - 1]; } else { a[i] += a[i + 1]; } } } 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 = {1, 2, 3}; arrayMystery(a1); int[] a2 = {8, 2, 3, 1, 6}; arrayMystery(a2); int[] a3 = {1, 1, 1, 1, 1, 1}; arrayMystery(a3); int[] a4 = {40, 10, 25, 5, 10, 30}; arrayMystery(a4); int[] a5 = {15, 6, -1, 4, 8, -2, 7, 4}; arrayMystery(a5); _____________________________ _____________________________ _____________________________ _____________________________ _____________________________

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

View Full Document
2 of 17 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 ReferenceMystery { public static void main(String[] args) { int x = 1; int[] a = new int[4]; x++; a[x - 1] = 3; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); x++; a[x - 1] = 2; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); } public static void mystery(int x, int[] a) { a[x]++; x--; a[x - 1] = a[x + 1]; System.out.println(x + " " + Arrays.toString(a)); } }
3 of 17 3. Inheritance Mystery Assume that the following classes have been defined: public class McCain extends Biden { public void republican() { System.out.print("mccain-R "); } } public class Palin { public void republican() { System.out.print("palin-R "); } public void democrat() { republican(); System.out.print("palin-D "); } public String toString() { return "palin"; } } public class Obama extends Palin { public void republican() { super.republican(); System.out.print("obama-R "); } } public class Biden extends Palin { public String toString() { return "biden"; } public void democrat() { System.out.print("biden-D "); super.democrat(); } } Given the classes above, what output is produced by the following code? Palin[] politicians = {new Biden(), new Palin(), new McCain(), new Obama()}; for (int i = 0; i < politicians.length; i++) { System.out.println( politicians[i] ); politicians[i].republican(); System.out.println(); politicians[i].democrat(); System.out.println(); System.out.println(); }

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

View Full Document
4 of 17 4. File Processing Write a static method named reportBlankLines that accepts a Scanner containing an input file as a parameter and that outputs the line numbers of any blank lines and that reports the total number of blank lines in the file. For example, given the following input file: Remember that a file can have blank lines like the one below: A blank line: is read as a String of length 0 by Scanner Your method should print the following output: line 4 is blank line 6 is blank line 9 is blank total blank lines = 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 17

final_sample_7 - CSE 142 Sample Final Exam#7(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