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

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

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

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 8 October 2013 Midterm

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

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

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

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 Introduction to Computer Programming. 13 February 2013 Make-Up Midterm Midterm

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

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 Spring 2016 Midterm

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 Introduction to Computer Programming Spring Exam 1

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

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

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

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CIS Introduction to Computer Programming Spring Exam 2

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

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

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

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

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

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!

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

CSE 131 Introduction to Computer Science Fall Exam II

About this exam review

Midterm Exam 2 CS 455, Spring 2011

CS 1301 Exam 3 Fall 2014

CS 314 Exam 2 Spring

CS 113 MIDTERM EXAM 2 SPRING 2013

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

CS 314 Midterm 2 Fall 2012

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

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

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

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

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

Final Examination CS 125 Introduction to Computer Science Fall Hours

Csci 102: Sample Exam

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2010

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

Final Exam. COMP Summer I June 26, points

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

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

Quarter 1 Practice Exam

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

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

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

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

CS 307 Midterm 2 Spring 2008

CS100J November 20, 2001

We've tried to include the common errors and grading standard for every question.

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

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

CS 112 Midterm Exam Fall 2016

Exam Percentage: / 55 = %

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

CS 314 Final Fall 2012

STUDENT LESSON A12 Iterations

PRACTICE MIDTERM EXAM #2

EE 312 Fall 2017 Midterm 1 October 12, 2017

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CSE 123: Computer Networks Fall Quarter, 2017 MIDTERM EXAM

University of Utah School of Computing

Building Java Programs

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

Computer Science E-22 Practice Final Exam

EXAM Computer Science 1 Part 1

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

Repetition Structures

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

Introduction to Data Structures

Practice Midterm Examination

NATIONAL UNIVERSITY OF SINGAPORE

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

COS 226 Algorithms and Data Structures Fall Midterm

COS 126 Midterm 1 Written Exam Fall 2011

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

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

CSE 131 Introduction to Computer Science Fall Exam III

CS31: Introduction to Computer Science I Spring 2011

CSE 331 Midterm Exam 2/13/12

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

Problem Grade Total

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

CS 307 Midterm 2 Fall 2008

CS 314 Exam 2 Spring 2018

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

CSE 1223: Exam II Autumn 2016

Transcription:

CIS 110 Introduction to Computer Programming Fall 2017 Final Midterm Name: Recitation Section# : 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. We wish you the best of luck. Date Scores: [For instructor use only] Front Page 1 pt Question 1 8 pts Question 2 6 pts Question 3 9 pts Question 4 9 pts Question 5 10 pts Question 6 16 pts Question 7 6 pts Question 8 5 pts Question 9 10 pts Total: 80 pts

CIS 110 Final Midterm Fall 2017 Page 1 1.) Starting Slow (8 points) In the space below, illustrate a mergesort with the numbers [8, 4, 1, 3, 2, 7, 9, 0] (3 points) Which type of List is faster for each function call on a list variable. You can assume someelement and otherelement are both of type Element (which can be sorted). Assume the list is initially unsorted, and has at least 50 elements. (5 points) Operation ArrayList<Element> LinkedList<Element> They are both the same list.add(0, someelement) list.remove(0) list.get(25) list.add(otherelement) Collections.sort(list)

CIS 110 Final Midterm Fall 2017 Page 2 2) Inheritance (6 points) Consider the following Color class public class Color { public int red, green, blue; //between 0-255 public Color (int r, int g, int b) { red = r; green = g; blue = b; public void brighten() { //assume this method works We want a class called GrayscaleColor such that it extends color. A Grayscale color with a gray value of x would have RGB values of red = x, blue = x, and green = x. Write the constructor for the class below. You should not need more space than provided. Do NOT do any error checking. You may not add ANY variables. (2 point) public class GrayscaleColor extends Color{ public GrayscaleColor(int x) { public void darken() { //assume this method works If the following lines of code were written in order, clearly circle the lines of code that would produce SYNTAX errors. (4 points) i) Color red = new Color(255, 0, 0); ii) Color black = new GrayscaleColor(0); iii) GrayscaleColor white = new GrayscaleColor(255); iv) GrayscaleColor gray = new Color (127, 127, 127); v) red.blue = 127; vi) Color c = (Color) black; vii) red.darken(); viii) white.blue = 127; ix) white.brighten();

CIS 110 Final Midterm Fall 2017 Page 3 3) Comparing Numbers (9 points) A bank wants to keep track of both its largest debits AND its largest credits. Therefore, it wants to sort a List<Account> by the absolute value of the balance in DESCENDING order using the Collections.sort function. You can assume the class Account has a method getbalance(), that returns either a positive or negative number representing the amount of money tied to the credit (positive) or debit(negative). a) (1 point) Given the fill in the blank below, would this be done with a Comparable<Account> or Comparator<Account>? b) (4 points) Fill in the blanks below for the class you write to use in Collections.sort public class SortAbsValDescending implements { public int (Account a0, Account a1) { return Math.abs(.getBalance()) Math.abs(.getBalance()) c) (2 points) How would you use the above class to sort the List<Account> called accounts? Collections.sort( ) d) (2 points) Above, you answered the question about either Comparable or Comparator (depending on your answer to a). For the one you didn't choose, what is the method declaration for the one method need to implement the interface if you also applied it to Account (don't write the method, JUST the method declaration)?

