COS 126 General Computer Science Fall Exam 1

Similar documents
COS 126 General Computer Science Fall Exam 1

COS 126 General Computer Science Fall Midterm 1

COS 126 Midterm 1 Written Exam Spring 2015

COS 126 General Computer Science Spring Written Exam 1

COS 126 Written Exam 2 (Spring 2015)

COS 126 Midterm 1 Written Exam, Fall 2009

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

COS 126 General Computer Science Fall Written Exam 1

COS 126 General Computer Science Fall Exam 1

COS 126 General Computer Science Spring Written Exam 1

COS 126 Written Exam 2 Spring 18

What is TOY? An imaginary machine similar to: ! Ancient computers. ! Today's microprocessors.

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

COS 126 Midterm 1 Written Exam Fall 2011

11. A Computing Machine

COS 126 MIDTERM 1, FALL

COS 126 Midterm 1 Written Exam Fall 2012

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

COS 126 General Computer Science Spring Midterm 1

Computer Architecture /

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

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

! Ancient computers. ! Today's microprocessors. Memory. ! Stores data and programs. ! 256 "words." (16 bits each) ! Special word for stdin / stdout.

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

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

COS 126 Midterm 1 Written Exam Fall 2014!

COS 126 Written Exam 2, Fall 2009

Lecture A2: X-TOY Programming

12/11/ The TOY Machine II. Data Representation. What We've Learned About TOY. What We Do Today. Adding and Subtracting Binary Numbers

5. The TOY Machine II

CS 101 Spring 2007 Midterm 2 Name: ID:

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

Midterm 1 Written Exam

TOY II LINC LINC. !1 Introduction to Computer Science Sedgewick and Wayne Copyright 2007

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

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

COS 126 General Computer Science Spring Written Exam 2

COS 126 General Computer Science Spring Exam 2

Computer Science /21/2000

COS 226 Midterm Exam, Spring 2011

Final Exam. COMP Summer I June 26, points

CS 101 Fall 2006 Midterm 3 Name: ID:

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

Final Examination CS 125 Introduction to Computer Science Fall Hours

COS 126 General Computer Science Fall Programming Exam 2

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

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

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

ESC101 : Fundamental of Computing

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

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

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

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

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

COS 226 Algorithms and Data Structures Fall Final

COS 126 Midterm 1 Written Exam Fall 2013

COS 226 Algorithms and Data Structures Fall Midterm

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

CIS 110 Fall 2016 Introduction to Computer Programming 8 Dec 2016 Final Exam Answer Key

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

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

CS61c MIDTERM EXAM: 3/17/99

CSE 378 Final Exam 3/14/11 Sample Solution

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

18. Machine Language. Computer Systems. COMP1917: Computing 1. Machine Language Programming. History of Computer Technology

Virtual machines. Virtual machines. Abstractions for computers. Abstractions for computers. Virtual machines

a. a * c - 10 = b. a % b + (a * d) + 7 =

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Sample Exam I PAC II ANSWERS

CSE373 Fall 2013, Midterm Examination October 18, 2013

Question: Total Points: Score:

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

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

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

University of Palestine. Mid Exam Total Grade: 100

6.1 Combinational Circuits. George Boole ( ) Claude Shannon ( )

Birkbeck (University of London) Department of Computer Science and Information Systems. Introduction to Computer Systems (BUCI008H4)

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

Question: Total Points: Score:

COS 226 Midterm Exam, Spring 2009

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

CIS Introduction to Computer Programming. 5 October 2012 Midterm

Place your name tag here

C++ to assembly to machine code

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

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

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

CPSC213/2014W1 Midterm EXTRA Practice

CS 177 Week 15 Recitation Slides. Review

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

CDA 3103 Computer Organization Exam 1 (Sep. 22th, 2014)

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

Name: CMSC 313 Fall 2001 Computer Organization & Assembly Language Programming Exam 1. Question Points I. /34 II. /30 III.

