36-practice-final

36-practice-final - CS 106A Summer 2009 August 11, 2009...

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

View Full Document Right Arrow Icon
CS 106A Summer 2009 August 11, 2009 Practice Final Examination We have omitted the space for the exam in order to save trees, but please please please, set aside 3 hours to do this exam before you look at the answers! Final Exam: Terman Auditorium, August 14, 2009 - 8:30am-11:30am Problem 1 – Short Answer (11 points) 1a) Suppose that the char array letters has been declared and initialized as follows: char[] letters = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; This declaration sets up an array of six elements with the initial values shown in the diagram below: letters ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ Given this array, what is the effect of calling the method mystery(letters); if mystery is defined below as: private String mystery(char[] array) { String result = ""; for(int i = array.length-1; i >= 2; i--) { if(i < array.length/2 + i%4) { result += (char)((i-1)*(i-1) + 'h'); } else result += array[i]; } return result; } Work through the method carefully, and indicate what is returned by the call to mystery below: 1b) Below is a method Osvaldo wrote when he was developing the Hangman solutions . Unfortunately, as is often the case with Osvaldo’s code, the method is very buggy. Circle the bugs in the code and write a short sentence for each explaining the precise nature of the problem. public class HangmanLetterTester extends ConsoleProgram { //Instance Variables private char ch; No, really. This is actually Osvaldo’s original solution code.
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 – //Lots more Hangman code (possibly) /* * Method: revealLetter(word, display, ch) * -------------------- * Replaces ‘-‘ with ch in display, wherever ch * would occur in word. * * word – hidden word not shown to player * display – word displayed to player, with dashes * for letters not yet guessed * ch - letter guessed by player * * Example: * revealLetter(“bookkeeper”, “b--kk--p--”) * * should return the string “b--kkeepe-“. * This means our run should print out: * * Guessing e on b--kk--p-- gives us: b--kkeepe- */ public void run() { ch = 'e'; String word = "bookkeeper"; String soFar = "b--kk--p--"; String answer = revealLetter(word, soFar); ch = 'f'; println(“Guessing e on “ + soFar + “ gives us: “ + answer); } private String revealLetter(String word, String display) { String nuWord; for(int i = 1; i < word.length(); i++) { char ch = word.charAt(i); if(ch.equals(this.ch)) { nuWord += this.ch; }else{ nuWord += display; } } return nuWord; } } Problem 2 – Arrays (19 points) Odometer Since most of you have driven a car or at least have aspired to do so, we’re going to have you write a program that simulates an odometer. What is an odometer and what does it look like? Well it’s the reading that comes from how many miles you have driven a car, and looks like this:
Background image of page 2
– 3 – Pictured: The odometer from my Testarossa What you are going to do is write a program that is going to simulate an odometer by creating an array that will hold the 7 integers, with each element holding a digit from the
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/14/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 10

36-practice-final - CS 106A Summer 2009 August 11, 2009...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online