{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

final_sample_6

# final_sample_6 - CSE 142 Sample Final Exam#6(based on...

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

1 of 14 CSE 142 Sample Final Exam #6 (based on Summer 2008's final; thanks to Hélène Martin) 1. Array Mystery Consider the following method: public static void arrayMystery(String[] a) { for (int i = 0; i < a.length; i++) { a[i] = a[i] + a[a.length - 1 - 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 String[] a1 = {"a", "b", "c"}; arrayMystery(a1); String[] a2 = {"a", "bb", "c", "dd"}; arrayMystery(a2); String[] a3 = {"z", "y", "142", "w", "xx"}; arrayMystery(a3); _____________________________ _____________________________ _____________________________

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

View Full Document
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. public class Pokemon { int level; public Pokemon(int level) { this.level = level; } } public class ReferenceMystery { public static void main(String[] args) { int hp = 10; Pokemon squirtle = new Pokemon(5); battle(squirtle, hp); System.out.println("Level " + squirtle.level + ", " + hp + " hp"); hp = hp + squirtle.level; battle(squirtle, hp + 1); System.out.println("Level " + squirtle.level + ", " + hp + " hp"); } public static void battle(Pokemon poke, int hp) { poke.level++; hp -= 5; System.out.println("Level " + poke.level + ", " + hp + " hp"); } }
3 of 14 3. Inheritance Mystery Assume that the following classes have been defined: public class Dog extends Cat { public void m1() { m2(); System.out.print("dog 1 "); } } public class Lion extends Dog { public void m2() { System.out.print("lion 2 "); super.m2(); } public String toString() { return "lion"; } } public class Cat { public void m1() { System.out.print("cat 1 "); } public void m2() { System.out.print("cat 2 "); } public String toString() { return "cat"; } } Given the classes above, what output is produced by the following code? Cat[] elements = {new Dog(), new Cat(), new Lion()}; for (int i = 0; i < elements.length; i++) { elements[i].m1(); System.out.println(); elements[i].m2(); System.out.println(); System.out.println( elements[i] ); System.out.println(); }

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

View Full Document
4 of 14 4. File Processing Write a static method evaluate that accepts as a parameter a Scanner containing a series of tokens representing a numeric expression involving addition and subtraction and that returns the value of the expression. For example, if a Scanner called data contains the following tokens: 4.2 + 3.4 - 4.1 The call of evaluate(data); should evaluate the result as (4.2+3.4-4.1) = (7.6-4.1) = 3.5 and should return this value as its result. Every expression will begin with a real number and then will have a series of operator/number pairs that follow. The operators will be either + (addition) or - (subtraction). As in the example above, there will be spaces separating numbers and operators. You may assume the expression is legal. Your program should evaluate operators sequentially from left to right. For example, for this expression: 7.3 - 4.1 - 2.0 your method should evaluate the operators as follows: 7.3 - 4.1 - 2.0 = (7.3 - 4.1) - 2.0 = 3.2 - 2.0 = 1.2 The Scanner might contain just a number, in which case your method should return that number as its result.
5 of 14 5. File Processing Write a static method blackjack that accepts as its parameter a Scanner for an input file containing a hand of playing cards, and returns the point value of the hand in the card game Blackjack.

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