Birkbeck (University of London) Department of Computer Science and Information Systems. Introduction to Computer Systems (BUCI008H4)

CS S-08 Arrays and Midterm Review 1

CSE 351 Midterm - Winter 2017

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

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Unsigned Binary Integers

Transcription:

COS 126 General Computer Science Fall 2005 Exam 1 This test has 9 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, 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. 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 Possible 0 2 1 4 2 5 3 8 4 8 5 4 6 8 7 5 8 6 Total 50 Name: NetID: Preceptor: Frances Forrester Haakon Andrea Donna Curtis Eric The TOY instruction cheatsheet is on the last page of the exam. There is no list of StdDraw methods, because none of the questions require you to write graphics code. Feel free to tear out the last page in order to use the TOY reference more easily and to use the blank side for scratch paper.

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) Copy and sign the honor code on the front of the exam. 1. Number Systems (4 points) a) Give the hexadecimal representation of the binary integer 1111111011001001. Circle your answer. b) Give the decimal representation of the 16 bit 2 s complement binary number 1111111011001001. Circle your answer. c) Convert the decimal number 622 to hexadecimal. Circle your answer.

COS 126 Exam 1, Fall 2005 3 This page intentionally left blank.

4 PRINCETON UNIVERSITY 2. Java Programming, Arrays, Standard Input (5 points) You compile the following program: public class Presidents { public static void main(string[] args) { // int N = StdIn.readInt(); String[] firstnames = new String[N]; String[] lastnames = new String[N]; // for (int i = 0; i < N; i++){ String last = StdIn.readString(); String first = StdIn.readString(); int number = StdIn.readInt(); firstnames[number-1] = first; lastnames[number-1] = last; // int x = Integer.parseInt(args[0]); if (x < 1 x > N) System.out.println("Invalid request"); else System.out.println(firstNames[x-1] + " " + lastnames[x-1]); You have the following data file, US Presidents.txt, containing the names of the first 10 U.S. presidents in alphabetical order. 10 Adams John 2 Adams John 6 Harrison William 9 Jackson Andrew 7 Jefferson Thomas 3 Madison James 4 Monroe James 5 Tyler John 10 VanBuren Martin 8 Washington George 1

COS 126 Exam 1, Fall 2005 5 2. continued a) In general, what does this program do? b) What would you type to run this program using the given input file and a command line input of 3? c) What is the output that results from a command line input of 3?

6 PRINCETON UNIVERSITY 3. Adventures in Java Programming and Debugging (8 points) The following program is supposed to take command line input N and compute the sum from 1 to N (absolute value of N). It should print the sum if N is positive and print -sum if N is negative. (e.g., an input of 2 should output 3 and an input of 2 should output 3) 1 public class BuggyCode { 2 public static void main(string [] args) { 3 int N = args[0]; 4 int sum; 5 if (N < 0) sum = -N; 6 else sum = N; 7 if (N = 0) System.out.println(0); 8 else { 9 for (i = N; i!= 0; i++) 10 sum = sum + i; 11 if (N < 0) System.out.println(-sum); 12 else System.out.println(sum); 13 14 15 19 a) Running javac BuggyCode.java yields the following compilation time errors. BuggyCode.java:3: incompatible types BuggyCode.java:7: incompatible types BuggyCode.java:9: cannot resolve symbol : variable i BuggyCode.java:9: cannot resolve symbol : variable i BuggyCode.java:9: cannot resolve symbol : variable i BuggyCode.java:10: cannot resolve symbol : variable i BuggyCode.java:10: incompatible types How do you fix the compilation time bugs? line number Correct code

COS 126 Exam 1, Fall 2005 7 3. Continued b) Now that it compiles correctly, you run it with an input of 2 and you wait, and you wait, and you wait. Finally, you hit Ctrl-C. Which line is causing the trouble? Fix it so that the code now runs correctly. line number Correct code c) Trace through your corrected code for an input of 2. Fill out the table below for each listed variable. N sum i output

