CIS 554 -- Scala Quiz 2 Name __________________________________

Please do not provide more than the question asks for. If I ask for a statement, don't put it in a function. If I ask for a function, don't put it in a class. If I ask to compute a value, don't print the result. And so on. I may or may not, at my discretion, mark such unnecessarily verbose answers as wrong.

  1. (1 point) Complete the following sentence:

    A function is tail recursive if

    any recursive call is the very last thing done in the function
          or, more accurately,
    every possible path through the function returns either a result computed without recursion, or a result that is the unmodified result (i.e. no further work is done) of a recursive call.





  2. (1 point) What is the result of calling the following function with the argument 5? (Careful: Trick question!)

    def fiddle(n: Int) {
      (1 to n).toList map (1.0 / _)

    }


    ()
    Since there is no "=" sign before the body, the Unit value is returned.



  3. (1 point) Suppose you have the definition

    def area(width: Int)(height: Int) = width * height

    What is the type of this function? Please express your answer the same way the Scala REPL would.

    area: (width: Int)(height: Int)Int




  4. (1 point) Define atomic operation.

    An operation that, from the viewpoint of another thread, is instantaneous; it either has been completed, or has not been started.



  5. (1 point) Define daemon thread.

    A thread that dies when all non-daemon threads terminate.
    Another way to say this: It is a thread that does not prevent the JVM from exiting.




  6. (1 point) By making a class into a case class, you get seven additional methods. Name three of them.

    Any three of: equals, hashCode, toString, copy, apply, unapply, a factory method named after the class.
    1/2 credit if you got only two right, or if you didn't understand "three."



  7. (1 point) What is the result of this expression?   "abcde" ensuring (_.length > 3)

    "abcde"



  8. (1 point) What is the result of this expression? List(1, 2, 3) flatMap(x => List(x, 4))

    List(1, 4, 2, 4, 3, 4)


  9. (1 point) Distinguish between the Scala values Null and Nothing. (Notice the capitalization, it's important.)

    Null and Nothing are both types. Null is a subtype of all AnyRef types, and Nothing is a subtype of all types.

    Irrelevant additional information: There is exactly one value of type Null, called null. There are no values of type Nothing. Also, None is a value of type Option, and has nothing to do with this question.




  10. (1 point) Write an actor named yes_man that prints "I agree" when sent the message true, and exits when sent the message false.

        val yes_man = actor {
          loop { //optional
            receive {
              case true => println("I agree.")
              case false => exit()
            }
          } //end optional loop
    }
    Note: I meant to say "each time it is sent the message true," but I didn't, so the enclosing loop is optional.
    Either way, almost everyone got this right!!