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

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

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 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

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

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

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

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 2018 Midterm. Recitation ROOM :

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

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

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

CIT Special final examination

CIS Introduction to Computer Programming Spring Exam 2

CIS 110 Introduction To Computer Programming. February 29, 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 Summer 2016 Final. Recitation # (e.g., 201):

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

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

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

CSE 131 Introduction to Computer Science Fall Exam II

Midterm Exam 2 CS 455, Spring 2011

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

CSE 131 Introduction to Computer Science Fall Exam II

Csci 102: Sample Exam

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!

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

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CS100M November 30, 2000

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

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

CSE 131 Introduction to Computer Science Fall Exam III

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

CSE 131 Introduction to Computer Science Fall Exam I

Computer Science 1 Ah

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Final Exam. COMP Summer I June 26, points

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

CS 314 Exam 2 Spring

About this exam review

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

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

PRACTICE MIDTERM EXAM #2

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

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

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

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

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

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CSE 1223: Exam II Autumn 2016

CS 113 MIDTERM EXAM 2 SPRING 2013

CS171 Midterm Exam. October 29, Name:

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

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

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

CS100J November 20, 2001

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Computer Science Foundation Exam

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

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

Introduction to Programming (CS112): Sample

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Summer Final Exam Review Session August 5, 2009

Largest Online Community of VU Students

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

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

Csci 102: Sample Exam

CIS Fall Data Structures Midterm exam 10/16/2012

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CS61BL: Data Structures & Programming Methodology Summer 2014

CSE 303 Midterm Exam

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

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

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

COM S 211/ENGRD 211 May 15, 2003

CS 1301 Exam 1 Fall 2010

CSE 131S Introduction to Computer Science Summer SON Exam III

CS 367: Introduction to Data Structures Midterm Sample Questions

10. Creating Data Types

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

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CS170 (005): Introduction to Computer Science Exam 2

CSE 373, Winter 2013 Final Exam, Tuesday, March 19, Good luck! You can do it!

CS 112 Midterm Exam Fall 2016

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

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

EXAMINATIONS 2016 TRIMESTER 2

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

Transcription:

CIS 110 Introduction to Computer Programming Summer 2014 Final Name: PennKey (e.g., bhusnur4): 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 to do so by the proctor. You will have exactly 115 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 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. If a page becomes loose, reattach 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. We wish you the best of luck. Enjoy the rest of your summer! Scores: [For instructor use only] Question 0 1 pt Question 1 17 pts Question 2 16 pts Question 3 13 pts Question 4 13 pts Total: 60 pts

