Faculty of Science COMP-202A - Foundations of Computing (Fall 2014) - All Sections Final Examination

Size: px
Start display at page:

Download "Faculty of Science COMP-202A - Foundations of Computing (Fall 2014) - All Sections Final Examination"

Transcription

1 First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Foundations of Computing (Fall 2014) - All Sections Final Examination December 9 th, 2014 Examiners: Melanie Lyman-Abramovitch [Section 1 MWF (9:30-10:30)] 18:00-21:00 Juan Camilo Gamboa Higuera [Section 2 TR (13:00-14:30)] Jonathan Tremblay [Section 3 MWF (12:30-13: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 30 pages including this cover page, and is printed on both sides of the paper. On page 22, you will find information about useful classes and methods. You may detach the Appendix (page 22 onwards) from the examination if you wish. The Examination Security Monitor Program detects pairs of students with unusually similar answer patterns on mulitple-choice exams. Data generated by this program can be used as admissible evidence, either to initiate or corroborate an investigation or a charge of cheating under Section 16 of the Code of Student Conduct and Disciplinary Procedures. 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. MARKS WILL BE DEDUCED IF INFORMATION IS MISSING. Scoring The exam will be scored as follows: 1. Questions 1 to 9 are worth 1 point each 2. Questions 10 to 44 will be worth 2 points each 3. Questions 45 to 65 will be worth 1.5 points each Version-0

2 COMP-202A - Fall Final Examination - Version-0 Page 2 True/False Section (1 point each) 1. Object oriented programming paradigm is useful because it allows us to write more efficient programs (in terms of running time). (A) FALSE (B) TRUE 2. In a class that defines a new data type, you will not get a compile-time error if you do not define a constructor. (A) TRUE (B) FALSE 3. In a class, a non-static (instance) method can access static class variables. (A) TRUE (B) FALSE 4. In Java, each object gets its own copy of the static class variables. (A) FALSE (B) TRUE 5. Inside a class, you can declare multiple methods with the same name, provided that all such methods have different input arguments. (A) TRUE (B) FALSE 6. Inside a class, you can declare multiple methods with the same name, and the same input arguments, provided that all such methods have a different return type. (A) FALSE (B) TRUE

3 COMP-202A - Fall Final Examination - Version-0 Page 3 7. Consider the Node class in the appendix. The following code Node[] mynodes = new Node[10]; for( int i = 0; i < mynodes.length - 1; i++ ) mynodes[i] = new Node(i); mynodes[i].setnext(mynodes[i+1]); will produce an array of 10 Node objects. Each Node will have its next attribute pointing to the next Node in the array (except for the last one, which has next = null). (A) FALSE (B) TRUE 8. What is printed by the following code? String str1 = null; String str2 = "abc"; System.out.println(str2.equals("abc") && str1.equals(null)); (A) TRUE (B) FALSE 9. After executing this code int a = 57; int b = 43; a += b; b = a - b; a -= b; a += b; b = a - b; a -= b; a += b; b = a - b; a -= b; the values of a and b will be swapped. (A) TRUE (B) FALSE

4 COMP-202A - Fall Final Examination - Version-0 Page 4 Regular multiple choice (2 points each) 10. What is printed at the end of this piece of code? int num; for (num = 1; num <= 10; num++) System.out.println(num); (A) 11 (B) 10 (C) "" (D) 9 (E) This gives a compiler error 11. How many times does the letter K get printed in the following code? for (int i = 0; i < 5; i++) System.out.println("K"); for (int j = 0; j < i; j++) System.out.println("K"); System.out.println("K");; (A) 20 (B) 5 (C) 10 (D) 0 (E) 25

5 COMP-202A - Fall Final Examination - Version-0 Page What will the following print? public class Test public static void main (String [] args) boolean b1 = true; boolean b2 = false; boolean b3 = true; if ((b1 && b2) (b2 && b3) b2) System.out.print("ok "); if ((b1 && b2) (b2 && b3) (b1 b2)) System.out.println("dokey"); (A) dokey (B) ok dokey (C) ok (D) No output is produced (E) Compile-time error 13. Given the following boolean expression: (!a && b) ==!(a b) And given the following value combinations for a and b: I. a = true, b = true II. a = true, b = false III. a = false, b = true IV. a = false, b = false For which of the combinations does the above boolean expression evaluate to true? (A) I and II. (B) I and III. (C) II and III. (D) I, II, and IV. (E) II and IV. 14. Which of the following are correct array declarations? I. public static int [5] a; II. public int[] a; III. private final int[] a = 1, 3; (A) II and III. (B) None. (C) All. (D) I and III. (E) I and II.

6 COMP-202A - Fall Final Examination - Version-0 Page Which of the following Java expressions has a value of 0? I. 1.0/2 II. (int) 1.0 / 2.0 III. (int) 1.0 / 2 IV. (int) (1.0 / 2) (A) III and IV. (B) II, III, and IV. (C) II. (D) III. (E) II and IV. 16. The following method takes two integer arrays and a target integer as input. It is supposed to return true if there is an element from the first array and an element from the second array whose sum is the target integer. For instance, given arrays a=[1,4,3] and b=[2,6,3] and target 7, the method should return true because a[1]+b[2] = 4+3 = 7. However, if the target is 8, it should return false, as no element in array a, added with an element of array b results in 8. What should be the body of this method? public static boolean foo (int[] a, int[] b, int target) // What should go here? (A) for(int j = 0; j < b.length; j++) for(int i = 0; i < a.length; i++) if(a[i] + b[j] == target) return true; return false; (B) for(int i = 0; i < a.length + b.length; i++) if(a[i] + b[i] == target) return true; else return false; (C) for(int i = 0; i < a.length; i++) if(a[i] + b[i] == target) return true; else return false; (D) for(int i = 0; i < a.length + b.length; i++) if(a[i] + b[i] == target) return true; return false;

7 COMP-202A - Fall Final Examination - Version-0 Page 7 (E) for(int i = 0; i < a.length; i++) for(int j = 0; j < b.length; j++) if(a[i] + b[j] == target) return true; else return false; 17. What does the following piece of code print? public class Test public static void main(string[] args) int[] a = 2,4,6,8; for(int i = 0; i < a.length; i++) int[] b = foo(a); System.out.print( b[i] + " "); public static int[] foo(int[] array) for(int i = 0; i < array.length; i++) array[i]++; return array; (A) (B) (C) It would give compile-time error. (D) (E) It would give run-time error.

8 COMP-202A - Fall Final Examination - Version-0 Page What kind of exception would be caught below? public class Test public static void main(string[] args) try String[] s = new String[3]; s[1] = "Hello "; s[2] = "World "; for(int i = 0; i <= s.length - 1; i++) System.out.print(s[i].length()); catch(exception e) System.out.println(e); (A) NullPointerException (B) No exception would be caught. (C) ArrayIndexOutOfBoundsException (D) NumberFormatException (E) InputMismatchException 19. Suppose we have the following two classes in separate files. public class Brownie private int weight; private int calories; private boolean isyummy; public void eat() weight = 0; calories = 0; isyummy = Math.random() > 0.5; public class ILoveBrownies public static void main(string[] args) Brownie b1 = new Brownie(); b1.eat();

9 COMP-202A - Fall Final Examination - Version-0 Page 9 Which of the following statements is true? (A) It will compile and run without errors. (B) Brownie class won t compile because the eat method is trying to access a private variable. (C) Brownie class won t compile because there is no constructor. (D) We ll get a run-time error because the variables weight, calories and isyummy are not initialized. (E) Brownie class won t compile because isyummy is declared to have type boolean but in the eat method, we are trying to assign it a non-boolean value. 20. What is the difference between a doubly-linked list and a singly-linked list? (A) Nodes in the doubly-linked list have a reference to the previous node in the list, as well as the next node. (B) The doubly-linked list has a reference to the last node in the list, as well as the first node, while the singly-linked list has only one such reference. (C) It is easier to print a singly-linked list. (D) A doubly-linked list can hold twice as much data as a singly-linked list. (E) A doubly-linked list is a chain of lists, while a singly-linked list is a chain of nodes. 21. Recall, a hash function, h, is a function that maps input of any size to output in some finite set. For example, in class we saw a hash function that takes as input any String and outputs a number between 0 and 25, according to the lexicographical ordering of the first letter of the String. What is a collision in the context of hashing? (A) When more than one key is hashed to the same output value. (B) When several keys are the same and entries get over-written. (C) When a key is hashed to a value that is too large to fit in the array. (D) When a key is hashed and the output is the same as the input. (E) When two different hash tables have the same name. Sort 22. Consider the array a = 5,7,1,4,1. If we sort this array using the Selection Sort (code 2) algorithm described in the appendix, what will be displayed the first time the print statement on line 7 executes? (A) [1, 7, 5, 4, 1] (B) [1, 7, 1, 4, 5] (C) Nothing (D) [1, 1, 5, 4, 7] (E) [1, 1, 7, 4, 5] 23. Consider again the array a = 5,7,1,4,1 and the Selection Sort (code 2) algorithm. What is the last value of the variable minpos, before the algorithm exits? (A) 4 (B) 0 (C) 1 (D) 5 (E) 2

10 COMP-202A - Fall Final Examination - Version-0 Page Consider the Insertion Sort (code 3) algorithm described in the appendix. How can we change this code to have the algorithm sort from largest to smallest instead? (A) Change line 7 to: while(j > 0 & & a[j 1] < x) (B) Change line 3 to: for(int i=a.length-1; i > 0; i- -) (C) Change line 12 to: a[i] = x; (D) Impossible (E) Change line 9 to: a[j-1] = a[j]; 25. Consider the Cocktail Sort (code 4) algorithm described in the appendix. What would happen if we removed the code on lines 16-19? (A) This would cause the algorithm to take more steps to complete. (B) This is a logical error (the array will not be sorted). (C) This changes nothing - the code is useless. (D) This would cause a run-time error. (E) This would cause a compile-time error. 26. Consider the Cocktail Sort (code 4) algorithm described in the appendix. If we input the array a = 3,4,2,6,1, what will be printed by the print statement on line 15 the first time it is executed? (A) [3, 2, 4, 6, 1] (B) [3, 2, 4, 1, 6] (C) [3, 4, 2, 1, 6] (D) [1, 2, 3, 4, 6] (E) Nothing 27. Consider the Cocktail Sort (code 4) algorithm described in the appendix. If we input the same array (a = 3,4,2,6,1), what will be printed by the print statement on line 28 the first time it is executed? (A) [3, 2, 4, 1, 6] (B) [1, 3, 2, 4, 6] (C) [2, 3, 4, 1, 6] (D) [1, 2, 3, 4, 6] (E) Nothing 28. Consider the Cocktail Sort (code 4) algorithm described in the appendix. If we input the array (a = 3,4,1,6,2) 1, how many times total will the print statements on line 15 and 28 execute. For example, if line 15 happens four times, and line 28 happens five time, then your answer should be 9. (A) 5 (B) 3 (C) 4 (D) 6 (E) 0 1 This is a different array from the previous question

11 COMP-202A - Fall Final Examination - Version-0 Page Consider the Cocktail Sort (code 4) algorithm described in the appendix. What is the fewest number of lines of code that would have to be added, removed, or changed in order for this algorithm to sort from largest to smallest? (A) 2 (B) 1 (C) 3 (D) Everything inside both for loops. (E) Everything inside the second for loop. Binary answers For the following questions you are to use your choices as they were the representation of a binary number. More precisely, A = 2 4, B = 2 3, C = 2 2, D = 2 1, and E = 2 0 position in a base 2 number. For example if you wanted to answer the number 23, you would translate it first as base 2 number, which is 10111, you would blacken A, C, D and E. 2 Lets look at this process on your answer sheet, we have these 5 circle you can blacken. Normally you only pick one, here you might have to blacken more than one! A B C D E Following the previous example, the answer we want to input is 23, its binary representation is 10111, hence the circle you would blacken are the following: A B C D E Please follow this process for the next 4 questions. 30. What is the following base 10 number, 31, in base 2? 31. What is printed by the following code in base 2? int sum = 0; for(int x = 0; x< 5; x++) sum+=x; System.out.println(sum);

12 COMP-202A - Fall Final Examination - Version-0 Page What is printed by the following code in base 2? public static void main(string[] args) int[] a = 18,-11,4,-8; int[] b = -1,1,-1,-1; Manipulation(a,b); System.out.println(a[0]+a[1]+a[2]+a[3]); public static void Manipulation(int[] b, int[] a) for (int i = 0; i<a.length ; i++) b[i]=a[a.length-i-1] * b[i]; 33. Base 10 is the set of numbers created from the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Whereas base 2, which is the set of numbers created with 0, 1. We could similarly define base 3 as the ordered set of characters created from X,Y,Z 3. What is the following base 3 number, YXZ, in base 2? Random graph For this section you will need to look at the classes Graph and GraphNode present in the appendix. A Graph consists of a number of nodes, each of which has connections to other nodes in the graph. You can think of nodes as a representation of a person in a social network (e.g. Facebook). The ArrayList connectedto variable in GraphNodes is the list of friends that particular person has. For example, in the following graph, the node 0 is friends with node 1 and 2. On the other hand, node 2 and node 1 are not friends, hence the lack of link In the constructor of the Graph, the process of connecting nodes is randomized. Moreover, we will not allow a node to be connected to itself and we are going to assume that the arguments passed to the Graph constructor respect the following conditions: n > 2, and n > nbconnections. 3 Pay close attention to the character order

13 COMP-202A - Fall Final Examination - Version-0 Page Which line of code would you add to the Graph.java (code 5) on line 18 to pick two random nodes from your array of nodes? These will then be connected to each other. N.B. Math.random() returns a real number (double) between 0 and 1. (A) GraphNode node1 = graph[(int)(math.random() *n)]; GraphNode node2 = graph[(int)(math.random() *n)]; (B) GraphNode node1 = graph[(int)math.random() *n]; GraphNode node2 = graph[(int)math.random() *n]; (C) GraphNode node1 = graph[math.random() *n]; GraphNode node2 = graph[math.random() *n]; (D) int pos = (int)(math.random()*n); GraphNode node1 = graph[pos]; GraphNode node2 = graph[pos]; (E) int pos = (int)math.random()*n; GraphNode node1 = graph[pos]; GraphNode node2 = graph[pos]; 35. Why do we check on line 20 of code 5 that node1 is not equal to node2? N.B. reread the header of this section. (A) We want to make sure that we do not connect a node to itself (B) It serves no real purpose (C) The code is wrong is should be if(node1 == node2) (D) To check if node1 is neighbour with node2 (E) To avoid an infinite loop 36. Please fill the method Explore() in the GraphNode.java (code 6) line 36. This method explores each node that is connected to the evaluated node. This algorithm marks its explored variable to true when entering the node, and will explore every neighbour that was not already explored. This method will then be used to determine the connectivity of the graph. (A) this.explored = true; for (int i = 0; i<connectedto.size() ; i++) if(!connectedto.get(i).explored) connectedto.get(i).explore(); (B) for (int i = 0; i<connectedto.size() ; i++) if(!connectedto.get(i).explored) connectedto.get(i).explore(); (C) this.explored = true; for (int i = 0; i<connectedto.size() ; i++) connectedto.get(i).explore(); (D) for (int i = 0; i<connectedto.size() ; i++) connectedto.get(i).explore(); (E) for (int i = 0; i<connectedto.size() ; i++) if(!connectedto.get(i).explored) connectedto.get(i).explore();

14 COMP-202A - Fall Final Examination - Version-0 Page 14 this.explored = true; 37. Please fill the method CheckConnectivity() in the Graph.java (code 5) on line 41. This method runs an algorithm to check if the graph is connected 4 and prints onto the console true if the graph is connected and false otherwise. Please pay attention to the other methods available. (A) graph[0].explore(); CheckNodes(); System.out.println(this.connected); (B) CheckNodes(); System.out.println(this.connected); (C) for (int i = 0; i<graph.length ; i++) if(graph[i].explored == false) this.connected = false; (D) for (int i = 0; i<graph.length ; i++) if(graph[i].explored == false) this.connected = false; System.out.println(this.connected); (E) graph[0].explore(); CheckNodes(); 38. Imagine we have the following configuration in an instance of graph: If the node 0 is at position 0 in the graph, and we print its content, what would be printed into the console, e.g. System.out.println(graph[0]);? 5. N.B. The ordering of Nodes written after connected to: is unimportant. (A) 0 is connected to: 1, 3, (B) 0 is connected to: 1, 3 (C) 0 is connected to:, (D) 3 is connected to: 0, 1, 2, 3, 4, (E) 1 is connected to: 0, 3 4 A graph which is connected if there is a path from any point to any other point in the graph 5 This implicitly called tostring method

15 COMP-202A - Fall Final Examination - Version-0 Page 15 Cookie and CookieJar class In the next several questions, you will look at the classes Cookie and CookieJar provided in the appendix, and answer some questions about them. In the process, you will add to the classes. You should think of these classes as being written in separate java files in the same folder. 39. Add the proper code to line 8 of the Cookie class (A) public Cookie (String t, int c) (B) public static Cookie (String t, int c) (C) public Cookie (String type, int calories) (D) public void Cookie (String t, int c) (E) public Cookie (this.type, this.calories) 40. Provide the content of the getter method for the calories property (line 15). To make people feel better, the method should return only 90% of the actual calorie value of the cookie. (A) return (int) (0.9 * calories); (B) return 0.9 * calories; (C) return int 0.9 * calories; (D) return calories; (E) return 0.9 * (double) calories; 41. The eat method should go through the cookie jar, find the first cookie of the type given as input and take it out of the jar. It should then return the number of calories of that cookie, or 0 if no cookie of that type is in the jar. What is the correct line of code to be added at line 31? (A) for (int i = 0 ; i < cookies.size() ; i++) (B) if (cookies.gettype(type)) (C) for (int i = 1; i <= cookies.size() ; i++) (D) Nothing is missing (E) while (cookies.size() > 0)

16 COMP-202A - Fall Final Examination - Version-0 Page The bake method of the CookieJar class should take as input arguments the type of cookie to be baked, the number of cookies to be baked, and what the calories of each of these cookies will be (calpercookie). No return value is needed. The bake method should be accessible from other classes. What is the correct method header? (A) public void bake (String type, int number, int calpercookie) (B) private void bake (String type, int number, int calpercookie) (C) public int bake (String type, int number, int calpercookie) (D) public static void bake (String type, int number, int calpercookie) (E) private static void bake (String type, int number, int calpercookie) 43. The bake method should create the number of cookies of the type and calorie as given and then add them to the cookie jar on which the method was called. Which of the below is the correct method body given that the input parameters are named type, number and calpercookie? (A) for (int i = 0 ; i < number ; i++) cookies.add(new Cookie(type, calpercookie)); (B) for (int i = 0 ; i < number ; i++) cookies.add(new Cookie()); (C) cookies.add(number * new Cookie(type, calpercookie); (D) Cookie c = new Cookie(type, calpercookie) cookies.add(number * c); (E) addcookies(type, number, calpercookie); 44. What do lines 50 and 51 print and how many cookies are left in mycj after the last print statement (just before the main method ends)? (A) line 50: 90 line 51: 0 6 cookies left (B) line 50: 270 line 51: 0 4 cookies left (C) line 50: 100 line 51: cookies left (D) line 50: 90 line 51: cookies left (E) line 50: 90 after that the program crashes

17 COMP-202A - Fall Final Examination - Version-0 Page 17 Coding Section (1.5 points each) In this section you are to write methods that use multiple questions, one line of code per question. The combination of questions can then form the whole method. Make sure you explore all of the questions that define one method before deciding on your answer, as the obvious first choice might not be the right one. Also, do not forget the documentation in the Appendix at the back as it may help for some of these questions. Coding Question - Counting the elements in a list that have a given value The appendix shows a simple List class for integers. The class maintains only a reference to the first element (start) of the list but not the last element of the list. The class so far contains the constructor, a method to add an element to the front of the list, and the tostring method. The elements of the list are of type Node. You can find the Node class also in the appendix. You have to write an additional method for this class that takes as input an integer and returns how often this integer appears in the list. For example, if the list contains 2, 4, 20, 4, 2, 4 and 2 is the input, then the result is 2. If the input is 4, then the result is 3, and when the input is 1, the result should be 0. Use the following 8 questions to write your code. Pay close attention to how your answers group together with each other. 45. Line 1 (A) public int countvalue(int value) (B) public static void countvalue() (C) public void countvalue(int value) (D) public int countvalue(node n) 46. Line 2 (E) public static int countvalue(int value) (A) Node current = this.start; (B) first.value = value; (C) this.start = current; (D) Node current = new Node(value); 47. Line 3 (E) Node n = null; (A) int counter = 0; (B) int counter; (C) if (current == null); (D) if (current!= null); 48. Line 4 (E) while (current!= null) (A) while (current!= null) (B) return; (C) counter = current.getvalue(); (D) counter++; (E) counter = 1;

18 COMP-202A - Fall Final Examination - Version-0 Page Line 5 (A) if (current.getvalue() == value) (B) else (C) return counter; (D) while (current!= null) (E) current = current.nextvalue(); 50. Line 6 (A) counter++; (B) counter = counter + current.getvalue(); (C) return counter; (D) start = null; (E) while(current.getnext() == null) 51. Line 7 (A) current = current.getnext(); (B) (C) return current.getvalue(); (D) counter--; (E) start = current.getnext(); 52. Line 8 (A) return counter; (B) return ; (C) (D) current = current.getnext(); (E) break; Coding Question - Display triangle You are to write a method named Triangle that takes as input an integer height and prints a triangle of that height. This method should be callable from another method, for instance, from the main method. For example if the input is 4, the following is printed on the console: * *** ***** ******* Use the 7 following questions to write your code. Pay close attention to how your answers group together with each other.

19 COMP-202A - Fall Final Examination - Version-0 Page Line 1 (A) public static void Triangle(int height) (B) public static int Triangle(int height) (C) public static void Triangle() (D) public static float Triangle() (E) public static free Triangle(boolean free) 54. Line 2 (A) for (int i = 0; i<height ; i++) (B) for (int i = height; i>0 ; i--) (C) for (int i = 0; i<height/2 ; i--) (D) for (int i = 1; i<height ; i--) (E) for (int i = 1; i>=height/2 ; i+=10) 55. Line 3 (A) for (int j = 0; j< height - i-1; j++) (B) for (int j = 0; j< height - i * 2; j++) (C) for (int j = 0; j< height ; j++) (D) for (int j = height; j< height - i; j+=height) (E) for (int j = 0 ; j< 2 * height; j+=height) 56. Line 4 (A) System.out.print(" "); (B) System.out.print(""); (C) System.out.print("*"); (D) System.out.print("\n"); (E) System.out.print("null"); 57. Line 5 (A) for (int j = 0; j< i*2+1; j++) (B) for (int j = -i*2; j<= i*2; j++) (C) for (int j = i; j<= i*2; j+=i) (D) for (int j = 0; j<= i*2; j++) (E) for (int j = -i; j> i; j++) 58. Line 6 (A) System.out.print("*"); (B) System.out.print(" "); (C) System.out.print("\n"); (D) System.out.print(";)"); (E) System.out.print(""); 59. Line 7 (A) System.out.print("\n"); (B) System.out.print("*"); (C) System.out.print("null"); (D) System.out.print(" "); (E) System.out.print("");

20 COMP-202A - Fall Final Examination - Version-0 Page 20 Coding Question - Sum square difference Write a method called sumsqdiff that takes an input an integer upperbound and returns the difference between the square of the sum and the sum of the squares of all natural numbers, up to and including that bound. For example, if the upperbound is 10, your method should compute the following: ( i) i=1 i=1 ( ) 2 ( ) i 2 (1) Use the 6 following questions to write your code. Pay close attention to how your answers group together with each other. 60. Line 1 (A) public static int sumsqdiff(int upperbound) (B) public static void sumsqdiff(int upperbound) (C) public static int sumsqdiff(double upperbound) (D) public static int sumsqdiff() 61. Line 2 (E) public static double sumsqdiff(double upperbound) (A) int sum = 0; int sqsum = 0; (B) int sum = 1; int sqsum = 1; (C) int sum = 0; int sqsum = upperbound; (D) int sum = 0; double sqsum = upperbound; 62. Line 3 (E) int sum = 1; double sqsum = upperbound; (A) for(int i=1; i<=upperbound; i++) (B) for(int i=1; i<=sqsum; i++) (C) for(int i=0; i<=upperbound; i++) (D) for(int i=1; i<upperbound; i++) 63. Line 4 (E) for(int i=sqsum; i>=0; i--) (A) sum+=i; (B) sqsum+=i; (C) sum+=sqsum; (D) sqsum+=2*i; (E) sum++;

21 COMP-202A - Fall Final Examination - Version-0 Page Line 5 (A) sqsum=sqsum + i*i; (B) sqsum+=i; (C) sqsum=sqsum*sum*i; (D) sqsum=sqsum + i*sum; (E) sqsum*=sqsum; 65. Line 6 (A) return sum*sum - sqsum; (B) result = sum*sum - sqsum; (C) result = sum - sqsum; (D) return sum*sum + sqsum; (E) return = sqsum - sum*sum;

22 COMP-202A - Fall Final Examination - Appendix Page 22 SUMMARY OF JAVA STANDARD LIBRARY METHODS FOR SELECTED CLASSES Arrays (package java.util.arrays Methods: public int[] copyofrange(int[] original, int from, int to): Returns a subset of the original starting at from and finishing at to, excluding to. to might lie outside of the array. 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 char charat(int i): Gets the char at position i of the String. Note that counting starts from 0 so that to get the first character of the String you should input i equals 0. 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. 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.

23 COMP-202A - Fall Final Examination - Appendix Page 23 1 public static void bubblesort(int[] array) 2 3 int n = array.length; 4 boolean madeswap = true; 5 while(madeswap) 6 7 madeswap = false; 8 //Scanning 9 for (int j = 0; j < n - 1; j++) if (array[j] > array[j+1] ) //Swapping 14 swap(array,j,j+1); 15 madeswap = true; public static void swap(int[] a, int i, int j) int t = a[i]; 24 a[i] = a[j]; 25 a[j] = t; 26 Code 1: Bubble Sort

24 COMP-202A - Fall Final Examination - Appendix Page 24 1 public static void selectionsort(int[] a) 2 3 for(int start = 0; start<a.length; start++) 4 5 int minpos = smallest(a,start); 6 swap(a,start,minpos); 7 System.out.println(Arrays.toString(a)); public static int smallest(int[] a, int starter) int minpos = starter; 14 for (int i = starter; i<a.length ; i++) if(a[i]<a[minpos]) minpos = i; return minpos; public static void swap(int[] a, int v, int w) int t = a[v]; 27 a[v] = a[w]; 28 a[w] = t; 29 Code 2: Selection Sort 1 public static void insertionsort(int[] a) 2 3 for(int i = 1; i<a.length; i++) 4 5 int j = i; 6 int x = a[i]; 7 while(j>0 && a[j-1] > x) 8 9 a[j] = a[j-1]; 10 j--; a[j] = x; 13 System.out.println(Arrays.toString(a)); Code 3: Insertion Sort

25 COMP-202A - Fall Final Examination - Appendix Page 25 1 public static void cocktailsort(int[] a) 2 3 boolean swapped = true; 4 while(swapped) 5 6 swapped = false; 7 for(int i=0; i<a.length-2; i++) 8 9 if(a[i] > a[i+1]) swap(a, i, i+1); 12 swapped = true; System.out.println(Arrays.toString(a)); 16 if(!swapped) break; for(int j=a.length-2; j>=0; j--) if(a[j] > a[j+1]) swap(a, j, j+1); 25 swapped = true; System.out.println(Arrays.toString(a)); public static void swap(int[] a, int v, int w) int t = a[v]; 36 a[v] = a[w]; 37 a[w] = t; 38 Code 4: Cocktail Sort

26 COMP-202A - Fall Final Examination - Appendix Page 26 1 public class Graph 2 3 GraphNode[] graph; 4 boolean connected; 5 public Graph(int n, int nbconnections ) 6 Graph.connected = false; 7 //set the nodes 8 graph = new GraphNode[n]; 9 10 for (int i = 0; i<graph.length ; i++) //Create the nodes 11 graph[i] = new GraphNode(i); int counterconnections = 0; while(counterconnections<nbconnections) 17 //Get two random nodes 18 //PLEASE FILL THIS if(node1!= node2) 21 boolean connected = node1.connected(node2); 22 if(! connected) 23 node1.connect(node2); 24 node2.connect(node1); 25 counterconnections++; public void CheckNodes() 31 //Assume this is connected 32 this.connected = true; 33 //Check if there is one node that was not explored 34 for (int i = 0; i<graph.length ; i++) 35 if(graph[i].explored == false) 36 this.connected = false; public void CheckConnectivity() 41 //PLEASE FILL THIS public String tostring() 44 String s = "Graph\n"; 45 for (int i = 0; i<graph.length ; i++) 46 s+= graph[i].tostring() + "\n"; return s; Code 5: Random graph class

27 COMP-202A - Fall Final Examination - Appendix Page 27 1 import java.util.*; 2 3 public class GraphNode 4 5 ArrayList<GraphNode> connectedto; 6 boolean explored; 7 int id; 8 9 public GraphNode(int i) connectedto = new ArrayList<GraphNode>(); 12 //list of neighbours 13 explored = false; 14 id = i; public void Connect(GraphNode toadd) connectedto.add(toadd); public boolean Connected(GraphNode toconnect) if(connectedto.contains(toconnect)) return true; else return false; public void Explore() //PLEASE FILL THIS public String tostring() String s = ""; 41 s+= id + " is connected to: "; 42 for (int i = 0; i<connectedto.size() ; i++) s+= connectedto.get(i).id +", "; return s; Code 6: Node for random graph

28 COMP-202A - Fall Final Examination - Appendix Page 28 1 // This class is used to represent a cookie 2 public class Cookie 3 4 public String type; // The type of cookie: brownie, Xmas cookie,... 5 public int calories; 6 7 // Constructor 8 //... this line is still missing 9 10 this.type = t; 11 this.calories = c; public int getcalories() 15 //... this line is still missing // This class is used to represent a cookie jar 19 public class CookieJar ArrayList<Cookie> cookies; // All the cookies in the jar // Constructor 24 public CookieJar() 25 //... this line is still missing /* go through the cookie jar, find the first cookie of the type given as input and take it out; return the number of calories of that cookie, or 0 if no cookie of that type is in the jar. */ 28 public int boolean eat (String type) Cookie c; 31 //... this line is still missing c = cookies.get(i); 34 if (c.type.equals(type)) 35 cookies.remove(i); 36 return c.getcalories(); return 0; // bake a number of cookies of a certain type 42 //... this method is still missing public class Test 46 public static void main (String [] args) 47 CookieJar mycj = new CookieJar(); // mycj is the new cookie jar. 48 mycj.bake("brownies", 4, 250); 49 mycj.bake("lemon", 3, 100); 50 System.out.println(myCJ.eat("Lemon")); 51 System.out.println(myCJ.eat("Chocolate")); Code 7: Cookie and CookieJar classes

29 COMP-202A - Fall Final Examination - Appendix Page 29 1 public class List 2 3 private Node start; 4 5 public List() 6 7 start = null; public void addlist(int value) Node temp = start; start = new Node(value); 15 start.setnext(temp); public String tostring() String s = ""; 21 Node current = start; 22 while(current!= null) s += current.getvalue() ; 25 if(current.getnext()!= null) 26 s+= "->"; 27 current = current.getnext(); return s; Code 8: List class

30 COMP-202A - Fall Final Examination - Appendix Page 30 1 public class Node 2 3 private int value; 4 private Node next; 5 6 public Node(int value) 7 8 this.value = value; 9 this.next = null; public Node getnext() return this.next; public void setnext(node n) this.next = n; public int getvalue() return value; Code 9: List node class

Faculty of Science COMP-202A - Foundations of Computing (Fall 2015) - All Sections Midterm Examination

Faculty of Science COMP-202A - Foundations of Computing (Fall 2015) - All Sections Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Foundations of Computing (Fall 2015) - All Sections Midterm Examination November 5 th, 2015 Examiners: Melanie Lyman-Abramovitch

More information

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination 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

More information

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination November 7th, 2012 Examiners: Daniel Pomerantz [Sections

More information

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination 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

More information

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination February 23 rd, 2016 Examiners: Yang Cai [Section 1 TR (10:00-11:30)] 18:00-21:00 Jackie Chi Kit

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2011) - All Sections Midterm Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2011) - All Sections Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2011) - All Sections Midterm Examination Monday, October 31, 2011 Examiners: Daniel Pomerantz

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination Tuesday, November 3, 2009 Examiners: Mathieu Petitpas

More information

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination Thursday, March 11, 2010 Examiners: Milena Scaccia

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination Thursday, December 11, 2008 Examiners: Mathieu Petitpas [Section 1] 14:00

More information

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Final Examination

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Final Examination Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Final Examination April 21 st, 2016 Examiners: Yang Cai [Section 1 TR (10:00-11:30)] 14:00-17:00 Jackie Chi Kit Cheung

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination Wednesday, December 16, 2009 Examiners: Mathieu Petitpas

More information

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination Wednesday, April 29, 2009 Examiners: Mathieu Petitpas

More information

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name: Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name: CS Login Name: NetID (email): @wisc.edu Circle your Lecture: Lec001

More information

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault Computer Sciences 200 Midterm Exam 2 Thursday, November 15th, 2018 100 points (15% of final grade) Instructors: Jim Williams and Marc Renault (Family) Last Name: (Given) First Name: CS Login Name: NetID

More information

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a Spring 2017 Name: TUID: Page Points Score 1 28 2 18 3 12 4 12 5 15 6 15 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. i Some API Reminders

More information

COMP102: Test. 26 April, 2006

COMP102: Test. 26 April, 2006 Name:.................................. ID Number:............................ Signature:.............................. COMP102: Test 26 April, 2006 Instructions Time allowed: 90 minutes (1 1 2 hours).

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2010) - All Sections Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2010) - All Sections Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2010) - All Sections Final Examination Wednesday, December 8, 2010 Examiners: Maja Frydrychowicz

