Fall '97 -- Midterm Solutions ============================= TEST out of 50. 1. [10 marks] (a) class Person { String name; double height, weight; Person (String name, double height, double weight) { this.name = name; this.height = height; this.weight = weight; } String toString () { return name + " is " + height + " cm. tall and weighs " + weight + " kg."; } } (b) Person p = new Person("name", 70.5, 80.1); 2. [10 marks] 1 for saying compile or run or logical or no error and the rest of the marks for the explanation. (a) int i = 0.1; 3 marks compile time error float assigned to an integer (b) String s = new String{}; 2 marks compile time error {} should be () (c) int i = 10; int j = 9; int k = i / (i - j - 1); 3 marks run time error divide by zero (d) System.out.println ("one + one = " + (1 * 1)); 2 marks logical error no one + one is not = 1 3. [10 marks] (a) String one = "can "; String two = "be, "; String three = "don't you think"; System.out.println("As easy as " + one + two + three); As easy as can be, don't you think (b) if (args.length == 1) System.out.println("args[0] = " + args[0]); else if (args.length == 2) { System.out.println("args[0] = " + args[0]); System.out.println("args[1] = " + args[1]);} else if (args.length == 3) { System.out.println("args[0] = " + args[0]); System.out.println("args[1] = " + args[1]); System.out.println("args[2] = " + args[2]); } args[0] = my args[1] = name args[2] = is (a) for (int i = 0; i < args.length;i++) System.out.println("args["+i+"] = " + args[i]); int count = 0; while (count < 5) { count++; System.out.println (count + " )"); } (d) 1) 2) 3) 4) 5) 4. [10 marks] 1. Give an example of three primitive types and their declarations. 2 marks for each wrapper class (6 marks). int count = 12; Also: byte, short, float, ... double price = 32.87; char unit = 'F'; 2. Give two examples of wrapper classes and create an instance of each. 2 marks for each wrapper class (4 marks). Double d = new Double(2.3); Also: Boolean, Byte, Character, Long,... String s = new String("name"); Float f = new Float(2.3f); Integer i = new Integer(53); 5. [10 marks] double x_Square_Plus_x (double x) { return x*x+x; }