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

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

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

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. 13 February 2013 Make-Up Midterm Midterm

CIS Introduction to Computer Programming. 5 October 2012 Midterm

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

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

COS 126 Written Exam 2 (Spring 2015)

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

CIS Introduction to Computer Programming Spring Exam 1

COS 126 General Computer Science Fall Exam 1

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

COS 126 Midterm 1 Written Exam Spring 2015

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

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

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

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

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

COS 126 General Computer Science Fall Exam 1

COS 126 General Computer Science Fall Midterm 1

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

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

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

COS 126 General Computer Science Spring Written Exam 1

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

CIS Introduction to Computer Programming Spring Exam 2

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

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

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

What is TOY? An imaginary machine similar to: ! Ancient computers. ! Today's microprocessors.

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

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

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

Computer Architecture /

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key

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

COS 126 Written Exam 2 Spring 18

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

11. A Computing Machine

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

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

! Binary and hex. ! Box with switches and lights. ! 4,328 bits = (255 " 16) + (15 " 16) + (8) = 541 bytes! ! von Neumann architecture.

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

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

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

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

Summer Session 2004 Prelim I July 12, CUID: NetID:

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

Midterm Exam (REGULAR SECTION)

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

Computer Science /21/2000

CIS 110 Fall 2016 Introduction to Computer Programming 8 Dec 2016 Final Exam Answer Key

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

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

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

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

About this exam review

Queues. CITS2200 Data Structures and Algorithms. Topic 5

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

CS171 Midterm Exam. October 29, Name:

CSE 331 Midterm Exam 2/13/12

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

COS 126 Written Exam 2, Fall 2009

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

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

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

Week 6. Data structures

12/11/ The TOY Machine II. Data Representation. What We've Learned About TOY. What We Do Today. Adding and Subtracting Binary Numbers

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

5. The TOY Machine II

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

CIS 120 Midterm I October 2, Name (printed): Username (login id):

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key for review problems

CSE 143 SAMPLE MIDTERM

CS2012 Programming Techniques II

CSCD 326 Data Structures I Queues

CSE 143: Computer Programming II Autumn Sample Solutions

COMP 103. Data Structures and Algorithms

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

TOY II LINC LINC. !1 Introduction to Computer Science Sedgewick and Wayne Copyright 2007

APCS Semester #1 Final Exam Practice Problems

COS 126 Midterm 1 Written Exam, Fall 2009

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

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

Prelim One Solution. CS211 Fall Name. NetID

: Advanced Programming Final Exam Summer 2008 June 27, 2008

CS111: PROGRAMMING LANGUAGE II

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

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

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

Data Structures and Algorithms Winter term 2012

CONDITIONAL EXECUTION

Transcription:

CIS 110 Introduction to Computer Programming 7 May 2012 Final Exam Name: Recitation # (e.g. 201): Pennkey (e.g. bjbrown): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this examination. Signature Date Scores: 1 1 2 5 3 9 4 12 5 13 6 20 7 30 Total: 90

CIS 110 Final Instructions You have 110 minutes to finish this exam. Time will begin when called by a proctor and end precisely 110 minutes after that time. If you continue writing after the time is called, you will receive a zero for the exam. This exam is closed-book, closed-notes, and closed-computational devices. Except where noted, you can assume that code included in the question is correct and use it as a reference for Java syntax. This exam is long. If you get stuck part way through a problem, it may be to your advantage to go on to another problem and come back later if you have time. When writing code, the only abbreviations you may use are for System.out.println, System.out.print, and System.out.printf as follows: System.out.println S.O.PLN System.out.print S.O.P System.out.printf S.O.PF Otherwise all code must be written out as normal, including all curly braces and semicolons. Please do not separate the pages of the exam. If a page becomes loose, write your name on it and use the provided staplers to reattach the sheet when you turn in your exam so that we don t lose it. If you require extra paper, please use the backs of the exam pages or the extra sheet(s) of paper 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 ). Staple an extra sheets you use to the back of your exam when you turn it in using the provided staplers. If you have any questions, please raise your hand and an exam proctor will come to answer them. When you turn in your exam, you will be required to show ID. If you forgot to bring your ID, please talk to an exam proctor immediately. Good luck, have fun! 1

