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 1 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 2 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 Drei extends Zwei public void method1 System out print Drei 1 public String toString return Drei public class Zwei extends Eins public void method2 System out print Zwei 2 method1 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 3 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 4 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 String String String String k1 k2 k3 k4 place winds dinosaur PITS pots chalk row g ends to Call Returned Value k1 returns 1 k2 returns 3 k3 returns 1 k4 returns 6 findFirstMatch input findFirstMatch input findFirstMatch input findFirstMatch input 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 5 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 int int int int int a1 a2 a3 a4 a5 8 3 5 7 2 4 15 22 8 19 31 3 10000000 5 29 4 100 5 32 Returned Value returns 7 returns 24 returns 10000030 returns 96 returns 1 range a1 range a2 range a3 range a4 range a5 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 You should not modify the contents of the array 6 of 15 7 Array Programming Write a static method named zeroOut that accepts two arrays of integers a1 and a2 as parameters and replaces any occurrences of a2 in a1 with zeroes The sequence of elements in a2 may appear anywhere in a1 but must appear consecutively and in the same order For example if variables called a1 and a2 store the following values int a1 1 2 3 4 1 2 3 4 5 int a2 2 3 4 The call of zeroOut a1 a2 should modify a1 s contents to be 1 0 0 0 1 0 0 0 5 Note that the pattern can occur many times even consecutively For the following two arrays a3 and a4 int a3 5 …
View Full Document
Unlocking...