Exam 3: FinalSolutions and Commentary
Part of
the homework for CS:2820, Spring 2017
|
Mean = 10.55 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X ____X_X___X_X_X_X_X_X_X_X_X_X_X_X_X_X___X___ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20
Mean = 20.55 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X ____X_X_______X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X___X___X_X_X_X_______X___ 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34. 36. 38
Mean = 21.57 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X __________X_X_X_________X___X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30
Mean = 20.98 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X ____________X___X___X___X_X_X___X_X_X_X_X_X_X_X_X___X_X_X_X_X___ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30
Mean = 62.58 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X __X___X_X_______X___X_X_X___X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X___X___X___X__ 16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60. 64. 68. 72. 76. 80. 84. 88. 92 Grades |- - D D + +|- - C C + +|- - B B B + +|- - A A + +
Simulation.schedule( time, // 1 (float t)-> System.out( "A" ) // 2 ); // 3 Simulation.schedule( time + 1, // 4 (float t)-> Simulation.schedule( t + 1, // 5 (float t)-> System.out( "B" ) // 6 ) // 7 ); // 8 Simulation.schedule( time + 1, // 9 (float t)-> Simulation.schedule( time + 1, // 10 (float t)-> System.out( "C" ) // 11 ) // 12 ); // 13
a) What is the order of the output of the letters A, B and C? If multiple orders are possible, give them all. (1 point)
___ACB________________________________________________________________
About 1/4 got this. For half credit, just under 1/2 suggested ACB or ABC. Among those earning no credit, almost 1/8 gave either ABC or CBA (neither of which is a possible output from the above code), while almost 1/16 suggested strings of 5 letters such as ABCBA.
b) Which line or lines above forces the variable time to be final or effectively final? Give the line number(s) from the comment on that line. (1 point)
___10_________________________________________________________________
Over 1/4 got this right, and about 1/16 listed line 10 along with one or two other lines. Almost 1/4 listed 3 or 4 lines in addition to line 10, while over 1/8 earned no credit, mostly listing numerous lines other than line 10. The most popular line listed incorrectly was line 1, listed by over 1/4, while lines 4 and 9 were listed by 1/4 each.
c) How many events does the code above schedule at the time the method that contains it is called? (1 point)
___3____(lines 1, 4 and 9)____________________________________________
Just over 1/4 gave this answer. Partial credit went to the 1/8 who said 2 or 4. No credit went to those suggesting 0 (few), 1 (over 1/4) or 5 (1/8).
d) How many events are scheduled at time t = time + 1? (1 point)
___3____(lines 4, 9 and 10)___________________________________________
Under 1/2 got this. Partial credit went to the 3/8 who said 2 and the 1/10 who said 4. About 1/4 earned no credit.
____no________________________________________________________________
3/8 earned full credit. Almost as many earned half credit for saying that it would break lambda expressions, but all this does is convert final variables to effectively final variables, and these remain legal in lambda expressions. About 1/4 earned no credit, many giving long and difficult to understand answers.
____NaN never equals anything, including NaN__________________________
Under 1/2 earned full credit. Half credit was offered to the 1/9 who suggested how to fix the problem instead of explaining it, or the 1/9 who explained that NaN was not a number without finishing the explanation.
The remainder earned no credit. One wrong answers was interesting: Over 1/8 thought, incorrectly that NaN was not a float but rather a Float, so the == operator was not applicable, since it compares object handles and not values. The problem is, NaN is a value of the primitive type float.
public class Key {} public class Lock <Obj> { public static LockException extends RuntimeException{} private Obj o; private Key k; public Lock( Obj oo, Key kk ) { o = oo; k = kk; } public Obj unlock( Key ku ) throws LockException { if (ku == k) return this.o else throw LockException; } }
a)
Lock <String> ls = new Lock <String> ( "Hello", new Key() );
Is there any way to recover the string locked into the variable ls?
If so, how?
(1 point)
____No________________________________________________________________ ______________________________________________________________________
Almost 3/8 got this. Over 1/4 got partial credit for suggesting that the value could be recovered by using the right key to unlock the object; the problem with this is that the given code throws away the key. Wrong answers were dominated by people suggesting changing the definition of class Lock, but the question was about the code given.
Some seem to have thought that the key was something a program could guess by trial and error, as if it was a cryptographic key. This is not the case! Objects in class Key have no properties other than their identity, and if Java ever lets you to try to forge the identity of an object, that is a flaw in the implementation and should be fixed in the next release.
b) If the k field of class Lock was public, how would your answer to part a) change? (1 point)
____ls.unlock(ls.k)___________________________________________________ ______________________________________________________________________
1/2 got this, mostly with the above code, although a few suggested changing the key to a known value. 1/4 got partial credit for saying it could be done, mostly with too many words and too little detail to understand what they were suggesting.
c) What do we prevent by defining class Lock to be final? (1 point)
____We prevent creation of subclasses_________________________________ ______________________________________________________________________
Under 3/8 got this. A few more had vague answers that were probably right but were insufficiently clear to earn full credit. Among those earning no credit, Almost 1/2 seemed to think that declaring a class to be final caused all of its fields to become final. This demonstrates that, despite the time spent on the use of final in lecture and in the textbook, the concept is difficult.
d) Which variables and parameters in the above are effectively final? (1 point)
____variables o and k____parameters oo, kk and ku_____________________ ______________________________________________________________________
Over 1/8 got this. Among those earning partial credit, about 3/16 suggested o but not k or visa versa, over 7/16 omitted any parameters, and an additional 3/15 did not notice ku. Almost 1/8 also listed classes or methods when asked for variables and parameters. Very few earned no credit.
e) If we mark fields o and k as final, how does this change the security of Lock objects. (1 point)
____There is no change________________________________________________ ______________________________________________________________________
1/4 got this. 1/2 earned half credit by saying that making the fields final prevents the code from changing them, but all the fields are already effectively final, which is to say, the code does not change them. Among wrong answers, 1/8 made it abundantly clear that they did not understand what a final variable is.
f) Over the past semester, we used code like, SomeClass<Vehicle>. Suppose, instead, we were to use SomeClass<Lock<Vehicle>>. Here, SomeClass is a generic class written by someone else, for example, LinkedList. What can we gain by wrapping our data in a lock? (1 point)
____Putting vehicles in locks prevents SomeClass from tampering_______ ____with them_________________________________________________________
1/8 did well. 3/8 earned over half credit for saying that locks preven tampering with vehicles without identifying the adversary the example defends against. An additional 1/8 had vague answers that might be on this track. The remainder mostly wrote answers that had no clear connection to the functionality of class Lock.
____Applying a method before all fields are initialized is dangerous__
Over 1/2 got this right, and a few earned partial credit for vague answers or for confusing fields with parameters. 3/8 had answers that focused on irrelevant issues.
class t { public interface A { int op( int a, int b ); } public interface B { int op( int a ); } static void x( A p ) { System.out.println( "x1()=" + p.op( 1, 2 ) ); } static void x( B p ) { System.out.println( "x2()=" + p.op( 1 ) ); } public static void main( String [] args ) { x( (int a) -> -a ); x( (int a, int b) -> a + b ); } }
a) Method x is overloaded. How does the compiler select between the two versions of x? (1 point)
____By the type of the parameter to X, which is distinguished_________ ____by the number and type of parameters in the lambda expression_____
1/4 did well. Another 1/4 earned half credit by stating that the number of parameters is used (omitting types and not noting that both versions of x have exactly one parameter). 1/16 were penalized for using the term inputs instead of parameters, and another 1/16 earned a bit of credit for merely saying parameters without saying what about them matters.
b) What output would you expect this program to produce? (1 point)
___x2()=-1____________________________________________________________ ___x1()=3_____________________________________________________________
Over 1/2 did perfectly. 1/10 were penalized for omitting the numeric values, 1/16 omitted the minus sign on the first line, and 1/16 omitted the strings before the numbers on each line. Almost 1/8 transposed the two lines, and 1/20 omitted just the number 3.
Among those earning no credit, a significant number reported that the code contained bugs that would prevent it from working. In fact, this is a complete working Java program (as the introduction to the problem asserts).
class A { // 1 static int i = 5; // 2 int j = 10; // 3 static class B { // 4 int bi = i + 1; // 5 } // 6 Class C { // 7 int ci = j + 2; // 8 } // 9 B b = new B(); // 10 C c = new C(); // 11 } // 12a) The default constructor for class A directly executes some assignments above. Which? (1 point)
____lines 3, 10 and 11________________________________________________
1/10 did well. 3/8 got just line 3, omitting 10 and 11, while 1/8 omitted line 3 but earned some credit. 1/16 omitted 10 but included 11; these apparently thought that the fact that class B is static changed the time at which the instance was created. This drives home the fact that some students still have difficulty understanding the meaning of the keyword static. Almost 3/16 earned no credit.
Among the lines included incorrectly, 7/16 thought line 2 should be executed by the default constructor for class A. See the next part for additional discussion of this.
1/16 thought line 8 should be executed by the default constructor for class A. In fact, this is executeed by the default constructor for class C.
b) Some assignments above are executed before the main method is even called. Which? (1 point)
____Line 2____________________________________________________________
Only 1/16 got this. 3/16 were penalized for also listing class B (it is not an assignment), and almost as many also listed line 5 (bi is a non-static field of a static class). 3/16 listed lines 2 and 3 (most of these also listed lines 2 and 3 in part a).
The difficulties here again emphasize that many students still have difficulty understanding the keyword static.
c) A constructor used in this code has an implicit parameter. Which one, what parameter? (1 point)
____The call to C() on line 11 takes this as an implicit parameter____
3/16 did well. 1/10 forgot to say C while giving this or forgot this while saying C. 1/4 said (for a small penalty) that the implicit parameter was j, not this. The constructor uses this to locate j, but as demonstrated in class, the compiler complains about this when you make a mistake here.
d) Instances of class C hold copies of outer variables such as j. When are the copies made? (1 point)
____In the constructor for C when it is called________________________
5/8 did well, and there was little partial credit. This part of the problem was included as a hint to help with the previous part, but in many cases, the hint doesn't seem to have helped.
____pq_pAq_pBBq_pABCAABBAq_pAAAAAAAAAAq_bd____________________________
1/4 did well. 3/16 gave just one example. 1/16 left it blank, and 3/16 gave answers with no connection to the given regular expression.
Among those earning partial credit, 3/16 had problems with operator predicence in the regular expression, thinking that the | operator had a high precidence, connecting just q|b or q|bd instead of dividing the entire regular expression into two halves. 1/16 had trouble with the * operator, either seeing it as a wildcard or as applying to the following instead of previous character. Finally, a whopping 5/8 did not notice the p and q bracketing [ABC]*.
Many students seemed unprepared for this question. Perhaps it is not, strictly speaking, object oriented programming, but regular expressions were covered in lectures and a homework assignment, and anything covered in the homework was explicitly mentioned as being fair game for an exam question.