CIS 110 Introduction to Computer Programming Spring 2016 Midterm

Similar documents
CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

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

CIS Introduction to Computer Programming. 5 October 2012 Midterm

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

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

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

CIS Introduction to Computer Programming Spring Exam 1

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

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

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

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

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. 17 December 2012 Final Exam

CIS Introduction to Computer Programming Spring Exam 2

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

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

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

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 Summer 2016 Final. Recitation # (e.g., 201):

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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

1.4 Arrays. A Foundation for Programming. Arrays. Many Variables of the Same Type. This lecture. Store and manipulate huge quantities of data.

! 52 playing cards in a deck. ! 5 thousand undergrads at Princeton. ! 1 million characters in a book. ! 10 million audio samples in an MP3 file.

Debugging [continued]

1.4 Arrays. A Foundation for Programming. Many Variables of the Same Type. Arrays. !!! any program you might want to write

CS 112 Introduction to Programming

Debugging [continued]

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

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

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

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

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

COS 126 Midterm 1 Written Exam Fall 2011

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

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

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

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

COS 126 Midterm 1 Written Exam Fall 2012

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

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Exam Percentage: / 55 = %

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

Practice Midterm 1. Problem Points Score TOTAL 50

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

Final Exam. COMP Summer I June 26, points

Question: Total Points: Score:

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

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

CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm

CONDITIONAL EXECUTION

COS 126 Midterm 1 Written Exam Spring 2015

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

Practice Midterm 1 Answer Key

CSE 1223: Exam II Autumn 2016

COS 126 General Computer Science Fall Written Exam 1

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

COS 126 General Computer Science Spring Written Exam 1

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Place your name tag here

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

Question: Total Points: Score:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Question: Total Points: Score:

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

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

Question: Total Points: Score:

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

APCS Semester #1 Final Exam Practice Problems

CMSC330 Spring 2017 Midterm #1 Solution

AP CS Unit 3: Control Structures Notes

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

COS 126 General Computer Science Fall Exam 1

CS170 Introduction to Computer Science Midterm 2

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

EE 312 Fall 2017 Midterm 1 October 12, 2017

CS 1301 Exam 1 Fall 2010

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Introduction to Computer Science Midterm 3 Fall, Points

CS 177 Spring 2010 Exam I

CS 113 MIDTERM EXAM 2 SPRING 2013

Instructor: Yu Wang 11/16/2012

Java Simple Data Types

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Question: Total Points: Score:

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

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

Transcription:

CIS 110 Introduction to Computer Programming Spring 2016 Midterm Name: Recitation # (e.g., 201): Pennkey (e.g., eeaton): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this examination. Signature Date Instructions: Do not open this exam until told by the proctor. You will have exactly 110 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 computational 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 answers must be written on the exam booklet; answers written on scrap paper will not be counted. All code must be written out in proper java format, including all curly braces and semicolons. Do not separate the pages. If a page becomes loose, re-attach it with the provided staplers. Staple 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. 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 ). 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. Scores: [For instructor use only] Cover Page Section 1 Section 2 Section 3 Question 4.1 Question 4.2 Section 5 Section 6 Section 7 Total: 1 pt 8 pts 11 pts 14 pts 8 pts 10 pts 10 pts 10 pts 8 pts 80 pts