More information

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID: CMPS 12A - Winter 2002 Final Exam A March 16, 2002 Name: ID: This is a closed note, closed book exam. Any place where you are asked to write code, you must declare all variables that you use. However,

More information

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

More information

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Final Examination

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Final Examination Wednesday, April 27, 2011 Examiners: Daniel Pomerantz

More information

CSE 1223: Exam II Autumn 2016

CSE 1223: Exam II Autumn 2016 CSE 1223: Exam II Autumn 2016 Name: Instructions: Do not open the exam before you are told to begin. This exam is closed book, closed notes. You may not use any calculators or any other kind of computing

More information

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can

More information

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name: CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, 2006 Name: Email: This is a closed note, closed book exam. There are II sections worth a total of 200 points. Plan your time accordingly.

More information

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015 1 COE 212 Engineering Programming Welcome to the Final Exam Tuesday December 15, 2015 Instructors: Dr. Salim Haddad Dr. Bachir Habib Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1.

More information

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016 First Name (Print): Last Name (Print): Student Number: The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II 2016 Instructor: Dr. Bowen Hui Tuesday, April 19, 2016 Time: 6:00pm

More information

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

1 Short Answer (15 Points Each)

1 Short Answer (15 Points Each) Name: Write all of your responses on these exam pages. If you need extra space please use the backs of the pages. 1 Short Answer (15 Points Each) 1. Write the following Java declarations, (a) A double

