exam1_solution
12 Pages

exam1_solution

Course Number: CS 170, Fall 2009

College/University: Emory

Word Count: 1386

Rating:

Document Preview

CS170 Exam 1 February 24, 2009 Instructions: This exam is governed by the Emory Honor Code. This exam is closed book and closed notes. You will have the full class period (75 minutes). The point values are indicated beside each problem. You should assume that any code fragment that appears in this exam is embedded in a correct program. Name (print): ___solution__________________ This table is for grading,...

Unformatted Document Excerpt
Coursehero >> Georgia >> Emory >> CS 170

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Exam CS170 1 February 24, 2009 Instructions: This exam is governed by the Emory Honor Code. This exam is closed book and closed notes. You will have the full class period (75 minutes). The point values are indicated beside each problem. You should assume that any code fragment that appears in this exam is embedded in a correct program. Name (print): ___solution__________________ This table is for grading, please leave it blank. Problems # of Questions Points Score Multiple Choice Short Answers Bonus Total 18 5 1 27 36 64 2 102 Multiple Choice Questions (2 points each) Choose the best answer for each question. 1. The _____C_______ locates and executes program instructions, carries out arithmetic operations, and fetches data from storage and input devices. A) primary storage B) secondary storage C) CPU D) Java compiler 2. ____D_________ translates high-level language program into machine language program. A) CPU B) Memeory C) The operating system D) Java compiler 3. The JDK command to compile a Java source file Test.java is _____A___________. A) javac Test.java B) javac Test C) java Test.java D) java Test 4. What is the printout of the following code: ____A____________. double x = 5.5; int y = (int) x; System.out.println("x is " + x + " and y is " + y); A) x is 5.5 and y is 5 B) x is x and y is y C) x is 5.5 and y is 6 D) The above code will cause an error 5. Whats the value of x after the following code fragment? ____D_________ int x = 5; x *= x; int y = x++; A) 5 B) 6 C) 25 D) 26 Note: int y = x++; is equivalent to: int y = x; x++; 6. What happens if we try compiling a syntactically-correct .java file which contains no main method? ___D__________ A) It will cause a compiler error B) It will cause a run-time error C) It will throw an exception D) It will work fine Note: it will work fine to compile a .java file with no main method, however, it will cause a runtime error when try running the resulting .class file. 7. Given s1, s2, s3 declared below, which of the following statements correctly computes their average? ___C__________ int s1 = 80; int s2 = 100; int s3 = 80; A) double ave = s1 + s2 + s3 / 3.0; B) double ave = (s1 + s2 + s3 ) / 3; C) double ave = ((s1 + s2) + s3 ) / 3.0; D) double ave = s1 / 3 + s2 / 3 + s3 / 3; 8. Which of the following Java statements will not cause a syntax error? ___C__________ A) int x = 3; x / 2 = 1; B) int x; 1 = x % 2; C) int x = 3; x = x; D) int x; int y = x; 9. Which of the following are all fundamental data types in Java? ____C_________ A) Double, Integer, Char, String B) double, int, char, string C) double, int, short, long D) Double, Integer, Short, Long 10. If a and b are ints such that b is not 0, what is the value of the following expression? ___D_______ a-(a/b)*b A) 0 B) a C) a-b D) a%b Note: a/b performs an integer division 11. Given the declarations below, which of the following statements need an explicit cast in order to compile? ___C__________ int i1=10, i2=20; double d1=1.5, d2=2; A) i1 = i1/i2; B) d1 = d1/d2; C) i1 = i1/d2; D) d1 = Math.sqrt(d2) 12. What is the output of the following fragment? ____C_______ for (int i = 0; i < 15; i++) { if (i % 4 == 1) System.out.print(i + " "); } A) 1 3 5 7 9 11 13 B) 1 3 5 7 9 11 13 15 C) 1 5 9 13 D) 1 4 8 12 13. Analyze the following code: _____A_________ Code 1: boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: boolean even = (number % 2 == 0); A) Both Code 1 and Code 2 are correct, but Code 2 is better B) Code 1 has syntax errors C) Code 2 has syntax errors D) Both Code 1 and Code 2 has syntax errors 14. Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? (hint: please indent the statement correctly first.) _______A_________ if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0"); A) x < 0 and z > 0 B) x > 0 and y > 0 C) x < 0 and z < 0 D) no printout Note: else if is matched with the closest if which is the second if 15. Which of the following should be declared as a void method? _______D________ A) Write a method that returns a random integer from 1 to 100 B) Write a method that checks whether a number is positive C) Write a method that converts an uppercase letter to lowercase D) Write a method that prints integers 1 from to 100 16. What is the number of iterations in the following loop: _____D______ for (int i = 1; i < n; i++) { // iteration } A) 2*n B) n C) n + 1 D) n - 1 17.(int)(Math.random() * (65535 + 1)) returns a random number (assume the following ranges are inclusive) ____A_______. A) between 0 and 65535 B) between 1 and 65535 C) between 0 and 65536 D) between 1 and 65536 18. What is sum after the following loop terminates? ___A_______ int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); A) 6 B) 5 C) 8 D) 7 item = 1, sum = 1 item = 2, sum = 3 item = 3, sum = 6, break II. Short Answers 1. (10 points) Suppose you have 6 1/2 gallons of milk and want to store them in milk jars that can hold up to 0.75 gallons each. You want to know ahead of time, how many completely filled jars you will have. The following program has been written for that purpose. What is wrong with it? Why? How can you fix it? public class MilkJarCalculator { public static void main(String args[]) { double milk = 6.5; // gallons double jarCapacity = 0.75; // gallons int completelyFilledJars = milk / jarCapacity; System.out.println(completelyFilledJars); } } It will have a compiler error. milk / jarCapacity will return a double value, it is illegal to assign a double value to an int variable (narrowing conversion). The following fixes the problem by converting the division result to int explicitly: int completelyFilledJars = (int) (milk/jarCapacity); Note that the following is not a correct fix as casting operator will be evaluated first, so milk will be converted to an int, since jarCapacity is still double, the division result is still double. int completelyFilledJars = (int) milk/jarCapacity; Consider the program below for question 2 and 3. public class GreatestCommonDivisor { public static void main(String[] args) { // Generate two random integers int n1 = (int) (Math.random()*100); int n2 = (int) (Math.random()*100); // Print out the greatest common divisor System.out.println(gcd(n1, n2)); } /** return the greatest common advisor (gcd) of two integers */ public static int gcd(int n1, int n2) { int gcd = 1; // Initial gcd is 1 int k = 2; // Possible gcd while (k <= Math.max(n1,n2) ) { if (n1 % k == 0 && n2 % k == 0) gcd = k; // Update gcd k++; } return gcd; // Return gcd } } (10 points) 2. Identify all the method names that are defined in the code above. For each of methods identify return types and formal parameters. Method name main gcd return type void int formal parameters String[] args int n1, int n2 (10 points) 3. Rewrite the gcd method using for loop instead of while loop. public static int gcd(int n1, int n2) { int gcd = 1; // for (int k=2; k if (n1 % k == gcd = k; // } Initial gcd is 1 <= Math.max(n1,n2); k++ ) { 0 && n2 % k == 0) Update gcd return gcd; // Return gcd } (14 points) 4. Write a method that computes the mean of all numbers between a given range. The method signature is as follows. It should take two integers as parameters (a and b) and should return a double value. The returned value is the mean of all numbers that are greater or equal to a and less or equal to b. You can assume a < b. For instance, mean(7, 9) should return 8 (the mean of 7, 8, 9 is 8); mean (1, 4) should return 2.5 (the mean of 1, 2, 3, and 4 is 2.5). public static double mean(int a, int b) public static double mean(int a, int b){ int sum = 0; for (int i=a; i<=b; i++) sum += i; double m = (double)sum/(b-a+1); return m; } Note: The casting operator is needed in order to perform a double division double m = (double)sum/(b-a+1); Alternative: public static double mean(int a, int b){ double sum = 0; for (int i=a; i<=b; i++) sum += i; double m = sum/(b-a+1); return m; } (20 points) 5. Write a method that returns the sum of all digits in an integer. The method signature is as follows. You can assume number > 0. For instance, sum(1234) should return 10 and sum(12345) should return 15. public static int sum(int number) public static int sum(int number) { int remainder = number; int sum = 0; while (remainder > 0) { int digit = remainder % 10; remainder = remainder / 10; sum += digit; } return sum; } III. Optional (extra 2 points) Any comments and suggestions about the class so far (lectures, labs, homeworks ) that would help me improve the class would be appreciated! End of Exam
MOST POPULAR MATERIALS FROM CS
MOST POPULAR MATERIALS FROM Emory