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

Similar documents
CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

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

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

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

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

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

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

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

CIS Introduction to Computer Programming Spring Exam 1

CIS Introduction to Computer Programming. 5 October 2012 Midterm

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

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

COS 126 Midterm 1 Written Exam Fall 2011

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

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

COS 126 General Computer Science Spring Written Exam 1

CS170 Introduction to Computer Science Midterm 2

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

About this exam review

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

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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

CSE 1223: Exam II Autumn 2016

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

COS 126 General Computer Science Fall Written Exam 1

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

Question: Total Points: Score:

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

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

Arrays. Eng. Mohammed Abdualal

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

Prelim One Solution. CS211 Fall Name. NetID

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

COS 126 Midterm 1 Written Exam Fall 2012

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS61B Lecture #6: Arrays

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

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

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

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

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

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

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

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

University of Massachusetts Amherst, Electrical and Computer Engineering

CIS 341 Final Examination 30 April 2013

Functions. Arash Rafiey. September 26, 2017

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Name CIS 201 Midterm II: Chapters 1-8

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Question: Total Points: Score:

Csci 102: Sample Exam

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Section 05: Midterm Review

Project 1. due date Sunday July 8, 2018, 12:00 noon

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

COS 126 Midterm 1 Written Exam Fall 2013

CSE 143. Computer Programming II

Full file at

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

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 101 Fall 2006 Midterm 3 Name: ID:

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

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

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

Final Exam. COMP Summer I June 26, points

CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015

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

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Handout 4 Conditionals. Boolean Expressions.

Introduction to Software Development (ISD) Week 3

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

APCS Semester #1 Final Exam Practice Problems

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

