Part 1. Multiple Choice Questions (15 marks) 1. Which of the following contains instructions on how to initialize objects of a class? a. method c. Constructor b. function d. Module 2. Which of the following is used to for linking a Java class to an interface? a. Extends c. Throws b. inherits d. Implements 3. Which of the following is used to refer to the parent class of the class in which it appears? a. final c. Super b. this d. Static 4. One of the following is included in Java SDK: a. document generator c. NetBeans b. Java IDE d. All of the above 5. Which of the following is used to define a data field that is shared by all instances of a class? a. public c. static b. final d. Protected 6. Which of the following is the concept that Java relies on for providing its input/output facilities? a. Import c. Variables b. Packages d. Streams 7. Which of the following happens when subclasses modify the methods they inherit from a superclass. a. Inheritance c. Extension b. Overriding d. Overloading 8. To which of the following Java packages does the class HashMap belong? a. java.lang c. java.swing b. java.io d. java.util 9. Which of the following is used for defining groups of classes related by inheritance? a. packages c. abstract classes b. interfaces d. None of the above 10. Which of these data type requires the most amount of memory? a. double c. short b. float d. int 2
Part2. Essay: Answer the following questions. (30 marks) 1. Briefly explain the meaning of the following characteristics of Java language: - Threaded. - Dynamic. 2. Briefly describe the concepts of objects and classes in java. 3. Briefly describe the difference between primitive and reference variables. 4. Briefly describe the concepts of overriding and overloading in Java. 5. List one difference between checked exception and unchecked ones. 6. Name clash happens in Java when importing two packages to a java program, and these packages have two classes with the same name. Briefly explain two methods to solve a name clash problem. Give examples. 3
Part 3. Problem Solving: Answer the following questions (55 marks) 1. Develop 3 classes Shape, Circle, and Rectangle according to the following specifications: (20 marks) The class hierarchy: The Shape class has: a public instance variables, String color. a private class variable noofsahpes to store the number of created Shape objects. a one-argument constructor that sets the values of color to a given value, and increments noofshapes by 1. a static method getnoofshapes() that returns the number of created Shape objects. The Rectangle class has: two public instance variables int height and int width. a three-argument constructor that sets the values of color, height and width to given values. The Circle class has: a public instance variable, int radius. a two-argument constructor that sets the values of color and radius to given values. a public method tostring() that returns a string consisting of the circle s color and radius; for example, Color: Red, Radius: 10. 2. Re-write the following code using StringBuffer instead of String, and for instead of while: (10 marks) String[] names = new String[5]; int i = 0; while(i < names.length){ names[i] = ""; i++; 4
3. Write Java code to copy the first line of text from the file c:\input.txt and to the file c:\output.txt. Use the BufferedReader class to read the input, and the PrintWriter class to write the output. (10 marks) 4. Consider the following code and then answer the questions about it. (15 marks) public class CarChoice { public static void main(string[] args) { Scanner in = new Scanner(System.in); HashMap<Integer, String> carlist; carlist = new HashMap<Integer, String>(); carlist.put(1, "Toyota"); carlist.put(2, "Ford"); carlist.put(3, "Honda"); for (Integer idx : carlist.keyset()) System.out.println(idx+"-"+carList.get(idx)); System.out.println("Which brand would you prefer?"+ "(enter a number)"); try{ int br = in.nextint(); if (carlist.containskey(br)) System.out.println(carList.get(br) + " is a nice brand."); else System.out.println("Wrong brand number!"); catch(inputmismatchexception e){ System.out.println("Error! Not a number!"); (a) What will be displayed on the screen once the code is executed? (b) What will be the output if the user enters the following values: i - 1 ii - 4 iii - e Note: The method containskey()in the code above returns true if the HashMap contains a mapping for the specified key, and false otherwise. (c) Give an example from the code about the use of defensive programming, and another example about exception handling. 5
MULTIPLE CHOICE 1. ANS: C PTS: 2 KEY: Unit 1 & 3 MSC: MTA - Spring 2011 2. ANS: D 3. ANS: C 4. ANS: A 5. ANS: C 6. ANS: D 7. ANS: B 8. ANS: D 9. ANS: C 10. ANS: A ESSAY 1. ANS: Threaded: allow a program to do several things at once. Dynamic: Java programs can adapt to changes in their environment even while the program is running. 2. ANS: Objects in Java are defined by means of classes. A class is much like a template that defines the structure of: 1.the stored data associated with an object and 2.the program code that is executed when particular methods are invoked. 3. ANS: 1.Primitive variables store data values. e.g. int, float, etc 2.Reference variables do not themselves store data values, but are used to refer to objects. e.g. String. 4. ANS: * Overriding means that subclasses modifies the methods they inherit from a superclass. The method signature must stay the same. * Overloading means that a class can have more than one method of the same name, but with different signature. Overloading is not directly related to inheritance 5. ANS: Checked exceptions: - Programmers MUST include code to declare or handle any checked exceptions that might occur. - The Java compiler will report an error if this has not happened. Unchecked exceptions: - These exceptions need not be caught or declared. - Java contains facilities for catching the exceptions that can occur during the running of a program. They are normally due to programming errors. 6. ANS: Example of a name class: import java.util.*; import java.sql.*;... Date d1 = new Date(DATE_IN); //Date is ambiguous both libraries have a class with the name Date and thus we get an ambiguous name. Solution (1): to use the fully qualified name of the Date class (i.e. Method1 above) java.sql.date d1 = new java.sql.date(date_in); 6
Solution (2): to import only those classes you actually use, for example: import java.util.arraylist; import java.sql.* PROBLEM 1. ANS: public class Shape { public String color; private static int noofshapes; public Shape(String clr) { color = clr; noofshapes++; public static int getnoofshapes() { return noofshapes; public class Rectangle extends Shape{ public int height, width; public Rectangle(String clr, int h, int w) { super(clr); height = h; width = w; public class Circle extends Shape{ public int radius; public Circle(String clr, int rad) { super(clr); radius = rad; public String tostring(){ return "Color: " + color + ", Radius: " + radius; 2. ANS: StringBuffer[] names = new StringBuffer[5]; for(int i = 0; i < names.length; i++){ names[i] = new StringBuffer(""); 3. ANS: BufferedReader in = new BufferedReader(new FileReader("c:/input.txt")); PrintWriter out = new PrintWriter(new File("c:/output.txt")); out.write(in.readline()); out.close(); in.close(); 7
4. ANS: (a) 1 - Toyota 2 - Ford 3 - Honda Which brand would you prefer? (enter a number) (b) i) Toyota is a nice brand. ii) Wrong brand number! iii) Error! This is not a number! (c) Example of defensive programming: if..else.. statement is used to check that the use entered a correct number. Example of exception handling: try..catch.. statement is used to check that the user entered numeric value. 8