final_sample_4 - CSE 142 Sample Final Exam #4 (based on...

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

View Full Document Right Arrow Icon
1 of 14 CSE 142 Sample Final Exam #4 (based on Winter 2008's final) 1. Array Mystery Consider the following method: public static void arrayMystery(int[] a) { for (int i = a.length - 1; i >= 0; i--) { if (a[i] == a[a.length - i - 1]) { a[i]++; a[a.length - i - 1]++; } } } 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 = {1, 8, 3, 8, 7}; arrayMystery(a1); int[] a2 = {4, 0, 0, 4, 0, 0, 4, 0}; arrayMystery(a2); int[] a3 = {9, 8, 7, 6, 4, 6, 2, 9, 9}; arrayMystery(a3); int[] a4 = {42}; arrayMystery(a4); int[] a5 = {5, 5, 5, 6, 5, 5, 5}; 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 14 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[2]; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); x++; a[1] = a.length; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); } public static void mystery(int x, int[] list) { list[x]--; x++; System.out.println(x + " " + Arrays.toString(list)); } }
Background image of page 2
3 of 14 3. Inheritance Mystery Assume that the following classes have been defined: public class Brian extends Lois { public void b() { a(); System.out.print("Brian b "); } public String toString() { return "Brian"; } } public class Lois extends Meg { public void a() { System.out.print("Lois a "); super.a(); } public void b() { System.out.print("Lois b "); } } public class Meg { public void a() { System.out.print("Meg a "); } public void b() { System.out.print("Meg b "); } public String toString() { return "Meg"; } } public class Stewie extends Brian { public void a() { super.a(); System.out.print("Stewie a "); } public String toString() { return super.toString() + " Stewie"; } } Given the classes above, what output is produced by the following code? Meg[] elements = {new Lois(), new Stewie(), new Meg(), new Brian()}; for (int i = 0; i < elements.length; i++) { elements[i].a(); System.out.println(); elements[i].b(); System.out.println(); System.out.println( elements[i] ); 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. File Processing Write a static method named tokenStats that accepts as a parameter a Scanner containing a series of tokens. It should print out the sum of all the tokens that are legal integers, the sum of all the tokens that are legal doubles but not integers, and the total number of tokens of any kind. For example, if a Scanner called data contains the following tokens: 3 3.14 10 squid 10.x 6.0 Then the call of tokenStats(data); should print the following output: integers: 13 real numbers: 9.14
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 / 14

final_sample_4 - CSE 142 Sample Final Exam #4 (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