sample final exam cse 1020 winter 2014

10 Pages
185 Views
Unlock Document

Department
Electrical Engineering and Computer Science
Course
EECS 1520
Professor
All Professors
Semester
Winter

Description
1 (8 marks) (a) A static method is invoked on a(n) .................. Answer: class, or class name Marking scheme: 4 marks for a correct answer, 0 marks otherwise. (b) A non-static method is invoked on a(n) .................. Answer: object, or instance, or reference Marking scheme: 4 marks for a correct answer, 0 marks otherwise. 2 (20 marks) (a) What is the state of an object? Answer: The values of all of its attributes. Marking scheme: 4 marks for a correct answer, 0 marks otherwise. (b) What is the identity of an object? Answer: Its address, or some other unique indentifier. Marking scheme: 4 marks for a correct answer, 0 marks otherwise. (c) What does the binary operator == check? Answer: Equality of identity, or equality of value. Marking scheme: 4 marks for a correct answer, 0 marks otherwise. (d) What does the method equals check? Answer: Equality of state. Marking scheme: 4 marks for a correct answer, 0 marks otherwise. (e) Consider the following fragment of Java code: 1 Fraction f1 = new Fraction(1, 2); 2 Fraction f2 = new Fraction(3, 4); 3 Fraction f3 = f1; Circle the expressions below that evaluate to true: f1 == f1 f1 == f2 f2 == f3 f1 == f3 f1.equals(f1) f1.equals(f2) f2.equals(f3) f1.equals(f3) Answers: Underlined above. Marking scheme: 1 mark for each correct answer, −1 for each incorrect answer. Lowest possible total mark is 0. 1 3 (8 marks) What are the two differences between a Set and a List? Answer: No duplicate elements allowed (Set) versus duplicate elements allowed (List). Unordered (Set) versus sequential order (List); or only iterator-based access (Set) versus indexed-based access (List). Marking scheme: 4 marks for each of the two differences. 4 (28 marks) Consider the following fragment of (legal) Java code: 1 Number num = new BigInteger("123456789123456789"); 2 double val = num.doubleValue(); 3 num = ((BigInteger) num).subtract(new BigInteger("1")); BigInteger extends Number. The method doubleValue() is polymorphic. The cast on line 3 is necessary for the code to compile. (a) What class or classes are searched during early binding for the method doubleValue()? Justify your answer. Answer: The declared type of num is Number; thus, early binding searches Number. Marking scheme: – 2 marks for mentioning the declared type – 2 marks for the correct class (Number). (b) What class or classes are searched during early binding for the method subtract(BigInteger)? Justify your answer. Answer: The declared type of num is Number, which is cast to BigInteger for the invocation; thus, early binding searches BigInteger. Marking scheme: – 2 marks for mentioning the declared type – 4 marks for mentioning the cast – 2 marks for the correct class (BigInteger). (c) What class or classes are searched during late binding for the method doubleValue()? Justify your answer. Answer: The actual (or runtime) type of num is BigInteger; thus, late binding searches BigInteger. Marking scheme: 2 – 6 marks for mentioning “actual type” – 6 marks for identifying the actual type as BigInteger – 4 marks for the correct class (BigInteger). 5 (4 marks) Explain how composition differs from aggregation. Answer: Composition is a stronger form of aggregation where the lifetime of the object and its aggregated objects are the same. Alternative answer: In a composition, the whole cannot exist without its parts. In an aggregation, the whole and the parts can exist without each other. Marking scheme: 4 marks for a correct answer. 6 (28 marks) Consider the following UML diagram. Exception RuntimeException IndexOutOfBoundsException ArrayIndexOutOfBoundsException (a) Consider the following main method. 1 public static void main(String[] args) 2 { 3 PrintStream output = System.out; 4 5 try 6 { 7 output.println(args[-1]); 3 8 ... 9 } 10 catch (ArrayIndexOutOfBoundsException e) 11 { 12 output.println("Invalid array index is used"); 13 } 14 catch (Exception e) 15 { 16 output.println("Something went wrong!"); 17 } 18 } The above main method produces the following output. Invalid array index is used Explain what happens when the code is run. In your explanation, use the following phrases if they are relevant (not all may be relevant): – throw(s) an exception – terminate(s) immediately – determine(s) the appropriate – substitutable/substitutability Answer: On line 7, args[-1] throws an exception of type ArrayIndexOutOfBoundsException. Execution of the try block terminates immediately. The virtual machine determines that the appropriate exception handler is catch (ArrayIndexOutOfBoundsException e) on line 10. Program flow goes to the handler which produces the given output. Marking scheme: – 4 marks for correctly using “throws an exception” (or its equivalent) – 4 marks for correctly using “terminates immediately” (or its equiva- lent) – 4 marks for correctly using “determines the appropriate” (or its equivalent) (b) Consider the following main method. 1 public static void main(String[] args) 2 { 3 PrintStream output = System.out; 4 5 try 6 { 7 output.println(args[-1]); 4 8 ... 9 } 10 catch (IndexOutOfBoundsException e) 11 { 12 output.println("Invalid index is used"); 13 } 14 catch (Exception e) 15 { 16 output.println("Something went wrong!"); 17 } 18 } The above main method produces the following output. Invalid index is used Explain what happens when the code is run. In your explanation, use the following phrases if they are relevant (not all may be relevant): – throw(s) an exception – terminate(s) immediately – determine(s) the appropriate – substitutable/substitutability Answer: On line 7, args[-1] throws an exception of type ArrayIndexOutOfBoundsException. Execution of the try block terminates immediately. The virtual machine determines that the appropriate exception handler is catch (IndexOutOfBoundsException e) on line 10 because ArrayIndexOutOfBoundsException is substitutable for IndexOutOfBoundsException. Programflow goes to the handler which produces the given output. Marking scheme: – 4 marks for correctly using “throws an exception” (or its equivalent) – 4 marks for correctly using “terminates immediately” (or its equiva- lent) – 4 marks for correctly using “determines the appropriate” (or its equivalent) – 4 marks for correctly using “substitutable” (or its equivalent) 7 (14 marks) Consider the following UML diagram. String
More Less

Related notes for EECS 1520

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit