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

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

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 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

CIS Introduction to Computer Programming. 5 October 2012 Midterm

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

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

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

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

CIS Introduction to Computer Programming Spring Exam 1

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

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

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

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

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

CIS Introduction to Computer Programming Spring Exam 2

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

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

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

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

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 Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

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

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

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

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

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2010

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!

COS 126 Midterm 1 Written Exam Fall 2011

CSE 131 Introduction to Computer Science Fall Exam II

COS 126 General Computer Science Fall Written Exam 1

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

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CIS 110 Fall 2015 Introduction to Computer Programming 6 Oct 2015 Midterm Exam Answer Key

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

CS100J February 13, 2001

Question: Total Points: Score:

CS 177 Spring 2010 Exam I

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

CIS 110 Spring 2012 Final Exam, 7 May 2012, Answer Key. Miscellaneous

CS 2316 Exam 4 Fall 2011

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

COS 126 General Computer Science Spring Written Exam 1

COS 126 Midterm 1 Written Exam Fall 2012

Final Exam. COMP Summer I June 26, points

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

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

CSE 1223: Exam II Autumn 2016

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Question: Total Points: Score:

University of Massachusetts Amherst, Electrical and Computer Engineering

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

CS100M November 30, 2000

ECE264 Fall 2013 Exam 1, September 24, 2013

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

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

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

McGill University School of Computer Science COMP-202A Introduction to Computing 1

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

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

CS100J February 13, 2001

Question: Total Points: Score:

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CIS 265 Summer 2015 Exam 1 First Name Last Name

Building Java Programs

COMPUTER APPLICATIONS

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

Introduction to Computer Science Midterm 3 Fall, Points

COS 126 Midterm 1 Written Exam, Fall 2009

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 1301 Exam 1 Spring 2011

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

CSE 131 Introduction to Computer Science Fall Exam I

Examination Questions Midterm 1

Building Java Programs

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Following is the general form of a typical decision making structure found in most of the programming languages:

Question: Total Points: Score:

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

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

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

CS170 Introduction to Computer Science Midterm 2

Instructor: Ashley Taylor Spring 2018 May 10, Midterm Exam NAME (LAST, FIRST): Problem TOTAL

COS 126 General Computer Science Spring Written Exam 1

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Transcription:

CIS 110 Introduction to Computer Programming Fall 2017 Midterm Name: Recitation ROOM : Pennkey (e.g., paulmcb): DO NOT WRITE YOUR ID# ABOVE, YOU WILL LOSE A POINT 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. You will lose 5 points if your cell phone goes off. 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 closedcomputational 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 2 points per page. Turn in all scratch paper to your exam. Do not take any sheets of paper with you. If you require extra paper for scratch work, 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. Date Scores: [For instructor use only] Question 1 6 pts Question 2 16 pts Question 3 10 pts Question 4 7 pts Question 5 11 pts Question 6 4 pts Question 7 11 pts Total: 65 pts

