final_sample_2 - CSE 142 Sample Final Exam #2 (based on...

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

View Full Document Right Arrow Icon
1 of 12 CSE 142 Sample Final Exam #2 (based on Winter 2005's final; thanks to Stuart Reges) 1. Array Mystery Consider the following method: public static int arrayMystery(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 arrayMystery if the integer array in the left-hand column is passed as its parameter. Original Contents of Array Value Returned int[] a1 = {8}; int result1 = arrayMystery(a1); int[] a2 = {14, 7}; int result2 = arrayMystery(a2) ; int[] a3 = {7, 1, 3, 2, 0, 4}; int result3 = arrayMystery(a3); int[] a4 = {10, 8, 9, 5, 6}; int result4 = arrayMystery(a4); int[] a5 = {8, 10, 8, 6, 4, 2}; int result5 = 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 12 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 y = 1; int x = 3; int[] a = new int[4]; mystery(a, y, x); System.out.println(x + " " + y + " " + Arrays.toString(a)); x = y - 1; mystery(a, y, x); System.out.println(x + " " + y + " " + Arrays.toString(a)); } public static void mystery(int[] a, int x, int y) { if (x < y) { x++; a[x] = 17; } else { a[y] = 17; } System.out.println(x + " " + y + " " + Arrays.toString(a)); } }
Background image of page 2
3 of 12 3. Inheritance Mystery Assume that the following classes have been defined: public class Pen extends Sock { public void method1() { System.out.print("pen 1 "); } } public class Lamp { public void method1() { System.out.print("lamp 1 "); } public void method2() { System.out.print("lamp 2 "); } public String toString() { return "lamp"; } } public class Book extends Sock { public void method2() { System.out.print("book 2 "); super.method2(); } } public class Sock extends Lamp { public void method1() { System.out.print("sock 1 "); method2(); } 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(); 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 12 4. File Processing 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:
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 / 12

final_sample_2 - CSE 142 Sample Final Exam #2 (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