COS 126 Midterm 1 Written Exam Fall 2013

Similar documents
COS 126 Midterm 1 Written Exam Fall 2012

COS 126 Midterm 1 Written Exam Fall 2011

COS 126 Midterm 1 Written Exam Fall 2014!

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Fall Written Exam 1

COS 126 Midterm 1 Written Exam Spring 2015

COS 126 Midterm 1 Written Exam, Fall 2009

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

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

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

COS 126 General Computer Science Spring Written Exam 1

Midterm 1 Written Exam

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

COS 126 General Computer Science Fall Exam 1

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

COS 226 Midterm Exam, Spring 2011

COS 126 General Computer Science Fall Exam 1

COS 126 MIDTERM 1, FALL

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

COS 126 Written Exam 2, Fall 2009

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

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

COS 126 General Computer Science Fall Exam 1

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

COS 126 General Computer Science Fall Midterm 1

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

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

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

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

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

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

COS 226 Midterm Exam, Spring 2009

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

COS 126 Midterm 2 Programming Exam Fall 2012

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

Final Exam. COMP Summer I June 26, points

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

COS 126 General Computer Science Spring Midterm 1

2.1 Functions. f (x, y, z)

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)

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

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

A Foundation for Programming

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm

COS 126 Written Exam 2 (Spring 2015)

COS 226 Midterm Exam, Spring 2010

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

CS 101 Spring 2007 Midterm 2 Name: ID:

COS 126 General Computer Science Spring Written Exam 2

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

CS 101 Fall 2005 Midterm 2 Name: ID:

CS 112 Introduction to Programming

COS 126 Exam 2 Review Part 1

Examination Questions Midterm 1

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

ESC101 : Fundamental of Computing

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CIS Introduction to Computer Programming. 5 October 2012 Midterm

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

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

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

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

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

This exam is open book. Each question is worth 3 points.

COS 126 General Computer Science Fall Programming Exam 2

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

CSE 131 Introduction to Computer Science Fall Exam II

Computer Programming, I. Laboratory Manual. Final Exam Solution

COMP 110 Programming Exercise: Simulation of the Game of Craps

CSE 11 Midterm Fall 2008

b. Suppose you enter input from the console, when you run the program. What is the output?

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

COS 226 Written Exam 2 Fall 2016

1. Consider the following program in a PCAT-like language.

Fall 2005 CS 11 Final exam Answers

CSE 131 Introduction to Computer Science Fall Exam II

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

Problem Score Max Score 1 Syntax directed translation & type

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

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

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

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

Programming Problems 22nd Annual Computer Science Programming Contest

Question: Total Points: Score:

1 Lexical Considerations

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

CS170 Introduction to Computer Science Midterm 2

Lecture 5: Methods CS2301

COS 126 General Computer Science Spring Exam 2

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

CS 101 Exam 2 Spring Id Name

2.4 Modular Programming

COS 226 Algorithms and Data Structures Fall Midterm

Section 05: Midterm Review

2.1, 2.2 Functions and Libraries

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

University of Palestine. Mid Exam Total Grade: 100

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

Transcription:

NAME: login id: Precept: COS 126 Midterm 1 Written Exam Fall 2013 qüáë= íéëí= Ü~ë= U= èìéëíáçåëi= ïéáöüíéç= ~ë= áåçáå~íéçk= qüé= Éñ~ã= áë= ÅäçëÉÇ= ÄççâI=ÉñÅÉéí=íÜ~í= óçì= ~êé= ~ääçïéç= íç= ìëé= ~= çåé= é~öé= ÅÜÉ~íëÜÉÉíK= kç= Å~äÅìä~íçêë= çê= çíüéê= ÉäÉÅíêçåáÅ= ÇÉîáÅÉë= ~êé= ééêãáííéçk= dáîé=óçìê=~åëïéêë=~åç=ëüçï=óçìê=ïçêâ=áå=íüé=ëé~åé=éêçîáçéçk= Print your name, login ID, and precept number on this page=eåçïfi=~åç=write out and sign the Honor Code pledge=äéñçêé=íìêåáåö= áå=íüáë= é~ééêk=notew=fí=áë=~=îáçä~íáçå=çñ=íüé=eçåçê= `ççé=íç= ÇáëÅìëë=íÜáë= ãáçíéêã=éñ~ã= èìéëíáçå=ïáíü=~åóçåé=ìåíáä=~ñíéê= ÉîÉêóçåÉ=áå=íÜÉ= Åä~ëë=Ü~ë= í~âéå=íüé= Éñ~ãK=vçì=Ü~îÉ=RM=ãáåìíÉë=íç=ÅçãéäÉíÉ=íÜÉ=íÉëíK I pledge my honor that I have not violated the Honor Code during this examination. Signature 1 /8 2 /10 3 /12 4 /6 5 /8 6 /12 7 /8 8 /6 TOTAL /70

1. Types and Casts (8 points). dáîé=íüé=íóéé=~åç=î~äìé=çñ=é~åü=çñ=íüé=ñçääçïáåö=g~î~=éñéêéëëáçåëk= fñ=~å=éñéêéëëáçå=ïáää=åçí=åçãéáäéi=ïêáíé=illegal=ìåçéê=type=~åç=éìí=~å=x=áå=valuek=vçì=ãìëí=ñáää= áå=éîéêó=éåíêók=båíêáéë=äéñí=ää~åâ=ïáää=äé=ã~êâéç=áååçêêéåík expression type value ( 7 / 2 ) * 2.0 double 6.0 1 + 2 + "0" + 3 * 4 String "3012" "3" - "2" Illegal X true == false boolean false 4 < 5 < 6 Illegal X 0.5 + 1/2 double 0.5 Math.max(5.0, 2) double 5.0 Integer.parseInt("1.5*2") Illegal X

2. Miscellaneous (10 points). Answer the following questions. ^K tüáåü=çñ=íüé=ñçääçïáåö=~êé=íêìé=çñ=ëí~åç~êç=áåéìí=estdinf\=`áêåäé=all=íü~í=~ééäók EáF kç=äáãáí=çå=áíë=äéåöíük= EááF `~ååçí=ìëé=áå=~åó=éêçöê~ã=íü~í=ìëéë=åçãã~åçjäáåé=áåéìík= EáááF `~å=~ååééí=áåéìí=~í=~åó=íáãé=çìêáåö=éêçöê~ã=éñéåìíáçåk= EáîF `~å=äé=áåíéêãáñéç=ïáíü=ëí~åç~êç=çìíéìík= EîF fí=áë=~å=~äëíê~åíáçå=áãéäéãéåíéç=~ë=~=äáäê~êók= EîáF fí=áë=~=êéåìêëáîé=ëí~íáå=ãéíüççk _K tüáåü=çñ=íüé=ñçääçïáåö=äéëí=çéëåêáäéë=ïü~í=áë=~=ç~í~=íóéé\=`áêåäé=íüé=single best=~åëïéêk EáF ^=ëéí=çñ=î~äìéëk= EááF ^=ëéí=çñ=çééê~íáçåëk= EáááF ^=ëéèìéååé=çñ=më=~åç=nëk= EáîF ^=ëéí=çñ=î~äìéë=~åç=çééê~íáçåë=çå=íüçëé=î~äìéëk= EîF qüé=~êöìãéåíëi=å~ãéi=~åç=êéíìêå=î~äìé=ñçê=~=ñìååíáçåk= `K tü~í=~êé=íüé=êé~ëçåë=íü~í=~=éêçöê~ã=ãáöüí=ìëé=~å=~êê~ó\=`áêåäé=all=íü~í=~ééäók EáF qç=âééé=ç~í~=çñ=çáññéêéåí=íóééë=íçöéíüéê=áå=çåé=éä~åék= EááF qç=ñ~åáäáí~íé=éêçåéëëáåö=ä~êöé=~ãçìåíë=çñ=ç~í~k= EáááF qç=~îçáç=ìëáåö=ñçê=äççéëk= EáîF qç=êééêéëéåí=~=îéåíçê=çê=~=ã~íêáñk= EîF qç=êééêéëéåí=~=ëçìåç=ï~îék=

3. Arrays (12 points). Consider the following code. public class ArrayQ public static void main(string[] args) int N = args.length; int[] a = new int[n]; a[i] = Integer.parseInt(args[i]); if (a[i] < a[n-1-i]) int t = a[i]; a[i] = a[n-1-i]; a[n-1-i] = t; StdOut.print(a[i] + " "); StdOut.println(); For each sequence at left, when ArrayQ is run with those numbers on standard input, it will write one of the sequences at right on standard output. Identify which output is produced by each input by writing a letter in each blank. You must fill in all blanks and use all letters (one letter will be used twice). C 3 2 1 4 5 A. 1 2 3 4 5 A 1 2 3 4 5 B. 2 3 1 4 5 D 1 3 5 2 4 C. 3 2 1 4 5 E 1 5 2 4 3 D. 1 2 5 3 4 A 5 4 3 2 1 E. 1 4 2 5 3 B 2 3 1 4 5

4. Redirection and piping (6 points). Consider the following Java program: public class AddX public static void main(string[] args) int N = Integer.parseInt(args[0]); int x = Integer.parseInt(args[1]); int[] a = new int [N]; a[i] = StdIn.readInt(); a[i] = a[i] + x; for (int i = 0; i < N-1; i++) StdOut.println(a[i] + " "); Assume that the contents of the file input.txt are as shown by the following more command: % more input.txt 10 20 35 30 A. What is the result of the following command? Circle your answer. % java AddX 4 3 < input.txt 13 23 38 B. What is the result of the following command? Circle your answer. % java AddX 3-1 < input.txt java AddX 2 3 12