8 PRINCETON UNIVERSITY 4. Recursion (8 points) Consider the following mutually recursive methods. public static boolean foo(int x){ if (x == 0) return true; else return bar(x-1); public static boolean bar(int x){ if (x == 0) return false; else return foo(x-1); (a) What will foo(2) return? (b) What will foo(3) return? (c) What will foo(4) return? (d) What will foo(5) return? (e) What does foo(-1) do? (f) What will foo(5000) return? (g) How many calls to bar will foo(5000) generate? (h) In general, what does foo(n) determine?

COS 126 Exam 1, Fall 2005 9 5. Analysis of Algorithms (4 points) For each of the four Java methods (i.e., functions) below, circle the order of growth of the average running time of the method. (Assume operations like + and * take a constant amount of time.) Don t worry about the numbers getting too big to fit in an int. a) public static void inittriangle(int[][] A){ // A is a square matrix (NxN). int N = A.length; // For a 2D array, array.length is number of rows for (int i = 0; i < N; i++) for (int j = 0; j < i; j++) A[j][i] = -A[i][j]; constant logn N N log N N 2 2 N b) public static double median (double[] A) { int N = A.length; quicksort(a); //sort the elements of A using quicksort if ((N % 2) == 1) return A[N/2]; else return (A[N/2-1] + A[N/2]) / 2.0; constant logn N N log N N 2 2 N c) public static double getmedian (double[] A) { // given an ordered array of length N, return the median int N = A.length; if ((N % 2) == 1) return A[N/2]; else return (A[N/2-1] + A[N/2]) / 2.0; constant logn N N log N N 2 2 N d) public static void manypeak (int N, double size) { if (N > 0) manypeak(n/2, size/2); for (int i = 1; i <=N; i++) { drawpeak(size); //method drawpeak() takes constant time if (N>0) manypeak(n/2, size/2); constant logn N N log N N 2 2 N

10 PRINCETON UNIVERSITY 6. Arrays, Functions, Analysis of Algorithms (8 points) The following two methods do the same job. They each take an ORDERED array of ints and a target number as arguments. public static boolean mystery1(int[] array, int target) { for (int i = 0; i < array.length; i++) { if (array[i] == target) return true; else if (array[i] > target) return false; return false; public static boolean mystery2(int[] array, int target) { int low = 0; int high = array.length - 1; while (low <= high) { int mid = (low + high) / 2; if (array[mid] == target) return true; else if (array[mid] < target) low = mid + 1; else high = mid - 1; return false; Use this array to answer the following questions: int[] a = { 2, 5, 11, 14, 15, 27, 31; a) What does mystery1(a, 5) return? b) Fill in the trace table to show that mystery2(a, 5) returns the same thing. target low hi mid return value

COS 126 Exam 1, Fall 2005 11 6. Continued c) What do mystery1(a, 20) and mystery2(a, 20) return? d) In general, what do these methods do? e) How many comparisons with the target will mystery1(a, 32) make? f) How many comparisons with the target will mystery2(a, 32) make? g) Which one would you use for an array of a million elements? h) Why?

12 PRINCETON UNIVERSITY 7. Recursive Graphics (5 points) Suppose that the method drawshadedsquare() draws a gray square with a black outline. The square is centered at (x,y) and has side length size. Consider the following recursive function. 1 2 3 4 5 6 public static void recur(int n, double x, double y, double size){ drawshadedsquare(x, y, size); recur(n-1, x + size/2., y + size/2., size/2.1); recur(n-1, x + size/2., y - size/2., size/2.1); recur(n-1, x - size/2., y - size/2., size/2.1); recur(n-1, x - size/2., y + size/2., size/2.1); if (n <= 0) return; The method call recur(n, 256.0, 256.0, 256.0) is supposed to produce the recursive pattern above. All 6 numbered statements are correct, but they are not necessarily in the correct order. Here are some proposed orderings: A) 1 2 3 4 5 6 B) 6 1 3 5 4 2 C) 6 3 5 1 4 2 D) 6 2 4 1 3 5 E) 1 6 3 4 5 2 F) 6 5 3 1 2 4

