CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key

Similar documents
CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key for review problems

CIS 110 Introduction To Computer Programming. November 21st, 2011 Exam 2

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key for review problems

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

CIS Introduction to Computer Programming. 5 October 2012 Midterm

Assignment 11: Critters

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

CSE 142 SPRING 2008 FINAL EXAM

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

CIS 110 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

CIS Introduction to Computer Programming Spring Exam 1

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

CSE 142, Winter 2007 Final Exam. Name:

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

Building Java Programs

CS 312 Final Fall 2016

CSE 142, Autumn 2010 Final Exam Wednesday, December 15, Name:

Critters. Critter #2 Attack.ROAR Attack.POUNCE Attack.SCRATCH. Critter #1

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CS 112 Introduction to Programming

You must bring your ID to the exam.

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

CSc 110, Spring 2017 Lecture 38: Critters. Adapted from slides by Marty Stepp and Stuart Reges

Admin. CS 112 Introduction to Programming. Admin. Admin. Recap. Recap: Polymorphism and Arrays. the Google doc to record the teaming

CSE 142 Sample Final Exam #1

Building Java Programs

CSE 142 Sample Final Exam #1 (based on Spring 2005's final; thanks to Stuart Reges)

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

CSE142 Sample Final Exam Key Autumn 2018

Building Java Programs

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

Sample Midterm Exam #2

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSE 142, Spring 2010 Final Exam Wednesday, June 9, Name: Student ID #: Rules:

Homework 9: Critters (cont.)

CS 312 Final Fall 2013

CSE142 Final Exam Key Summer 2018

CS 112 Introduction to Programming

Model Solutions. COMP 102: Test 1. 6 April, 2016

CSE 142, Summer 2010 Midterm Exam, Friday, July 30, 2010

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

Critter #1 Attack.ROAR random winner #2 wins #1 wins Attack.POUNCE #1 wins random winner #2 wins Attack.SCRATCH #2 wins #1 wins random winner

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

COMP 110 Programming Exercise: Simulation of the Game of Craps

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CSc 110, Spring 2017 Lecture 37: Critters. Adapted from slides by Marty Stepp and Stuart Reges

CS 112 Introduction to Programming. (Spring 2012)

1. Reference Mystery, 5 points. What output is produced by this program? import java.util.*;

Midterm Review Session

The keyword list thus far: The Random class. Generating "Random" Numbers. Topic 16

Computer Science 145

APCS Semester #1 Final Exam Practice Problems

Topic 16. battle -they are strictly limited in number, they require fresh horses, and must only be made at decisive moments." -Alfred North Whitehead

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CSE 142 Sample Final Exam #2

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

XC Total Max Score Grader

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Final Examination CS 125 Introduction to Computer Science Fall Hours

Sample Examination Paper Programming and Software Development

CS 312 Final Fall Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION. Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _

Building Java Programs Sample Final Exam #1. Name of Student. Section (e.g., AA)

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Building Java Programs

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!

SUN Certified Programmer for J2SE 5.0 Upgrade. Download Full Version :

Name Section Number. CS210 Exam #1 *** TURN OFF ALL ELECTRONIC DEVICES *** Practice

CSE331 Winter 2014, Midterm Examination February 12, 2014

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Com S 227 Spring 2013 Topics and review problems for Final Exam Wednesday, April 8, 7:00 9:00 pm in Hoover 2055 and 1213 ** You must bring your ID **

Final Exam. COMP Summer I June 26, points

CSE 143X: Accelerated Computer Programming I/II HW5: Critters (due Friday, October 30, :30pm)

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

c) And last but not least, there are javadoc comments. See Weiss.

CS 177 Week 15 Recitation Slides. Review

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CSE 142 Sample Final Exam #4

CIS Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

CS 307 Midterm 2 Spring 2011

CSc 110 Sample Final Exam #1

CIS 110: Introduction to Computer Programming

Building Java Programs

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CSE 11 Midterm Fall 2008

Transcription:

CIS 110 Introduction To Computer Programming December 19th, 2011 Final Answer key

CIS 110 final instructions You have 120 minutes to finish this exam. Time will begin when called by a proctor and end precisely 120 minutes after that time. If you continue writing after the time is called, you will receive a zero for the exam. This exam is closed-book, closed-notes, and closed-computational devices except for a one page sheet (8.5 by 11 ) of double-sided notes. When writing code, the only abbreviations you may use are as follows: System.out.println S.O.PLN System.out.print S.O.P System.out.printf S.O.PF Otherwise all code must be written out as normal. Please do not separate the pages of the exam. If a page becomes loose, please make sure to write your name on it so that we don t lose it, and use the provided staplers to reattach the sheet when you turn in your exam. If you require extra paper, please use the backs of the exam pages or the extra sheet of paper provided at the end of the exam. Clearly indicate on the question page where the graders can find the remainder of your work (e.g., back of page or on extra sheet ). If you have any questions, please raise your hand and an exam proctor will come to answer them. When you turn in your exam, you will be required to show ID. If you forgot to bring your ID, please talk to an exam proctor immediately. Good luck, have fun! 1