CIS 110 Final Midterm Fall 2017 Page 4 4) Birthdays (9 points) A pop-culture obsessed CIS 110 student wanted to keep track of every celebrity's age. To do this, they wrote a class called Person (since celebrities are people too). The class Person will keep track of every instance created by adding that instance to a list. This is so when advanceyear() is called, everyone ages uniformally. However, the student didn't finish because they were too in shock from learning that James Franco is nearly 40. The student failed to finish the constructor, failed to specify which variables and methods need to be static, and did not yet write advanceyear(). Answer the questions on the next page about this code. YOU CANNOT RIP THIS PAGE OFF. public class Person { private String name; private int age; private int currentyear = 2017; public List<Person> people = new ArrayList<Person>(); * Constructor public Person(String name, int birthyear) { this.name = name; age = currentyear birthyear; people. * Standard tostring() method public String tostring() { return name + " : " + age; * Returns the oldest person in the list public Person getoldestcelebrity() { int max = 0; Person out = null; for (Person p : people) { if (p.age > max) { max = p.age; out = p; return out; * Increase the current year by one, and ages every * person created by one year. public void advanceyear() { //TODO: Part D

CIS 110 Final Midterm Fall 2017 Page 5 a) On the previous page, fill in the blank in the constructor that adds the newly created instance to the list people. (2 points) b) In the space below, list the VARIABLES (not methods) that should be static for the code to work. (2 points) c) In the space below, list the METHODS (not variables) that should be static for the code to work. (2 points) d) In the space below, write the function advanceyear(). This function should age every person in people by one year and increase the current year. This function body should be no longer than 4 lines of code. If it is longer than 4 lines, it will NOT be graded, and you will get zero points. (3 points)

CIS 110 Final Midterm Fall 2017 Page 6 5) Breaking the Bank (10 points) The following class below attempts to simulate a bank account. However, there are a number of bugs. On the next page are several test cases. Each time a value or function output is inside of test(), fill in the expected output (based on the method description in the header comment) and the ACTUAL output (the result of execution the code). If the test fails, explain the bug fix under fix. If the test does not fail, leave fix blank. public class BankAccount { public double amount; //amount in the bank public Account (double startingbalance) { amount = startingbalance; * Returns the current balance of the account. public double getbalance() { return 5; //this is intentionally wrong, see next page * Returns whether or not your account is either at zero or * "in the red" (that is, you owe the bank money) public boolean isempty() { return account < 0; * Add money to the account. public void deposit(double amount) { amount += amount; * Withdraw money. If you try to withdraw too much, * do not allow the withdrawal, but subtract $10 from the * account as an overdraft penalty public void withdraw(int cash) { if (cash < amount) { amount -= 10; amount -= cash * Adds 10% interest to the account. public void addinterest() { amount = amount * 0.1;

CIS 110 Final Midterm Fall 2017 Page 7 Test Case Expected Actual Fix Account a = new Account (50.0); test(a.getbalance()); 50.0 5 In getbalance(), change "return 5" to return balance. Account a = new Account(0.0); test(a.isempty()); Account a = new Account(10.0); a.deposit(20.0); test(a.amount); Account a = new Account(15.0); a.withdraw(10.0); test(a.amount); Account a = new Account(11.0); a.withdraw(15.0); test(a.amount); Account a = new Account(100.0); a.addinterest(); test(a.amount); (2 points per row)

CIS 110 Final Midterm Fall 2017 Page 8 6) Tracing A Schedule (16 points) A new room reservation system for the engineering buildings is being designed. A Reservation class is being tested within the ReservationSystemDemo class. Using the main function in ReservationSystemDemo (next page), fill out the table at the bottom of the next page with the values at each Point (denoted by comments). public class Reservation { private int roomnum; private int numpeople; private String reserver; private int starttime; private int endtime; public Reservation() { this.roomnum = 100; this.numpeople = 100; public Reservation(int roomnum, int starttime, int endtime) { this.roomnum = roomnum; this.numpeople = 30; this.starttime = starttime; this.endtime = endtime; public Reservation(int roomnum, int numpeople, String reserver) { this.roomnum = roomnum; this.numpeople = numpeople; this.reserver = reserver; this.starttime = 12; this.endtime = 12; public void addtime(int hours) { endtime += hours; public boolean changereserver(string newreserver) { if (newreserver!= null) { this.reserver = newreserver; return true; return false; public String getreserver() { return this.reserver; public int getendtime() { return this.endtime;

CIS 110 Final Midterm Fall 2017 Page 9 public class ReservationSystemDemo { public static void main(string[] arr) { Reservation a = new Reservation(); Reservation b = new Reservation(309, 12, 14); Reservation c = new Reservation(100, 200, "Jane"); a.addtime(2); // Point A b.changereserver(a.getreserver()); a = c; a.addtime(2); // Point B if (c.getendtime() == 12) { c = new Reservation(); else if (b.getendtime() == c.getendtime()) { b.addtime(-1); // Point C a = new Reservation(202, 50, "Will") b = a; c = b; b = new Reservation(); a.addtime(2); //Point D Point a.numpeople a.reserver b.endtime c.endtime Point A Point B Point C Point D (1 point per box)

CIS 110 Final Midterm Fall 2017 Page 10 7) Bit Strings (6 points) Below is a partially complete function to adds two BitStrings of 8 bits each. For example, the BitString representing 5 plus the BitString representing 7 should result in the bitstring representing 12. However, in a misguided attempt to hold the attention of a younger audience, Will randomly named all variables after social networks. While we deal with explaining to Will that he's old now, and he needs to accept that, you fill in the blanks below. You are not allowed to change any variable names, or add any code. You can only fill in the blanks. public static String addbitstrings(string twitter, String facebook) { //set initial values String instagram = ; //output String int snapchat = ; //carry bit for(int pinterest = 7; pinterest >= 0; pinterest--) { //character from twitter int imgur = twitter.charat(pinterest) - ; //character from facebook int reddit = facebook.charat(pinterest) - ; //find the sum bit and add it to the string instagram = (( ) % 2) + instagram; //determine the new carry bit snapchat = ( ) / 2; return instagram;

CIS 110 Final Midterm Fall 2017 Page 11 8) InLinkedListCeption (5 points) A professor is keeping track of his students grades on a Recursion homework by using a LinkedList represented with the following Node class. public class Node { public Student s; public Node next; This uses the following Student class: public class Student { public String name; //Student's name public int score; //score on the recursion Homework The professor wants to know which student did worst on the homework. So he has tasked you with writing a recursive function to return the name of the student with the lowest score. Luckily, you understand recursion, so this should be easy. Write a function getlowestscorestudent(node node) that returns the Student object of the student with the lowest score in either node or in any Node AFTER it. You can assume no two students have the same score on the homework. If the first student in the list is called "head", then getlowestscorestudent(head) should return the Student with the lowest score in the class. If you write an iterative solution, instead of a recursive solution, you will get ZERO points on this question. Don't even think about using the words for, while, etc. public Student getlowestscorestudent(node node) {

CIS 110 Final Midterm Fall 2017 Page 12 9) I'll take Merge on Rye, Hold the Sort (10 points) In this question, you will write a function that combine two SORTED List<Integer> and output a single SORTED list. You cannot use Collections.sort(), or any sorting algorithm. No recursion, and no nested loops. You can assume the two lists are already sorted. You can also assume that if there are duplicates, their order doesn't matter. You do not have to error check if the lists are sorted OR null, but it is possible either or both lists are empty. This function must not add or remove elements to the lists a or b, or change those two lists in any way. public List<Integer> merge(list<integer> a, List<Integer> b) {

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

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

CIS 110 Final Midterm Fall 2017 Page 15 SCRATCH PAPER THIS PAGE WILL NOT BE GRADED. YOU MAY RIP IT OFF, BUT YOU MUST TURN IT IN AT THE END OF CLASS