COS 126 General Computer Science Fall Exam 1

Similar documents
COS 126 General Computer Science Fall Exam 1

COS 126 General Computer Science Fall Exam 1

COS 126 General Computer Science Fall Written Exam 1

COS 126 General Computer Science Spring Written Exam 1

COS 126 Midterm 1 Written Exam, Fall 2009

COS 126 Midterm 1 Written Exam Fall 2012

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Spring Midterm 1

COS 126 General Computer Science Spring Written Exam 2

COS 126 General Computer Science Fall Midterm 1

Midterm 1 Written Exam

COS 226 Algorithms and Data Structures Fall Final

COS 126 Midterm 1 Written Exam Spring 2015

COS 126 Midterm 1 Written Exam Fall 2011

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

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

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

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

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Name CIS 201 Midterm II: Chapters 1-8

Final Exam. COMP Summer I June 26, points

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

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

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CSE373 Fall 2013, Midterm Examination October 18, 2013

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

COS 226 Midterm Exam, Spring 2011

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

COS 126 Midterm 1 Written Exam Fall 2014!

COS 226 Midterm Exam, Spring 2009

COS 126 Written Exam 2, Fall 2009

CS150 Sample Final Solution

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS 101 Spring 2007 Midterm 2 Name: ID:

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

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

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

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

CS 206 Introduction to Computer Science II

: Intro Programming for Scientists and Engineers Final Exam

CS1800 Discrete Structures Final Version A

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

CSE wi: Practice Midterm

CSE 247 Data Structures and Algorithms Fall Exam I

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

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Computer Science E-119 Practice Midterm

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

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

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Sorting. Bubble Sort. Selection Sort

COS 126 Midterm 1 Written Exam Fall 2013

COS 226 Algorithms and Data Structures Spring Second Written Exam

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

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

COS 226 Algorithms and Data Structures Fall Midterm

CS 112 Midterm Exam Fall 2016

CS150 Sample Final. Name: Section: A / B

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Final Examination CS 125 Introduction to Computer Science Fall Hours

COS 226 Algorithms and Data Structures Fall Midterm

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

Computer Science 1 Ah

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CSE 143 Lecture 14. Sorting

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

CS 111X - Fall Test 1

BBM 201 DATA STRUCTURES

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

Unit #2: Recursion, Induction, and Loop Invariants

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CMSC 150 LECTURE 7 RECURSION

Prelim One Solution. CS211 Fall Name. NetID

COS 226 Algorithms and Data Structures Fall Midterm

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

Final Exam COMP Fall 2004 Dec 16, 2004

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!

11/22/1999 7pm - 9pm. Name: Login Name: Preceptor Name: Precept Number:

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CSE 373. Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort reading: Weiss Ch. 7. slides created by Marty Stepp

Unit #3: Recursion, Induction, and Loop Invariants

1st Semester Examinations CITS1001 3

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

COS 226 Midterm Exam, Spring 2010

COS 226 Algorithms and Data Structures Spring Midterm Exam

COS 126 General Computer Science Spring Exam 2

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

Transcription:

COS 126 General Computer Science Fall 2007 Exam 1 This test has 10 questions worth a total of 50 points. You have 120 minutes. The exam is closed book, except that you are allowed to use a one page cheatsheet, 8.5 by 11 inches, one side only, handwritten by you. No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. Partial credit will be given for partially correct answers. Write out and sign the Honor Code pledge before turning in the test: I pledge my honor that I have not violated the Honor Code during this examination. Signature Problem Score Problem Score 0 5 1 6 2 7 3 8 4 9 Sub 1 Sub 2 Total Name: NetID: Preceptor: Donna Adam Shirley JP Tom Ed Corey Zia Martin 1

2 PRINCETON UNIVERSITY 0. Miscellaneous. (2 points) (really) (a) Write your name and Princeton NetID in the space provided on the front of the exam, and circle the name of your preceptor. (b) Write and sign the honor code on the front of the exam. 1. Number representation (4 points) (a) Each year, Princeton Computer Science majors design a W T-shirt. To the left is the highly creative binary effort from a recent class. Which class? (in decimal, we mean) For 1 extra point, explain how to figure it out with just two or three subtractions and no additions. (b) An extra-geeky class might even have used hexadecimal instead of binary. What would the shirt read then? (c) In what year will you (or did you) graduate from college? Write it down in decimal and binary.