More information

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz The in-class quiz is intended to give you a taste of the midterm, give you some early feedback about

More information

CS 211: Existing Classes in the Java Library

CS 211: Existing Classes in the Java Library CS 211: Existing Classes in the Java Library Chris Kauffman Week 3-2 Logisitics Logistics P1 Due tonight: Questions? Late policy? Lab 3 Exercises Thu/Fri Play with Scanner Introduce it today Goals Class

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. For the following one-dimensional array, show the final array state after each pass of the three sorting algorithms. That is, after each iteration of the outside loop

More information

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

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions: CS 170 Exam 2 Version: A Fall 2015 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

More information

NATIONAL UNIVERSITY OF SINGAPORE

NATIONAL UNIVERSITY OF SINGAPORE NATIONAL UNIVERSITY OF SINGAPORE SCHOOL OF COMPUTING EXAMINATION FOR CS1020 Semester 2: AY2011/12 CS1020 Data Structures and Algorithms I April 2012 Time allowed: 2 hours Matriculation number: INSTRUCTIONS

More information

McGill University School of Computer Science COMP-202A Introduction to Computing 1

McGill University School of Computer Science COMP-202A Introduction to Computing 1 McGill University School of Computer Science COMP-202A Introduction to Computing 1 Midterm Exam Thursday, October 26, 2006, 18:00-20:00 (6:00 8:00 PM) Instructors: Mathieu Petitpas, Shah Asaduzzaman, Sherif