CIS 110 final cheat sheet /** 1. For syntax, look at the format of the code presented in the questions * (unless otherwise stated, it is all syntactically correct code). */ /** 2. Useful methods for String objects */ charat(index) // Returns the character at the given (zero-based) index. endswith(text) // Returns true if the string ends with the given text. indexof(character) // Returns the (zero-based) index of the given character. length() // Returns the length of the string. startswith(text) // Returns true if the string starts with the given text. substring(start, stop) // Returns the characters from the start index to just // before the stop index tolowercase() // Returns a new string with all lower case characters. touppercase() // Returns a new string with all upper case characters. /** 3. Useful methods for Scanner objects */ new Scanner(src) // Makes a new Scanner from the given source. next() // Returns the next token from the Scanner. hasnext() // Returns true if there is a token to read. nextline() // Returns the next line from the Scanner. hasnextline() // Returns true if there is a line left to read. nextx() // Returns the next token as a X, e.g., Int, Double. hasnextx() // Returns true if there is a token left and it s an X. /** 4. Useful methods for Random objects */ new Random() // Creates a new random object. nextint() // Returns a random int. nextint(max) // Returns a random int in the range 0 to max-1. nextdouble() // Returns a random double in the range 0.0 to 1.0. nextboolean() // Returns a random boolean. /** 5. Useful methods from the Arrays class */ Arrays.toString(arr) // Returns a formatted String representing arr, // e.g., [1, 2, 3]. Arrays.equals(arr1, arr2) // Returns true iff arr1 is pairwise equal to arr2. Arrays.copyOf(arr, len) // Returns a copy of arr up to the specified length. // truncating or padding the new array to meet it. Arrays.fill(arr, val) // Replaces the elements of arr with val Arrays.deepToString(arr) // Variants that work with Arrays.deepEquals(arr1, arr2) // multidimensional arrays. 2

The critter class // The Critter class is the base class for all critters in the simulation. public abstract class Critter { // Defines the types of food a critter can eat: grass or meat. public static enum FoodType { GRASS, MEAT // Defines the possible directions a critter can move: the four cardinal // directions along with "no move" (CENTER). public static enum Direction { NORTH, EAST, SOUTH, WEST, CENTER // The speed at which a critter moves: fast, medium, or slow. public static enum Speed { FAST, MEDIUM, SLOW // Returns the direction this critter should move for the next step of the // simulation. public abstract Direction getmove(); // Returns the type of food this critter eats. public abstract FoodType getfoodtype(); // Returns the color of this critter. public abstract Color getcolor(); // Returns the speed of this critter. public abstract Speed getspeed(); // Returns the String representation of this critter. This should be a // a single character which will be rendered onto the simulation board. public String tostring() { throw new UnsupportedOperationException( "Implementors must provide their own tostring() methods!"); 3

Express yourself 1. (5 points) Evalute these Java expressions to their final values. (a) 20 / (5 * 3) - 10 / 5 + 2 1 (b) 83 % 2 + 100 % 2-101 % 2 0 (c) (12 >= 5 && 6 < 4) (19 > 3 &&!true) false (d)!(!(12 < 3)!(9 > 5)) (e) false String s = "Last test!"; s.substring(5, 9) + s.charat(9) + s.substring(0, 4) "test!last" 4

