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 is executed? You should show your workings. int y = 0; for (int x = 4; x <= 12; x += 2) { y += x; System.out.println(y); (5 marks) Workings: COIY018H5 Page 1 of 10 c Birkbeck College 2018
2. Let country be a variable of type String that possibly contains null. Write a Java expression (of type boolean) that is evaluated to true if country is a non-empty string (recall that the empty string is ""); and evaluated to false otherwise. The expression should never throw a NullPointerException. (5 marks) 3. What output is produced when the following Java program fragment is executed in each case? (a) a is 1. (b) a is 2. (c) a is 3. You should show your workings. if (a >= 2) if (a == 3) System.out.print("T"); else System.out.print("D"); else System.out.print("U"); (5 marks) COIY018H5 Page 2 of 10 c Birkbeck College 2018
4. What output is produced when the following Java program is executed? public class D18 1 { public static void main(string[] args) { for(int i = 1; i <= 4; i++) { for(int j = 1; j <= i + 3; j++) if (j > 4 - i) System.out.print("x"); else System.out.print(" "); System.out.println(); You should show your workings. (5 marks) COIY018H5 Page 3 of 10 c Birkbeck College 2018
5. A run is a sequence of adjacent repeated values. Implement a method public static int getlongestrunlength(int[] v) that computes and returns the length of the longest run in an array. For example, the longest run in the array with elements 1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 3, 1 has length 4 because the array contains 4 consecutive 2s. (20 marks) COIY018H5 Page 4 of 10 c Birkbeck College 2018
6. Implement a method public static double weightedsum(double[] values, double[] weights) that, given two arrays values and weights of floating-point numbers, returns the weighted sum i values i weights i : for example, if values is { 1, 2, 3 and weights is { 0.1, 0.2, 0.7, then the method returns 1 0.1 + 2 0.2 + 3 0.7 = 2.6. Assume that the two parameter arrays are of the same length. (10 marks) COIY018H5 Page 5 of 10 c Birkbeck College 2018
7. What output is produced when the following Java program is executed? public class E18 1 { public static void main(string args[]) { int[] a = { 1, 3, 2, 1 ; System.out.println(g(a, 1) == g(a, 2)? "yes" : "no"); public static int g(int a[], int i) { int idx = a[i]; idx = a[idx]; return a[idx]; You should show your workings. (10 marks) Workings: COIY018H5 Page 6 of 10 c Birkbeck College 2018
8. Suppose you have declared an interface IntegerExpression as follows: public interface IntegerExpression { int getvalue(); (a) Write a class IntegerConstant that implements interface IntegerExpression. The class should have a constructor and an instance variable, val, of type int. The constructor takes an int as a parameter and stores it in val. (7 marks) (b) In class IntegerConstant, implement method getvalue() that returns the value stored in val. (4 marks) (c) Write a class Addition that implements interface IntegerExpression. The constructor of Addition should take two instances of IntegerExpression, the left- and right-hand side arguments of the + operation, respectively. Use suitable instance variables to store references to the two subexpressions. Method getvalue() should return the result of adding the values provided by the two subexpressions. (6 marks) (d) Write a class Multiplication that implements interface IntegerExpression in such a way that its method getvalue() returns the result of multiplying the values provided by the two subexpressions; see (c). (6 marks) (e) Override method tostring() in the three classes, IntegerConstant, Addition and Multiplication, in such a way that they render the expression in reverse Polish notation, where the operators follow their operands, and, if there are multiple operations, operators are given immediately after their second operands. For example, (1 + 2) 3 is 1 2 + 3 in reverse Polish notation. In other words, the methods tostring() should be implemented so that after executing the following Java fragment: IntegerExpression p1 = new Addition(new IntegerConstant(1), new IntegerConstant(2)); IntegerExpression p2 = new Multiplication(p1, new IntegerConstant(3)); the call p2.tostring() would return "1 2 + 3 *". Note that "1 2 +" would be produced by the call to p1.tostring(). (10 marks) COIY018H5 Page 7 of 10 c Birkbeck College 2018
COIY018H5 Page 8 of 10 c Birkbeck College 2018
COIY018H5 Page 9 of 10 c Birkbeck College 2018
9. Find five compile-time errors in the following class: public class Q18 1 { public void main(string[] args) { final boolean help = false; int i; while (i < args.length() &&!help) { if (args[i] == "--help") help = "true"; else i++; return i; How would you correct the errors you have found (with as few changes as possible)? (7 marks) COIY018H5 Page 10 of 10 c Birkbeck College 2018