CSCI 3134 summer 2012 Test #2 p.1 Test time: 90 minutes (max, no extension) Your name: Score: / 60. Note: Try to provide precise answers. Budget your time over the questions! Your writing should be easy to read. When possible, try to print your answers. You may use the back of the page as scratch paper or to write your answers. Using a calculator is allowed. Sharing of a calculator (or anything else) is NOT allowed. If possible, leave an empty seat between you and your neighbor(s). A. (25 pts) Fill in the blanks (1 point per question). Note: Each answer may consist of multiple words. 1) Every year or two, the capacities of computers have approximately doubled without any increase in price. This is called the ________________________________. 2) A machine language is ________________________________, meaning that a machine language can be used on only one type of computer. 3) When declaring a method, the keyword that indicates that the method does not return a value is ________________________________. 4) The set of public methods defined in a class is called that class’s . 5) After the following Java statements execute, the value of the variable result is . int a, b, c, result; a = 100; b = 52; c = 32; result = a > b ? a-b : c-b; 6) All the objects of a class share one copy of the class’s 7) Constant variables are declared with keyword fields. ; they must be initialized before they are used and cannot be modified thereafter. 8) All objects in Java have a special method named that returns a String representation of the object’s contents. 9) Suppose class A has a static method called method1( ), which takes no parameters and returns a boolean value. Suppose class B is a client of class A. How would a method in class B call class A’s method1? Answer: 10) When defining a class, it is possible to have several methods with the same name that each operates on different types or numbers of arguments. This feature is called . 11) Given a class A, the protected members in A can be accessed by members of class A, by members of A’s and, by members of other classes in the same package as A. CSCI 3134 summer 2012 12) The principle of Test #2 p.2 recommends that instance variables of a Java class should be declared as private, because the actual data representation within the class is of no concern to its clients. 13) How would you interpret the following UML diagram? ClassA ClassA ClassB. ClassB 14) Suppose a class called ABC has only one instance variable called abcDef, which is of type String. Show how the set method for this instance variable would be defined. 15) A program called FileProcessing has a main method declared as follows: public static void main (String args[ ] ). A sample command to run the program is java FileProcessing file1.dat file2.dat. How would the first parameter in the command line, file1.dat, be referenced within the main method? Answer: 16) A Java applet is classified as a client-side technology because the Java code of the applet is executed in the Java runtime environment of the . 17) To handle an exception, place any code that might throw an exception in a statement. 18) When a program is executed, array element indices are checked for validity — all indices must be greater than or equal to 0 and less than the length of the array. If an attempt is made to use an invalid index to access an element, an exception called occurs. 19) Abstract methods do not provide 20) Constructors and . methods cannot be declared abstract. 21) The primary purpose of an abstract class is to provide an appropriate superclass from which other classes can inherit and thus share a common . 22) Suppose class A has a subclass called class B. Both A and B have got a private attribute named xyz, along with the set and get methods for that attribute. Within a method in class B, how would it reference attribute xyz defined in A? Answer: 23) Java views each file as a sequential stream of 24) Java provides a mechanism called written to or read from a stream. . that enables entire objects to be CSCI 3134 summer 2012 Test #2 p.3 25) Suppose a method in class A is defined as follows. How would method1 be accessed? Answer: The method can be accessed by (a) other methods of class A, and (b) methods in the float method1( float value) { return value * value; } B. (5 pts) Draw a UML class diagram to show the design of ClassA. public class ClassA { private String mesg = "hello"; public void method1(int value) { if (value > 0) { System.out.printf("\nmesg: %s", mesg); } else { System.out.printf("\nmesg: %s", mesg + value); } } public double method1(double score) { return score * score; } } of class A. CSCI 3134 summer 2012 Test #2 C. Program Tracing 1) (5 pts) Given the definitions of ClassA and ClassB, show the screen output. public class ClassA { String mesg = "hello"; public void method1(int value) { if (value > 0) System.out.printf("\nmesg: %s", mesg); else System.out.printf("\nmesg: %s", mesg+value); } } //classA public class ClassB extends ClassA { String mesg = "hi"; public void method1(int score) { if (score > 50) { System.out.printf("\nmesg: %s", mesg); } else { System.out.printf("\nmesg: %s", mesg + score); } } public static void main(String args[]) { ClassA objA = new ClassA(); objA.method1(50); ClassA objA2 = new ClassB(); objA2.method1(50); ClassB objB = new ClassB(); objB.method1(50); } } //classB Screen Output: p.4 CSCI 3134 summer 2012 Test #2 2) (5 pts) Given the definitions of ClassA and ClassB, show the screen output. public class ClassA { String mesg = "hello"; public void method1(int value) { if (value > 0) { System.out.printf("\nmesg: %s", mesg); } else { System.out.printf("\nmesg: %s", mesg + value); } } public void method1(double score) { System.out.printf("\nscore is: %s", score > 0 ? mesg+score : "negative"); } } public class ClassB extends ClassA { String mesg = "hi"; public void method1(int score) { if (score > 50) { System.out.printf("\nmesg: %s", mesg); } else { System.out.printf("\nmesg: %s", mesg + score); } } public static void main(String args[]) { ClassA objA = new ClassA(); objA.method1(50.55); ClassA objA2 = new ClassB(); objA2.method1(50.55); ClassB objB = new ClassB(); objB.method1(50.55); } } Screen Output: p.5 CSCI 3134 summer 2012 Test #2 p.6 D. (5 pts) The following Java application contains errors. Point out the statement(s) that contain errors. Explain what each of the errors is, and how it can be fixed. public class OOPExercises { public static void main(String[] args) { A objA = new A(); System.out.println("in main(): "); System.out.println("objA.a = "+getA( ) ); objA.a = 222; } } Point out the error(s) and how they can be fixed. public class A { int a = 100; public void setA( int value) { a = value; } public int getA() { return a; } } //class A E. (5 pts) The UML diagram shown below represents the design of the Employee class. It contains a reflexive association called superior/subordinate. Write down your interpretation of that association in this class. Your interpretation: CSCI 3134 summer 2012 Test #2 F. (5 pts) Convert the UML diagram in E above into a Java class. Do NOT include any additional data attributes that are not shown in the diagram. Add proper get and set methods for the existing attributes. G. (5 pts) Show the screen output of the following program. public class ClassB { public void draw(int rows) { for (int r = 1; r <= rows; r+=2) { for (int c = 1; c <= r; c++) { System.out.printf("*"); } System.out.println(); } } public static void main(String args[]) { ClassB objB = new ClassB(); objB.draw(9); } } //class Screen output: p.7