CSE250, Spring 2022. Plain-Text Version of Assignment 3 and its key. Note: you had options ABCD in random orders. Here are the answers without the letters. 1. Legal, and println(r.toString) would now say "3.0 x 7.0" 2. Compile-time error, because r was declared val. 3. Compile-time error, because the Rectangle fields are immutable. 4. Rectangle r2 is now 2.0 x 3.0 5. Same answer as Q4: it prints Rectangle r2 is now 2.0 x 3.0 6. Compile-time error: The variable ell is a val so it cannot be reasisigned. 7. Compile-time error: The modification ell ::= "Declaration: " is treated as if it were ell = "Declaration: " :: ell, but that is illegal because ell is a val. 8. Legal: Indexing is OK with lists, so word gets "course" 9. Runtime error: exception raised---ell(4) is an index out of bounds exception, just like for an Array 10. Compile-time error: This is a List[String], and String is an immutable type 11. Legal and the test is false: As in all of Java, Python, and JavaScript, arr1 == arr2 will only compare the references, which are unequal because these are different objects. 12. Legal and the comparison gives true: The .toList calls convert them to value objects, which can be compaed for equality. 13. The condition is true: Unlike with Lists, equality of Sets does not depend on the order. 14. O(n^2) --- more precisely, Theta(n^2) since it always takes that long. Reverse of a singly linked list takes Theta(n) time and that gets multiplied by n again from the loop 15. O(n) --- more precisely Theta(n) since the loop always takes that time. Now the O(n) time for the loop gets multiplied by O(1) time for reverse implemented that way. The questions, with the letters I used before randomizing, were: 1. Suppose we have class Rectangle(var length: Double, var width: Double) { override def toString = s"$length x $width" } and somewhere else val r = new Rectangle(6.0,7.0) Then the statement r.length = 3.0 is: Choose One Answer A. A compile-time error, because r was declared using val. B. A compile-time error, because Rectangle is an immutable class C. A run-time error, because the length field cannot be changed D. Legal, and println(r.toString) would now say "3.0 x 7.0" 2. Multiple Choice: (2) Val and Var: Suppose we have class Rectangle(var length: Double, var width: Double) { override def toString = s"$length x $width" } (same as in question 1), but now suppose after defining the class the same way we have the statements val r = new Rectangle(6.0,7.0) and later r = new Rectangle(3.0,7.0) This is: A. Compile-time error, because r was declared val. B. Run-time error, because the length field cannot be changed.. C. Compile-time error, because Rectangle is an immutable class. D. Legal, and r.toString will now print out as "3.0 x 7.0" 3. Multiple Choice: (3) Val and Var: Now suppose the class were instead declared as class Rectangle(val length: Double, val width: Double) and we have the statements var r = new Rectangle(1.0,2.0) r.length = 2.0 What happens now? Choose One Answer A. Compile-time error, because the Rectangle fields are immutable. B. Compiles and runs OK, because r is a var. C. Not an error, because Rectangle is not a case class. D. Run-time error, because length can only == width if it's a Square. 4. Multiple Choice: (4) Values and References: Using the class Rectangle(var length: Double, var width: Double) with toString method from questions 1 & 2 (not 3), what happens with the following? var r1 = new Rectangle(2.0,5.0) var r2 = r1 r1.width = 3.0 println("Rectangle r2 is now " + r2.toString) Choose One Answer A. Rectangle r2 is now 2.0 x 3.0 B. Rectangle r2 is now 2.0 x 5.0 because only r1 got changed C. The code won't compile because no setter method was defined for width. D. The code won't compile because it needs r2 to be declared as a Rectangle. 5. Multiple Choice: (5) Values and References: Using the class Rectangle(var length: Double, var width: Double) with toString method from questions 1 & 2 (not 3), what happens with the following? val r1 = new Rectangle(2.0,5.0) val r2 = r1 r1.width = 3.0 println("Rectangle r2 is now " + r2.toString) (The only change from Q4 is that r1 and r2 are now declared val..) Choose One Answer A. Same answer as Q4: it prints Rectangle r2 is now 2.0 x 3.0 B. Same answer as Q4: rectangle r2 is unaffected so it still prints 2.0 x 5.0. C. Compile-time error because r1 is val. D. Different answer from Q4: rectangle r2 is unaffected because, being val, Scala made it a separate copy from r1. 6. Multiple Choice: (6) Lists (and val and var): The next five questions are shorter ones (2 pts. each) with the same options: (A) Compile-time error, (B) Run-time error, or (C) Legal. Option (D) is not used. They all involve the declaration val ell = List("When", "in", "the", "course") followed by the line of code the question is about. Here, does the following line not compile, compile but give an error when run, or run fine? ell = List("of", "human", "events") Choose One Answer A. Compile-time error B. Run-time error (exception raised) C. Legal 7. Multiple Choice: (7) Lists (and val and var): This time val ell = List("When", "in"...Click for more optionsPoints:2 Question This time val ell = List("When", "in", "the", "course") is followed by ell ::= "Declaration: " Choose One Answer A. Compile-time error B. Run-time error (exception raised) C. Legal 8. Multiple Choice: (8) Lists (and val and var): This time val ell = List("When", "in", "the", "course") is followed by var word = ell(3) Choose One Answer A. Compile-time error B. Run-time error (exception raised) C. Legal 9. Multiple Choice: (9) Lists (and val and var): This time val ell = List("When", "in", "the", "course") is followed by var word = ell(4) Answer A. Compile-time error B. Run-time error (exception raised) C. Legal 10. Multiple Choice: (10) Lists (and val and var): This time val ell = List("When", "in", "the", "course") is followed by ell(3)(2) = 'a' //trying to change "course" to "coarse" Choose One Answer A. Compile-time error B. Run-time error C. Legal 11. Multiple Choice: (11) Arrays: Suppose we initialize two arrays to the same String values: var arr1 = Array("one","two","three") var arr2 = Array("one","two","three") We then write if (arr1 == arr2) { ... What happens? Choose One Answer A. Compile-time error B. Legal and the test is true C. Legal and the test is false D. Legal but the result is indeterminate because the arrays could be in any order. 12. Multiple Choice: (12) Containers: Again, we initialize two arrays to the same String values: var arr1 = Array("one","two","three") var arr2 = Array("one","two","three") Now we write if (arr1.toList == arr2.toList) { ... What happens? Choose One Answer A. Compile-time error B. Legal and here the comparison gives true. C. Legal and here the comparison gives false. D. Not legal because Lists can't be compared. 13. Multiple Choice: (13) Containers: Now suppose the code is: val set1 = Set("one","two","three") val set2 = Set("three","two","one") if (set1 == set2) { ... What now? Choose One Answer A. The condition is true. B. Compile-time error because equality of sets is not a supported operation C. The condition is false because they are two distinct Set objects. D. The condition is false because sets can only be compared in order and the ordering of set1 and set2 is different. 14. Multiple Choice: (14) O-Notation Timing Analyisi: Question Suppose you have a for-loop for (i <- arr.indices) { ...body...} where each time the body calls reverse on a singly-linked list of the same size n as the array arr. What is the asymptotic running time of the whole loop? Choose One Answer 1A. O(n^2) --- more precisely, Theta(n^2) since it always takes that long. B. O(n) --- more precisely, Theta(n) since it always takes that long. C. O(n^3) --- more precisely, Theta(n^3) since it always takes that long. D. O(1) time. 15. Multiple Choice: (15) O-Notation Timing Analyisi: Suppose you have a for-loop for (i <- arr.indices) { ...body...} where each time the body calls reverse on a doubly-linked list (DLL) of the same size n as the array arr. The DLL class implements reverse by swapping the head and end "pointers" and changing state so that next goes in reverse order. What is the asymptotic running time of the whole loop? Choose One Answer A. O(n^2) --- more precisely Theta(n^2) since the loop always takes that time. B. O(n) --- more precisely Theta(n^2) since the loop always takes that time. C. O(n^3) --- more precisely Theta(n^3) since the loop always takes that time. D. O(log n) time.