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

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

COS 126 Midterm 1 Written Exam Fall 2011

CIS Introduction to Computer Programming. 5 October 2012 Midterm

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

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

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

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

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 110 Spring 2013 Make-Up Midterm, 13 February 2013, Answer Key. Miscellaneous

CIS Introduction to Computer Programming Spring Exam 1

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

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

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

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

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

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

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

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):

Final Exam. COMP Summer I June 26, points

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

COS 126 Midterm 1 Written Exam Fall 2012

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

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

CIS Introduction to Computer Programming Spring Exam 2

COS 126 General Computer Science Fall Written Exam 1

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

COS 126 Midterm 1 Written Exam Fall 2014!

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

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

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

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

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

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

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

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

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

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

CSE 1223: Exam II Autumn 2016

APCS Semester #1 Final Exam Practice Problems

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

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

CS 1301 Exam 1 Fall 2010

Introduction to Software Development (ISD) Week 3

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Question: Total Points: Score:

CSCI 1226 Sample Midterm Exam

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

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

COS 126 General Computer Science Spring Written Exam 1

COS 126 Midterm 1 Written Exam Fall 2013

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

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

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

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

COS 126 General Computer Science Fall Exam 1

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

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

CIS 120 Midterm I February 16, 2015 SOLUTIONS

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

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CSCI 1226 Second Midterm Test

COS 126 Midterm 1 Written Exam, Fall 2009

A Foundation for Programming

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

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

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

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

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

Condi(onals and Loops

CS 177 Spring 2010 Exam I

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CIS 341 Final Examination 4 May 2017

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

Repetition with for loops

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Lecture 5: Methods CS2301

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

Transcription:

CIS 110 Introduction To Computer Programming February 29, 2012 Midterm 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 Scores: 1 2 3 4 5 6 7 8 9 10 11 Total (100 max) Date

CIS 110 Practice Midterm 1 Instructions This practice midterm is substantially longer than the real midterm will be so that you see a wider range of practice questions. So don t panic at the length! All instructions after this one are the instructions that would appear on the actual test. If you are doing this practice test as a homework assignment, you may use books, notes, etc. You can even work in groups! You have 50 minutes to finish this exam. Time will begin when called by a proctor and end precisely 50 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. When writing code, the only abbreviations you may use are for System.out.println and System.out.print as follows: System.out.println S.O.PLN System.out.print S.O.P Otherwise all code must be written out as normal. Please do not separate the pages of the exam. If a page becomes loose, please make sure to write your name on it so that we don t lose it, and use the provided staplers to reattach the sheet when you turn in your exam. If you require extra paper, please use the backs of the exam pages or the extra sheet 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 ). 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

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 Types and Casts 2. (7 points) Give the type and value of each of the following Java expressions. If an expression will not compile, write Illegal under type and put an X in value. You must fill in every entry. Entries left blank will be marked incorrect. Expression Type Value (7 / 2) * 2.0 (7 / 2.0) * 2!(!!false &&!!true) "1.1" + "2.2" 1 < 2 < 3 1 + 2.2 + "1.1" + "a" Integer.parseInt("123") 2