CIS 110 Midterm Fall 2017 Page 1 1.) Miscellaneous (6 points) 1.1) (1 point) The Easy One: Check that your exam has all 10 pages (excluding the cover sheet, which is not numbered). 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.2) Draw the result of the following code (do not worry about color): (3 points) PennDraw.circle(0.25, 0.75, 0.25); PennDraw.filledSquare(0.75, 0.25, 0.25); PennDraw.filledRectangle(0.6, 0.5, 0.1, 0.5); 1.3) Write the following while loop as a for loop that produces the same output: (2 points) int x = 15; while (x > 0) { System.out.println(x 1); x = x / 2; for (int x = 15; x > 0; x = x /2) { System.out.println(x 1);

CIS 110 Midterm Fall 2017 Page 2 2) Datatypes (16 points) In the table below, the left column will list some code involving a variable X. You must fill out the two columns on the right. You must identify the data type of the variable X (from data types be have mentioned in class) and the value of X after the code is executed. However, the code may have errors. If the code results in a compile time error, write CE under Type of X and explain the error under value of X. (1 point per box) Instructions corrections: If the code results in a runtime error, write RE under Type of X and explain the error under value of X. Code Type of x Value of x x = 5; x = 7; int 7 int a = 6.5; x = a; String s = "abc"; x = s.charat(s.charat(0) - s.charat(2)); int y = 5 / 3; x = int y++; String s = "abc"; char y = 'a'; x = (s + y) - y; boolean a = false; boolean b =!(a && true); x = (!a &&!b) (a!(b && a)) CE RE CE CE boolean Cannot assign 6.5 to an integer. Index out of bounds, since it checks for s.charat(-2); y declared twice Cannot subtract from a String. true x = (int) ((int) 4.9) + 2.9 double 6.9 x = {"a", "b", "c", "d" x[0]++; CE Cannot increment a String with ++ int y = Double.parseDouble("3"); char z = '3'; x = y == z; int y = 5 % 2; int z = 9 % 3; x = y + y + z + " is fun!" CE String Cannot convert Double to Integer automatically "2 is fun!"

CIS 110 Midterm Fall 2017 Page 3 3) Loops and Arrays (10 points) With all the events going on the world today, many people are finding that they can't even. Can't even keep up with it. Can't even cope. Just can't even. Luckily, as computer programmers, we are here to help. The below function takes in an array of numbers from the command arguments and returns an array with only odd numbers. Example, if the input array were [10, 8, 7, 2, 3, 4], the output of function would be [7, 3] Unfortunately, the programmer writing this code can't even finish. So fill in the blanks public static int[] canteven(int[] input) { //find the number of odd values in the inputarray int numodd = 0; for (int i = 0; i < input.length; i++){ if (input[i] % 2 == 1) { numodd++; //create an array of odd values int[] odds = new int[numodd]; int counter = 0; //where we next insert into odds array for (int i = 0; i < input.length; i++){ if (input[i] % 2!= 0) { odds[counter] = input[i]; counter++; //increment counter return odds;

CIS 110 Midterm Fall 2017 Page 4 4) Tracking Wild Animals (7 points) A bunch of wild animals have invaded this code, making it very hard to read. Trace your way through this code and below write exactly what would print to the console in the box below. public class DangerousAnimals { public static void main(string[] args) { double[] zebra = new double[16]; for (int lion = 0; lion < zebra.length; lion++) { if (lion % 2 == 1 && lion > 9) { zebra[lion] = lion * 1.5; else if (lion < 7) { zebra[lion] = lion * 2; else { zebra[lion] = lion + (lion % 2); 19 16 10 8 10 for (int rhinoceros = zebra.length - 3; rhinoceros >= "hippo".length(); rhinoceros -= 2) { System.out.println((int) zebra[rhinoceros]); Hint: The end result of zebra after the first loop is: 0, 2, 4, 6, 8, 10, 12, 8, 8, 10, 10, 16.5, 12, 19.5, 14, 22.5

CIS 110 Midterm Fall 2017 Page 5 5) Recursion can be fun! (11 points) Some students really do not enjoy learning recursion. So to help, we made a fun recursion function. And to illustrate this fact, we called it "fun". This function does not do any meaningful mathematical operation, so it's just for fun. public static int fun(int x, int y, int z) { System.out.println(x + " " + y + " " + z); if (z!= 0) { return fun(y, x, 0); else if (y == 0) { return x; else if (x == 0) { return y; else { return fun(y/2, Math.abs(y-x), 1); Notice the print statement on line one. In the boxes below, write what prints for each function call. Hint You are given the correct number of lines. Then in the box to the right of it, write the final return value of this call. fun(2, 0, 0) Return value 1.2,0,0 2 fun(3, 3, 0) 1.3,3,0 2.1,0,1 1 3.0,1,0 fun(2, 2, 1) 1.2,2,1 2.2,2,0 3.1,0,1 1 4.0,1,0

CIS 110 Midterm Fall 2017 Page 6 6) Bug Infestation (4 points) An exterminator has been cataloguing the bugs she finds by putting them into arrays (before well exterminating them). She gives each bug a number, and if she finds a second type of the same bug, it gets the same number. For example, a termite might be a 0, an ant 1, a cockroach 2, etc. So if her array is [0,2,1,1,2,2,0,0] it means she found 3 termites, 2 ants, and 3 cockroaches. After she cleans out a house, she takes her array of bugs back to her computer to find out how many of each bug there was. She wrote a function, numoccurrences() below that would take the above array, and a range of bug ID numbers, and return the number of each bug. Examples: numoccurrences({0,2,1,1,2,2,0,0, 3) should return an array [3, 2, 3] numoccurrences({2,2,0,2,3,2, 4) should return an array [1, 0, 4, 1] Unfortunately, our stalwart exterminator must now deal with bugs of a different kind. She wrote the following code to count the number of each bug, but her code is infested with well bugs: 1 public static int[] numoccurrences(int[] arr, int range) { 2 int[] numocc == new double[arr.length]; 3 for (int i = 0; i <= arr.length; i++) { 4 numocc[arr[j]]++; 5 6 return arr; 7 Rewrite the code in the box below with the same structure and number of lines of code. Just fix the bugs in each line (there are bugs on lines 2, 3, 4, and 6.). Do not write your own function, but instead fix the function above. public static int[] numoccurrences(int[] arr, int range) { int[] numocc = new int[range]; for (int i = 0; i < arr.length; i++) { numocc[arr[i]]++; return numocc;

CIS 110 Midterm Fall 2017 Page 7 7) Building a Better Sportsball (11 points) You may not know this, but Dr. McBurney is the inventor of the greatest of outdoor games: Sportsball. In sports ball, teams of two play against each other, where every player's skill is denoted by an integer. The larger the integer, the better the player. However, ticket sales have been dwindling in the NSL (National Sportsball League) due to one team (the Topeka Fightin' Fighters) dominating the league and making it uninteresting to watch. To rectify this, Will has decided to turn to the world of code to make sure the league will be competitive. And he's recruiting you to salvage his league before a bunch of unpaid Sportsball players break his legs. Specifically, you are to write a function isequalenough with the following declaration: public static boolean isequalenough(int[] players, int inequalityfactor) { players: an array of integers, with each integer denoting the skill of one sportsball players. inequalityfactor: an integer denoting the maximum allowed inequality. Returns false if the greatest sum of any two integers is greater than ineqaulityfactor times the smallest sum of any two integers. You can assume all individual strengths and inequalityfactor are positive, and that there are an even number of players and the length of players is >= 2. Example: If players = [1, 3, 8, 1] and inequalityfactor = 3, the greatest possible sum is 11 and the smallest 2. Since 11 is greater than 2 * 3, this returns false. Example: If players = [4, 3, 8, 5] and inequalityfactor = 2, the greatest possible sum is 13, and the smallest 7. Since 13 is NOT greater than 7 * 2, this returns true. Use the next two pages to write this function, but do NOT write any code you wish to have graded on the back of any page. Note that there are tons of ways to do this. This is just one way. public static boolean isequalenough(int[] players, int inequalityfactor) { int min = players[0] + players[1]; int max = players[0] + players[1]; for (int i = 0; i < players.length; i++) { for (int j = 0; j < players.length; j++) { if (i!= j) { max = Math.max(max, players[i] + players[j]); min = Math.min(min, players[i] + players[j]); return!(max > min * inequalityfactor);

CIS 110 Midterm Fall 2017 Page 8

CIS 110 Midterm Fall 2017 Page 9 Extra Space for Answers DO NOT RIP THIS PAGE OFF. ANY WRITING ON THIS PAGE CAN BE GRADED