Southern College Kolej Selatan 南方学院 Final Examination Semester 3 / Year 2008 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE CLASS : CS08-A + CS08-B LECTURER : SO YONG QUAY Student s ID: Batch No: Notes to candidates: 1) The question paper consists of 6 pages and 13 questions. 2) There are three sections in this question paper; the students are required to answer all the questions. 3) Return the question paper with your answer booklet and disk.
Section I: Multiple Choices Questions (2% each) 1. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect? a. String s = new String("new string"); b. String s3 = s1 + s2; c. String s3 = s1.concat(s2); d. s1 >= s2 e. int i = s1.length(); 2. A subclass inherits from its superclasses. a. private data b. protected data c. public data d. a and c e. b and c 3. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect? a. String s3 = s1 - s2; b. int i = s1.compareto(s2); c. char c = s1[0]; d. char c = s1.charat(s1.length() - 1); e. a and c. 4. Suppose you want to display a text in multiple lines, which component should you use? a. JButton b. JLabel c. JTextField d. JTextArea 5. describes the state of an object. a. attributes b. methods c. constructors d. no of the above 1 / 6
Section II: Show the output of the program (2% each) 6. Show the output of the following program. public class C { Object[] o = {new A(), new B(); System.out.print(o[0]); System.out.print(o[1]); class A extends B { public String tostring() { return "A"; class B { public String tostring() { return "B"; 7. What is displayed on the console when the following program is run? class Test { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to HTML"); finally { System.out.println("The finally clause is executed"); System.out.println("End of the block"); 2 / 6
8. Show the output of the following program: class A { public A() { System.out.println("The default constructor of A is invoked"); class B extends A { public B() { System.out.println("The default constructor of B is invoked"); public class C { B b = new B(); 9. What is the printout of the following code? public class FE200703_12 { public static void main(string[] args){ try { int value = 50; if (value < 40) throw new Exception ("Value is too small"); catch(exception ex){ System.out.println(ex.getMessage()); System.out.println("Continue after the catch block"); What would be the printout if the line int value = 50; is changed to int value =30; 3 / 6
10. What is displayed when the following program is run? class Test { try { System.out.println("Welcome to Java"); int[] a = new int[10]; a[0] = 1; System.out.println("Welcome to HTML"); finally { System.out.println("The finally clause is executed"); 4 / 6
Section III: Complete the program 11. Write programs to perform add, subtract, multiply, and divide operations. (See the following figure.) The user enters two numbers in the text fields, Number1 and Number 2, and display the result in Result text field when the Add, Subtract, Multiply or Divide button is clicked. If Number 1 or Number 2 were not an integer, the program would throw a NumberFormatException. If Number 2 were zero and the user press the Divide button, the program would throw an ArithmeticException (DivideByZeroException). Display the exception in a message dialog box, as shown in following figure. (35%) 12. Write a program to read from the file Text.txt that replaces a string in a text file with a new string and saves the change into a new file New.txt. Your program also needs to prompt the user to enter two string, old string and new string. (20%) 5 / 6
13. Design a class named MyPoint to represent a point with x and y-coordinates. The class contains: (25%) Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point (0, 0). A constructor that constructs a point with specified coordinates. Two get methods for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint type. A method named distance that returns the distance from this point to another point with specified x and y-coordinates. Implement the class and write a test program that creates two points (0, 0) and (10, 30.5) and displays the distance between them. 000 6 / 6