More information

Unit 4: Classes and Objects Notes

Unit 4: Classes and Objects Notes Unit 4: Classes and Objects Notes AP CS A Another Data Type. So far, we have used two types of primitive variables: ints and doubles. Another data type is the boolean data type. Variables of type boolean

More information

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013 1 COE 212 Engineering Programming Welcome to Exam II Monday May 13, 2013 Instructors: Dr. Randa Zakhour Dr. Maurice Khabbaz Dr. George Sakr Dr. Wissam F. Fawaz Name: Solution Key Student ID: Instructions:

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Largest Online Community of VU Students

Largest Online Community of VU Students WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions

More information

Department of Computer Science Purdue University, West Lafayette

Department of Computer Science Purdue University, West Lafayette Department of Computer Science Purdue University, West Lafayette Fall 2011: CS 180 Problem Solving and OO Programming Exam 1 Solutions Q 1 Answer the questions below assuming that binary integers are represented

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Final Examination Question Max

More information

I. True/False: (2 points each)

I. True/False: (2 points each) CS 102 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2009 What is your name?: There are three sections: I. True/False..............50 points; (25 questions, 2 points each) II. Multiple

More information

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list } Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will

More information

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201): CIS 110 Introduction to Computer Programming Summer 2018 Final Name: Recitation # (e.g., 201): Pennkey (e.g., paulmcb): My signature below certifies that I have complied with the University of Pennsylvania

More information

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp ) Sorting 4/23/18 Administrivia HW on recursive lists due on Wednesday Reading for Wednesday: Chapter 9 thru Quicksort (pp. 271-284) A common problem: Sorting Have collection of objects (numbers, strings,

More information

Sorting. Bubble Sort. Selection Sort

Sorting. Bubble Sort. Selection Sort Sorting In this class we will consider three sorting algorithms, that is, algorithms that will take as input an array of items, and then rearrange (sort) those items in increasing order within the array.

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. Name: Write all of your responses on these exam pages. 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. 2. Java is a platform-independent

More information

A Quick and Dirty Overview of Java and. Java Programming

A Quick and Dirty Overview of Java and. Java Programming Department of Computer Science New Mexico State University. CS 272 A Quick and Dirty Overview of Java and.......... Java Programming . Introduction Objectives In this document we will provide a very brief

More information

NATIONAL UNIVERSITY OF SINGAPORE

NATIONAL UNIVERSITY OF SINGAPORE NATIONAL UNIVERSITY OF SINGAPORE SCHOOL OF COMPUTING TERM TEST #1 Semester 1 AY2006/2007 CS1101X/Y/Z PROGRAMMING METHODOLOGY 16 September 2006 Time Allowed: 60 Minutes INSTRUCTIONS 1. This question paper

More information

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Spring 2013 COMP Midterm Exam Solutions March 07, 2013 Spring 2013 COMP 110-003 Midterm Exam Solutions March 07, 2013 UNC Honor Pledge: I certify that no unauthorized assistance has been received or given in the completion of this work. Signature: Read this

More information

Practice Midterm 1 Answer Key

Practice Midterm 1 Answer Key CS 120 Software Design I Fall 2018 Practice Midterm 1 Answer Key University of Wisconsin - La Crosse Due Date: October 5 NAME: Do not turn the page until instructed to do so. This booklet contains 10 pages

More information

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016 1 COE 212 Engineering Programming Welcome to the Final Exam Thursday December 15, 2016 Instructors: Dr. Salim Haddad Dr. Bachir Habib Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1.

More information

ESC101 : Fundamental of Computing

ESC101 : Fundamental of Computing ESC101 : Fundamental of Computing End Semester Exam 19 November 2008 Name : Roll No. : Section : Note : Read the instructions carefully 1. You will lose 3 marks if you forget to write your name, roll number,

More information

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

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination CSI 1100 / 1500 Final Examination Page 1 of 13 CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination Duration : 3 hours 09:30 December 9, 2004 Professors: Alan Williams, Daniel

More information

CS 177 Week 15 Recitation Slides. Review

CS 177 Week 15 Recitation Slides. Review CS 177 Week 15 Recitation Slides Review 1 Announcements Final Exam on Friday Dec. 18 th STEW 183 from 1 3 PM Complete your online review of your classes. Your opinion matters!!! Project 6 due Just kidding

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Java Foundations Certified Junior Associate

Java Foundations Certified Junior Associate Java Foundations Certified Junior Associate 习题 1. When the program runs normally (when not in debug mode), which statement is true about breakpoints? Breakpoints will stop program execution at the last

More information

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018 Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 22 Mar 2018 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment

More information

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201): CIS 110 Introduction to Computer Programming Summer 2018 Final Name: Recitation # (e.g., 201): Pennkey (e.g., paulmcb): My signature below certifies that I have complied with the University of Pennsylvania

More information

Fall 2005 CS 11 Final exam Answers

Fall 2005 CS 11 Final exam Answers Fall 2005 CS 11 Final exam Answers 1. Question: (5 points) In the following snippet of code, identify all places where type casting would occur automatically or would need to occur through a forced cast.

More information

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring

More information

SAMPLE EXAM Final Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 16 December 2010

SAMPLE EXAM Final Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 16 December 2010 SAMPLE EXAM Final Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 16 December 2010 NAME (Printed) NAME (Signed) E-mail Exam Rules Show all your work. Your

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

Sample Examination Paper Programming and Software Development

Sample Examination Paper Programming and Software Development THE UNIVERSITY OF MELBOURNE DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING Sample Examination Paper 2008 433-520 Programming and Software Development Exam Duration: 2 hours Total marks for this

More information

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Term 1 Unit 1 Week 1 Worksheet: Output Solution 4 Term 1 Unit 1 Week 1 Worksheet: Output Solution Consider the following what is output? 1. System.out.println("hot"); System.out.println("dog"); Output hot dog 2. System.out.print("hot\n\t\t"); System.out.println("dog");

More information

CS1083 Week 2: Arrays, ArrayList

CS1083 Week 2: Arrays, ArrayList CS1083 Week 2: Arrays, ArrayList mostly review David Bremner 2018-01-08 Arrays (1D) Declaring and using 2D Arrays 2D Array Example ArrayList and Generics Multiple references to an array d o u b l e prices

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 101 Spring 2007 Midterm 2 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015 CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 6 problems on the following 7 pages. You may use your single-sided handwritten 8 ½ x 11 note sheet during