5. Debugging and recursion (8 points). Consider the following program, which is intended to take N from the command line and compute H N = 1 + 1/2 + 1/3 +... + 1/N. 1 public class Harmonic 2 3 public static double H(int N) 4 5 if (N = 0) return 0; 6 return H(N-1) + 1/N; 7 8 public static void main (String[] args) 9 10 int N = Integer.parseInt(args[0]); 11 StdOut.println(H[N]); 12 13 få=íüé=ëé~åéë=éêçîáçéç=äéäçïi=áçéåíáñó=ein ten words or lessi=é~åüf= íüêéé=äìöë=~åç= ~= ééêñçêã~ååé= éêçääéã=áå=íüáë=åççék=rëé=íüé=äáåé=åìãäéêë=íç=êéñéê=íç=íüé=åççéi=~åç=åáêåäé=óçìê=~åëïéêëk=you do not have to fix the bugs. A. A bug that will keep the code from compiling: line 5: N = 0 should be N == 0 B. Another bug that will keep the code from compiling: line 11: H[N] should be H(N) C. A bug that will cause the program to print the wrong answer (assuming the compile-time bugs are fixed): line 6: integer division (sample fix: 1.0/N) D. A performance problem that makes the program unusable when N is huge (and for which an easy alternative is available): Necessary memory gets larger as N gets larger. (Causes stack overflow. Fix using a loop instead.)

6. Methods and scope (12 points). Consider the following program, which is not intended to do anything other than test your understanding of functions, scope and side effects. public class FunctionQ public static int[] f(int[] x, int a) for (int i = 0; i < x.length; i++) x[i] *= a; a = x[2]; return x; public static void main(string[] args) int[] x = 1, 2, 3; int a = 4; int[] y = f(x, a); y[0] = 0; StdOut.println(a); StdOut.println(x[0]); StdOut.println(y[0]); StdOut.println(x[1]); Give the output of this program in the blanks provided below. A. First line: 4 B. Second line: 0 C. Third line: 0 D. Fourth line: 8

7. Performance (8 points). b~åü=äéííéêéç=áíéã=äéäçï=ëééåáñáéë=~=èì~åíáíó=íü~í=áë=~=ñìååíáçå=çñ=kk= fçéåíáñó=íüé=çêçéê=çñ=öêçïíü=eïáíü=êéëééåí=íç=kf=çñ=é~åü=çåé=äó=ïêáíáåö=çåé=çñ=íüé=ïçêçë=linear, quadratic, cubic çê=exponential áå=íüé=åçêêéëéçåçáåö=ää~åâë=~í=êáöüík A. Memory used by the code boolean[] myarray = new boolean[n*n*n]; cubic B. Time required to execute the code int[] myintarray = new int[n]; linear C. Number of lines printed by the code for (int i=0; i<n; i++) for (int j=0; j<3; j++) StdOut.println("COS 126"); linear D. Number of lines printed by the code for (int i=0; i<n; i++) for (int j=n; j>0; j--) StdOut.println("COS 126"); quadratic E. The time it takes to draw a Sierpinski triangle of order N exponential F. The order of growth of the running time of a program that runs for 3 days when N is 100000, 24 days when N is 200000, and 6.5 months when N is 400000. cubic G. The order of growth of the running time of a program that runs for 4 seconds when N is 100,000, 10 seconds when N is linear 250,000, and 1 minute when N is 1,500,000. H. Memory used by the code String x = "hi"; for (int i=0; i<n; i++) x += x; exponential

8. Recursion (6 points). Consider the following program, which includes a recursive method: 1 public class Pattern 2 3 public static void draw (int n, double r, double x, double y) 4 5 if (n == 0) return; 6 7 if (n % 2!= 0) StdDraw.setPenColor(StdDraw.GRAY); 8 else StdDraw.setPenColor(StdDraw.WHITE); 9 10 draw(n-1, r/2, x-r, y); 11 12 draw(n-1, r/2, x+r, y); 13 14 15 16 public static void main (String[] args) 17 18 draw(5,.25,.5,.5); 19 20 Suppose we add the line StdDraw.filledCircle(x, y, r); to the program either on line 9, 11, or 13 (before, between, and after the recursive calls respectively). If we do so, each version produces one of the diagrams below. `áêåäé=íüé=åçêêéåí=~åëïéê=eäéñíi=åéåíéê=çê=êáöüíf=áå=é~åü=êçïk=pçãé=~åëïéêë=ã~ó=äé=ìëéç=ãçêé= íü~å=çååék fñ=ïé=~çç=íüé=åçãã~åç=çå=äáåé=vi=ïüáåü=çá~öê~ã=áë=öéåéê~íéç\== äéñí= ÅÉåíÉê== êáöüí fñ=ïé=~çç=íüé=åçãã~åç=çå=äáåé=nni=ïüáåü=çá~öê~ã=áë=öéåéê~íéç\== äéñí= ÅÉåíÉê== êáöüí fñ=ïé=~çç=íüé=åçãã~åç=çå=äáåé=npi=ïüáåü=çá~öê~ã=áë=öéåéê~íéç\== äéñí= ÅÉåíÉê== êáöüí