Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 16 Mar 2017 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment is executed? You should show your workings. int x = 2; int y = 2; while (x < 10) { y += x; x += 2; System.out.println(y); (5 marks) 22 Workings: before after x y condition y x 2 2 2 < 10 is true 4 4 4 4 4 < 10 is true 8 6 6 8 6 < 10 is true 14 8 8 14 8 < 10 is true 22 10 10 22 10 < 10 is false COIY018H5 Page 1 of 10 c Birkbeck College 2017
2. Let country be a variable of type String. Write a Java expression (of type boolean) that is evaluated to true if country is either an empty string ("") or null; and evaluated to false otherwise. (5 marks) country == null country.equals("") 3. What output is produced when the following Java program fragment is executed in each case? (a) a is -1. (b) a is 0. (c) a is 1. You should show your workings. if (a >= 0) if (a == 0) System.out.print("Z"); else System.out.print("P"); System.out.print("N"); (a) N (b) ZN (c) PN the else belongs to the second if (5 marks) COIY018H5 Page 2 of 10 c Birkbeck College 2017
4. What output is produced when the following Java program is executed? public class D17 1 { public static void main(string[] args) { for(int i = 1; i <= 5; i++) { for(int j = 0; j <= 5; j++) if (i <= 3 && j <= 6 - i && j >= i - 1) System.out.print("x"); else System.out.print("."); System.out.println(); You should show your workings. xxxxxx.xxxx...xx........ (5 marks) COIY018H5 Page 3 of 10 c Birkbeck College 2017
5. Implement a method public static int largestdiff(int[] arr) that takes an array arr of integer numbers and returns the largest difference between the consecutive elements of arr. Assume that arr contains at least two elements. For instance, if the input array is { 0, 1, -2, 4, 7, the differences between the consecutive elements are: 1 0, {{ 2 1, {{ 4 ( 2), {{ 7 4, {{ and so the output is 6. = 1 = 3 = 6 = 3 (20 marks) public static int largestdiff(int[] arr) { int result = arr[1] - arr[0]; for (int i = 2; i < arr.length; i++) { int diff = arr[i] - arr[i-1]; if (diff > result) result = diff; return result; alternatively: public static int largestdiff(int[] arr) { int result = arr[1] - arr[0]; for (int i = 1; i < arr.length - 1; i++) { int diff = arr[i+1] - arr[i]; if (diff > result) result = diff; return result; COIY018H5 Page 4 of 10 c Birkbeck College 2017
6. Implement a method public static void normalise(double[] v) that, given an array v of floating-point numbers, divides each of its elements by the sum of elements of v: for example, if the parameter array is { 0, 1, 2, 3, then the method modifies the array so that it contains because the sum of elements is 6. { 0, 0.166..., 0.333..., 0.5 (10 marks) public static void normalise(double[] v) { double sum = 0; for (double d : v) // alternatively: for (int i = 0; i < v.length; i++) sum += d; // sum += v[i]; for (int i = 0; i < v.length; i++) v[i] /= sum; COIY018H5 Page 5 of 10 c Birkbeck College 2017
7. What output is produced when the following Java program is executed? public class E17 1 { public static void main(string args[]) { int[] a = { 1, 1, 2 ; int[] b = { 2, 0, 0 ; System.out.println(g(a) == g(b)? "yes" : "no"); public static int g(int a[]) { int idx = a[0]; idx = a[idx]; return a[idx]; You should show your workings. no Workings: get: (10 marks) When the method g is called in g(a), a in g refers to the array { 1, 1, 2 and so, we int idx = a[0]; becomes 1 idx = a[1]; becomes 1 return a[1]; returns 1 When the method g is called in g(b), a in g refers to the array { 2, 0, 0 and so, we get: int idx = a[0]; becomes 2 idx = a[2]; becomes 0 return a[0]; returns 2 As 1 2, the method main prints "no". COIY018H5 Page 6 of 10 c Birkbeck College 2017
8. Suppose you have declared an interface Expression as follows: (a) public interface Expression { String gettype(); Write a class Literal that implements interface Expression. The class should have three constructors and two instance variables, val and tp, of type String. The first constructor takes an int as a parameter and sets tp to "int". The second constructor takes a double as a parameter and sets tp to "double". The third constructor takes a String as a parameter and sets tp to "string". Each of the constructors stores the parameter in val. (10 marks) (b) In class Literal, implement methods getvalue and gettype that return the values stored in val and tp, respectively. (4 marks) (c) Write a class OperationPlus that implements interface Expression. The constructor of OperationPlus should take two instances of Expression, the left- and righthand side arguments of the + operation, respectively. Use suitable instance variables. Method gettype should first get the types of the left- and right-hand side arguments of the + operation and then return "int" if both types are "int"; "double" if one type is "double" and the other is either "double" or "int"; "string" if one of the types is "String"; null otherwise. (11 marks) (d) Override method tostring in classes Literal and OperationPlus in such a way that after executing the following Java fragment: Expression p1 = new OperationPlus(new Literal(1), new Literal(2.0)); Expression p2 = new OperationPlus(p1, new Literal("s")); the call p2.tostring() would return "((1@int + 2.0@double) + s@string)". In other words, tostring of Literal should return the value and its type separated by @; method tostring of OperationPlus should return the result of applying tostring to both arguments of the + operations, which are separated by " + " and enclosed in brackets. (8 marks) COIY018H5 Page 7 of 10 c Birkbeck College 2017
(a) public class Literal implements Expression { private String val; private String tp; public Literal(int x) { val = "" + x; tp = "int"; public Literal(double x) { val = "" + x; tp = "double"; public Literal(String x) { val = x; tp = "string"; (b) public String getvalue() { return val; public String gettype() { return tp; COIY018H5 Page 8 of 10 c Birkbeck College 2017
(c) public class OperationPlus implements Expression { private Expression left, right; public OperationPlus(Expression left, Expression right) { this.left = left; this.right = right; public String gettype() { String t1 = left.gettype(); String t2 = right.gettype(); if (t1.equals("int") && t2.equals("int")) return "int"; if (t1.equals("double") && t2.equals("double") t1.equals("double") && t2.equals("int") t1.equals("int") && t2.equals("double")) return "double"; if (t1.equals("string") t2.equals("string")) return "string"; return null; (d) In class Literal: public String tostring() { return val + "@" + tp; In class OperationPlus: public String tostring() { return "(" + left.tostring() + " + " + right.tostring() + ")"; or public String tostring() { return "(" + left + " + " + right + ")"; COIY018H5 Page 9 of 10 c Birkbeck College 2017
9. Find five compile-time errors in the following Java class declaration: public class Y17 1 { private static final int x = null; public boolean f(string args) { Y17 1 y = new Y17 1(args); y.x = 21; String x = args; int[] array = { 22, 23, 24 ; for (int d : array) x = x + d; System.out.print("current d is " + d); return array[array[25]]; How would you correct the errors you have found (with as few changes as possible)? (7 marks) public class Y17 1 { private static int x = 0; // 0, not null public int f(string args) { // return type Y17 1 y = new Y17 1(args); // default constructor has no arguments y.x = 21; // drop final from declaration of x to write there String x = args; int[] array = { 22, 23, 24 ; for (int d : array) { // need {... ; otherwise, the scope of d x = x + d; // is limited to this statement System.out.print("current d is " + d); return array[array[25]]; COIY018H5 Page 10 of 10 c Birkbeck College 2017