final_sample_8 - CSE 142 Sample Final Exam #8 (based on...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 of 12 CSE 142 Sample Final Exam #8 (based on Summer 2009's final; thanks to Victoria Kirst) 1. Array Mystery Consider the following method: public static void arrayMystery(int[] a) { for (int i = 1; i < a.length - 1; i++) { a[i] = a[i + 1] + 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 = {3, 7}; arrayMystery(a1); int[] a2 = {4, 7, 4, 2, 10, 9}; arrayMystery(a2); int[] a3 = {1, 5, 0, 0, 5, 0}; arrayMystery(a3); int[] a4 = {13, 0, -4, -2, 0, -1}; arrayMystery(a4); int[] a5 = {2, 4, 6, 8, 16}; arrayMystery(a5); _____________________________ _____________________________ _____________________________ _____________________________ _____________________________
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 of 12 2. Reference Semantics Mystery (Missing; we didn't give this type of question that quarter.) 3. Inheritance Mystery Assume that the following classes have been defined: public class Denny extends John { public void method1() { System.out.print("denny 1 "); } public String toString() { return "denny " + super.toString(); } } public class Cass { public void method1() { System.out.print("cass 1 "); } public void method2() { System.out.print("cass 2 "); } public String toString() { return "cass"; } } public class Michelle extends John { public void method1() { System.out.print("michelle 1 "); } } public class John extends Cass { public void method2() { method1(); System.out.print("john 2 "); } public String toString() { return "john"; } } Given the classes above, what output is produced by the following code? Cass[] elements = {new Cass(), new Denny(), new John(), new Michelle()}; for (int i = 0; i < elements.length; i++) { elements[i].method1(); System.out.println(); elements[i].method2(); System.out.println(); System.out.println( elements[i] ); System.out.println(); }
Background image of page 2
3 of 12 4. File Processing Write a static method called runningSum that accepts as a parameter a Scanner holding a sequence of real numbers and that outputs the running sum of the numbers followed by the maximum running sum. In other words, the n th number that you report should be the sum of the first n numbers in the Scanner and the maximum that you report should be the largest such value that you report. For example if the Scanner contains the following data: 3.25 4.5 -8.25 7.25 3.5 4.25 -6.5 5.25 your method should produce the following output: running sum = 3.25 7.75 -0.5 6.75 10.25 14.5 8.0 13.25 max sum = 14.5 The first number reported is the same as the first number in the Scanner (3.25). The second number reported is the sum of the first two numbers in the Scanner (3.25 + 4.5). The third number reported is the sum of the first three numbers in the Scanner (3.25 + 4.5 + -8.25). And so on. The maximum of these values is 14.5, which is reported on the second line of output. You may assume that there is at least one number to read.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
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_8 - CSE 142 Sample Final Exam #8 (based on...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online