1 0.) THE EASY ONE (1 point) Check that your exam has all 11 pages (excluding the cover sheet and scratch paper). Write your name on the front of the exam. Sign the certification that you comply with the Penn Academic Integrity Code. 1.) RECURSION (17 points total) 1.1) Bad Sierpinski (2 points) A student wrote the following code for the Sierpinski triangle assignment. If you look carefully, you will notice a logical flaw which results in a weird shape when they run the program. Which shape will it result in? Circle the result of the code below. Assume that singletriangle() draws a single equilateral triangle with the bottom vertex at (x, y). public class Sierpinski { public static void sierpinski(int n, double size, double x, double y) { if (n == 0) { return; singletriangle(size, x, y); sierpinski(n - 1, size / 2, x, y + size * Math.sqrt(3) / 2.0); sierpinski(n - 1, size / 2, x size / 2, y); public static void main(string[] args){ sierpinski(3, 0.5, 0.5, 0);

2 1.2.) Cruel recursion (10 points total) This scarily named class is doing something with arrays but has very few comments. The comments that are there are correct. Find out what it is doing. 1) What will be printed when we run java RecursiveNightmare? (6 points) 2) In 20 words or fewer, explain what meanie() does. meanie() is a weird name for a function; what would be a better name for it? (4 points) public class RecursiveNightmare { public static double meanie(int[] x) { int kruger = x.length; if (kruger == 0) { return 0; if (kruger == 1) { return x[0]; int[] temp = new int[kruger / 2]; int[] temp2 = new int[kruger kruger / 2]; // copy half the array into temp for (int i = 0; i < kruger / 2; i++) { temp[i] = x[i]; // copy the second half into temp2 for (int i = kruger / 2; i < kruger; i++) { temp2[i kruger / 2] = x[i]; double freddie = temp.length + temp2.length; return (meanie(temp) * temp.length + meanie(temp2) * temp2.length) / freddie ; public static void main(string[] args) { int[] abc = {1, 0, -1, 2, 2, -1, 1, 0; double r = meanie(abc); System.out.println(r);

3 1.3) Walk, trot, Cantor, gallop and recurse! (5 points) One popular recursive drawing corresponds to a famous mathematical construction by Georg Cantor. The Cantor set is created by repeatedly deleting the open middle third of a set of line segments. Complete the cantorize() and main() functions so that when the Cantor class is run with a command-line argument N, a representation of the Cantor set is drawn to N levels. The command java Cantor 6 should draw a Cantor set to 6 levels (beginning our numbering from 1), as seen in the picture below. Your code must involve recursion. As long as the pattern below is produced to any scale, your answer is correct. You may assume that drawline(x, y, len) draws a horizontal line beginning at (x, y) with length len. The exact pixel value of the line length does not matter. public class Cantor { public static void drawline(double x, double y, double len) { StdDraw.line(x, y, x + len, y); public static void cantorize( /* TODO */ ) { // TODO complete the body of this method public static void main(string[] args) { // TODO complete the body of this method

4 2) OOPs :( (16 points total) 2.1) True or false? To initialize a variable of type Object, you have to call the constructor of Object. (1 point) 2.2) Pow (5 points) Here is the code for a class representing complex numbers (from the textbook). We would like to add another method called pow() that takes an integer i and computes the value of the complex number raised to the ith power. Write the pow() method. Remember that a to the ith power is a a a i times. You can assume that i is positive. public class Complex { private final double re; private final double im; public Complex(double real, double imag) { re = real; im = imag; public String tostring() { return re + " + " + im + "i"; public double abs() { return Math.sqrt(re * re + im * im); public Complex plus(complex b) { double real = re + b.re; double imag = im + b.im; return new Complex(real, imag); public Complex times(complex b) { double real = re * b.re im * b.im; double imag = re * b.im + im * b.re; return new Complex(real, imag); // TODO write the pow() method here

5 2.3) Bugs (7 points) The following attempt has been made at writing a class with some instance variables and methods. It is riddled with bugs. Correct these bugs. (Sorry, we are not telling you the number of bugs this time.) Fill your answers in the next page where we have a blank space for each line; you may not need all of the spaces provided. You may assume that the author of the code has given the fields and methods names that reflect their intention. 1 public class StudentDemo { 2 public static void main(string[] args){ 3 // create a student named Eric Eaton 4 Student s = Student("Eric Eaton", 19, "South Korea"); 5 if (s.canvote()) { 6 System.out.println("The student named " + s.name + " can vote"); 7 8 9 10 11 class Student { 12 private String name; 13 private String country = "Canada"; 14 private int age = 18; 15 16 public Constructor(String n, String ctry, int age) { 17 // initialize name, country and age 18 String country = ctry; 19 name = n; 20 age = age; 21 22 23 public static int getvotingage(string country) { 24 if (country.equals("singapore")) return 21; 25 else if (country.equals("south Korea")) return 19; 26 27 28 public boolean canvote() { 29 if (age >= getvotingage()) return false; 30 return true; 31 32 // end of Student class

6

7 2.4) JButton (3 points) Java has many built-in classes. Among them, there is one called JButton, which creates a button like those you see on web pages; another is Dimension, which is used to represent the width and height of user interface elements such as buttons; yet another is Color, which represents the colors that are displayed on screen. Below are extracts from the public APIs for those classes. JButton JButton() JButton(String text) void setpreferredsize(dimension d) void setbackground(color c) Creates a button with no text Creates a button with the specified text Sets the size of the button to d Sets the background color of the button to c Dimension Dimension() Dimension(int width, int height) Creates an instance of Dimension representing a height of 0 and a width of 0 Creates an instance of Dimension representing specified height and width Color Color(int red, int green, int blue) Creates an instance of Color representing a color with specified red, green, and blue components Suppose we have a JButton which has been declared as follows: JButton submitbutton; We would like to make submitbutton refer to a blue button with width 80 and height 40, with the text Submit inside it. Write code that does this, using the API documentation that we have provided above. (Your code will run in the same scope as the above declaration.) Recall that blue is represented by 0 red, 0 green and 255 blue. // TODO write your code here

8 3) SOARTING (13 points total) 3.1) True or false? Mergesort is the fastest way to sort any array, regardless of the data within it. (1 point) 3.2) If, on average, it takes 10 seconds to sort an array of length 10,000 using insertion sort, how long will it take to sort an array of length 20,000 using the same algorithm (all other things being equal)? (2 points) 3.3) Hors d œuvre (4 points) Assume we are using selection sort on the character array char[] name = {'b','e','n','e','d','i','c','t'; (where we sort the characters in alphabetical order). What is the state of name after each step of the selection sort algorithm? Fill in the table below, using as many rows as necessary to complete the selection sort algorithm. name[0] name[1] name[2] name[3] name[4] name[5] name[6] name[7]

9 3.4) Appetizer (6 points) Assume you have two Stacks, both of which contain ints in sorted order. Stack has the following public API. Stack() void push(int i) int pop() int peek() boolean isempty() Creates a Stack Push i to the top of the Stack Pop the element at the top of the Stack and return it Return the element at the top of the Stack, without popping it Returns true if the Stack is empty Write a public static method mergestacks() that takes two sorted Stacks, and returns a new Stack where the elements of the two Stacks are merged. The merging is to be done in the same manner as you have seen with the mergesort of arrays, as illustrated below. Stack stack1 Stack stack2 mergestacks(stack1, stack2) 3 4 5 7 9 6 10 11 3 4 5 6 7 9 10 11 public static Stack mergestacks(stack stack1, Stack stack2) { // TODO complete the body of this method

10 4) LINKED LISTS (13 points) 4.1) Main course (10 points) Assume that we have defined the following classes. public class Node { public int item; public Node next; public class LinkedList { public Node first; In the LinkedList class, write a public void method, delalternate(), that removes every other element in the list. (If we give first the index 0, then only the elements with even indices should remain in the list.) Be careful to cover all cases! public void delalternate() { // TODO complete the body of this method

11 4.2) Dessert (3 points) Assume that we have defined the following classes. public class Student { private String name; private String college; public class Course { private String name; private String department; private String semester; You want to add another field to the Student class. It is called courses and it represents all the courses that the Student has taken so far. The most common thing that a Student does is take courses. Courses can be added and dropped and Students tend to do both those things a lot, so the data structure needs to be optimized for that. Would you pick an array or linked list representation for courses? Explain why. Given your choice, will it be simpler (in terms of the number of operations needed) to add a course, or will it be simpler to drop a particular course in courses? Explain why. You need not write any code: this is a conceptual question.

Extra page This page intentionally left blank 12

Extra page This page intentionally left blank 13