Method mysteries: the next generation 2. (10 points) Given methods mystery1 and mystery2, evaluate the following Java expressions to their final values. Note that mystery2 is found on the next page. public static int mystery1(int x, boolean b) { if (b) { if (x > 0) { x = x - 1; else { x *= -2; else { if (x <= 0) { x++; if (x == 0) { x += 100; return x; (a) mystery1(-5, false) -4 (b) mystery1(3, true) 2 (c) mystery1(-1, false) 100 (d) mystery1(-2, true) 4 (e) mystery1(10, false) 10 5

public static int[] mystery2(int[] arr) { if (arr.length <= 1) { return null; int[] ret = new int[arr.length/2]; for (int i = 0; i < ret.length; i++) { if (arr[i * 2] > arr[i * 2 + 1]) { ret[i] = arr[i * 2]; else { ret[i] = arr[i * 2 + 1]; return ret; (a) mystery2(new int[]{3) null (b) mystery2(new int[]{3, 5) [5] (c) mystery2(new int[]{1, 3, 9, 8, 5, 3) [3, 9, 5] (d) mystery2(new int[]{4, 2, 0, 0, 1, 5) [4, 0, 5] (e) mystery2(new int[]{2, 10, 8, 5, 4) [10, 8] 6

Be assertive 3. (15 points) For the labeled points in the code fragment below, identify each of the assertions in the table as being always true, never true, or sometimes true or sometimes false. You may abbreviate always, never, and sometimes as A, N, and S respectively. public static int mystery(int x, int y) { if (y < 0 y > 2 x <= 0) { return 0; Random r = new Random(); int z = x * x; // POINT A while (z >= x) { // POINT B if (y == 0) { z += 1; y = r.nextint(5) + 1; // POINT C else { x += y; z -= 2; y = r.nextint(2); // POINT D // POINT E return z; z >= x y >= 0 && z < 0 y <= 2 A Always Always Never B Always Sometimes Never C Always Sometimes Never D Sometimes Always Sometimes E Never Always Sometimes 7

Polymorph 4. (15 points) For the class hierarchy below, fill out the table with the results of method calls to speak() on objects in that class hierarchy. For example, the top-left corner of the table is the result of the call: Animal a = new Goat(); a.speak(); Several of the combinations of variables and assignments below cause compiler errors. For those that cause compiler errors, write Error in place of the output. class Animal { public String speak() { return "Noise!"; class Goat extends Animal { public String speak() { return "Bleat!"; class MountainGoat extends Goat { public String speak() { return super.speak() + " going up!"; class Cat extends Animal { public String speak() { return "Meow"; class Dog extends Animal { public String speak() { return "Woof"; = new Goat() = new Cat() = new MountainGoat() Animal a "Bleat!" "Meow" "Bleat! going up!" Goat g "Bleat!" Error "Bleat! going up!" MountainGoat m Error Error "Bleat! going up!" Cat c Error "Meow" Error Dog d Error Error Error 8

Flying one more time 5. (20 points) Write a static method printtriangle that takes an integer n and prints a triangle of height n in a particular format. The rows of the triangle are made up of periods (.). However, certain rows are made up of different characters as described below. We consider the first row of the grid to have row number 1. (a) If the row number is a multiple of 3, then the row is made up of greater-than signs (>). (b) If the row number is a multiple of 5, then the row is made up of less-than signs (<). (c) If the row number is both a multiple of 3 and 5, then the row is made up of exclamation marks (!). If n < 1, then printtriangle does nothing. Here are some examples of invocations of printtriangle. printtriangle(5)... >>>... <<<<< printtriangle(1). printtriangle(15)... >>>... <<<<< >>>>>>...... >>>>>>>>> <<<<<<<<<<... >>>>>>>>>>>>......!!!!!!!!!!!!!!! public static void printtriangle(int n) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { if (i % 3 == 0 && i % 5 == 0) { System.out.print("!"); else if (i % 3 == 0) { System.out.print(">"); else if (i % 5 == 0) { System.out.print("<"); else { System.out.print("."); System.out.println(); 9

Nom nom nom 6. (20 points) Write a static method getwins that takes a Scanner that reads from a text file in a particular format and a String representing a player s name and returns the number of wins for that player found in the file. The file contains a series of game entries where each record represents the outcome of a single game, one record per line, in the following format: <player1name> <player2name> <winner> The winner field can either be 1 or 2 corresponding to the first player winning or the second player winning, respectively. Say that our scanner points to the following file, then here are some example invocations of getwins and the values it returns. If a person is not found in the file, then getwins returns zero. You may also assume that the input file is well-formed. john james 1 john jane 2 john julia 1 james jane 2 james julia 2 jane julia 1 getwins(file, "john") 2 getwins(file, "james") 0 getwins(file, "julia") 1 getwins(file, "jane") 3 public static int getwins(scanner file, String name) { int numwins = 0; while (file.hasnextline()) { Scanner line = new Scanner(file.nextLine()); String p1 = line.next(); String p2 = line.next(); String winner = ""; if (line.nextint() == 1) { winner = p1; else { winner = p2; if (winner.equals(name)) { numwins++; return numwins; 10

I ve got 99 problems and an array ain t one 7. (20 points) Write a method intersperse that takes a String s and an array of Strings arr and returns a new String array that intersperses s among the elements of arr. By intersperse, we mean that the new array is a copy of arr except between each element of arr we insert a copy of s. That is, the returned array is laid out as follows: [arr[0], s, arr[1], s,..., s, arr[arr.length-1]] In the case of a null array, intersperse returns null. In the case of an array of length zero, intersperse returns a new array of length zero. Note that an array of length zero is initialized in the usual way except that you pass zero for its length. Here are some example calls of intersperse and their results. String s String[] arr Result "pie" ["hello", "world", "wow!"] ["hello", "pie", "world", "pie", "wow!"] "sigh" ["huh?"] ["huh?"] "hrm" [ ] [ ] public static String[] intersperse(string s, String[] arr) { if (arr == null) { return null; if (arr.length == 0) { return new String[0]; String[] ret = new String[arr.length * 2-1]; for (int i = 0; i < arr.length - 1; i++) { ret[i * 2] = arr[i]; ret[i * 2 + 1] = s; ret[ret.length - 1] = arr[arr.length - 1]; return ret; 11

Didn t we just do this? 8. (20 points) Write a subclass of Critter called Rhino that meets the following specification: Constructor Food type Movement Color Speed tostring Rhino(int numadditionalsteps): numadditionalsteps is the number of additional steps a Rhino takes in each direction. Grass Chooses a random direction (from north, south, east, or west) and moves in that direction for numadditionalsteps + 3 steps. After this, it chooses a new random direction and repeats the process. Normally gray. However, when the Rhino is on its last 3 steps of its current direction, it becomes red. Slow Normally "R". However, when the Rhino is on its last 3 steps of its current direction, it becomes "!". For reference, the essential pieces of the critter class are reproduced as part of your cheat sheet. (Solution on the next page) 12

public class Rhino extends Critter { private int counter; private int numadditionalsteps; private Direction direction; private Direction chooserandomdirection() { Random rand = new Random(); int val = rand.nextint(4); if (val == 0) { return Direction.NORTH; else if (val == 1) { return Direction.SOUTH; else if (val == 2) { return Direction.EAST; else { return Direction.WEST; public Rhino(int numadditionalsteps) { this.numadditionalsteps = numadditionalsteps; public Direction getmove() { if (counter == numadditionalsteps + 3) { direction = chooserandomdirection(); counter = 0; counter++; return direction; public FoodType getfoodtype() { return FoodType.GRASS; public Color getcolor() { if (counter >= numadditionalsteps) { return Color.RED; else { return Color.GRAY; public Speed getspeed() { return Speed.SLOW; public String tostring() { if (counter >= numadditionalsteps) { return "!"; else { return "R"; 13

That s a wrap! 9. (25 points) (a) Write a method swaprandom that takes a Random object, an integer array arr, an integer index k, and an integer length len and swaps the element at index k with the element found at a random index i where k <= i < k + len. Note that swaprandom does not return anything nor does it output anything to the console. If k is an invalid index, len is negative, or k + len > arr.length then swaprandom does nothing. Here are some example invocations of swaprandom over the example array arr [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and the resulting changes to arr. The example calls to swaprandom here are independent and do not affect the results of the other example calls. Remember that swaprandom behaves randomly, so these examples represent one such possible result for each method invocation. k len Changed arr 0 10 [7, 1, 2, 3, 4, 5, 6, 0, 8, 9] 3 2 [0, 1, 2, 4, 3, 5, 6, 7, 8, 9] 6 5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] public static void swaprandom(random rand, int[] arr, int k, int len) { if (k < 0 k >= arr.length len < 0 k + len > arr.length) { return; int index = rand.nextint(len) + k; int temp = arr[k]; arr[k] = arr[index]; arr[index] = temp; 14

(b) Use swaprandom to write a method fisheryatesshuffle that takes an integer array arr and shuffles arr according to the Fisher-Yates shuffle algorithm. The Fisher-Yates algorithm shuffles the elements of the input arr as follows: For each i from 0 to arr.length - 1: Choose a random index k in the range i k < arr.length. Swap arr[i] with arr[k]. For example, say that arr is the following array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Then one possible way that Fisher-Yates may shuffle arr is: [5, 8, 1, 2, 7, 6, 4, 9, 0, 3] Note that fisheryatesshuffle does not return anything nor does it output anything to the console. Also if a null array is passed to fisheryatesshuffle, then the method does nothing. You may not use the Collections.shuffle method to shuffle the elements of the input array. public static void fisheryatesshuffle(int[] arr) { if (arr == null) { return; Random rand = new Random(); for (int i = 0; i < arr.length; i++) { swaprandom(rand, arr, i, arr.length - i); 15

Scratch paper 16