COS 126 EXAM 1, FALL 2007 3 2. Short Answer (3 points) (a) Many years ago programmers used to write the following code: y = y ^ y; believing it accomplished a certain operation particularly quickly. What operation is this code designed to accomplish? (Remember that ^ is the Java operator for exclusive-or.) (b) What will the following code print? public static void changegrade(int grade) { grade = grade - 10; public static void main(string[] args) { int grade = 90; changegrade(grade); System.out.println(grade); (c) A newly-discovered comet is heading for Earth. Princeton astronomers say that a very small uncertainty in our knowledge of its location will cause a huge uncertainty about whether it will collide with Earth or not. Accurate calculation of the comet s future trajectory: i. is an ill-conditioned problem ii. will take time proportional to N 3, where N is the number of planets, comets and asteroids in the orbit of the comet iii. is an example of a numerically unstable algorithm iv. will take time proportional to NlogN, where N is as in ii above, but the planets, comets, and asteroids have first been sorted in order of mass Please circle your answer.

4 PRINCETON UNIVERSITY This page is intentionally left blank.

COS 126 EXAM 1, FALL 2007 5 3. Input and Output (8 points) A DNA sequence can be written as a string where the characters denote the following bases A = adenine, T= thymine, C = cytosine, and G = guanine. DNA has the interesting property that A always pairs with T and C always pairs with G. Often you are given just one strand of DNA, but you would like to work with the complementary strand, the strand that pairs with the single strand of DNA that you are working with. This problem asks you to write a program that prints the sequence of the complementary strand. (a) Given the following string which represents a DNA sequence what would its complementary strand be? ATGCATGC (b) Write code that reads in a legal DNA sequence (such as in (a) of this question) from standard input, one character at a time, and outputs the complementary strand of DNA. (A char is a primitive data type that represents one character, so you may use the conditional operators == and!= as needed. A char constant must use single quotes, e.g., char x = A ; The method StdIn.readChar() will read in one character at a time. The input characters will not be separated by white space.) public class Complement { public static void main (String[] args) { (c) Write the command you would use to run this program reading input from the file sequence.txt.

6 PRINCETON UNIVERSITY 4. Arrays and Loops (8 points) Below is a partially complete program that is supposed to do the following: from standard input read an integer N followed by a sequence of N records of cars sold by a dealership in 2006. Each record consists of four fields, separated by whitespace: Make, Model, Dealer Cost, and Sale Price. The program then prints the record of the individual car sale that earned the highest profit. (This is New Jersey, so you may assume that no cars were sold at a loss.) For example, if this were the input file inventory.txt: 5 Honda Civic 10000.00 15000.00 Nissan Maxima 20000.00 35000.00 Nissan Altima 20000.00 25000.00 Ford Focus 10000.00 12000.00 Chrysler Aspen 30000.00 32000.00 then the program should behave thusly: % java Inventory < inventory.txt % Make: Nissan, Model: Maxima, Cost: 20000.0, Sale: 35000.0, Profit: 15000.0 (The single decimal place in the output is correct. Don t try to format the dollar outputs.) The program starts below and continues on the next page; just fill in the blanks to complete it. In case of a profit tie, by the way, just pick any car with the maximum profit. public class Inventory { public static void main(string[] args) { // read the number of records int N = StdIn.readInt(); // initialize four parallel arrays String[] make = new String[N]; String[] model = new String[N]; double[] cost = new double[n]; double[] sale = new double[n]; double profit; int maxindex = 0; double maxprofit = 0.0; // index of item with max profit // max profit

COS 126 EXAM 1, FALL 2007 7 4. Arrays and Loops (continued from previous page) for (int i=0; ) { // read in the data // calculate the profit profit = // save index of record with max profit if( ) { maxindex = maxprofit = // print the record of the item with the maximum profit System.out.println( );

8 PRINCETON UNIVERSITY 5. Recursion (6 points) Consider the following program. 1:public class RecursionQuestion { 2: 3: public static int mystery(string[] a, int x) { 4: System.out.println(a[x]); 5: 6: // Base case 7: if( x < 2 ) { return x; 8: 9: // Recursive case 10: 11: if( x % 2 == 1 ) { // x is odd 12: return mystery(a, (x - 1) / 2) - 1; 13: 14: else { // x is even 15: return mystery(a, x / 2) + 1; 16: 17: 18: 19: public static void main(string[] args) { 20: String[] a = {"The", "truth", "is", "rarely", "pure", "and", 21: "never", "simple"; 22: 23: int param = 0; 24: 25: int result = mystery(a, param); 26: System.out.println(a[result]); 27: 28: 29: (a) What does the program print out?

COS 126 EXAM 1, FALL 2007 9 5. Recursion (continued from previous page) (b) Suppose we change line 23 to int param = 5; Please fill in the following recursive call trace, given this change. mystery (a, 5) mystery (a, ) mystery (a, ) return 1 return return (c) Using your answers from (b), what does the program print out? (Do not forget the final print statement from the main function!)

10 PRINCETON UNIVERSITY 6. Sorting and Analysis (6 points) (a) Consider the following code, which sorts the N elements of an array of ints: public class CoolSorts { public static void bubblesort(int[] a) { // Make repeated passes through array // swapping adjacent elements if they are out of order. // When nothing needs to be swapped, array is sorted. int N = a.length; boolean swapped = true; while (swapped) { swapped = false; for (int i = 0; i < N-1; i++) if (a[i] > a[i+1]) { swap(a, i, i+1); swapped = true; public static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; i. For the best case (already sorted) input, the order of growth of the running time of this program will be which of the following functions of N? Circle one: logn N NlogN N 2 2 N N! ii. For the worst case input, same question. (Hint: start by finding an upper bound for the number of times the while loop is executed.) Circle one: logn N NlogN N 2 2 N N!

COS 126 EXAM 1, FALL 2007 11 6. Sorting and Analysis (continued from previous page) (b) Murphy has proposed and written another sorting algorithm for the CoolSorts class. It also sorts integer arrays of size N: public static void murphysort(int[] a) { while (!issorted(a)) { // Swap elements randomly until everything is in sorted order. for (int i = 0; i < a.length; i++) { int j = i + (int) (Math.random() * (a.length - i)); //same swap method as before swap(a, i, j); public static boolean issorted(int[] a) { // return true if array elements are in sorted order for (int i = 1; i < a.length; i++) { if (a[i-1] > a[i]) return false; return true; i. Suppose Murphy is lucky and the input array is already sorted. In this best case scenario, the order of growth of the running time of this program will be which of the following functions of N? Circle one: logn N NlogN N 2 2 N N! ii. Now suppose the input array is in a completely unsorted order, but Murphy is lucky in a different way: the program somehow never permutes the array into the same configuration twice. In this scenario, which of the following functions of N expresses the worst case order of growth of the running time of this program? Circle one: logn N NlogN N 2 2 N N!

12 PRINCETON UNIVERSITY 7. Graphics, Functions, and Loops (6 points) Consider the following program, which uses Fibonacci numbers to draw a picture. You may assume that the fib function has been implemented in some clever way, but note that this one returns 0 if n <= 0. The function StdDraw.square(x, y, radius) draws a square centered at (x,y) with sides of length 2*radius. public class Shape { public static int fib(int n) { // Returns nth Fibonacci number for n > 0 // fib(1) is 1; fib(2) is 1; fib(n) is sum of previous two // Returns 0 for n <= 0 public static void drawshape(int n) { double x = 0; double y = 0; for (int i = n; i > 0; i--) { double radius = fib(i); double small = fib(i-2); double big = fib(i+1); StdDraw.square(x, y, radius); int direction = i % 4; if (direction == 0) { // go down x += small; y -= big; else if (direction == 1) { // go right x += big; y += small; else if (direction == 2) { // go up x -= small; y += big; else if (direction == 3) { // go left x -= big; y -= small; public static void main(string[] args) { int n = Integer.parseInt(args[0]); int rad = fib(n) + 2*fib(n-1); StdDraw.setXscale(-rad, rad); StdDraw.setYscale(-rad, rad); drawshape(n);

COS 126 EXAM 1, FALL 2007 13 7. Graphics, Functions, and Loops (continued from previous page) (a) What would appear if you ran the command: java Shape 3? (You could try java Shape 1 as a warmup.) Here s a handy grid you can write on: (b) What would appear if you ran the command: java Shape 5? And again a handy grid:

14 PRINCETON UNIVERSITY 8. Java Programming, Recursion, Functions, and Loops (5 points) Here is Euclid s Algorithm, expressed as a recursive function, just as you saw in lecture. Please rewrite it as a nonrecursive function that uses a while loop. public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); 9. Definition (2 points) In one sentence, what do you think the word algorithm means? (2 points for any serious answer)