FORM 2 (Please put your name and form # on the scantron!!!!)

CS 101, Spring 2014 April 1st Exam 2 Question 1. [3 points] What output is printed by the following code?

CSEN 202: Introduction to Computer Programming Spring term Final Exam

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

Pace University. Fundamental Concepts of CS121 1

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

public static<e> List<E> removeoccurrences(list<e> origlist, E remove) {

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Transcription:

CIS 110 Introduction to Computer Programming 12 February 2013 Midterm Answer Key 0. (1 point) Miscellaneous (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 Values 1. (9 points) Give the type and value for each of the following expressions and variable declarations. When there is more than one statement, give the type and value that the last statement computes. If the code would result in a syntax error, write N/A in the first column, and give the reason for the error in the second column. You do not need to write the exact error message. Type 6 / 4 int 1 Value String s = "Hello"; s.length; N/A The correct syntax is s.length() 0.0 / 0 double NaN "Hello " + 4 + 3 String "Hello 43"!!!false &&!!true boolean true String s = StdOut.print("Hello"); N/A StdOut.print is a void function, cannot assign to a String Math.pow(5, 2) double 25.0 6++ N/A Cannot increment a literal value char[] arr = { 5, 3 ; arr[2]; N/A array index out of bounds

CIS 110 1 Find the Bugs 2. (10 points) The following program is supposed to print out the second-largest double value specified on the command line, or -Infinity if less than two numbers are given. But Hanssen didn t test it before sending it to Dr. Brown, and it s full of bugs. Write the line number and corrected line of code for ten bugs in the program. Assume that all arguments represent valid double values and that all the values are different. Write your answers on the following page. 1 public static void class SecondBiggest { 2 public static void main(string[] args) { 3 int N = args.length; 4 int[] input = new int[n]; 5 for (int i = 1; i <= N; i++) 6 input[i] = args[i]; 7 System.out.println(second(input)); 8 9 10 public static double second(double[] arr){ 11 double biggest = Double.NEGATIVE_INFINITY; 12 double secondbiggest = Double.POSITIVE_INFINITY; 13 if (arr.length < 2) return; 14 for (int i = 0, i < arr.length, i++) { 15 double temp = arr[i]; 16 if (arr[i] > biggest) biggest = temp; 17 18 19 for (int j = 0; j < arr.length; j++) { 20 temp = arr[j]; 21 if ((temp > secondbiggest) && (temp!= biggest)) { 22 secondbiggest = temp; 23 24 return secondbiggest 25 26 Bugs 1 and 10 were worth 0.5 points, bug 9 was worth 2 points, all others were worth 1 point. Bug 1: 1: public class SecondBiggest { Bug 2: 4: double[] input = new double[n]; Bug 3: 5: for (int i = 0; i < N; i++) Bug 4: 6: input[i] = Double.parseDouble(args[i]) Bug 5: 12: double secondbiggest = Double.NEGATIVE INFINITY Bug 6: 13: return Double.NEGATIVE INFINITY Bug 7: 14: for (int i = 0; i < arr.length; i++) Bug 8: 20: double temp = arr[j] Bug 9: 21: if ((temp > secondbiggest) && (temp!= biggest)) Bug 10: 24: return secondbiggest;

CIS 110 2 Recursive Graphics 3. (15 points) The figures in this question were all generated using the function below. Assume the graysquare() function draws a gray square with a black outline. For each figure, give values of r and z that could draw it. Assume the starting values of x and y are 0.5, and that the starting value of z is an integer between 0 and 10. Hint: Don t worry about scale. We will accept any values of r and z that could generate the figures below at some scale. However, the values we used were all multiples of one eighth. public static void draw(double x, double y, double r, double z){ graysquare(x, y, r); if (r < 0.05) return; if (z % 2 == 0) draw(x - r, y - r, r / 2, z - 1); // lower left if (z % 1 == 0) draw(x - r, y + r, r / 2, z - 1); // upper left else if (r < 1) draw(x + r, y + r, r / 2, z - 1); // upper right if (z % 3 == 0) draw(x + r, y - r, r / 2, z - 1); // lower right r:0.5 z:3 r:0.25 z:2 r:0.25 z:3 r:0.5 z:1 r:1 z:5 r:0.125 z:0

CIS 110 3 Prime Directive 4. (20 points) Using the Allen Telescope Array, the SETI ( Search for Extra-Terrestrial Intelligence ) Institute recently picked up an unusual radio signal originating in the Orion nebula. Detailed, top-secret analysis of the signal has revealed it to be a Java program, giving indisputable proof of the language s universal appeal. Unfortunately, nobody has been able to figure out yet what the program does: public class Vulcan { public static void main(string[] romulan) { int klingon = Integer.parseInt(romulan[0]); if (klingon < 1) return; for (int ferengi = 2; ferengi <= klingon; ferengi++) { if (klingon % ferengi == 0) { while (klingon % ferengi == 0) klingon /= ferengi; System.out.println(ferengi); (a) What does the program Vulcan print out when run with the argument 0? Nothing (b) What does Vulcan print out when run with the argument 1? Nothing (c) What does Vulcan print out when run with the argument 3? 3 (d) What does Vulcan print out when run with the argument 8? 2 (e) What does Vulcan print out when run with the argument 24? 2 3 (f) What does Vulcan do? Your answer should be at most 30 words. Print all prime factors of its argument, or nothing if the argument is less than 2.

CIS 110 4 The Sum of its Parts 5. (30 points) Write two versions of a static function, sumdigits, that takes an integer and returns the sum of its digits. For example, the sum of the digits in 1234 is 10 because 1+2+3+4 = 10, and the sum of the digits in 52 is 7 (the minus sign is not a digit, after all). The first version, on this page, should be iterative and must not make any function calls (not even to library functions like Math.max()). The second version, on the next page, should be recursive but must not call any other functions and must not contain any loops. You do not need to include comments in either version, and you do not need to write a class around them. Hint: Start with the version of the function that is most intuitive to you. (a) Iterative version (no function calls): This part was graded out of 14 points, with a bonus point if the program was both correct AND elegant. public static int sumdigits(int x) { if (x < 0) x = -x; int sum = 0; while (x > 0) { sum += x % 10; x /= 10; return sum; (b) Recursive version (no loops): This part was graded out of 13 points, with two bonus points if the program was both correct AND elegant. public static int sumdigits(int x) { if (x < 0) return sumdigits(-x); if (x == 0) return 0; return x % 10 + sumdigits(x / 10);