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

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

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

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

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

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

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

CIS Introduction to Computer Programming. 5 October 2012 Midterm

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

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

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

COS 126 Midterm 1 Written Exam Spring 2015

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

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

CIS Introduction to Computer Programming Spring Exam 1

COS 126 General Computer Science Fall Exam 1

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

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Fall Exam 1

Prelim One Solution. CS211 Fall Name. NetID

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

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

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

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

CS 206 Introduction to Computer Science II

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

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

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

COS 126 Written Exam 2 (Spring 2015)

COS 126 Midterm 1 Written Exam, Fall 2009

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

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

CIS 110 Introduction to Computer Programming 20 December 2013 Final Exam. Answer Key

Queues. CITS2200 Data Structures and Algorithms. Topic 5

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

CS 315 Data Structures mid-term 2

CIS Introduction to Computer Programming Spring Exam 2

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

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

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

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CS171 Final Practice Exam

CSC 1052 Algorithms & Data Structures II: Linked Queues

Binary Trees: Practice Problems

Name CPTR246 Spring '17 (100 total points) Exam 3

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

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

CS 2301 Exam 3 B-Term 2011

Lesson 26: ArrayList (W08D1)

COS 126 General Computer Science Fall Written Exam 1

CS 314 Exam 2 Spring 2018

Section 1: True / False (2 points each, 30 pts total)

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

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

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

Section 05: Solutions

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

I have neither given nor received any assistance in the taking of this exam.

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

CS 6371: Advanced Programming Languages

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

CIS 341 Final Examination 4 May 2017

CmpSci 187: Programming with Data Structures Spring 2015

Expression: Expression: Statement: Output: Expression: Expression:

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CS II: Data Structures Discussion worksheet: Week 15

Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU. Please put student id on last page. Instructions:

Logic, Algorithms and Data Structures Recursion and Stacks. By: Jonas Öberg

INF2220: algorithms and data structures Series 1

Question: Total Points: Score:

ECE264 Spring 2013 Final Exam, April 30, 2013

COS 126 Written Exam 2, Fall 2009

Final Exam. Spring Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

COS 126 Midterm 1 Written Exam Fall 2012

Computer Science II Fall 2009

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

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

CSE 331 Midterm Exam 2/13/12

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

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

Computer Science /21/2000

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

CSCE 2014 Final Exam Spring Version A

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

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

COS 126 Exam 2 Review Part 1

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

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

Warmup : Name that tune!

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012

UNIVERSITY REGULATIONS

Transcription:

CIS 110 Spring 2012 Final Exam, 7 May 2012, Answer Key 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. ( 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[][]; 2: int sum; 3: for (int j = 0; j < myarray.length; j++) { : for (int i = 0; j < myarray[0].length; i++) { : myarray[i][j] = i + j; 6: int sum = i + j; 7: 8: 9: System.out.println(sum); Bug 1: 1: int[][] myarray = new int[][]; Bug 2: 2: int sum = 0; Bug 3: : i < myarray[0].length Bug : : myarray[j][i] = i + j; Bug : 6: sum += i + j; 1

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? - 9 (b) What will java Madness 3 print out? -1 9 (c) What will java Madness 2 print out? 0 9 2

Why. (12 points) Consider the following class, then answer the questions on the following page: What will each of the following code snippets print? If the code will cause a runtime error, write Exception. (a) Why w = new Why(); Exception (b) Why w = new Why("Why did the chicken cross the road?"); Why Why did the chicken cross the road?? Why not? (c) Why w = new Why("Why did the chicken cross the road?", "To get to the other side."); Why Why did the chicken cross the road?? To get to the other side. (d) Why w = new Why(null, ""); Exception (e) Why w = new Why("", null); Why? null (f) Why w = new Why(null, null); Exception 3

TOY. (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: C01 if (R[0] == 0) pc <- 1 12: 1331 R[3] <- R[3] + R[1] 13: 9302 Mem[02] <- R[3] 1: 8202 R[2] <- Mem[02] 1: 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: C01 if (R[0] == 0) pc <- 1 20: 0000 halt (a) Describe, in 20 words or less, what the above program does: The program prints out 1 2 8 (hex). (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] The program prints out powers of 2 until it wraps around, then prints zeros forever.

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: (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) { if (name == null) return; if (isfull()) return; for (int i = first; i!= last; i = (i + 1) % rb.length) if (name.compareto(rb[i]) == 0) return; rb[last] = name; last = (last + 1) % rb.length; size++; (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 Your answer: Jeff Nate Catherine Rupi Kathryn <- first, last

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: (a) Implement the recursive insert() method as described in the problem description. public void insert(comparable v) { if (v.compareto(value) < 0) { if (left!= null) left.insert(v); else left = new BST(v); else { if (right!= null) right.insert(v); else right = new BST(v); (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() { if (right!= null) right.printreversesorted(); printvalue(); if (left!= null) left.printreversesorted(); 6