Elance Scala Test Answers

When importing all the names of a package or class, which character do you use instead of " * "?




What is the defaut parameter call semantics?

By Inference
By Value
By Name
By Reference



What is the largest Tuple that Scala supports?




What is a higher-order function?

Scala does not support higher-order functions
Higher-order functions are parameterless functions that return themselves
Higher-order functions are functions that return functions
Higher-order functions are functions that take other functions as parameters.



When a class inherits from a trait, it inherits all the code contained in the trait and implements the trait's:




Everything, including numbers and functions, are _______ in Scala.




Scala's "Unit" roughly corresponds to which Java type?




What is a class with a single instance?

A mono object
A singleton object
A static object
A single argument



When no super-class is specified, ______ is implicitly used.

You must always specify super-class
scalac use.ref



What is the name of the Scala compiler?




Which of the following best describes Scala?

An object oriented language
A functional language
All of these choices describe Scala
A language that runs on the VM



True or False? Like pre 1.5 Java, Scala suffers from lack of genericity.




Does Scala support the return keyword?

Yes, but it is not idiomatic Scala and therefor discouraged.
Yes, all methods and functions must have at least one return statement.
Yes, but only for traits.



True or False? Multiple classes can be imported from the same package by enclosing them in curly braces {}.




Scala is:

An advanced Java language implemented by Martin Odersky
An object-functional language that supports functional programming constructs
A pure functional programming language
A dynamic language that can be used to program in object-oriented style efficiently
An advanced Object-Oriented Language much better than Java that runs on the JVM



What is an expression following the "if" keyword?

A tree
An array
A guard
A wild-card



Which statement best describes an Iterator

Scala does not support Iterators
An Iterator is a stream of incoming items where advancing to the next item consumes the current item
An Iterator trait is mixed into all collection types
An iterator is a collection type



True or False? Methods taking one argument can be used with infix syntax?




Explain how "abc".length returns 3

A subclass of java.lang.String is generated at runtime, adding the `length` method to its signature.
All string literals can be processed by Scala String interpreter.
All string literals are an instance of scala.collection.immutable.StringOps
An implicit conversion converts the java.lang.String into a scala.collection.immutable.StringOps, which supports a length method.



True or False? Scala compiler will never require you to specify the result type of a function.




What is the value of the following expression? { val a = List(1,2,3) val b = List(4,5,6) (a,b).zipped.map(_+_) }




Which of the following is a pattern matching any value, without giving it a name, represented by " _ "?

A simple class
A function
A placeholder
A guard



Witch one of the following operators is use for sequencing Parsers




It is possible to override methods inherited from a _____ in Scala.




Which statement is true about sealed classes.

A sealed class' instances cannot be modified.
A sealed class may not be directly inherited, except if it is defined in the same source file.
There is no such thing as a sealed class.
A subclass of a sealed class can inherited anywhere only within the same package.



Does Scala support tail-call recursion?

Yes, the JVM runtime supports tail-call optimization.
Partly, because the stack is infinite in Scala.
Partly at the compiler level. The compiler will try and unwind the recursive call into a loop.



Which statement best describes a partial function?

A partially defined function.
An internal function type that is used by the scala.collection.immutable package.
A function that support currying
When applying the function, you do not pass in arguments for all of the parameters defined by the function, but only for some of them, leaving the remaining ones blank



What is the tool "schema2src" used for?

Unifying types
Sealing classes



Describe class AnyRef

AnyRef is the root Object in Scala
All types except the value types descend from AnyRef
AnyRef is derived from AnyVal
There is no such class



Scala supports which types of polymorphism?

None of these
Subtype and parametric
Subtype, ad-hoc and parametric polymorphism
Ad-hoc and parametric



In the expression: List(1,2,3) reduceLeft ( (a,b) => a+b ) `b` refers to:

The next element in the list
The current sum while iterating through the list
The "fold" operation
The return value for the expression



Classes in Scala, in contrast to Java, can have ______.




Which statement about case classes is false?

You can construct instances of these classes without using the new keyword
Case classes as sealed and therefor cannot be extended
The toString method is automatically redefined
The equals method is automatically redefined



What is the result type of the following expression? List(1, 2, true, false)




The following code will > var x=100; var y=200; x->y

a Tuple with Arity 2
automatically create a List[T] with x and y as members with T of 'Int' type
assign 100 to variable y
treat x and y as same references in the further code
produce a compile error



A valid description of a covariant type parameter would be:

