Birkbeck (University of London) Software and Programming 1 In-class Test 1.1 9 Feb 2017 Student Name Student Number Answer all questions 1. Consider the following sequence of Java statements: int m = 3; int n = 9 - m; m = 4 + n / m * 2 + 3; n = 32 / m + m * 3; What is the value of n after these statements are executed? Show your workings. (7 marks) 35 Workings: (a) n = 6, (b) m = 4 + 2 * 2 + 3 = 11, (c) n = 2 + 33 = 35 COIY018H5 Page 1 of 7 c Birkbeck College 2017
2. Given a variable word of type String, write an expression of type boolean, which is evaluated to true if the word contains between 4 and 8 (inclusive) characters and comes lexicographically before "Birkbeck", and to false otherwise. (8 marks) word.length() >= 4 && word.length() <= 8 && word.compareto("birkbeck") < 0 sp1-02-17.pdf / p. 18 and p. 19 3. What is the scope of a variable? (a) the statement in which the variable is declared; (b) the statement in which the variable is initialised; (c) the part of the program where the variable is visible; (d) another name for the type of the variable. (3 marks) (c) sp1-03-17.pdf, p. 15 4. Which of the following are valid Java identifiers (i.e., possible names of variables/methods)? (a) Int (b) def (c) R2D2 (d) 3PO (e) double float (f) char (g) function (a), (b), (c), (e), (g) sp1-01-17.pdf / p. 20 (7 marks) COIY018H5 Page 2 of 7 c Birkbeck College 2017
5. Identify and explain five compile-time errors in the following Java code: public class f00 bar { public void hi 2(int q) double d = 170; String s = 42; while (d < 200) { String b = "d + d = s"; //// now check parameter if (3 == q+1) d = d + 1 b =+ "aa"; print("d = s"); How would you correct the errors you have found? (10 marks) public class f00 bar { public void hi 2(int q) { // 1: curly bracket missing double d = 170; String s = "42"; // 2: wrong literal type while (d < 200) { String b = "d + d = s"; //// now check parameter if (3 == q+1) d = d + 1; // 3: missing semicolon b += "aa"; // 4: =+ is not applicable to String System.out.println("d = s"); // 5: unknown method print COIY018H5 Page 3 of 7 c Birkbeck College 2017
6. Implement a method to determine the type of electromagnetic radiation of a given wavelength: radiation with wavelengths up to 10 nm is called X-rays; radiation with wavelengths between 10 nm and 400 nm is called ultraviolet light; radiation with wavelengths between 400 nm and 800 nm is called visible light; radiation with wavelengths of at least 800 nm is called infrared light. The method should take one argument of type double, the wavelength in nm, and return the kind of radiation, of type String. (10 marks) public static String radiationtype(double wavelength) { if (wavelength <= 10) return "X-rays"; if (wavelength <= 400) return "ultraviolet light"; if (wavelength <= 800) return "visible light"; return "infrared light"; COIY018H5 Page 4 of 7 c Birkbeck College 2017
7. What is printed as a result of executing the following fragment of code? int x = 1; int y = x + 2; while (x < 50) { x = x * y; System.out.println(x + 1); y = 2 * y - 3; Show your workings. 4 10 28 82 Workings: x y x < 50 new x printout x + 1 new y 1 3 true 3 4 3 3 3 true 9 10 3 9 3 true 27 28 3 27 3 true 81 82 3 81 3 false (10 marks) 8. What are the type and the value of the following expression s.length() + 3 > b && b % 2 > 0? s.length() + 1.5 : b / 2.0 if s is "123" and b is 5? double and 4.5 (because both arguments of the logical AND are true) (5 marks) COIY018H5 Page 5 of 7 c Birkbeck College 2017
9. Implement a method that returns true if its argument of type String is a word that (a) consists of upper-case letters only and (b) contains at least one occurrence of letter Q. (20 marks) public static boolean isqword(string s) { boolean foundq = false; for (int i = 0; i < s.length(); i++) { char c = s.charat(i); if (! Character.isUpperCase(c)) return false; if (c == Q ) foundq = true; return foundq; COIY018H5 Page 6 of 7 c Birkbeck College 2017
10. (a) Transform the for loop in the following fragment of code into a while loop. String s = "1"; for (int i = 2; i <= n; i++) if (n % i == 0) s = s + " " + i; System.out.println("result: " + s); (b) Suppose the type of n is int and its value is 18. What is printed out as a result of executing this fragment of code? (c) Explain the action of this fragment of code (for a given positive integer n). (20 marks) (a) String s = "1"; int i = 2; // first argument of the for loop while (i <= n) { // second argument of the for loop turns into // the while condition if (n % i == 0) s = s + " " + i; i++; // do not forget the third argument of the for loop // and the curly brackets System.out.println("result: " + s); (b) The output is: result: 1 2 3 6 9 18 (c) The program prints the divisors of n (=18; in general, any positive integer). COIY018H5 Page 7 of 7 c Birkbeck College 2017