Q1. Scala bytecode can run on top of Java VM. What is the fundamental difference between Java object.clone() and Scala object.copy()?
- One is a Java object, the other is a Scala object.
- clone() will copy class structures but not the data, while copy() will also copy data into new objects.
- There is no difference.
- copy() allows you to change values during the copying process; clone() does not.
val m1 = Map("a"->1,"b"->2,"c"->3)
m1("a")
- a
- 2
- b
- 1
- monads
- literal functions
- partially applied functions
- parallel collections
- ArgumentExceptions
- AssertionException
- DiagrammedAssertions
- JUnit
Q5. What data type would you use to store an immutable collection of objects that contain a fixed number of varying types?
- Array
- ImmutableCollection
- List
- Tuple
Q6. After defining a function in the interpreter, Scala returns the following. What does the ()
indicate?
myfnc: ()Unit
- The function has no side effects.
- The function takes no parameters.
- The function returns no value.
- Returning unit types to the function is a closures.
- hexadecimal
- short
- floating point
- long
Q8. When you convert a map to a list using the toList
method of the map, the result will be of which type?
-
List[(String, String)]
-
List[(Array, Array)]
-
List[(Collection, Collection)]
-
List
val x = (1234, "Active")
- List
- Map
- Tuple
- Array
- AnyVal
- AnyRef
- Method
- Null
Reference: Null in Scala Standard library. The question is a bit incorrect - Null is a subtype of every type except those of value classes
- Yes and no. It is different depending on the for construct and what it does.
- Yes, because the for section does not expose its scope.
- No, because for-yield shares the same scope, even though they are within separate curly braces.
- Yes, because they are within different curly braces.
Example: yield-body has access to the e
variable from the for-body
val a = Array(1, 2, 3, 4, 5)
for {
e <- a if e > 2
} yield e
- using regex
- using monads
- using string matching
- using case classes
Note: ambiguous question, it's not clear what kind of pattern matching is meant here.
val y = List('a','b')
val z = y::List('c')
- List(a,b,c)
- List(List(a, b), c)
- List(c,a,b)
- List(c,List(a,b))
- assert
- require
- precondition
- mustHave
Q15. Which Scala type may throw an exception or a successfully computed value, and is commonly used to trap and propagate errors?
-
scala.util.ExceptionHandling
-
scala.Catch.Throw
-
scala.exception.TryFinally
-
scala.util.Try
Reference: scala.util.Try
val y = (math floor 3.1415 * 2)
- short
- double
- int
- bigInt
-
%
-
_
-
^
-
-
Reference: Pattern Matching
Q18. You have created an array using val. Can you change the value of any element of the array—and why or why not?
- Yes, the reference to the array is immutable, so the location that the array points to is immutable. The values in the array are mutable.
- The 0th element is immutable and cannot be modified. All other elements can be modified.
- Yes, val does not make arrays immutable.
- No, val makes the array and values of the array immutable.
Explanation:
val a1 = Array(1, 2, 3)
a1{1} = 3 // OK
a1 = Array(1, 3, 3) // error: reassignment to val
def main () {
var a = 0
for (a<-1 until 5){println(a)}
- 1,2,3,4,5
- 0,1,2,3,4
- 1,2,3,4
- 2,3,4,5
- singletons
- stationary objects
- functional objects
- fixed objects
Note: singletons may have mutable state
- use array named args
- use tuple named args
- use numbered variables with a _ prefix for example _ 1, _ 2, _ 3
- use numbered variables with a $ prefix - for example $1, $2, $3
- 4
- an error
- 6
- 3
val MyFuture = Future {runBackgroundFunction() }
- myFuture.onComplete
- myFuture(status)
- myFuture.Finished
- complete(myFuture)
-
%
-
&
-
_
-
-
- polyinheritance
- multilevel inheritance
- multimode inheritance
- hierarchical inheritance
Q26. One way to improve code reliability is to use __
, which will evaluate a condition and return an error if the condition is violated.
- packages
- polymorphisms
- assertions
- traits
- If the first else-if does not succeed, then no other else-ifs are tested.
- If an else-if does not succeed, then none of the remaining else-if statements or elses will be tested.
- All else-if statements are tested in all cases.
- If an else-if succeeds, then none of the remaining else-if statements or elses will tested.
- recursive methods
- currying methods
- redefining methods
- overriding methods
-
_
-
*
-
%
-
&
Reference: Repeated Parameters in Scala
myClass.foreach(println _)
-
myClass.foreach(println ())
-
myClass.foreach(print NIL)
-
myClass.loop(println ())
-
myClass.foreach(x => println(x))
- Immutable objects use less memory than their mutable counterparts.
- Immutable objects do not require error handling.
- Immutable objects can be used in classes, mutable objects cannot.
- Immutable objects are threadsafe.
Q32. You want to create an iteration loop that tests the condition at the end of the loop body. Which iteration would you use?
- do-while loop
- while loop
- for loop
- do-until loop
Q33. What can you use to make querying a database more efficient, by avoiding the need to parse the SQL string every time a query is executed from Scala?
- database driver
- connection
- prepared statement
- SQL view
Reference: PreparedStatement from Java which is also used in Scala
- Set
- Seq
- Hash
- Map
- use
- include
- import
- assertion
- %
- DIV
- //
- /
- method
- fields and methods
- fields, methods, and packages
- fields
- singleton
- assertion
- trait
- monad
- when the function has no side effects
- when the function returns a Unit type
- when the function is recursive
- when the function has side effects
- so only methods in the same file can access the field
- so only methods in the same package can access the field
- so only methods in the same class could access the field
- so only methods defined in a Java class can access the field
- They do the exact same thing
-
==
won't work on objects -
==
cannot be applied toString
-
==
is a wrapper of.equals()
and checks for nulls
- ||
- &&
- &
- %
- AltFuture
- Future
- AltProcess
- AltThread
- private function
- block function
- local function
- method
- multimode method
- polymorphic method
- closure
- collection method
- IllegalArgumentException
- NumberFormatException
- NullPointerExcepetion
- MalformedParameterException