TOY Reference Card INSTRUCTION FORMATS................ Format 1: opcode d s t (0-6, A-B) Format 2: opcode d addr (7-9, C-F) ARITHMETIC and LOGICAL operations 1: add R[d] <- R[s] + R[t] 2: subtract R[d] <- R[s] - R[t] 3: and R[d] <- R[s] & R[t] 4: xor R[d] <- R[s] ^ R[t] 5: shift left R[d] <- R[s] << R[t] 6: shift right R[d] <- R[s] >> R[t] TRANSFER between registers and memory 7: load address R[d] <- addr 8: load R[d] <- mem[addr] 9: store mem[addr] <- R[d] A: load indirect R[d] <- mem[r[t]] B: store indirect mem[r[t]] <- R[d] CONTROL 0: halt halt C: branch zero if (R[d] == 0) pc <- addr D: branch positive if (R[d] > 0) pc <- addr E: jump register pc <- R[d] F: jump and link R[d] <- pc; pc <- addr Register 0 always reads 0. Loads from mem[ff] come from stdin. Stores to mem[ff] go to stdout. 2

Miscellaneous 1. (1 points) (a) Write your name, recitation number, and PennKey (username) on the front of the exam. (b) Sign the certification that you comply with the Penn Academic Integrity Code Find the Bugs 2. (5 points) Give five bugs in the following code. The line numbers are included for your convenience and are not part of the program. (Hint: Think about what the program is trying to do to find bugs in the logic.) 1: int[][] myarray = int[5][4]; 2: int sum; 3: for (int j = 0; j < myarray.length; j++) { 4: for (int i = 0; j < myarray[0].length; i++) { 5: myarray[i][j] = i + j; 6: int sum = i + j; 7: 8: 9: System.out.println(sum); Bug 1: Bug 2: Bug 3: Bug 4: Bug 5: 3

Method Madness 3. (9 points) Consider the following program: public class Madness { public static int[] y = { 7, 3, 2 ; public static int x = 2; public static void main(string[] args) { int x = Integer.parseInt(args[0]); x = mysterymethod(x, y); System.out.println(x); for (int i = 0; i < y.length; i++) { System.out.println(y[i]); public static int mysterymethod(int y, int[] z) { for (int i = 0; i < z.length; i++) z[i] += x; return x - y; (a) What will java Madness 7 print out? (b) What will java Madness 3 print out? (c) What will java Madness 2 print out? 4

Why 4. (12 points) Consider the following class, then answer the questions on the following page: public class Why { private String question; private String answer; public Why() { answer = "Because."; public Why(String q) { question = q; answer = "Why not?"; public Why(String q, String a) { question = q; answer = a; public String question() { String q = "Why"; if (question.length() > 0) q = q + " " + question; q = q + "?"; return q; public String answer() { return answer; public String tostring() { return question() + "\n" + answer() + "\n"; 5

What will each of the following code snippets print? If the code will cause a runtime error, write Exception. (a) Why w = new Why(); System.out.println(w); (b) Why w = new Why("Why did the chicken cross the road?"); System.out.println(w); (c) Why w = new Why("Why did the chicken cross the road?", "To get to the other side."); System.out.println(w); (d) Why w = new Why(null, ""); System.out.println(w); (e) Why w = new Why("", null); System.out.println(w); (f) Why w = new Why(null, null); System.out.println(w); 6

TOY 5. (13 points) Consider what happens when the following TOY program is executed by pressing RUN with the program counter set to 10: 01: 0001 ( 0000 0000 0000 0001 ) 02: 0010 ( 0000 0000 0001 0000 ) 03: 0012 ( 0000 0000 0001 0010 ) 10: 8101 R[1] <- Mem[01] 11: C014 if (R[0] == 0) pc <- 14 12: 1331 R[3] <- R[3] + R[1] 13: 9302 Mem[02] <- R[3] 14: 8202 R[2] <- Mem[02] 15: 2321 R[3] <- R[2] - R[1] 16: C320 if (R[3] == 0) pc <- 20 17: 91FF mem[ff] <- R[1] 18: 1111 R[1] <- R[1] + R[1] 19: C014 if (R[0] == 0) pc <- 14 20: 0000 halt (a) Describe, in 20 words or less, what the above program does: (b) Describe, in 20 words or less, what the program would do if the instruction at memory address 11 were replaced with the following: 11: 8302 R[3] <- Mem[02] 7

Whiteboarding 6. (20 points) The list of names of students waiting for help during office hours is a good example of a ring buffer. Students add their names to the end of the list, and TAs help students from the front of the list. When the list reaches the end of the board, it wraps around. But there is one wrinkle: students only get to have their name on the board at most once. Consider the following class to implement this behavior, then answer the two questions on the following pages: public class Whiteboarding { private String[] rb; // the array backing the ring buffer private int first; // index of the first valid element in the buffer private int last; // index one past the last valid element in the buffer private int size; // the number of elements currently in the buffer private Whiteboarding() { // forbid the default constructor // Create a whiteboard that can hold capacity names public Whiteboarding(int capacity) { if (capacity <= 0) throw new RuntimeException("Invalid capacity."); rb = new String[capacity]; // return the number of names currently on the board public int size() { return size; // return whether the board is empty/full public boolean isempty() { return size == 0; public boolean isfull() { return size == rb.length; // Add name to the end of the queue if it is not // already in the queue, and the queue is not full public void enqueue(string name) { /*... */ // Delete and return the name from the front of the queue, or // return null if the queue is empty public String dequeue() { /*... */ // Return the name from the front of the queue, or null if the queue // is empty, but do not delete the element public String peek() { /*... */ 8

(a) Implement the enqueue() method so that it behaves according to its comment. Make sure the values of all instance variables are consistent with their comments when enqueue() returns. (Hint: recall that if s and t are strings, then s.compareto(t) will return 0 if the two strings are the same.) public void enqueue(string name) { 9

(b) What will the contents of the rb array be after the following sequence of statements? Indicate which entries in the array correspond to first and last Whiteboarding w = new Whiteboarding(5); w.enqueue("dan"); w.enqueue("jeff"); w.enqueue("catherine"); w.enqueue("dan"); w.enqueue("rupi"); w.enqueue("kathryn"); w.enqueue("nate"); w.dequeue(); w.peek(); w.dequeue(); w.peek(); w.enqueue("kathryn"); w.enqueue("jeff"); w.enqueue("nate"); Your answer: 10

Binary Search Trees 7. (30 points) A binary search tree (BST) is a linked data structure, where each node contains a value and pointers to two other nodes, left and right. These nodes are referred to as its children. Just as the next node of a linked list is itself the start of a linked list, the left and right children of a BST node are themselves BSTs, called the left and right sub-trees. What makes BSTs interesting is an additional requirement that the every value in the left sub-tree of a node n must be less than the value stored in n, and every value stored in the right sub-tree of n must be greater than or equal to the value stored in n. If the left or right sub-trees of n are empty, then the left and/or right pointers will be null. Here are some examples of BSTs: 6 2 2 5 7 5 5 2 5 8 6 7 8 7 8 5 9 Searching for a value v in a BST n is very efficient using a simple recursive method. If n.value == v you re done. Otherwise, if v < n.value search the left sub-tree. Otherwise search the right sub-tree. If n == null then the tree doesn t contain v. Values can be added using a similar recursive method. A value v should be added to the left or right sub-tree depending on whether it is less than n.value or not. If that sub-tree is null, a new node containing the value v should be appended there. The nice thing about BSTs is that, if your values are well distributed, the size of the left and right sub-tree will be about equal and searching and insertion will be much faster than in a linked list. (More complex data structures, such as red-black trees guarantee this property.) Consider the following BST class on the following page, then write the insert() and printreversesorted() methods in the spaces provided. 11

public interface Comparable { // return 0 if this equal other // -1 if this is less than other // 1 if this is greater than other int compareto(object other); public class BST { private Comparable value; private BST left; private BST right; public BST(Comparable v) { value = v; // return true if this tree contains v // or false if it doesn t public boolean find(comparable v) { /*... */ int cmp = v.compareto(value); if (cmp == 0) return true; else if (cmp < 0) { if (left!= null) return left.find(v); else return false; else { if (right!= null) return right.find(v); else return false; // insert v into this tree public void insert(comparable v) { /*... */ // print the value of this node public void printvalue() { System.out.println(value); // print the tree contents in sorted order, one value per line public void printsorted() { /*... */ // print the tree contents in reverse sorted order, one value per line public void printreversesorted() { /*... */ 12

(a) Implement the recursive insert() method as described in the problem description. public void insert(comparable v) { 13

(b) Implement the recursive printreversesorted() method to print all the values in the tree in reversesorted order, as described in the problem description. public void printreversesorted() { 14

Postscript (extra paper) 15

Postscript (extra paper) 16