final_sample_4

# final_sample_4 - CSE 142 Sample Final Exam#4(based on...

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

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

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

View Full Document
2 of 14 2. Reference Semantics Mystery The following program produces 4 lines of output. Write the output below, as it would appear on the console. import java.util.*; // for Arrays class public class ReferenceMystery { public static void main(String[] args) { int x = 0; int[] a = new int[2]; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); x++; a[1] = a.length; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); } public static void mystery(int x, int[] list) { list[x]--; x++; System.out.println(x + " " + Arrays.toString(list)); } }
3 of 14 3. Inheritance Mystery Assume that the following classes have been defined: public class Brian extends Lois { public void b() { a(); System.out.print("Brian b "); } public String toString() { return "Brian"; } } public class Lois extends Meg { public void a() { System.out.print("Lois a "); super.a(); } public void b() { System.out.print("Lois b "); } } public class Meg { public void a() { System.out.print("Meg a "); } public void b() { System.out.print("Meg b "); } public String toString() { return "Meg"; } } public class Stewie extends Brian { public void a() { super.a(); System.out.print("Stewie a "); } public String toString() { return super.toString() + " Stewie"; } } Given the classes above, what output is produced by the following code? Meg[] elements = {new Lois(), new Stewie(), new Meg(), new Brian()}; for (int i = 0; i < elements.length; i++) { elements[i].a(); System.out.println(); elements[i].b(); System.out.println(); System.out.println( elements[i] ); System.out.println(); }

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

View Full Document
4 of 14 4. File Processing Write a static method named tokenStats that accepts as a parameter a Scanner containing a series of tokens. It should print out the sum of all the tokens that are legal integers, the sum of all the tokens that are legal doubles but not integers, and the total number of tokens of any kind. For example, if a Scanner called data contains the following tokens: 3 3.14 10 squid 10.x 6.0 Then the call of tokenStats(data); should print the following output: integers: 13 real numbers: 9.14 total tokens: 6 If the Scanner has no tokens, the method should print: integers: 0 real numbers: 0.0 total tokens: 0
5 of 14 5. File Processing Write a static method named pizza that accepts as its parameter a Scanner for an input file. Imagine that a college dorm room contains several boxes of leftover pizza. A complete pizza has 8 slices. The pizza boxes left in the room each contain either an entire pizza, half a pizza (4 slices), or a single slice. Your method's task is to figure out the fewest boxes needed to store all leftover pizza, if all the partial pizzas were consolidated together as much as possible.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern