First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination November 11th, 2013 Examiners: Jonathan Tremblay [Sections 3 (3:30-4:30) and 2 (1:00-2:30)] 18:00-21:00 Dan Pomerantz [Section 1 (10:30-11:30)] Instructions: DO NOT TURN THIS PAGE UNTIL INSTRUCTED This is a closed book examination; only a letter-sized (8.5 by 11 ) crib sheet is permitted. This crib sheet can be single or double-sided; it can be handwritten or typed. Non-electronic translation dictionaries are permitted, but instructors and invigilators reserve the right to inspect them at any time during the examination. Besides the above, only writing implements (pens, pencils, erasers, pencil sharpeners, etc.) are allowed. The possession of any other tools or devices is prohibited. Answer all questions on the scantron sheet. This examination has 8 pages including this cover page, and is printed on both sides of the paper. On page 7, you will find information about useful classes and methods. You may detach page 7 from the examination if you wish. MAKE SURE TO WRITE YOUR NAME AND STUDENT ID ON THE SCANTRON AS WELL AS TO FILL IN THE BUBBLE PROPERLY AT THE BEGINNING OF THE EXAM
COMP-202A - Fall 2013 - Midterm Examination Page 2 Practice exam The following is designed to give you some practice with answering multiple choice questions. Solutions are listed below. We strongly suggest that you take this exam by writing down all your answers on a separate piece of paper, double checking your work, and only then looking at the solutions to confirm your answers. This is not meant to be a simulation of the real exam in that the exact length may be a bit different. Some notes on your real midterm exam: 1. There are approximately 50 questions total. 2. Of these, approximately 24 are separate questions (worth 2 points each), 14 are linked questions (worth 3 points each) referring to the same piece of code and asking many questions about it, and 10 are true false (worth 1 point each) 3. For the group of questions referring to the same piece of code, it is very important to know your assignments inside and out as the code will share some relation to that. 4. There is no penalty for guessing, so you should guess! 5. All questions within a section are weighted equally, so remember not to waste too much time on one question. 6. You are allowed to bring with you a crib sheet to the exam. The crib sheet can be two sided and typed and you can put whatever you want on it. We recommend putting examples that you found particularly tricky or confusing on it to help you. The point is not to make you memorize random facts that you ll forget afterwards anyway and in fact preparing the crib sheet (and selecting the important bits) can be a key part of studying. 7. Remember that the midterm is designed to be practice for the final. So even though there is an option in the final grading to skip your midterm grade, it is strongly recommended you take the midterm as seriously as possible. 8. There are other old midterms posted. Although there is no programming question on the exam, you can still use the old midterms to practice because the short answer format is similar. The main difference is you now are given choices instead. 9. Good luck!
COMP-202A - Fall 2013 - Midterm Examination Page 3 1. Which of the following is NOT a legal Java statement: (A) int x = 2; (B) double x = 4 + "" + 3; (C) double x = 3.0 + 4; (D) String x = "7 / 2"; (E) int x = 3; 2. How many times is the condition of the while loop checked in the following code? int i = 0; while (i < 30) { System.out.println("Repeat!"); i = i + 2; (A) 15 (B) 30 (C) 10 (D) 1 (E) 16 3. Suppose you have a method with the following header: public static void fun(string x, boolean b) Which of the following are legal ways to call the method: I. fun("3 + 1", true); II. fun(3 + "", false); III. fun("3.0",!true); (A) I only (B) II only (C) III only (D) II and III (E) I, II, and III 4. Which of the following Java expressions do not have a value of true? (A) true (B)!!true (C)!false (D)! (false && true) (E)! (false true)
COMP-202A - Fall 2013 - Midterm Examination Page 4 Type of Errors For each of the following answer whether they would cause an compiler error, runtime error, or a bug. In other cases the mentioned error may not cause a problem for the computer and is merely a question of style. In this case you should choose Style In other cases the question may not cause a problem at all in which case you should answer No Error For example, omitting a statement may lead to any of the above errors depending on where the omission occurred. 5. Omitting comments from your code. 6. Using a variable without declaring it. 7. Accessing an array out of bounds. 8. Storing an int expression into a variable of type double. 9. Incorrect indentation 10. What prints as a result of the following code being run?
COMP-202A - Fall 2013 - Midterm Examination Page 5 public static void main(string[] args) { int x = 1; int y = 3; x = foo(x,y); System.out.println(x + " " + y); public static int foo(int x, int y) { x = x * 2; y = x; return x; (A) 1 3 (B) 2 3 (C) 3 2 (D) 2 3 (E) 2 2 11. What value is returned by the following method if the input array is { 2, 3, 4, 1, 2, 1 public static int mystery(int[] array) { int foo = 0; for (int i = 0; i < array.length; i++) { if (i % 2 == 0) { foo = foo - array[i]; else { foo = foo + array[i]; return foo; (A) 3 (B) -3 (C) 5 (D) -5 (E) 13 12. How many times does the letter A print in the following code? for (int i = 0; i < 6; i++) { for (int j = 2; j <= 5; j+= 2) { System.out.println("A"); (A) 0
COMP-202A - Fall 2013 - Midterm Examination Page 6 (B) 4 (C) 12 (D) 20 (E) 30 13. How many times does the letter B print in the following code? for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { System.out.println("B"); System.out.println("B"); System.out.println("B"); (A) 8 (B) 18 (C) 24 (D) 30 (E) 36 14. What String does the following code return if the input to the method is the String with contents ABCD. public static String mystery(string input) { String tricky = ""; for (int i = 0; i < input.length(); i++) { tricky = (char) (input.charat(i) + 3); return tricky; (A) ABCD (B) BCDE (C) CDEF (D) DEFG (E) There is a compiler error because you can t add a char to an int. True false questions In the following questions answer A for true or B for false. 15. In Java, all variables must be declared
COMP-202A - Fall 2013 - Midterm Examination - Appendix Page 7 16. In Java, all method must be commented. 17. A String can be cast to an int by writing (int) before the String expression. 18. In Java, loops are allowed inside of other loops 19. A method can return more than one value in Java. SUMMARY OF JAVA STANDARD LIBRARY METHODS FOR SELECTED CLASSES String (package java.lang) Methods: public boolean equals(object anobject): Compares this String to anobject. public int length(): Calculates the length of this String. public boolean equalsignorecase(string anotherstring): Compares, ignoring case considerations, this String to anotherstring. public int compareto(string anotherstring): Compares this String to anotherstring lexicographically; returns a negative value if this String occurs before anotherstring, a positive value if this String occurs after anotherstring, and 0 if both Strings are equal. public int comparetoignorecase(string anotherstring): Compares, ignoring case considerations, this String to anotherstring lexicographically; returns a negative value if this String occurs before anotherstring, a positive value if this String occurs after anotherstring, and 0 if both Strings are equal. public String substring(int start, int finish): Returns a new String composed of the this String starting from index start and up to, but not including index of finish public String replace(char c, char d) : Returns a new String with all occurrences of the character c in the this String replaced by the character d. public char[] tochararray(): Converts this String to a new character array. File (package java.io) Methods: public FileSstring pathname): Creates a new File instance that corresponds to the given pathname. Scanner (package java.util) Methods: public Scanner(Inputstream source): Constructs a new Scanner that produces values scanned from the specified input stream. public Scanner(File f): Constructs a new Scanner that produces values scanned from the specified File public double nextdouble(): Scans the next token of the input as a double. public boolean nextboolean(): Scans the next token of the input as a boolean. public int nextint(): Scans the next token of the input as an int. public String nextline(): Advances this Scanner past the current line and returns the input read. public boolean hasnextline(): Checks whether there are further lines left to scan. PrintStream (package java.io) Methods: public void print(boolean b): Prints boolean value b. public void print(double d): Prints double value d. public void print(int i): Prints int value i. public void print(object o): Prints Object o. public void print(string s): Prints String s. public void println(): Terminates the current line by writing the line separator string.
COMP-202A - Fall 2013 - Midterm Examination - Appendix Page 8 public void println(boolean b): Prints boolean value b and then terminates the line. public void println(double d): Prints double value d and then terminates the line. public void println(int i): Prints int value i and then terminates the line. public void println(object o): Prints Object o and then terminates the line. public void println(string s): Prints String s and then terminates the line. Math (package java.lang) Methods: public static double pow(double a, double b): Returns the value of a raised to the power of b. public static double sqrt(double a): Returns the correctly rounded positive square root of double value a. public static double random(): Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. public static double exp(double a): Returns Euler s number e raised to the power of double value a. (base e) of double value a. of double value a.
COMP-202A - Fall 2013 - Midterm Examination - Appendix Page 9 Solutions to above questions 1 B 2 E 3 E 4 E 5 D 6 A 7 B 8 E 9 D 10 B (or D since they are the same!) 11 B 12 C 13 E 14 D 15 A 16 B 17 B 18 A 19 B