Java Expressions 3. (3 points) Assume that a, b, and c are variables of type boolean. Consider the following three conditions: (a) (a b) && c (b) (a && b) c (c)!(a && b) && c Which of the expression(s) above is (are) always true if a and b have different values (i.e. one is true and one is false) and c is true? Circle the expressions that evaluate to true. Array Declarations 4. (8 points) Among the following code fragments, circle the ones that will not cause a compile-time error. (a) int[] a = int[10]; (b) int[] a = new int[10]; (c) int[10] a; (d) int[10] a = new int[10]; (e) int[] a = {1, 2, 3; int b = a; (f) int[] a; (g) int a = {1, 2, 3; (h) int[] a = {1, 2, 3; int[] b = a; 3

5. (7 points) Consider the following code: Syntax and Scope public class Cubes { public static int square(int i) { return i * i * i; public static void main(string[] args) { for (int i = 1; i <= 1000; i++) StdOut.println(square(i)); Among the following statements, circle those that are true: (a) Will not compile beacuse braces in square() are not on separate lines. (b) Will not compile because braces are missing in the for loop. (c) Will not compile because i is not declared in square(). (d) Prints only a few lines because square() rapidly increases the i used by main. (e) Prints the squares of the integers from 1 to 1000. (f) Prints the cubes of the integers from 1 to 1000. (g) Goes into an infinite loop. 4

Debugging 6. (9 points) Consider the following code snippet, which is intended to examine an array of of double values and print out the minimum value in the array and its location. The line numbers at the left are for your convenience in referring to the code. You may assume arr[] is an array of double values that has been properly declared and initialized. 1 int N = arr.length; 2 double min = 0; 3 int minlocation=0; 4 5 for (int i = 1; i <= N; i++) { 6 if (arr[i] < min) 7 min = arr[i]; 8 minlocation = i; 9 10 11 System.out.print("The minimal value is arr["); 12 System.out.println(minLocation + "] = " + min); In the spaces provide below, identify (in ten words or less, each) three bugs in the code that will result in either an incorrect answer being printed or a program crash. Circle your answers. (a) A bug: (b) Another bug: (c) A third bug: 5

Recursive Graphics 7. (6 points) Consider the following recursive method: public static void squarelife(double x, double y, double r) { if (r < 0.05) return; // Select a random integer from the set [0, 1, 2, 3, 4] int randomint = StdRandom.uniform(5); if (randomint < 1) squarelife(x - r, y - r, r / 2); // bottom left if (randomint < 2) squarelife(x - r, y + r, r / 2); // top left if (randomint < 3) squarelife(x + r, y - r, r / 2); // bottom right if (randomint < 4) squarelife(x + r, y + r, r / 2); // top right drawshadedsquare(x, y, r); Assume that the helper method drawshadedsquare() draws a gray shaded square of radius r that is outlined in black and centered on (x, y). (a) Suppose that a client program issues the call squarelife(0.5, 0.5, 0.25). Circle the figures that could possibly result. (b) Suppose that a client program issues the call squarelife(0.5, 0.5, 0.6). What is the maximum possible number of times drawshadedsquare() could be called as a result? Write your answer in the blank provided. Maximum number of squares drawn: 6

Recursive Methods 8. (3 points) The function below is supposed to sum all the odd, or all the even, positive integers up to n, depending on whether n is odd or even. public static int foo(int n) { if (n == 0 n == 1) return n; return n + foo(n - 2); Circle any of the following that are correct: (a) base case written incorrectly so function will get the wrong answer, or no answer, for some valid positive values of n. (b) reduction step written incorrectly, so function will get the wrong answer, or no answer, for some valid, positive values of n (c) function is tail-recursive and therefore will get the wrong answer, or no answer, for some valid positive values of n (d) this function looks fine to me (e) it doesn t have problems a, b, or c, but does have the following problem, which will cause it to get the wrong answer, or no answer, for some valid positive values of n (write the problem down in at most one sentence) 7

Array Whisperer 9. (20 points) Write a function duplicate that takes an integer array arr and an integer n as arguments and returns a new integer array that contains n copies of arr laid out end-to-end. Assume that you are passed a valid array whose length is at least 0 and that n is positive. Here are some example invocations of duplicate and their results: Array n Return Value { 1, 2, 3 3 { 1, 2, 3, 1, 2, 3, 1, 2, 3 { 0 5 { 0, 0, 0, 0, 0 { 1, 2, 3, 4, 5 1 { 1, 2, 3, 4, 5 8

Prime Numbers 10. (20 points) Write a function primes that takes an integer N and prints out the number of prime numbers less than N. Recall that a prime number is an integer with exactly two divisors: one and itself. (The smallest prime number is therefore 2.) Hint: use nested for loops and the % operator. 9

Be Assertive 11. (15 points) For each of the labeled points in the code fragment below, identify each of the assertions in the table as being always true, never true, or sometimes true and sometimes false. Assume that the call to StdIn.readInt() always succeeds 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 and become negative, or vice versa). Note: You may abbreviate always with A, never with N, and sometimes with S. public static int foo(int x) { int y = x; int z = 0; boolean b = true; b == true x == y z > 0 // POINT A A while (b) { // POINT B y = StdIn.readInt(); B if (x < y) { z = z + y - x; C // POINT C else if (x == y) { z = z - y + x; D b = false; // POINT D E // POINT E return z; Postscript (extra paper) 10