More information

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name: CSC 1051 Algorithms and Data Structures I Midterm Examination October 7, 2013 Name: Question Value Score 1 10 2 10 3 10 4 10 5 10 6 10 7 10 8 10 9 10 10 10 TOTAL 100 Please answer questions in the spaces

More information

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI Outline Chapter 3 Using APIs 3.1 Anatomy of an API 3.1.1 Overall Layout 3.1.2 Fields 3.1.3 Methods 3.2 A Development Walkthrough 3.2.1 3.2.2 The Mortgage Application 3.2.3 Output Formatting 3.2.4 Relational

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

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

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Name: Covers Chapters 1-3 50 mins CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone

More information

Arrays in Java Using Arrays

Arrays in Java Using Arrays Recall Length of an Array Arrays in Java Using Arrays Once an array has been created in memory, its size is fixed for the duration of its existence. Every array object has a length field whose value can

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information

UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

More information

Computer Science Foundation Exam

Computer Science Foundation Exam Computer Science Foundation Exam December 18, 015 Section I B COMPUTER SCIENCE NO books, notes, or calculators may be used, and you must work entirely on your own. SOLUTION Question # Max Pts Category

More information

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A CSC 1051 Algorithms and Data Structures I Midterm Examination March 1, 2018 Name: KEY A Question Value Score 1 20 2 20 3 20 4 20 5 20 TOTAL 100 Please answer questions in the spaces provided. If you make

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

