Final Examination Semester 3 / Year 2012 COURSE COURSE CODE TIME DEPARTMENT LECTURER

: JAVA PROGRAMMING : PROG1114 : 2 1/2 HOURS : COMPUTER SCIENCE : SO YONG QUAY

Student’s ID: Batch No:

Notes to candidates: 1) The question paper consists of 8 pages and 18 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 softcopy.

JAVA PROGRAMMING Section I: Multiple Choices Questions

(10 X 2 marks = 20 marks)

1. Analyze the following code: public class Test { public static void main(String args[]) { Test nc = new Test(); nc.t = nc.t++; } int t; Test() { } } a. b. c. d. e.

The program has a compilation error because t is not initialized. The program does not compile because the parameter list of the main method is wrong. The program compiles, but has a runtime error because t has no initial value. The program has a compilation error because you attempt to create an object of the Test inside the Test class. The program compiles and runs fine.

2. What is the output of the following statements? String s1 = new String (“Southern University College”); String s2 = “southern university college”; System.out.println(s1.compareTo(s2)); a. b. c. d. e.

true false 0 1 -1

3. An object has a _________ and ___________. a. b. c. d. e.

state, attribute behavior, method state, behavior method, class attribute, class

1/8

JAVA PROGRAMMING 4. Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect? a. b. c. d. e.

String s3 = s1 - s2; int i = s1.compareTo(s2); char c = s1[0]; char c = s1.charAt(s1.length() - 1); a and c.

5. How do you explicitly invoke a superclass’s constructor from a subclass? a. b. c. d. e.

super(); super(args); super.method(); super.method(args); a and b.

6. What modifier should you use so that a class in a different package cannot access the class, but its subclasses in any package can access it? a. b. c. d. e.

public private protected default none of above

7. Which class is the root of the Java GUI component classes? a. b. c. d. e.

java.awt.Component java.Component JFrame JApplet JWindow

8. Suppose you want to display a text in multi line, which component should you use? a. b. c. d. e.

JButton JLabel JTextField JTextArea JMultiText

2/8

JAVA PROGRAMMING 9. A subclass inherits _____________ from its superclass. a. b. c. d. e.

private method protected method public method a and c b and c

10. Which method can be used to create an input object for file temp.txt? a. b. c. d. e.

new Scanner("temp.txt") new Scanner(temp.txt) new Scanner(new File("temp.txt")) new Scanner(File("temp.txt")) new Scanner(File(temp.txt)

Section II: Show the output of the program 11. The java.util.Date class implements java.lang.Cloneable and overrides the equals method to return true if two objects have the same date and time. Show the output of the following code. (4 marks) import java.util.*; public class Test extends Object { public static void main(String[] args) { Date d1 = new Date(); Date d2 = new Date(349324); Date d3 = d1; System.out.println("(1) " + (d1 == d2)); System.out.println("(2) " + (d1 == d3)); System.out.println("(3) " + d1.equals(d2)); System.out.println("(4) " + d1.equals(d3)); } } 12. Show the output of the following program. public class Finalexam201203 { public static void main(String[] args){ try{ int value = 80; if (value < 100) throw new Exception("value is too small"); 3/8

(4 marks)

JAVA PROGRAMMING else throw new Exception("value is too big"); } catch (Exception ex){ System.out.println(ex.getMessage()); } System.out.println("Continue after the catch block"); } }

13. What is the output produced by the following program?

(4 marks)

public class FinalExam201103 { public static void main(String[] args){ for (int i = 0; i < 2; i++) { System.out.print(i + " "); try { System.out.println( 1 / 0); } catch (Exception ex) { System.out.print("Error division"); } } } }

14. What is the output produced by the following program? public class TestFinal201203 { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } catch (Exception ex) { System.out.println("Exception in main"); } }

4/8

(4 marks)

JAVA PROGRAMMING static void method() throws Exception { try { String s ="abc"; System.out.println(s.charAt(3)); } catch (RuntimeException ex) { System.out.println("RuntimeException in method()"); } catch (Exception ex) { System.out.println("Exception in method()"); } } }

15. What is the output of the following program?

(4 marks)

public class TestFinal201203 { public static void main(String[] args) { try { int[] list = new int[10]; System.out.println("list[10] is " + list[10]); } catch (ArithmeticException ex) { System.out.println("ArithmeticException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } finally { System.out.println ("There is an error in thi program"); } } }

16. Suppose the s1, s2, and s3 are three strings, given as follows: String s1 = “Welcome to Southern University College”; String s2 = s1; String s3 = new String (“Welcome to Southern College”);

5/8

JAVA PROGRAMMING What are the results of the following expressions? a) b) c) d) e)

s1.length(); s2.substring (4,15) s3.charAt(11); s1.indexOf(“sity”) s2.compareTo(s3);

(2 marks) (2 marks) (2 marks) (2 marks) (2 marks)

6/8

JAVA PROGRAMMING

Section III: Complete the program

17. Write a program to create a file named Final201203.txt, if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read the data back from the file and display the sorted data. (15 marks)

18. SU System Sales Company would like to provide a simple calculation system to calculate the total amount of computer notebook purchased by customers. Write a program that let the staff to select the notebook model from combo box and show the unit price (Figure 17.1), then prompts the staff to enter the quantity amount. After the staff clicks the “Calculate Total Amount” button, shows the total amount and detail information, as shown in Figure 17.2. Your program also needs to check the Number Format errors for each text fields (Figure 17.3). Assume that computer notebook model is store in one-dimensional array, notebook model and unit price are stored in a two-dimensional array, as shown in figure 17.4 and figure 17.5. (35 marks)

A sample input is as shown.

Figure 17.1 A sample output is as shown.

Figure 17.2

7/8

JAVA PROGRAMMING

Figure 17.3 Computer Model HP NB Dell NB Samsung NB Sony NB Asus NB Acer NB Toshiba NB Figure 17.4

Computer Price HP NB 2250 Dell NB 1500 Samsung NB 2600 Sony NB 2550 Asus NB 2750 Acer NB 1450 Toshiba NB 2350 Figure 17.5

The program should include the following methods:  int getPrice(int codeNo, String computerPrice[][]) : return the price of the computer model based on the item code passed in  String getItem(int codecodeNo, String computerModel[]) : return the computer model based on the item code passed in  void displayTotal(int codeNo, int quantity) : Display the item details and total amount based on the item code and quantity passed in

________000_________

8/8