COMP 401 Spring 2014 Midterm 1 I have not received nor given any unauthorized assistance in completing this exam. Signature: Name: PID: Please be sure to put your PID at the top of each page.
This page left intentionally blank. Do not write on it. Page 2 of 11
Part I: Multiple choice (45 points total) Directions: For each multiple choice question below, indicate all correct answers among the choices provided. More than one answer may be correct and at least one answer will be correct. Each question is worth 5 points. Selecting all of the choices or selecting none of the choices will result in no points awarded. 1) Which of these are NOT a value type? a) int b) null c) boolean d) String e) double[] 2) Which of these statements are true of an expression? a) A string is not an expression. b) An expression always results in a value. c) A literal interger is a valid expression. d) Expressions can not be combined using mathematical operators. e) A void method call can be part of an expression. 3) Which of these statements are true of loops? a) A for loop can always be rewritten as a while loop. b) The keyword end can be used to immediately exit the loop body. c) A loop cannot contain another loop. d) The body of a loop will execute at least once. e) A switch statement is a type of loop. 4) Which of the following are true of methods? a) The method signature indicates the return type of the method. b) Every statement in the method body will be executed. c) A return statement in a method ends execution of the method. d) A method can not call itself or cause itself to be called. e) A program is always executing within the context of some method. 5) Which of the following are true of arrays? a) The length of an array is available as a publicly accessible instance field. b) The index of the first item in an array is 1. c) An array can grow or shrink dynamically. d) An array is a reference type. e) Every element of an uninitialized array of a reference type is null. Page 3 of 11
6) Which of these statements about strings are true? a) Once created, a string can not change. b) The length of a string is available as a publicly accessible instance field. c) The == operator should be used to test the character- by- character equality of two strings. d) String is a built- in value type. e) A string literal is enclosed in double quotes. 7) Which of these statements are correctly related to the principle of encapsulation? a) Object fields should be marked as public. b) Encapsulation hides the details of how an abstraction is implemented. c) Interfaces should be used to define the publicly exposed behavior of an abstraction. d) Within a class, access to an object's fields must occur through getters and setters. e) Getters should validate their values if possible. 8) Which of these statements about Java interfaces are true? a) Interfaces can only specify public methods. b) An overloaded method can not be specified as part of an interface. c) An interface can by implemented by more than one class. d) A class can implement more than one interface. e) An interface can define an enumeration. 9) Which of these statements about method overloading is true? a) The return type of an overloaded method must remain the same. b) The access modifier of an overloaded method must remain the same. c) The number of parameters in an overloaded method must remain the same. d) The type of parameters in an overloaded method must remain the same. e) If one version of an overloaded method calls another version of the method, it must do so in the first statement of the method body. 10) Which of these statements about the iterator design pattern is true? a) An iterator will typically use a for or while loop internally. b) A collection is iterable if it provides a method that produces an iterator over its elements. c) The state of an iteration is encapsulated within the iterator. d) Multiple iterators can operate on a single iterable collection independently. e) Two iterators for the same iterable collection must produce the elements of the collection in the same order. Page 4 of 11
11) Which of these statements about interface extension is true? a) A class that implements an extended interface must also implement all of the methods declared in the parent interface. b) An extended interface may not overload methods already defined in its parent interface. c) An extended interface can only have a single parent interface. d) Extending an interface is an example of inheritance. e) Interface extension is most appropriate when the methods of the extended interface are independently meaningful with respect to the methods of the parent interface. 12) Which of these are valid variable names in Java? a) 1direction b) a_var c) break d) AVar_ e) more$moreproblems 13) Which of these statements about Java syntax is true? a) An expression can span multiple lines. b) A statement must end in a semicolon. c) A block of statements is enclosed by square braces. d) A.java file can be part of multiple packages. e) A variable first declared within a statement block is only accessible within that statement block. 14) Which of the following statements is true of classes? a) A class must include at least one constructor that accepts parameters. b) The this keyword is not meaningful within the body of a class method. c) A public class field can be accessed using the class name. d) The class name acts as a type name for objects of that class. e) Two classes in the same package may be given the same name as long as they define different instance fields. 15) Which of the following are legal method signatures? a) public void foo(int a) b) private static foo() c) double foo(int a, String b) d) int foo(a, b) e) public String[] foo(string foo) Page 5 of 11
Part II: Short answer / calculation (25 points total) Directions: Calculate the value of the variable or expression indicated. Each question is worth 5 points. 16) What is the value of the variable result after the code below executes? Answer: int[] a = new int[] {10, 20, 30; int[] b = new int[] {2, 1, 0; int[] c = a; int[] d = b; c[0] = d[2]; a[1] = c[d[b[2]]]; int result = a[0] + a[1] + a[2]; 17) What is the value of x after the code below executes? Answer: int x = 3; int y = 1; switch(x-y) { case 0: x += y; break; case 2: y += 3; case 4: x += y; break; case 6: x += 3; default: x = 0; y = 0; Page 6 of 11
Questions 18 and 19 use the definition of the function foo given below: String foo(string[] sa) { String s = ""; for (int i=0; i<sa.length; i++) { s += sa[i]; sa[i] = s; return s; Given the above definition for foo, assume the following two lines of code are executed: String[] my_strings = new String[] {"A", "B", "C"; String foo_result = foo(my_strings); 18) What is the value of foo_result? Answer: 19) What is the value of my_strings[1]? Answer: 20) What is the value of sum after the following code executes? Answer: int sum = 0; for (int i=0; i<=4; i++) { for (int j=i; j>0; j--) { if (i%2 == 0) { sum += j; else { sum -= j; Page 7 of 11
Part III: Reading and understanding code (15 points total) Directions: All of the questions in this section concern the code for the Invoice class given below. This class is intended to be an abstraction for keeping track of an invoice (i.e., a bill) of items. Each item is associated with a name and a price. The invoice as a whole associated with a tax rate, provided to the constructor when an Invoice object is created. Each question is worth 3 points. import java.util.list; import java.util.arraylist; public class Invoice { private double tax_rate; private List<String> item_names; private List<Double> item_prices; public Invoice(double tax_rate) { this.tax_rate = tax_rate; item_names = new ArrayList<String>(); item_prices = new ArrayList<Double>(); public void additem(string name, double price) { item_names.add(name); item_prices.add(price); public String finditemname(int index) { return item_names.get(index); public double finditemprice(int index) { return item_prices.get(index); public double gettotal() { double sum = 0.0; for (int i=0; i<item_names.size(); i++) { sum += item_prices.get(i); return sum; public double gettax() { return gettotal() * tax_rate; Page 8 of 11
21) How many instance fields does Invoice declare? 22) Which method(s), if any, act as getters following the JavaBeans convention? 23) Which method(s) act as a getter for a derived property? 24) Which method(s), if any, act as setters following the JavaBean convention? 25) Is the following statement true or false: The instance field tax_rate is immutable. Page 9 of 11
Part IV: Writing Code (20 points total) Directions: In this section, you will create and use an abstraction for the item information (i.e., name and price) used by the Invoice class as a new class called Item. Question 26 is worth 5 points, and Questions 27 is worth 15 points. 26) Write a definition for the class Item that includes an appropriate constructor and getter methods. Item objects should be immutable. Page 10 of 11
27) Rewrite the Invoice class so that it uses your new Item class and implements the following InvoiceInterface interface. In this new version, in addition to the existing constructor, add a second constructor that takes no parameters and creates a new Invoice object with a tax rate of 0.15. Extra credit for changing gettotal() to use a for- each loop to iterate over the items. public interface InvoiceInterface { void additem(item item); void additem(string name, double price); Item finditem(int index); double gettotal(); double gettax(); Page 11 of 11