final_sample_3 - CSE 142 Sample Final Exam #3 (based on...

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

View Full Document Right Arrow Icon
1 of 15 CSE 142 Sample Final Exam #3 (based on Autumn 2007'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]++; } } } 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 = {42}; arrayMystery(a1); int[] a2 = {1, 8, 3, 6}; arrayMystery(a2); int[] a3 = {5, 5, 5, 5, 5}; arrayMystery(a3); int[] a4 = {10, 7, 9, 6, 8, 5}; arrayMystery(a4); int[] a5 = {1, 0, 1, 0, 0, 1, 0}; 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 15 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[4]; x++; mystery(x, a); System.out.println("x is " + x + " and a is " + Arrays.toString(a)); x++; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); } public static void mystery(int x, int[] a) { x++; a[x]++; System.out.println(x + " " + Arrays.toString(a)); } }
Background image of page 2
3 of 15 3. Inheritance Mystery Assume that the following classes have been defined: public class Vier extends Drei { public void method2() { super.method2(); System.out.print("Vier 2 "); } public String toString() { return "Vier " + super.toString(); } } public class Zwei extends Eins { public void method2() { System.out.print("Zwei 2 "); method1(); } } public class Drei extends Zwei { public void method1() { System.out.print("Drei 1 "); } public String toString() { return "Drei"; } } public class Eins { public String toString() { return "Eins"; } public void method1() { System.out.print("Eins 1 "); } public void method2() { System.out.print("Eins 2 "); } } Given the classes above, what output is produced by the following code? Eins[] elements = {new Zwei(), new Eins(), new Vier(), new Drei()}; 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(); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 of 15 4. File Processing Write a static method named coinFlip that accepts as its parameter a Scanner for an input file. Assume that the input file data represents results of sets of coin flips that are either heads (H) or tails (T) in either upper or lower case, separated by at least one space. Your method should consider each line to be a separate set of coin flips and should output to the console the number of heads and the percentage of heads in that line, rounded to the nearest tenth. If this percentage is more than 50%, you should print a "You win" message. For example, consider the following input file:
Background image of page 4
Image of page 5
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 / 15

final_sample_3 - CSE 142 Sample Final Exam #3 (based on...

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

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