University of Toronto -- Department of Computer Science CSC 108F - Fall 1997 Midterm test (Clarke's section) Aids allowed: Textbook only. Time: 50 minutes 1. [10 marks] [8 marks for part (a), and 2 marks for part (b)] (a) Write a Person class with fields name, height, weight, with a constructor to set all of these fields and a toString method that can be used to print a single line of output. You decide what types to use to represent the fields. (b) Write a brief segment of code to instantiate an object of the Person class. (2 marks) 2. [10 marks] Each of the following code fragments may contain an error. If there is an error, circle it and briefly state what is wrong; otherwise, write "no error". (a) int i = 0.1; (b) String s = new String{}; (c) int i = 10; int j = 9; int k = i / (i - j - 1); (d) System.out.println ("one + one = " + (1 * 1)); 3. [10 marks] (a) What is the output of this program fragment? String one = "can "; String two = "be, "; String three = "don't you think"; System.out.println("As easy as " + one + two + three); (b) Using these command line arguments: my name is What is the output of the following program segment in main (String[] args)? 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]); } (c) Use a for loop and write code that accomplishes the task in (b) for any number of command line arguments. (d) What is the output of this program fragment? int count = 0; while (count < 5) { count++; System.out.println (count + " )"); } 4. [10 marks] (a) Give an example of three primitive types and their declarations. (b) Give two examples of wrapper classes and create an instance of each. 5. [10 marks] The following segment of a main method calls a class method called x_Square_Plus_x intended to be defined so that x_Square_Plus_x(x) = . Write the x_Square_Plus_x method. (All you do here is to write the x_Square_Plus_x method. No class declaration is needed, and you do not have to complete the main method.) double x = 4; y = x_Square_Plus_x (x); // y equals 20