0. (1 pt) Cover Page Information: Check that your exam has all 9 pages (excluding the cover page and scrap paper). 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. SECTION 1: MISCELLANEOUS (8 pts total) Circle the output of the code snippets. In case of any error in the code, choose option E. 1.1. (2 pts) int a = (int) A ; System.out.println((char) a); A. 65 B. A C. a D. None of the above E. Error 1.2. (2 pts) int b = (int) 4.5; double c = 2.0; System.out.println(b / c); A. 2 B. 2.25 C. 2.0 D. None of the above E. Error 1.3. (2 pts) int a = 3 * 5 % 7 + 2; System.out.println(a); A. 6 B. 17 C. 21 D. 3 E. Error 1.4. (2 pts) String[] arr = { hello, goodbye, see ya ; arr[2] = null; System.out.println(arr.length); A. 1 B. 3 C. 2 D. None of the above E. Error SECTION 2: LOOPS AND CONDITIONALS (11 pts total) 2.1. Fill in the blanks in the code below to produce the output: 3, 9, 27, 81, public static void fillinthefunction(){ int n = 1; while( A ){ n = B ; System.out.print(n +, ); System.out.println(); Blank A: (2 pts) Blank B: (1 pts)

2.2. (8 pts; 2 pts each) Match the pattern with the function that most likely produced it. If none of the functions match the pattern, write None in its corresponding blank; otherwise, write the letter corresponding to the matching function. i. ii. iii. iv. i. ii. iii. iv. public static void print() { int a = 5, num = 1, b = 5; for(int i = 1; i <= a; i++) { for(int j = a i; j >= 1; j--){ System.out.print( ); for(int k = 1; k <= b; k++){ System.out.print(num); num++; System.out.println(); num = 1; A. public static void print() { int a = 5, num = 1, b = 4; for(int i = 1; i <= a; i++){ for(int j = 1; j <= b; j++){ System.out.print( ); b--; for(int k = 1; k <= i; k++){ System.out.print(num); num++; System.out.println(); num = 1; C. public static void print() { int a = 5, num = 1, b = 5; for(int i = 1; i <= a; i++) { System.out.println(); for(int j = 1; j <= b; j++){ System.out.print(num); num++; num = i + 1; b--; B. public static void print() { int a = 3, num = 1; for(int i = 1; i <= a; i++){ for(int j = a i; j >= 1; j--){ System.out.print( ); int b = i * 2 1; for(int k = 1; k <= b; k++){ System.out.print(num); num++; System.out.println(); num = 1; D.

SECTION 3: VARIABLES, OPERATORS, & TYPES (14 pts total) 3.1. (14 pts) For each code fragment 1.) Fill in the most appropriate data type for??? in the 1 st column and 2.) give the value that z contains after the code has been executed in the 2 nd column. If the code would result in an error, write ERROR in the 1 st column and give the reason for the error in the 2 nd column. Code DataType/Error Value of z/reason??? z = 9; z++; int z = 10 int x = 100;??? z = x.length; ERROR x doesn t have a x.length field int x = 5;??? z = x / 2; int x = 0; int y = 1;??? z = (x = y); String x = "CIS";??? z = x + 1 + 1 + 0;??? z = Double.parseDouble(2.0); int[] x = {2, 5, 1, 0;??? z = x[x[0]]; String x = "Gorilla";??? z = x.charat(6);??? z = a < b ;

SECTION 4: FUNCTIONS! (18 pts total) 4.1. (8 pts) Write the non-recursive static function mutantbunnyears based on the function header below. For full credit, write the code as efficiently as possible: /** Function Name: mutantbunnyears * Parameters: * n - number of mutant bunnies in a line * Returns: * the number of ears in the mutant bunny line * assuming that even numbered mutant bunnies have 2 ears * and odd numbered mutant bunnies have 3 ears. * Examples: mutantbunnyears(1) would return 3 * mutantbunnyears(2) would return 3+2 = 5 * mutantbunnyears(3) would return 3+2+3 = 8 */

4.2. (10 pts) Write the static function numincreases based on the header below. Your implementation may be recursive or non-recursive. /** Function Name: numincreases * Function Description: * Returns the number of times consecutive elements of the * given array increase, reading left to right. * * For example, the array arr = {1,0,2,3,3 contains two (2) * increases (moving left to right though the array): * 1.) arr[1] to arr[2], since 0 < 2 * 2.) arr[2] to arr[3], since 2 < 3 * All other consecutive elements decrease or remain the same. * So, numincreases({1,0,2,3,3) returns 2 * Parameters: * arr - array of integers * Returns: * the total number of increases * Error Checking: * if the array is null, return 0 */

SECTION 5: DEBUGGING (10 pts total) The following program deals 5-card hands at random to the given number of players. java Deal 3 However, it contains a number of bugs. Find and correct the bugs by filling in the table on the next page. The first bug has been found for you and is listed as an example in the table. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public class Deal { public static void main(string[] args) { int CARDS_PER_PLAYER = 5 // number of players int numplayers = Integer.parseInt(args[1]); String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" ; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" ; // avoid hardwired constants int numsuits = suit.length; int numranks = rank.length; int numcards = numsuits * numranks; if (CARDS_PER_PLAYER * numplayers > numcards) { System.err.println("Too many players"); System.exit(1); // initialize deck with all combinations of ranks and suits String[] deck = String[numCards]; int r = 0; for (int i = 0; i < numranks; i++) { for (int j = 0; j < numsuits; j++) { deck[numsuits * i + j] = rank(i) + " of " + suit[j]; // shuffle the deck for (int i = 0; i <= numcards; i++) { int r = i + (Math.random() * (numcards - i)); String t = deck[i]; // swap the cards deck[r] = deck[i]; // at indices i and r deck[i] = t; // in the deck // print shuffled deck for (int i = 0; i < PLAYERS * CARDS_PER_PLAYER; i++) { System.out.println(deck[i]); if (i % CARDS_PER_PLAYER == CARDS_PER_PLAYER 1) System.out.println();

5.1. (10 pts) List the 7 errors (excluding the given example) that will cause the code to fail to compile or cause a runtime exception, and 1 logical bug in the code that will cause the program to not perform as expected. Each correction requires one line of code or less. Use the last row for the logical bug. Line Number Error Correction 3 Missing Semicolon int CARDS_PER_PLAYER = 5; Logical Bug:

SECTION 6: TRACERY (10 pts) Trace through the following code. Assume that the program is executed using the command: java HofstadterConway 5 Whenever you reach a System.out.println() statement, write the output that would be printed as a new row in the table. You may not need to use all of the table s rows. public class HofstadterConway { public static int HC(int n) { if(n == 1) return 1; if(n == 2) return 1; int a = HC(HC(n-1)) + HC(n HC(n 1)); System.out.println(n + " : " + a); return a; public static void main (String[] args) { int num = Integer.parseInt(args[0]); System.out.println( Answer: + HC(num)); BACKGROUND (Note: Not needed to answer the question above!) This function generates the Hofstadter Conway $10,000 Sequence! It acquired its name because John Horton Conway offered a prize of $10,000 to anyone who could find the first position, p in the sequence for which a(n)/n ½ < 1/20 for all n > p. The prize was subsequently claimed by Mallows, who found that p = 1,489. It was later found that Hofstadter had found the sequence and its structure 10-15 years before Conway posed his challenge.

SECTION 7: SORTING (8 pts total) Assume we are given the array: {8, 7, 3, 0, 9, 2, 6. Recall that both insertion sort and selection sort maintain a separation of the array into two parts: a left part that is kept in sorted order and a right part that is unsorted. Each step of the algorithm, one additional element is added to the left (sorted) portion. For example, in insertion sort, 8 is initially marked as sorted: {8, 7, 3, 0, 9, 2, 6. After the first step of sorting, 7 is added to the sorted section, thus making the state of the array: {7, 8, 3, 0, 9, 2, 6, where the sorted numbers are underlined. 7.1. (2 pts) What would be the state of the array after the third step of insertion sort? A. {0, 2, 3, 8, 9, 7, 6 B. {0, 2, 3, 6, 9, 7, 8 C. {0, 2, 3, 7, 9, 6, 8 D. {0, 3, 6, 8, 9, 2, 7 E. {0, 3, 7, 8, 9, 2, 6 7.2. (2 pts) What would be the state of the array after the third step of selection sort? A. {0, 2, 3, 8, 9, 7, 6 B. {0, 2, 3, 6, 9, 7, 8 C. {0, 2, 3, 7, 9, 6, 8 D. {0, 3, 6, 8, 9, 2, 7 E. {0, 3, 7, 8, 9, 2, 6 For the following questions, a swap occurs when the sorting algorithm switches the position of two elements. A comparison is defined as a check between two elements. For example, in the first step of insertion sort, both 7 and 8 are compared and swapped. 7.3. (2 pts) How many times is 8 swapped by the end of selection sort? A. 2 C. 3 E. None of the above B. 0 D. 4 7.4. (2 pts) How many elements are compared with 3 by the end of insertion sort? A. 1 C. 0 E. None of the above B. 2 D. 4 Since there are a number of people who still need to take the makeup midterm, please DO NOT discuss the exam or post anything about it on Piazza. Thank you! That s it!!

SCRAP PAPER (This page is intentionally blank)

ANSWER KEY SECTION 1: MISCELLANEOUS 1.1. (2 pts) B 1.2. (2 pts) C 1.3. (2 pts) D 1.4. (2 pts) B SECTION 2: CONDITIONALS AND LOOPS 2.1. Blank A: n <= 27 OR n < 81 Blank B: n * 3 2.2. i. B (2 pts) ii. C (2 pts) iii. None (2 pts) iv. D (2 pts) SECTION 7: SORTING 7.1. E (2 pts) 7.2. A (2 pts) 7.3. C (2 pts) 7.4. E (2 pts)

SECTION 3: VARIABLES, OPERATORS, TYPES 3.1. Code DataType/Error Value of z/reason int x = 5;??? z = x / 2; int or double z = 2 if int z = 2.0 if double int x = 0; int y = 1;??? z = (x = y); int z = 1 String x = "CIS";??? z = x + 1 + 1 + 0; String z = CIS110??? z = Double.parseDouble(2.0); ERROR parsedouble converts String to double. Argument should be a string. int[] x = {2, 5, 1, 0;??? z = x[x[0]]; int z = 1 String x = "Trump vs Sanders vs Clinton";??? z = x.charat(12); char z = d??? z = a < b ; boolean z = true

SECTION 4: FUNCTIONS! 4.1. Example full credit answers (using division and mod, without a loop) public static int mutantbunnyears(int numbunnies){ // count pairs of bunnies, which have 5 ears total int answer = 5 * numbunnies / 2; // add on 3 ears for the odd bunny answer += 3 * numbunnies % 2; return answer; public static int mutantbunnyears(int numbunnies){ return 5 * numbunnies / 2 + 3 * numbunnies % 2; Partial credit response public static int mutantbunnyears(int numbunnies){ int n = 0; int i = 1; while(i <= numbunnies){ if (i % 2 == 0) n += 2; else n += 3; i++; return n; 4.2 public static int numincreases (int[] arr) { if (arr == null) return 0; int sum = 0; for (int i = 1; i < arr.length; i++) { if (arr[i-1] < arr[i]) { sum++; return sum;

SECTION 5: DEBUGGING 5.1. You had to find 7 out of the 8 possible syntax errors, plus the logical error Line Number Error Correction 3 Missing Semicolon int CARDS_PER_PLAYER = 5; 6 Incorrect index to args. args[0] contains the number of players int PLAYERS = Integer.parseInt(args[0]); 23 Array initialization error. Missing keyword new String[] deck = new String[CARDS]; 27 Incorrect way of accessing array element. Change rank(i) to rank[i] deck[suits*i + j] = rank[i] + " of " + suit[j]; 32 Loop exceeds array bounds. for (int i = 0; i < CARDS; i++) { 33 Extra declaration of r Delete int 33 Missing cast to int r = i + (int)(math.random() * (CARDS-i)); 40 Undefined variable PLAYERS Change to numplayers 45 Missing closing bracket for main Add 34 Logical Error: Value of deck[r] is lost in swap. String t = deck[r]; deck[r] = deck[i]; deck[i] = t; SECTION 6: TRACERY (Hofstadter Conway $10,000 Sequence!) (10 pts) 3 : 2 3 : 2 4 : 2 3 : 2 3 : 2 4 : 2 3 : 2 5 : 3 Answer: 3