COS 126 Exam 1, Fall 2005 13 7. Continued i) If n = 5, which, if any, of the above orderings of statements could produce the picture shown above? List the letters of all that apply or write none. ii) If n = 6, which, if any, of the above orderings of statements could produce the picture shown above? List the letters of all that apply or write none. iii) If n = 4, which, if any, of the listed orderings of statements could produce a StackOverflow Error? List the letters of all that apply or write none. iv) Draw the picture that will result from ordering E when n = 2.

14 PRINCETON UNIVERSITY 8. TOY (6 points) // main program 10: 7A51 R[A] <- 0051 11: 8B50 R[B] <- mem[50] 12: FF20 R[F] <- pc; goto 20 13: 0000 halt //////////////////////////////////////// // Function: TOY_Mystery 20: 7101 R[1] <- 0001 21: A30A R[3] <- mem[r[a]] 22: 2BB1 R[B] <- R[B] - R[1] 23: CB2A if (R[B] == 0) goto 2A 24: 1AA1 R[A] <- R[A] + R[1] 25: A40A R[4] <- mem[r[a]] 26: 2243 R[2] <- R[4] - R[3] 27: D222 if (R[2] > 0) goto 22 28: 1340 R[3] <- R[4] 29: C022 goto 22 2A: 93FF write R[3] 2B: EF00 goto R[F] Your main program starts at address 10. There is a function which begins at address 20. There is an array in memory starting at address 51. Memory location 50 contains the size of the array. a) Trace through the program for the following data. // data 50: 0003 51: 0007 52: 0005 53: 0006 R[1] R[2] R[3] R[4] R[A] R[B] R[F] output

COS 126 Exam 1, Fall 2005 15 8. Continued b) What will the program output using the following data? If you have figured out what the function does, you do not need to make a full trace, but here is a table to use, just in case you need to do a few more iterations. // data 50: 000F 51: 0007 52: 0005 53: 0006 54: 0027 55: 00A5 56: 0060 57: 001B 58: 0002 59: 00A6 5A: 0038 5B: 003C 5C: 0012 5F: 0021 5E: 000A 5F: 0014 R[1] R[2] R[3] R[4] R[A] R[B] R[F] output c) If you store the array sequentially in the TOY machine starting at memory address 51, and your entire program, including functions, is stored in 00 through 4F, what is the maximum number of elements that the array can safely have? Your answer may be in decimal or hex, but you must indicate what base you are using. Circle your answer.

This page intentionally left blank.

TOY REFERENCE CARD INSTRUCTION FORMATS................ Format 1: opcode d s t (0-6, A-B) Format 2: opcode d addr (7-9, C-F) ARITHMETIC and LOGICAL operations 1: add R[d] <- R[s] + R[t] 2: subtract R[d] <- R[s] - R[t] 3: and R[d] <- R[s] & R[t] 4: xor R[d] <- R[s] ^R[t] 5: shift left R[d] <- R[s] << R[t] 6: shift right R[d] <- R[s] >> R[t] TRANSFER between registers and memory 7: load address R[d] <- addr 8: load R[d] <- mem[addr] 9: store mem[addr] <- R[d] A: load indirect R[d] <- mem[r[t]] B: store indirect mem[r[t]] <- R[d] CONTROL 0: halt halt C: branch zero if (R[d] == 0) pc <- addr D: branch positive if (R[d] > 0) pc <- addr E: jump register pc <- R[d] F: jump and link R[d] <- pc; pc <- addr Register 0 always reads 0. Loads from mem[ff] come from stdin. Stores to mem[ff] go to stdout.