{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cse142_final_sample_2

# cse142_final_sample_2 - CSE 142 Sample Final Exam#2 1...

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

CSE 142 Sample Final Exam #2 1. Expressions (5 points) For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double , String s in quotes). E x pre ssi o n V a lue 12 / 5 + 8 / 4 2.5 * 2 + 17 / 4 4.0 > 2.5 || (!(5 < 2) && 1.1 > 0) 21 / 2 + "7 % 3" + 17 % 4 46 / 3 / 2.0 / 3 * 4 / 5 2. Array Mystery (10 points) Consider the following method: public static int mystery(int[] array) { int x = 0; for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) { x++; } } return x; } In the left-hand column below are specific arrays of integers. Indicate in the right-hand column what value would be returned by method mystery if the integer array in the left-hand column is passed as its parameter. A rr a y V a lue R e tu r n e d int[] a1 = {8}; mystery(a1); int[] a2 = {14, 7}; mystery(a2); int[] a3 = {7, 1, 3, 2, 0, 4}; mystery(a3); int[] a4 = {10, 8, 9, 5, 6}; mystery(a4); int[] a5 = {8, 10, 8, 6, 4, 2}; mystery(a5); 1 of 8

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

View Full Document
3. Inheritance Mystery (10 points) Assume that the following classes have been defined: public class Pen extends Sock { public void method1() { System.out.println("pen 1"); } } public class Lamp { public void method1() { System.out.println("lamp 1"); } public void method2() { System.out.println("lamp 2"); } public String toString() { return "lamp"; } } public class Book extends Sock { public void method2() { System.out.println("book 2"); } } public class Sock extends Lamp { public void method1() { System.out.println("sock 1"); } public String toString() { return "sock"; } } Given the classes above, what output is produced by the following code? Lamp[] elements = {new Book(), new Pen(), new Lamp(), new Sock()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); } 2 of 8
4. File Processing (15 points) Write a static method named wordStats that accepts as its parameter a Scanner holding a sequence of words and that reports the total number of words and the average word length. For example, suppose the Scanner is scanning an input source that contains the following words: To be or not to be, that is the question. For the purposes of this problem, we will use whitespace to separate words. That means that some words include punctuation, as in "be," . (This is the same definition that the Scanner uses for tokens.) For the input above, your method should produce exactly the following output: Total words = 10 Average length = 3.2 5. File Processing (10 points)

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 ]}

### Page1 / 8

cse142_final_sample_2 - CSE 142 Sample Final Exam#2 1...

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

View Full Document
Ask a homework question - tutors are online