{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–5. 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); _____________________________ _____________________________ _____________________________ _____________________________ _____________________________
Image of page 1

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

View Full Document Right 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(); }
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.
Image of page 3

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

View Full Document Right Arrow Icon
4 of 12 5. File Processing Write a static method named plusScores that accepts as a parameter a Scanner containing a series of lines that represent student records. Each student record takes up two lines of input. The first line has the student's name and the second line has a series of plus and minus characters. Below is a sample input: Kane, Erica --+-+ Chandler, Adam ++-+ Martin, Jake +++++++ Dillon, Amanda ++-++-+- The number of plus/minus characters will vary, but you may assume that at least one such character appears and that no other characters appear on the second line of each pair.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern