CIS 110 Introduction to Computer Programming Summer 2018 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 2017 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 Introduction to Computer Programming 8 October 2013 Midterm

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

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

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

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

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

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

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

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

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

CIS Introduction to Computer Programming. 5 October 2012 Midterm

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

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 Fall 2017 Final Midterm. Recitation Section# :

CIS Introduction to Computer Programming Spring Exam 1

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

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

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

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

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

Final Exam. COMP Summer I June 26, points

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

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

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

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

Midterm Exam 2 CS 455, Spring 2011

CIS 110 Spring 2014 Introduction to Computer Programming 12 May 2014 Final Exam Answer Key

CS 1301 Exam 1 Fall 2010

CS 307 Midterm 2 Fall 2010

Largest Online Community of VU Students

CS 314 Exam 2 Spring

APCS Semester #1 Final Exam Practice Problems

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CS 1301 Exam 1 Fall 2010

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CIS 110 Spring 2013 Final Exam, 29 April 2013, Answer Key. Miscellaneous

CS 307 Midterm 2 Spring 2011

Data Structure (CS301)

Summer Final Exam Review Session August 5, 2009

CSE 143 SAMPLE MIDTERM

CS 307 Midterm 2 Spring 2008

CS 314 Exam 2 Spring 2016

Name CIS 201 Midterm II: Chapters 1-8

Data Structures and Algorithms Winter Semester

Final. Please write (printing clearly) the infonnation requested in the boxes at the top of this page.

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CSE351 Winter 2016, Final Examination March 16, 2016

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

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

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

COP 3330 Final Exam Review

Name: Pennkey: CIS 120 Final Exam December 21, Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam.

Final Examination CS 125 Introduction to Computer Science Fall Hours

PRACTICE MIDTERM EXAM #2

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id):

Csci 102: Sample Exam

CSE 131 Introduction to Computer Science Fall Exam II

You must bring your ID to the exam.

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

NATIONAL UNIVERSITY OF SINGAPORE

University of Maryland College Park Dept of Computer Science

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

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

DNHI Homework 3 Solutions List, Stacs and Queues

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

CS 314 Exam 2 Spring 2018

CS 177 Week 15 Recitation Slides. Review

CSE115 Introduction to Computer Science for Majors I Spring 2008 Final Exam. First/Given Name (PRINT) May 6, 2008

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Instructor: Yu Wang 11/16/2012

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

CS100J November 20, 2001

1. Data types ( =13 points)

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

ECE368 Exam 2 Spring 2016

CS 112 Midterm Exam Fall 2016

CS 216 Exam 1 Fall SOLUTION

CSE 143, Winter 2010 Final Exam Thursday, March 18, 2010

CS 113 MIDTERM EXAM 2 SPRING 2013

COS 226 Algorithms and Data Structures Fall Midterm

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

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

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

Transcription:

CIS 110 Introduction to Computer Programming Summer 2018 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 11 pts Question 2 8 pts Question 3 16 pts Question 4 10 pts Question 5 11 pts Question 6 23 pts Total: 80 pts

CIS 110 Final Summer 2018 SCORE Page 1 0) (1 point) The Easy One: Check that your exam has all 15 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 (11 pts total) 1.1) Exponents (4 points) Fill in the blank below for a recursive function that finds the value of x / y using integer division (i.e., dropping the remainder/decimal). You can assume x and y are POSTIVE (non-zero, non-negative) integers. public static int division(int x, int y) { if (x < y) { return 0; return division(x-y, y) + 1; 1.2) Counting an Array Consider the function countarray(int[] array, int value, int start) below that finds the number of times value appears in the array. This is done by finding, for each index start of the array, the number of times value appears between start and the end of the array. Thus, countarray([4,2,1,2,2,0], 2, 0) would return 3 (There are 3 twos in the array starting from zero) while countarray([4,2,1,2,2,0], 2, 3) would be 2 (Since we only starting counting at index 3, so the first two is not counted). This function is recursive in nature. a) (2 points) Give an example base case input to the function (i.e., the values of array, value, and start) AND what it would return. countarray([4,2,1,2,2,0], 2, 6) returns 0 This is the base case, instead of countarray([4,2,1,2,2,3], 2, 5) returns 3 since the base case above would work for an array of size 0, the base case below would not work for an array of size 0. And it is a valid question to ask "how many 3's are in this array of size 0", the answer is always 0.

CIS 110 Final Summer 2018 SCORE Page 2 b) (5 points) Using the same recursive function above. public static int countarray(int[] array, int count, int start){ if (start < 0 start > array.length) { //Error throw new RuntimeException( ERROR ); if (start == array.length) { return 0; else { if (array[start] == count) { return 1 + countarray(array, count; start + 1); else { return countarray(array, count; start + 1);

CIS 110 Final Summer 2018 SCORE Page 3 2) RECURSION TRACING (8 points total) Below is a mystery recursion function. Do not try to work out WHAT it's doing, as the functionality is completely made up. It does nothing useful. public static int mystery(int a, int b) { if (a == 0 b > 12) { System.out.println(a + "," + b); return 0; else if (b % 2 == 0) { System.out.println(a + "," + b); return mystery(a - 1, b + 1) + 3; else if (a % 2 == 0) { System.out.println(a + "," + b); return mystery(a - 1, b + 2) + 2; else { System.out.println(a + "," + b); return mystery(a - 1, 2 * a) - 3; In the boxes below, write whatever prints when the function is called with the given arguments in order. At the bottom of each box, say what the function call ultimate returns. a) mystery(3,3) b) mystery(4,2) 3,3 4,2 2,6 3,3 1,7 2,6 0,2 1,7 0,2 Returns: -3 Returns: 0

CIS 110 Final Summer 2018 SCORE Page 4 3) USING OBJECTS (16 points total) In this problem, you will be using the card class below. In Klondike Solitaire, as well as Freecell Solitaire, a player can move around the traditional French playing cards into columns. The rule is that each card can be placed under a card exactly ONE RANK higher, and of the opposite color (the colors are red and black). I.e., a red 5 can be placed on a black 6, but it cannot be placed on a red 6, or any card of any other rank. This class is here to model this. Do not worry about specific suits (hearts, clubs, diamonds, spades), just worry about red/black. public class Card { private char rank; //'A', or '2', or '3', etc. private boolean isred; //true if card is red, false if it's black public final char[] ALL_RANKS = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'; public Card(char rank, boolean isred) { this.rank = rank; this.isred = isred; public char getrank() { return rank; public boolean isred() { return isred; public int getrankindex(char ch) { for (int i = 0; i < ALL_RANKS.length; i++) { if (ALL_RANKS[i] == ch) { return i; throw new RuntimeException("ERROR: Invalid rank"); /** * This method returns true if child can be placed on parent in * Klondike or Freecell Solitaire (i.e., true if parent is black 6 and * child is red 5 */ public boolean isvalidparent(card parent, Card child) { //TODO: SEE NEXT PAGE Write one (1) line of code to produce a red '5': (2 points) Card c = new Card(5, true);

CIS 110 Final Summer 2018 SCORE Page 5 To Complete the Method isvalidparent, reorder all the lines of code below. Use all of the lines of code below exactly once. Do not use any line of code more than once, and do not write any code not included below. (You must rewrite the lines in their entiriety). (8 points) else { childrank = i; for (int i = 0; i < ALL_RANKS.length; i++) { if (ALL_RANKS[i] == child.rank) { if (ALL_RANKS[i] == parent.rank) { if (childrank + 1 == parentrank) { if (parent.isred() == child.isred()) { int childrank = -1; int parentrank = -1; parentrank = i; public boolean isvalidparent(card parent, Card child) { return false; return false; return true; public boolean isvalidparent(card parent, Card child) { if (parent.isred() == child.isred()) { return false; int childrank = -1; int parentrank = -1; for (int i = 0; i < ALL_RANKS.length; i++) { if (ALL_RANKS[i] == child.rank) { childrank = i; if (ALL_RANKS[i] == parent.rank) { parentrank = i; if (childrank + 1 == parentrank) { return true; else { return false;

CIS 110 Final Summer 2018 SCORE Page 6 In the space below, list the VARIABLE(S) (not methods) in Card that should be static. (2 points) ALL_RANKS In the space below, list the METHOD(S) (not variables) in Card that should be static. (2 points) getrankindex(); isvalidparent(); Which attributes of a Card can you change AFTER calling the constructor from outside of the file? (It's possible you can change all of them, none of them, or only certain ones.) (2 point) None 4) Object Theory (10 points) 1. How do you get a NullPointerException (you can either describe it or show a code example)? (3 points) Using a dot operator on a null object. Object o = null; System.out.println(o.toString());

CIS 110 Final Summer 2018 SCORE Page 7 2. What function do you implement in order to make an object be printed as something other than "ClassName@Address" (write the entire method declaration, including visibility and return type) (2 points) public String tostring() 3. Give a reason you would want a private method in a class, or say why there no reason to ever make a method private. (3 points) A helper method, that is a method that exists inside of the class to help implement things the class needs, should not be visible outside the class. 4. If a class is called Exam and you have exactly two instances (no more) of the class, midterm and finalexam, and there have a static String variable called semester, how should you change semester to "Summer 18" for both instances? (2 points) Exam.semester = "Summer 18"

CIS 110 Final Summer 2018 SCORE Page 8 5) SORTING (11 points) Sort each array in ascending (smallest to largest) order using the specified technique. Show the state of the array after each iteration through the sorting loop, or after each merge. a) Insertion sort {8, 3, 4, 1, 5, 2, 5 (4 points) 3 8 4 1 5 2 5 3 4 8 1 5 2 5 1 3 4 8 5 2 5 1 3 4 5 8 2 5 1 2 3 4 5 8 5 1 2 3 4 5 5 8 b) Merge sort {8, 3, 4, 1, 5, 2, 5 (4 points) (note that you can split either 3 4 or 4 3 and not lose points 8 3 4 1 5 2 5 8 3 4 1 5 2 5 3 4 1 5 2 5 8 34 15 25 348 1255 1234558 c) Which one of these sorts is recursive in nature? (don't say why, just name it) (1 point) Mergesort

CIS 110 Final Summer 2018 SCORE Page 9 d) Which of the below is code for a selection sort? (circle the one that is a selection sort). Both pieces of code sort an int[] variable called array. Both use the swap function as covered in class (that swaps the location of two values in an array). Note that method declarations have been removed, and variable names obfuscated. (1 point) for (int i = 1; i < array.lenth; i++) { for (int j = i; j > 0; j--) { if (array[j-1] > (array[j])) { swap(array, j - 1, j); for(int i=0; i<array.length-1; i++){ int a = array[i]; int b = i; for(int j=i+1; j<array.length; j++){ if(array[j] < a) { a = array[j]; b = j; swap(array, i, b); Below are two examples of a sorting algorithm on the same numbers. Which is a selection sort? (1 point) 8, 2, 6, 3, 6, 1 2, 6, 3, 6, 1, 8 2, 3, 6, 1, 6, 8 2, 3, 1, 6, 6, 8 2, 1, 3, 6, 6, 8 1, 2, 3, 6, 6, 8 8, 2, 6, 3, 6, 1 1, 2, 6, 3, 6, 8 1, 2, 6, 3, 6, 8 1, 2, 3, 6, 6, 8 1, 2, 3, 6, 6, 8 1, 2, 3, 6, 6, 8

CIS 110 Final Summer 2018 SCORE Page 10 6) LINKED DATA STRUCTURES (23 points) Below is the class LinkedList which is a SINGLY Linked List. Fill in all the blanks below for the constructor and the methods isempty and add. public class Node { public int value; public Node next; //Constructor that creates a node with the input value public Node(int value) { this.value = value; //1 point public class LinkedList { public Node head; //first element of the list /** * returns true if the list is empty */ public boolean isempty() { return head == null;//1 point /** * Adds the integer x as a new node to the END of the List */ public void add(int x) { Node newnode = new Node(x);//1 point if (isempty()) { else { head = newnode;//1 point Node t = head; //1 point while (t.next!= null) {//1 point t = t.next;//1 point t.next = newnode;

CIS 110 Final Summer 2018 SCORE Page 11 Write a function addrecursive(int x) that implements the same behavior as the existing add function, however does so recursively. You may need to write a helper function to do this. Use comments to clearly identify your base case or base cases. There will be no partial credit for writing an iterative solution. If you use either for or while even once, you will get no credit. (6 points) public void addrecursive(int x) { if (isempty()) { head = new Node(x); addhelper(head, x); public void addhelper(node n, int x) { if (n.next == null) { n.next = new Node(x); else { addhelper(n.next, x);

CIS 110 Final Summer 2018 SCORE Page 12 Using this implementation of LinkedList, write, on the next page, the function: public boolean issublist(linkedlist a, LinkedList b) { //this method should have been static, technically This function returns true if a is a sublist of b. That is, if all of the elements a can be found inside of b and in the same order. For Example, if: a = 5 3 1 b = 7 5 3 1 2 Then a is a sublist of b (that is, your function should return true). However, if a = 5 3 1 b = 7 5 3 2 1 A is NOT a sublist of b. This is because while every element of a is in B, they are not continuous. They are interrupted by the 2. Another example is: a = 5 3 1 b = 7 1 3 5 2 This is another case where a is NOT a sublist of b, because the elements are not in the same order. Another case where a is NOT a sublist of b is when: a = 7 5 3 1 2 b = 5 3 1 This is a case where a is NOT a sublist of b, but b is a sublist of A. Finally, if a and b are the same list, such as: a = 5 3 1 b = 5 3 1 Then a IS a sublist of b (that is, return true). Be careful, however. For example, below, a IS a sublist of B. If you look, you can see why this case might be tricky. a = 5 3 1 b = 5 3 5 5 3 1 5

CIS 110 Final Summer 2018 SCORE Page 13 As always, there are a number of ways to do this: here's one 10 points public static boolean issublist(linkedlist a, LinkedList b) { if (a.isempty()) { return true; else if (b.isempty()) { return false; Node trava = a.head; Node travb = b.head; while (travb!= null) { if (trava.value == travb.value) { Node tempa = trava; Node tempb = travb; while (tempb!= null) { if (tempa == null) { return true; if ( tempa.value!= tempb.value) { break; tempa = tempa.next; tempb = tempb.next; travb = travb.next; return false;