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

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

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

CIS 110 Introduction to Computer Programming Fall 2017 Midterm. Recitation ROOM :

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

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

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

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

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

CIS 110 Fall 2015 Introduction to Computer Programming 7 Oct 2015 Makeup Midterm Exam

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

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

CIS Introduction to Computer Programming. 5 October 2012 Midterm

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

CIS 110 Introduction to Computer Programming Fall 2017 Final Midterm. Recitation Section# :

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CIS Introduction to Computer Programming Spring Exam 1

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

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

CIS Introduction to Computer Programming Spring Exam 2

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CIS 110 Introduction to Computer Programming Fall 2017 Final Midterm. Recitation Section# :

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

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

CIS 371 Spring 2015 Computer Organization and Design 7 May 2015 Final Exam

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

CS 314 Exam 2 Spring

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Final Exam. COMP Summer I June 26, points

Computer Science E-22 Practice Final Exam

CS 1301 Exam 1 Fall 2010

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS 1301 Exam 1 Fall 2010

Maximum Grade: 5 points out of 10 of the exam.

COS 126 General Computer Science Spring Written Exam 1

CSE 131 Introduction to Computer Science Fall Exam III

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key

public int calculatedamage() { // Generate and return the damage inflicted on ship }

CS 1301 Exam 1 Spring 2011

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

CIT Special final examination

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Introduction to Computing II (ITI 1121) Final Examination

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

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!

CS 216 Exam 1 Fall SOLUTION

CS 314 Exam 2 Spring 2018

COS 126 General Computer Science Fall Written Exam 1

Csci 102: Sample Exam

COM S 211/ENGRD 211 May 15, 2003

Data Structures and Algorithms Winter term 2012

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

Points off Total off Net Score. CS 314 Final Exam Fall 2016

CSE 331 Midterm Exam 2/13/12

INTERFACES IN JAVA. Prof. Chris Jermaine

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CSE332 Summer 2012 Final Exam, August 15, 2012

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

Midterm Exam 2 CS 455, Spring 2011

PRACTICE MIDTERM EXAM #2

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

CSE 131 Introduction to Computer Science Fall Exam II

CS 314 Midterm 2 Spring 2013

CMPSCI 187 Midterm 1 (Feb 17, 2016)

Summer Session 2004 Prelim I July 12, CUID: NetID:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CS 314 Exam 2 Fall 2017

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

Computer Science 1 Ah

CSE 131S Introduction to Computer Science Summer SON Exam III

APCS Semester #1 Final Exam Practice Problems

The University of British Columbia

1. Data types ( =13 points)

Largest Online Community of VU Students

COS 226 Algorithms and Data Structures Fall Midterm

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CIT-590 Final Exam. Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point

1 Inheritance (8 minutes, 9 points)

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

Exam Percentage: / 55 = %

Computer Science E-119 Practice Midterm

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

CS2110 Assignment 3 Inheritance and Trees, Summer 2008

Introduction to Computing II (ITI 1121) Final Examination

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Summer Final Exam Review Session August 5, 2009

CS 2316 Exam 1 Spring 2014

CIS 110 Spring 2013 Make-Up Midterm, 13 February 2013, Answer Key. Miscellaneous

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CS 1322 Test 2 - Version 1 Tuesday June 24 th

Transcription:

CIS 110 Introduction to Computer Programming Summer 2017 Final Name: Recitation # (e.g., 201): Pennkey (e.g., paulmcb): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this examination. Signature Instructions: Do not open this exam until told by the proctor. You will have exactly 120 minutes to finish it. Make sure your phone is turned OFF (not to vibrate!) before the exam starts. Food, gum, and drink are strictly forbidden. You may not use your phone or open your bag for any reason, including to retrieve or put away pens or pencils, until you have left the exam room. This exam is closed-book, closed-notes, and closed- com putational devices. If you get stuck on a problem, it may be to your benefit to move on to another question and come back later. All code must be written out in proper java format, including all curly braces and semicolons. Do not separate the pages. You may tear off the one scratch page at the end of the exam. This scratch paper must be turned in or you lose 3 points. Turn in all scratch paper to your exam. Do not take any sheets of paper with you. If you require extra paper, please use the backs of the exam pages or the extra pages provided at the end of the exam. Only answers on the FRONT of pages will be grading. The back is for scratch work only. Use a pencil, or blue or black pen to complete the exam. If you have any questions, raise your hand and a proctor will come to answer them. When you turn in your exam, you may be required to show ID. If you forgot to bring your ID, talk to an exam proctor immediately. We wish you the best of luck. Date Scores: [For instructor use only] Question 0 1 pt Question 1 14 pts Question 2 6 pts Question 3 11 pts Question 4 8 pts Question 5 10 pts Question 6 6 pts Question 7 24 pts Total: 80 pts

CIS 110 Final Summer 2017 SCORE Page 1 0) (1 point) The Easy One: Check that your exam has all 16 pages (excluding the cover sheet). Write your name, recitation number, and PennKey (username) on the front of the exam. Sign the certification that you comply with the Penn Academic Integrity Code. 1.) RECURSION (14 pts total) 1.1) Exponents (2 points) Fill in the blank below for a recursive function that finds the value of x y where y is a non-negative integer, and x is not zero. It s worth noting that 3 4 = 3 * 3 3, 3 2 = 3 * 3 1. Note that any non-zero number to the power of zero is 1. public static int power(int x, int y) { if (y == 0) { //exponent is 0 return 1; return ; 1.2) Summing an Array Consider function sumarray(int[] array, int start) below that finds the sum of an array recursively. This is done by finding, for each index start of the array, the sum of all elements of the array from start to array.length 1 by adding start to the sum of all remaining elements. Thus, sumarray([4,2,1],0) would return 7 (adding every element from zero forward) while sumarray([4,2,1],1) would be 3 (since we would only add the elements 2 and 1 together. This function is recursive in nature. a) (2 points) Give an example base case input (in code) and what the output would be.

CIS 110 Final Summer 2017 SCORE Page 2 b) (4 points) Using the base case above, fill in the blank to make the function work public static int sumarray(int[] array, int start){ if (start < 0 start > array.length) { //Error throw new RuntimeException( ERROR ); if (start == ) { //base case return ; //base case output else { //recursive step return array[start] + ;.1.3) (6 points) Writing Recursion Given the following implementation of singly linked list of integers, answer the question on the next page: public class LinkedList{ public IntNode first; public LinkedList(){ first = null; public boolean isempty(){ return (first == null); public void insert(int x){ //Code redacted //You can assume insert works ----------------------------------------- public class IntNode{ public int value; public IntNode next; public IntNode(int value){ this.value = value; this.next = null;

CIS 110 Final Summer 2017 SCORE Page 3 Below, write a recursive method count(intnode node, int x) that returns the number of times x appears in an instance of LinkedList. Start by identifying the base case, then the recursive step. If you find you are not able to write a recursive solution, you may write an iterative (looping) solution. However, you will receive an automatic 2 point deduction for iterative solutions.

CIS 110 Final Summer 2017 SCORE Page 4 2) RECURSION TRACING (6 points total) I brought in JJ Abrams because I wanted to reboot an old exam question. Like most things he does, he ended up ruining it. I can t even recognize what this code does anymore. public class Nostalgia { public static int lensflare(int emospock, int supereight) { if (emospock < supereight) { return emospock; else { return lensflare(emospock supereight, supereight); public static void main(string[] args) { int newhoperemake = lensflare(args[0], args[1]); System.out.println(newHopeRemake); To help me out, tell me what prints as a result of the following calls (use the following blank page for notes). (1 point each) a) java Nostalgia 1 2 b) java Nostalgia 2 1 c) java Nostalgia 12 5 d) java Nostalgia 22 6 e) (2 points) What built in Java operation does this mimic?

CIS 110 Final Summer 2017 SCORE Page 5 3) USING OBJECTS (11 points total) You may not know this, but Professor McBurney is a diehard SportsBall fan (go Fightin Fighters ). SportsBall is truly the greatest modern game, fit for the attention spans of today s audiences, where every game between two teams is decided by a coin flip. That s the entire game. Imagine we had two teams: Team 1) Penn Quakers 5 wins, 3 losses. And Team 2) Princeton Tigers 3 wins, 4 losses. If the two teams played, they would have a 50% chance of either team winning. If Penn wins, the result would be Penn increasing their number of wins from 5 to 6, Princeton increasing their number of losses from 4 to 5. Penn would still have 3 losses, Princeton would still have 3 wins. If, however, Princeton wins, then Princeton goes from 3 wins to 4 wins, and Penn goes from 3 losses to 4 losses. Penn would still have 5 wins, and Princeton would still have 4 losses. You are given an incomplete Team.java on the next page. Each Team instance represents a SportsBall team. The play method is used to play a game between the two teams. This could be tested using the following code: public static void main(string[] args) { // create two teams that both have initially // zero wins and zero losses SportsBallTeam penn = new SportsBallTeam("Penn"); SportsBallTeam princeton = new SportsBallTeam("Princeton"); // play a match between them penn.play(princeton); //now one team should have 1 win, and the other //should have one loss

CIS 110 Final Summer 2017 SCORE Page 6 a) (7 points) Fill in the blanks to finish this class. public class SportsBallTeam { public final String name; private int wins, losses; //constructor public SportsBallTeam(String name) { this.name = name; wins = 0; losses = 0; //getters public int getwins() { return ; //getters public int getlosses() { return ; public void play(sportsballteam opponent) { if ( ) { //50% chance opponent wins ; //increment opponent wins else { ; //increment calling instance's losses ; //increment opponent losses ;//increment calling instance's wins b) (4 points) For both of the lines below, explain why they would result in a compile-time error if called from another class (assuming penn and princeton are existing SportsBallTeam variables): penn.name = "The best team ever"; princeton.wins = -10;

CIS 110 Final Summer 2017 SCORE Page 7 4.) STATIC (8 points total) Consider the following object. public class RGBColor { public final double r, g, b; public RGBColor RED = new RGBColor(1.0, 0.0, 0.0); public RGBColor GREEN = new RGBColor(0.0, 1.0, 0.0); public RGBColor BLUE = new RGBColor(0.0, 0.0, 1.0); public RGBColor WHITE = new RGBColor(1.0, 1.0, 1.0); public RGBColor(double r, double g, double b) { this.r = r; this.g = g; this.b = b; /** * Returns the grayscale (0.0-1.0) value of this color */ public double tograyscale() { return (r + g + b) / 3.0; /** * Returns the grayscale (0.0-1.0) value of the input color triplet */ public double tripletgrayscale(double r, double g, double b) { RGBColor temp = new RGBColor(r, g, b); return temp.tograyscale(); public String tostring() { return "Red: " + r + ", Green: " + g + ", Blue: " + b; public static void main(string[] args) { RGBColor yellow = new RGBColor (1.0, 1.0, 0.0); System.out.println(yellow); a) (3 points) Some of the fields should be static (in fact, if you don t make some of the fields static, this will crash when you create an instance). List the fields that should be static: (Do not list any methods, we are only talking about fields right now).

CIS 110 Final Summer 2017 SCORE Page 8 b) (3 points) What prints as a result of running main (assuming you made the proper corrections in a)? c) (1 point) Which method should be static (there is only one)? d) (1 point) How would you call the method from c outside of the class? Keep in mind how static methods are called in other classes. Just give one example of a line of code that calls this method and sets an output to a variable.

CIS 110 Final Summer 2017 SCORE Page 9 5.) SORTING (10 points) Sort each array in ascending order using the specified technique. Show the state of the array after each iteration through the sorting loop. a) Insertion sort {8, 3, 4, 1, 5 (4 points) b) Selection sort {8, 3, 4, 1, 5 (4 points) c) (1 point) Both of these sorts are far slower than what type of recursive sort? d) (1 point) Describe in plain English (not code) what the base case this sorting technique.