A type parameter which is allowed to vary up for super types.
A type parameter which is allowed to vary down as the class is subtyped.
A type parameter which is fixed for super types.
A type parameter which is fixed when the class is subtyped.



`Nil` is generally the same as:




In Scala, type parameters and abstract types may be constrained by a _____.

Type call
Type bound
Type safe
Type function



If you are defining scala classes in a 'package examplepackage', and want to ensure that a function 'foo' is only accessible by classes defined in the same package, how would you declare that function?

def foo={...} //default access
private[examplepackage] def foo = {...}
package def foo={...}//package private
[examplepackage]private def foo={...}
package[examplepackage] def foo={...}



What would be the result of: Option[String]("hi") match { case None=> "hello!" }

Nothing would happen because "hi" is not of type "None"
The statement would return "hello!"
A NullPointerException would be thrown.
A MatchError would be thrown.



In the expression: List(1,2,3).foldLeft(x){case (a,b) => a+b} `x` is:

A default value, only used if the list is empty
The "accumulator," which is the initial value for `a`
A list, to which the results are appended.
The "accumulator," which is the initial value for `b`



True or False? Scala provides static members (members or fields).




Which statement about pattern matching is true?

Patterns do not work with structural types
The order of the pattern match is irrelevant
The case set must be exhaustive
Pattern matching does not work with case classes



Which statement about List is false?

List is a proxy for java.util.ArrayList
A List is covariant
A List is optimal for last-in-first-out (LIFO), stack-like access patterns
A List is a finite immutable sequence



How would you define the method: def +(a:Int):Int in a Java interface that will be overriden or used in Scala code?

public int $plus(int a)
public int %plus(int a)
private int $plus(int a)
You can't define a scala operator using Java source code.
public int #plus(int a)



True or False? In the Interpreter, you can define a new val with a name that was already used before.




What would be returned by the following function: def foo(l:List[Int]) = { var x=l.head; l.collect{ case a:Int if a>x => x=a; a }; x } When passed: List(2,4,6,8,6,3,1)

Nothing, there's no return value.



`() => Unit` is best described as

`() => Unit` is not valid Scala
A pure function
A function literal
A procedure type definition



What is a lazy var?

A variable that is only evaluated once
A variable that once evaluated can only be referenced by the creating scope.
An immutable variable
`lazy var` is not supported by Scala



What will the following function return: def foo(o:Any) = { o match { case Option(x) => "hi!" case anything => anything } } When passed a 'None' object?

it will return the None because it matches on 'anything'
It won't compile
It will return 'anything'
It will return "hi!" because a 'None' is a type of 'Option'



How would you get a List that was the result of appending `5:Int` to a `List(1,2,3)`. The order of the elements in the resulting List is irrelevant.

List(1,2,3) :: 5
List(1,2,3) :+ 5
List(1,2,3) ::= 5
List(1,2,3) + 5
List(1,2,3) :: List(5)



Which of these are NOT ways in which case classes differ from standard classes?

Getter functions are automatically defined for the constructor parameters
A default definition for method "toString" is provided, and prints the value in a source form
Default definitions for the methods "equals" and "hashCode" are not provided
The "new" keyword is not mandatory to create instances of case classes



What is the data type of myVariable in the following: val myVariable = if (true) "Hello"




Are parenthesis `(` and curly braces `{` interchangeable?

Only if the method expects a partial function.
Not unless the function supports contravariant type parameters.
Not unless the function supports covariant type parameters.
Only if the method expects a single parameter.



Is it possible in Scala to declare a variable of type `Int` with a value of `null`?




Which statement about the Option[+A] class is false?

Option[+A] is meant to replace the usage of null.
Option[+A] is abstract.
scala.Nothing is derived from Option[+A]
None is derived from Option[+A]



Which predicate tests whether x is object identical to y?

x = y
x eq y
x == y
x.object == y.object



Which of the following descriptions of case classes is *NOT* correct:

Case classes can be used in matchers.
Case classes come with apply and unapply methods.
Case classes are always immutable.
Case classes have default serializers



def g(): Int = try { 1 } finally { 2 } Calling g() results in




Which of the following is not one of the ways to increment the value of a variable i of type Int by 1.

i = i + 1
i += 1



"Option" is:

an abstract class
an interface
a trait
a concrete class
a case class



What is the Scala development environment designed for use in schools?




What is the runtime class of Unit?




A function which associates a value to a (variable) name is known as a(n):




def f(): Int = try { return 1 } finally { return 2 } Calling f() results in




True or False? Scala compiler automatically infers function parameter types.