DOC PREVIEW
UW CSE 142 - Study Notes

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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); _____________________________ _____________________________ _____________________________ _____________________________ _____________________________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 + " " + 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)); } }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(); }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: H T H H T T t t T h H h For the input above, your method should produce the following output: 3 heads (60.0%) You win! 2 heads (33.3%) 1 heads (100.0%) You win! The format of your output must exactly match that shown above. You may assume that the Scanner contains at least 1 line of input, that each line contains at least one token, and that no tokens other than h, H, t, or T will be in the lines.5 of 15 5. File Processing Write a static method named findFirstMatch that accepts as its parameters a Scanner for an input file and an array of Strings keywords representing a list of keywords in a search. Your method will read lines from its input Scanner and should return the line number of the first line in the file that contains one or more words from keywords. If none of the keywords are found in the file, your method should return a -1. The search should be case-insensitive, so if a keyword was "banana", the line "yummy baNAna split" would be considered a line that contains the keyword. Your method should also match whole words only, so if the only keyword in the array was "ball", the line "football game" would not be considered a match. For example, consider the following input file saved in sidewalk.txt, consisting of 6 lines: Let us leave this place where the smoke blows black And the dark street winds and bends. Past the pits where the asphalt flowers grow We shall walk with a walk that is measured and slow, And watch where the chalk-white arrows go To the place where the sidewalk ends. The following table shows some calls to your method and their expected results with the following Scanner: Scanner input = new Scanner(new File("sidewalk.txt")); Array Call / Returned Value String[] k1 = {"place", "winds"}; findFirstMatch(input, k1) returns 1 String[] k2 = {"dinosaur", "PITS", "pots"}; findFirstMatch(input, k2) returns 3 String[] k3 = {"chalk", "row", "g", "ends"}; findFirstMatch(input, k3) returns -1 String[] k4 = {"to"}; findFirstMatch(input, k4) returns 6 You may assume that none of the words in the keywords array contain spaces, i.e. all keywords are single whole words, and the array contains at least one element. Do not modify the elements of the keywords array.6 of 15 6. Array Programming Write a static method named range that takes an array of integers as a parameter and returns the range of values contained in the array. The range of an array is defined to be one more than the difference between its largest and smallest element. For example, if the largest element in the array is 15 and the smallest is 4, the range is 12. If the largest and smallest values are the same, the range is 1. The following table shows some calls to your method and their results (the largest and smallest values are underlined): Array Returned Value int[] a1 = {8, 3, 5, 7, 2, 4}; range(a1) returns 7 int[] a2 = {15, 22, 8, 19, 31}; range(a2) returns 24 int[] a3 = {3, 10000000, 5, -29, 4}; range(a3) returns 10000030 int[] a4 = {100, 5}; range(a4) returns 96 int[] a5 = {32}; range(a5) returns 1 You may assume that the array contains at least one element (that its length is at least 1). You should not make any assumptions about the values of the particular elements in the array; they could be extremely large, very small, etc.


View Full Document

UW CSE 142 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?