CIS 110 Final Summer 2017 SCORE Page 10 6) INTERFACES (6 points) Answer the follow questions about the interface below: public interface GeoCoordinates{ public double getlongitude(); //get the longitude coordinate public double getlatitude(); //get the latitude coordinate (6 points) Write the java code for a class that implements this interface. This class should have a constructor that takes in two numbers, (double longitude, double latitude). DO NOT do any error checking on the coordinates. This will waste your time working on the exam and our time grading it. This means you don t have to check of longitude is between -180 and 180, or if latitude is between -90 and 90. Select your own name for the class.

CIS 110 Final Summer 2017 SCORE Page 11 7) LINKED DATA STRUCTURES (24 points) 7.1) Based on what we discussed in class, illustrate these data structures (indicating where the first node is for both, and the last node for queue). There is no fixed way to draw these lists, but order should be clear, as well as where the head node is. a) (3 points) A queue q implemented as a LinkedList after the instructions q.queue("cat"); q.dequeue(); q.queue("river"); q.queue("dog"); q.dequeue(); q.queue("bag"); q.queue("cat"); q.dequeue(); b) (3 points) A stack s implemented as a LinkedList after the instructions s.push("cat"); s.pop(); s.push("river"); s.push("dog"); s.pop(); s.push("bag"); s.push("cat"); s.pop();

