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

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

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

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

Question: Total Points: Score:

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

Instructor: Yu Wang 11/16/2012

Question: Total Points: Score:

Question: Total Points: Score:

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Place your name tag here

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

Question: Total Points: Score:

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Question: Total Points: Score:

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Question: Total Points: Score:

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

Question: Total Points: Score:

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

Question: Total Points: Score:

CS 190 Exam 2 Fall 2014

Question: Total Points: Score:

Exam Percentage: / 55 = %

CS170 (005): Introduction to Computer Science Exam 2

CS 1301 Exam 1 Fall 2010

CS 110 Exam 2 Spring 2011

Question: Total Points: Score:

CS 1301 Exam 1 Fall 2010

CS 155 Exam 2 Spring 2015

CS 110 Exam 2 Fall 2011

CS 1301 Exam 1 Fall 2011

CS Exam 2 - Spring 2010

CS 2316 Exam 4 Fall 2011

CS 1301 Exam 3 Spring 2014

CS 2316 Exam 1 Spring 2013

CS Exam 3 - Spring 2010

CS 1803 Fall 2010 Exam 1

Your (printed!) Name: CS 1803 Exam 3. Grading TA / Section: Monday, Nov. 22th, 2010

CS 1301 Exam 1 Spring 2011

CS 1301 Exam 1 Fall 2014

CS 2316 Exam 1 Spring 2014

CS Exam 3 - Spring 2010

CS 1803 Fall 2010 Exam 1

CS Name : Grading TA:

CS 1301 Exam 1 Fall 2014

CS 2316 Exam 4 Fall 2012

CS 190 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2013

CS 1301 Exam 2 Fall 2013

CS 1301 Exam 1 Spring 2014

CS 1301 Exam 1 Fall 2013

COS 126 General Computer Science Fall Written Exam 1

CS 1301 Exam 2 Fall 2014

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CS 110 Exam 1 Fall 2011

CS 1301 Exam 3 Fall 2014

CS 2316 Exam 4 Fall 2011

CS 1316 Exam 1 Summer 2009

COS 126 General Computer Science Spring Written Exam 1

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CS 1301 Post Exam 3 Practice Spring 2016

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

CS 1301 Exam 2 A Fall 2015

CS 1301 Exam 1 Spring 2015

CS 1301 Exam 1 Spring 2014

CS 2316 Exam 2 Summer 2011

CS 1301 Exam 2 Spring 2014

CS 1301 Exam 3 Spring 2012

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

CS 1301 Exam 3 A Spring 2015

CS 1301 Exam 2 A Fall 2015

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

CS 2316 Exam 4 Spring 2013

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSE 143 SAMPLE MIDTERM

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80

CS 2316 Exam 4 Fall 2012

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

CS 2316 Exam 4 Fall 2014

CS 2316 Exam 3 Spring 2013

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

CS 312 Midterm 2 Fall 2013

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

CS 2316 Exam 4 Spring 2012

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

Loops. CSE 114, Computer Science 1 Stony Brook University

CS 2316 Exam 3 Fall 2012

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

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

CS 101 Spring 2007 Midterm 2 Name: ID:

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

CS 1301 Exam 2 Fall 2010

CSE 1223: Exam II Autumn 2016

Transcription:

CS 170 Exam 2 Version: A Spring 2016 Name (as in OPUS) (print): Section: Seat Assignment: Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than the professor/proctor for ANY reason in ANY language in ANY manner. This exam is closed notes, closed books, and no calculator. Turn all mobile devices off and put them away now. You cannot have them on your desk. Write neatly and clearly indicate your answers. What I cannot read, I will assume to be incorrect. Stop writing when told to do so at the end of the exam. I will take 5 points off your exam if I have to tell you multiple times. Academic misconduct will not be tolerated. Suspected academic misconduct will be immediately referred to the Emory Honor Council. Penalties for misconduct will be a zero on this exam, an F grade in the course, and/or other disciplinary action that may be applied by the Emory Honor Council. Time: This exam has 8 questions on 9 pages including the title page. Please check to make sure all pages are included. You will have 70 minutes to complete this exam. I commit to uphold the ideals of honor and integrity by refusing to betray the trust bestowed upon me as a member of the Emory community. I have also read and understand the requirements and policies outlined above. Signature: Question: 1 2 3 4 5 6 7 8 Total Points: 6 3 6 15 5 7 18 10 70 Score: 1

1. For each print statement below, give the resulting value which is printed to the screen. Your answer should indicate type; that is, Strings should be enclosed in quotation marks ("), characters should be enclosed in single quotation marks ( ), and numerical types should correctly indicate floating point or integer precision. Each statement should be evaluated independently. In other words, assume the statements begin with the initial variable values below. If the statement would result in an error, you may simply write error as your answer. String word = "CS170 abcd"; int i = 4; int j = 20; char c = d ; (a) (1 point) System.out.println( word.length() ); (b) (1 point) System.out.println( word.charat(6) ); (a) 10 (c) (1 point) System.out.println( c + 5 ); (b) a The ASCII values A - Z are 65-90 and the values for a - z are 97-122. (d) (1 point) System.out.println( ++i ); (e) (1 point) System.out.println( j++ - i++ ); (f) (1 point) System.out.println("abcd" == word.substring(6)) (c) 100 + 5 == 105 (d) 5 (e) 16 (f) false 2. Code Tracing (a) (1 point) You invoke a program from the command line with: java MyProgram 20 3.14 CS true Give the value and type of args[1] in main. (a) 3.14, String (b) (2 points) What is returned by the following method if it is invoked with the call mystery(5)? public static int mystery(int n) { if(n <= 2) { return 4; else { return mystery(n-2) + n; (b) 12 2

3. Consider the following code: public static void swap1(int[] a) { int temp = a[0]; int last = a.length - 1; a[0] = a[last]; a[last] = temp; public static void swap2(int num1, int num2) { int temp = num1; num1 = num2; num2 = temp; public static void allzeros(int[] a, int num) { a = new int[num]; public static void main(string[] args) { int[] array = {10, 20, 30, 40, 50; //method call from following questions here (a) (2 points) Draw the array array if the comment in main is replaced with the statement: swap1(array); 50 20 30 40 10 (b) (2 points) Draw the array array if the comment in main is replaced with the statement: swap2(array[1], array[3]); 10 20 30 40 50 (c) (2 points) Draw the array array if the comment in main is replaced with the statement: allzeros(array, 5); 10 20 30 40 50 3

4. For each of the following questions, give the output of the code. Write your answer in the box provided next to the code. If the code would result in an infinite loop, give the first 3 outputs and then write infinite loop. If the code would result in an error for any reason, you can simply write error. (a) (3 points) for(int i = 3; i < 5; i++) { int j = 10; while(j > 5) { System.out.print((j-i) + " "); j -= 2; System.out.print("\n"); (b) (3 points) int n = 9; while (n > 0) { n -= 3; if (n % 4 == 2) { n++; System.out.println(n); (c) (3 points) int[] a = {10, 20, 30, 40, 50, 60; int[] b = {1, 2, 3, 4, 5; for(int i = 1; i <= 3; i++) { System.out.println( a[b[i]]); (d) (3 points) int i = 1; while(i < 5) { if (i % 2 == 0) { System.out.println("even " + i); continue; else { System.out.println("odd " + i); 7 5 3 6 4 2 Scoring: +1.5 pt each line of output 7 4 1-2 30 40 50 Scoring: 1 for each correct number. odd 1 even 2 even 2 infinite loop Scoring: 1.5pt for output; 1.5 pt for infinite loop 4

(e) (3 points) Assume the following method is invoked with easterbunny(24);. public static void easterbunny(int n) { if (n > 0) { if (n % 4 == 0) { System.out.println("hippity " + n); easterbunny(n/3); if (n % 3 == 0) { System.out.println("hop " + n); hippity 24 hippity 8 hop 24 Scoring: +1 each line of output 5. (5 points) Match each of the following code snippets in the left hand column to its output in the right hand column by drawing a line and connecting them. Each output may not be used or may be used more than once. A. int n = 3; int i = 0; while (i <= n-1) { System.out.print(i); B. int n = 3; int i = 0; while (i < n) { System.out.print(i); C. int n = 3; int i = 1; while (i <= n) { System.out.print(i); D. int n = 3; for (int i = n; i > 0; i--) { System.out.print(n-i); E. int n = 4; for (int i = 0; i < n; i += 3) { int j = n - i; while (j < n) { System.out.print(j); j++; I. 012 II. 023 III. 123 IV. 210 V. 321 VI. 0123 VII. 0234 VIII. 1234 IX. 3210 5

(A) answer: 012 (B) answer: 123 (C) answer: 123 (D) answer: 012 (E) answer: 123 6

6. The method sumodds is supposed to calculate and return the sum of all the odd numbers between a and b (extremes included), with a >= 0 and a <= b. However it contains a mistake that makes it behave incorrectly in some cases. 1 public static int sumodds(int a, int b) { 2 if (a < 0) { 3 a = 0; 4 5 int sum = 0; 6 while (a <= b) { 7 if (a % 2 == 1) { 8 sum += a; 9 a += 2; 10 else { 11 a += 2; 12 13 14 return sum; 15 (a) (2 points) Complete the sample method call below with values for which the method returns the correct result. Then give the value returned in this case. sumodds( any odd value, any value greater/equal to a); value returned: answers vary (b) (3 points) Complete the method call below with values for which the method returns an incorrect result. Then give the result that should be returned (if the method functioned as specified), and the result that is actually returned by the method as written. sumodds(any even value, any value greater/equal to a); value that should be returned: answers vary value returned: answers vary (c) (2 points) Fix the mistake so the method will always return the correct result if a >= 0 and a <= b. You do not need to rewrite the entire method; simply use the line numbers and rewrite the incorrect line(s). line 11 should be modified to a += 1 7

7. For each of the following parts, you will be writing a method named assum which takes an integer, n, and returns a String representing the value of n as the sum of n positive units. If n is negative or zero, the method returns the empty string. Examples: assum(3) returns "1+1+1" assum(1) returns "1" assum(-3) returns "" (empty String) (a) (5 points) Write assum using a for loop public static String assum(int n) { String result = ""; if (n > 0) { result += "1"; for (int i = 1; i < n; i++) { result += "+1"; return result; (b) (5 points) Write assum using a while loop public static String assum(int n) { String result = ""; if (n > 0) { result += "1"; int i = 1; while (i < n) { result += "+1"; return result; (c) (8 points) Write assum as a recursive method public static String assum(int n) { if (n <= 0) { return ""; else if (n == 1) { return "1"; else { return "1+" + assum(n - 1); 8

8. (10 points) Write a method named sumarrays that takes two arrays of integers as parameters, x and y. The method returns a new array where each element is the sum of the corresponding elements from x and y. If x and y have different lengths, the missing elements in the shorter array are treated as zeros. Your code should be able to handle an empty array as a parameter Examples: sumarrays({1, 2, 3, {5, 1, 2) returns {6, 3, 5 sumarrays({1, 2, 3, {5) returns {6, 2, 3 sumarrays({1, {5, 1, 2) returns {6, 1, 2 sumarrays({, {) returns { public static int[] sumarrays(int[] x, int[] y) { int[] result; if (x.length >= y.length) { result = new int[x.length]; else { result = new int[y.length]; for (int i = 0; i < result.length; i++) { int a = 0; if (i < x.length) { a = x[i]; int b = 0; if (i < y.length) { b = y[i]; result[i] = a + b; return result; Another solution with a different approach: public static int[] sumarrays(int[] x, int[] y) { int[] result = new int[math.max(x.length, y.length)]; for(int i = 0; i < x.length; i++) { result[i] += x[i]; for(int i = 0; i < y.length; i++) { result[i] += y[i]; return result; 9