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

Similar documents
CIS Introduction to Computer Programming. 5 October 2012 Midterm

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

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

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

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

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

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

COS 126 Midterm 1 Written Exam Fall 2011

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

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

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

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

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

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

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

CIS Introduction to Computer Programming Spring Exam 1

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

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

COS 126 Midterm 1 Written Exam Fall 2014!

Introduction to Computer Science Unit 2. Notes

AP CS Unit 3: Control Structures Notes

Question: Total Points: Score:

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

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

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

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

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

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

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

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

Decision-Making and Repetition

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

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

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

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

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

APCS Semester #1 Final Exam Practice Problems

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

Introduction to Computer Science Unit 2. Notes

Condi(onals and Loops

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

CSCI 135 Final Fall Problem Points Score Total 100

COS 126 Midterm 1 Written Exam Spring 2015

Conditionals, Loops, and Style

Building Java Programs A Back to Basics Approach 4th Edition Reges TEST BANK Full download at:

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CONDITIONAL EXECUTION

COS 126 General Computer Science Fall Exam 1

1.3 Conditionals and Loops

Computação I. Exercises. Leonardo Vanneschi NOVA IMS, Universidade Nova de Lisboa. Leonardo Vanneschi Computação I NOVA IMS

CONDITIONAL EXECUTION

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

Variables and data types

Introduction to Computer Science Unit 2. Exercises

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

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

COS 126 Midterm 1 Written Exam Fall 2012

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

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

CSE 142 Sample Midterm Exam #2

Practice Midterm 1. Problem Points Score TOTAL 50

COS 126 Midterm 1 Written Exam Fall 2013

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Csci 102: Sample Exam

CS111: PROGRAMMING LANGUAGE II

Full file at

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

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

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Introduction to Software Development (ISD) Week 3

1. An operation in which an overall value is computed incrementally, often using a loop.

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

COMP1006/ Summer Tutorial 1

University of Palestine. Mid Exam Total Grade: 100

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

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

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

Lecture 5: Methods CS2301

CS170 Introduction to Computer Science Midterm 2

CSE Fall 2015 Section 002 Exam 2, Time: 80 mins

CS S-08 Arrays and Midterm Review 1

COS 126 General Computer Science Fall Written Exam 1

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

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

ESC101 : Fundamental of Computing

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Practice Midterm 1 Answer Key

CSEN 202 Introduction to Computer Programming

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

3. Conditionals & Loops

Transcription:

CIS 110-001 1 CIS 110-001 Fall 2012 Midterm, 7 June 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 Money, money, money, money, money... 2. (4 points) Consider the following recursive function: public static double compoundinterest(double balance, int months, double rate) { if (months <= 0) return balance; balance = balance * rate; return compoundinterest(balance, months - 1, rate); (a) Is this function tail recursive (circle your answer)? YES (b) How many times will compoundinterest(0, 12, 1.05) call itself recursively? 12 times (c) What value will compoundinterest(0, 12, 1.05) return? 0 (d) Will the following non-recursive function produce the same or different results from the recursive function above? Circle your answer. DIFFERENT public static double iterativeinterest(double balance, int months, double rate) { for (int i = months; i >= 0; i--) balance = balance * rate; return balance;

CIS 110-001 2 Cabin 3. (7 points) Given the following class, answer the questions below and on the next page: public class Fever { public static int restless(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) sum = sum + arr[i]; return sum / arr.length; public static void main(string[] args) { int[] intargs = new int[args.length]; for (int i = 0; i < args.length; i++) intargs[i] = Integer.parseInt(args[i]); System.out.println(restless(intArgs)); (a) Circle the statements below that are true i. will not compile because the for loops do not have curly braces; ii. will not compile because restless() is called inside System.out.println; iii. intargs[] is args[] converted into an integer array; X iv. may crash with an ArrayIndexOutOfBounds error when run; v. does not print anything because the intargs[] array is different from the arr[] array ; vi. prints the approximate average of the command line arguments. X (b) Assuming that any errors you identified above are corrected, what will the program print out in each of the following cases? We will consider your answer correct as long as it shows you understand what the program will print out. i. % java Fever java.lang.arithmeticexception: / by zero ii. % java Fever 1.3 2.5 7.1 java.lang.numberformatexception: For input string: "1.3" iii. % java Fever 1 2 5 2 iv. % java Fever hello world java.lang.numberformatexception: For input string: "1.3"

CIS 110-001 3 Find the Bugs 4. (8 points) Identify eight errors in the following program that prevent it from compiling or from running and show how to correct them. Write your answers in the space provided on the next page. The line numbers are for your convenience and are not part of the program. 1: public class Foo() { 2: public static int main(string[] args) { 3: int rows = args[0]; 4: drawsomething(rows); 5: double hyp = computesomething(rows); 6: System.out.println("length(hypotenuse)" = hyp); 7: 8: public static void drawsomething(int rows) { 9: for (i = 0; i < rows; i++) { 10: for (j = 0; j < i + 1; j = j + 1) 11: System.out.print("*"); 12: System.out.println(); 13: 14: 15: public static double computesomething(int leg) { 16: int sqrt2 = Math.sqrt(2); 17: return sqrt2 * leg; 18: 19: Bug 1: 1: class Foo { Bug 2: 2: public static void main(string[] args) Bug 3: 3: int rows = Integer.parseInt(args[0]) Bug 4: 4: drawsomething(rows) // lower-case r Bug 5: 6: System.out.println("length(hypotenuse) = " + hyp); Bug 6: 9: for (int i = 0; i < rows; i++) { Bug 7: 10: for (int j = 0; j < i + 1; j = j + 1) Bug 8: 16: double sqrt2 = Math.sqrt(2);

CIS 110-001 4 Partial Sums 5. (15 points) The median, or middle value, of a list of numbers is extremely useful in a variety of computer and statistical algorithms. But it is notoriously slow to compute relative to the number of times it needs to be computed. Often, it is better to settle for an approximation of the median than to calculate it exactly. One of the simplest approximations is to pick three random elements, and calculate the median of those three. Write a function threemedian that takes a single argument N, reads in N doubles from standard input using StdIn.readDouble(), and returns the median of three randomly chosen elements. Assume that N is always at least three, and that all calls to StdIn.readDouble() succeed without error. Use Math.random() to pick the three random elements (recall that Math.random() returns a random number between 0 and 1, but never exactly 1). Do not write the code for the class that contains threemedian, only write the function itself. You do not need to comment your code. We awarded 14 points on this problem for a completely correct answer, and 15 for a correct and elegant answer along the lines of the second solution below. public static double threemedian(int N) { double[] arr = new double[n]; for (int i = 0; i < N; i++) arr[i] = StdIn.readDouble(); double a = arr[(int) (N * Math.random())]; double b = arr[(int) (N * Math.random())]; double c = arr[(int) (N * Math.random())]; if (a <= b && b <= c) return b; else if (c <= b && b <= a) return b; else if (b <= a && a <= c) return a; else if (c <= a && a <= b) return a; else return c; // ---------- or ---------- public static double threemedian(int N) { double[] arr = new double[n]; for (int i = 0; i < N; i++) arr[i] = StdIn.readDouble(); double a = arr[(int) (N * Math.random())]; double b = arr[(int) (N * Math.random())]; double c = arr[(int) (N * Math.random())]; double min = Math.min(Math.min(a, b), c); double max = Math.max(Math.max(a, b), c); return a + b + c - min - max;

CIS 110-001 5 Tracery 6. (15 points) For each of the labeled points in the code fragment below, identify each of the assertions in the table as being sometimes, always, or never true. Assume that bar is only called from within foo, and that the values of all ints stay within the valid range for integers (i.e. no value will grow so large that it will wrap around become negative, or vice cersa). Abreviate sometimes with S, always with A, and never with N. public static void foo(int x, int y, int z) { if (x > y && y <= 0) y = Math.abs(y - x) + 1; else if (x < y) z = Math.abs(z) + 1; else x = y - 1; A x > 0 S x > y N z > y S // Point A if (z > x) { z = x; x = y * y + 2; // Point B else { z = x; z = bar(y, Math.abs(x), z * z); // Point C B A A N C S N N D S S N E S S S if (y == x) y--; // Point D public static int bar(int z, int x, int y) { for (int i = 0; i < x; i++) y = y - x; // Point E return z + y;