CIS 110 Final Summer 2017 SCORE Page 12 7.2) In this question, you will write the insert method of a Sorted Doubly Linked List. Each element is added to the list in sorted order. For example, if the list contains: (first) 5 8 10 (last) And you inserted 7, the end result would be a list: (first) 5 7 8 10 (last) You CAN have duplicate values. For example, if you inserted 7 again, you would get (first) 5 7 7 8 10 (last) Notice it doesn t matter if you add the 7 before or after the previous 7. Using the following code, answer questions on the next page. YOU MAY NOT RIP THIS PAGE OFF.: public class SortedDoublyLinkedList{ public IntNode first; public IntNode last; public SortedDoublyLinkedList(){ first = null; last = null; public boolean isempty(){ return (first == null); public int size() { //TODO: Return the size of the list; public void insert(int x){ //TODO: Insert into the Linked List in sorted Order ---------------------------------------------------------------- public class IntNode{ public int value; public IntNode next; public IntNode(int x){ value = x; next = null; prev = null;

CIS 110 Final Summer 2017 SCORE Page 13 a) (4 points) Write the size() function to return the number of elements in the list. YOU MAY NOT ALTER THE OBJECT IN ANY WAY. You cannot assume a size variable exists. You should not need more space than given. The lower half of this page is for you to use as scractch space for both a) and b)

CIS 110 Final Summer 2017 SCORE Page 14 b) (14 points) Write the function insert(int x) such that the list inserts x in sorted ASCENDING ORDER. This means the first of the list must be the smallest value, the last of the list must be the largest value. Your insert function must work for all cases: Inserting into an empty list Inserting a new largest value into the list Inserting a new smallest value into the list Inserting a value that goes in the middle of the list

CIS 110 Final Summer 2017 SCORE Page 15 EXTRA ANSWER SPACE: You may NOT rip this page off, but work written on the FRONT of this page may be graded.

CIS 110 Final Summer 2017 SCORE Page 16 SCRATCH PAPER: You may rip this page off, but must turn it in at the end of the exam. If you take this page with you, you lose 3 points.