1st Semester Examinations CITS1001 3

1st Semester Examinations CITS1001 3 1st Semester Examinations CITS1001 3 Question 1 (10 marks) Write a Java class Student with three fields: name, mark and maxscore representing a student who has scored mark out of maxscore. The class has

More information

TeenCoder : Java Programming (ISBN )

TeenCoder : Java Programming (ISBN ) TeenCoder : Java Programming (ISBN 978-0-9887070-2-3) and the AP * Computer Science A Exam Requirements (Alignment to Tennessee AP CS A course code 3635) Updated March, 2015 Contains the new 2014-2015+

More information

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS Day Class Midterm Exam Dr. R. Khedri DURATION : 50 minutes McMaster University Midterm Exam (CAS) October 29, 2012 Please CLEARLY print: NAME:

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Introduction to Computing II (ITI 1121) Final Examination Instructor: Marcel Turcotte April 2010, duration: 3 hours Identification Student name: Student number: Signature: Instructions 1. 2. 3. 4. 5. 6.

More information

Question: Total Points: Score:

Question: Total Points: Score: CS 170 Exam 2 Section 002 Fall 2013 Name (print): 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

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

Final Exam COMP Fall 2004 Dec 16, 2004

Final Exam COMP Fall 2004 Dec 16, 2004 1. Closed book and closed notes. Final Exam COMP 14-062 Fall 2004 Dec 16, 2004 2. Write all scratch work and answers on the exam itself. If you need extra space, let me know. Indicate your final answer

More information

2. [20] Suppose we start declaring a Rectangle class as follows:

2. [20] Suppose we start declaring a Rectangle class as follows: 1. [8] Create declarations for each of the following. You do not need to provide any constructors or method definitions. (a) The instance variables of a class to hold information on a Minesweeper cell:

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

Computer Science 1 Ah

Computer Science 1 Ah UNIVERSITY OF EDINBURGH course CS0077 COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS Computer Science 1 Ah Resit Examination Specimen Solutions Date: Monday 1st September 2003 Time: 09:30 11:00

More information

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6 CPS109 Midterm Test Fall 05 page 1 of 6 Last Name First Name Student Number Circle Your Instructor Your last name here Your first name here Your student number here Ferworn Harley Instructions: (a) There

More information

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011

CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011 CSCI 135 Midterm Fundamentals of Computer Science I Fall 2011 Name: This exam consists of 12 problems on the following 11 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

ing execution. That way, new results can be computed each time the Class The Scanner

ing execution. That way, new results can be computed each time the Class The Scanner ing execution. That way, new results can be computed each time the run, depending on the data that is entered. The Scanner Class The Scanner class, which is part of the standard Java class provides convenient

More information

1. An operation in which an overall value is computed incrementally, often using a loop.

1. An operation in which an overall value is computed incrementally, often using a loop. Practice Exam 2 Part I: Vocabulary (10 points) Write the terms defined by the statements below. 1. An operation in which an overall value is computed incrementally, often using a loop. 2. The < (less than)

More information