Scala-Quiz MD
Scala-Quiz MD
## Scala
#### Q1. Scala bytecode can run on top of Java VM. What is the fundamental difference between Java
object.clone() and Scala object.copy()?
```scala
val m1 = Map("a"->1,"b"->2,"c"->3)
m1("a")
```
- [ ] a
- [ ] 2
- [ ] b
- [x] 1
- [ ] monads
- [ ] literal functions
- [ ] partially applied functions
- [x] parallel collections
#### Q4. What do you use in ScalaTest to see a detailed diagram of error messages when a test
fails?
- [ ] ArgumentExceptions
- [ ] AssertionException
- [x] 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
- [x] Tuple
#### Q6. After defining a function in the interpreter, Scala returns the following. What does the
`()` indicate?
```
myfnc: ()Unit
```
- [ ] hexadecimal
1 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
- [ ] short
- [x] 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)]`
- [x] `List`
```
val x = (1234, "Active")
```
- [ ] List
- [ ] Map
- [x] Tuple
- [ ] Array
- [ ] AnyVal
- [ ] AnyRef
- [ ] Method
- [x] Null
#### Q11. For the for-yield construct, is the scope separate between for-body and yield-body?
- [ ] 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.
- [x] 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
```scala
val a = Array(1, 2, 3, 4, 5)
for {
e <- a if e > 2
} yield e
```
- [ ] using regex
- [ ] using monads
- [ ] using string matching
- [x] using case classes
Note: ambiguous question, it's not clear what kind of [pattern matching](https://docs.scala-
lang.org/tour/pattern-matching.html) is meant here.
2 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
```
val y = List('a','b')
val z = y::List('c')
```
- [ ] List(a,b,c)
- [x] List(List(a, b), c)
- [ ] List(c,a,b)
- [ ] List(c,List(a,b))
- [x] 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`
- [x] `scala.util.Try`
[scala.util.Try](https://www.scala-lang.org/api/current/scala/util/Try.html)
#### Q16. What is the data type of y after this code is executed?
```
val y = (math floor 3.1415 * 2)
```
- [ ] short
- [x] double
- [ ] int
- [ ] bigInt
#### Q17. When using pattern matching, which character matches on any object?
- [ ] `%`
- [x] `_`
- [ ] `^`
- [ ] `-`
[Pattern Matching](https://docs.scala-lang.org/tour/pattern-matching.html)
#### Q18. You have created an array using val. Can you change the value of any element of the
array—and why or why not?
- [x] 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**:
```scala
val a1 = Array(1, 2, 3)
a1{1} = 3 // OK
a1 = Array(1, 3, 3) // error: reassignment to val
3 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
```
```scala
def main () {
var a = 0
for (a<-1 until 5){println(a)}
```
- [ ] 1,2,3,4,5
- [ ] 0,1,2,3,4
- [x] 1,2,3,4
- [ ] 2,3,4,5
- [ ] singletons
- [ ] stationary objects
- [x] functional objects
- [ ] fixed objects
#### Q21. You have written a Scala script. How would you access command-line arguments in the
script?
#### Q22. What does this code return? `val x = 3; if (x > 2) x = 4 else x = x*2`
- [ ] 4
- [x] an error
- [ ] 6
- [ ] 3
#### Q23. Which statement returns a success or a failure indicator when you execute this code?
- [x] myFuture.onComplete
- [ ] myFuture(status)
- [ ] myFuture.Finished
- [ ] complete(myFuture)
#### Q24. To denote a parameter that may be repeated, what should you place after type?
- [ ] `%`
- [ ] `&`
- [ ] `_`
- [x] `-`
#### Q25. What is called when a superclass has more than one subclass in Scala?
- [ ] polyinheritance
- [ ] multilevel inheritance
- [ ] multimode inheritance
- [x] hierarchical inheritance
4 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
#### 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
- [x] 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.
- [x] If an else-if succeeds, then none of the remaining else-if statements or elses will tested.
#### Q28. What do you call the process of changing the definition of an inherited method?
- [ ] recursive methods
- [ ] currying methods
- [ ] redefining methods
- [x] overriding methods
#### Q29. To denote a parameter that may be repeated, what should you place after the type?
- [ ] `_`
- [x] `*`
- [ ] `%`
- [ ] `&`
```scala
myClass.foreach(println _)
```
- [ ] `myClass.foreach(println ())`
- [ ] `myClass.foreach(print NIL)`
- [ ] `myClass.loop(println ())`
- [x] `myClass.foreach(x => println(x))`
#### Q32. You want to create an iteration loop that tests the condition at the end of the loop
body. Which iteration would you use?
#### 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
5 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
- [ ] connection
- [x] prepared statement
- [ ] SQL view
- [ ] Set
- [ ] Seq
- [x] Hash
- [ ] Map
#### Q35. Which term makes the contents of packages available without prefixing?
- [ ] use
- [ ] include
- [x] import
- [ ] assertion
#### Q36. If you wanted to find the remainder after division, what operator would you use?
- [x] %
- [ ] DIV
- [ ] //
- [ ] /
- [ ] method
- [x] fields and methods
- [ ] fields, methods, and packages
- [ ] fields
#### Q38. What defines methods and fields that can then be reused by mixing into classes?
- [ ] singleton
- [ ] assertion
- [x] trait
- [ ] monad
#### Q39. When do you need to explicitly state the return type in a function definition?
6 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
[Source:](https://www.programmersought.com/article/3717957705/)
- [ ] ||
- [ ] &&
- [x] &
- [ ] %
[Source:](https://docs.scala-lang.org/overviews/collections/sets.html)
- [ ] private function
- [ ] block function
- [x] local function
- [ ] method
A function defined within a block of code, such as within a method or another function, is called
a local function. This is because it is only visible and accessible within the scope of the block
in which it is defined, and is not accessible outside of that block.
#### Q44. What do you call a Scala method that is parametrized by type as well as by value?
- [ ] multimode method
- [x] polymorphic method
- [ ] closure
- [ ] collection method
- [x] IllegalArgumentException
- [ ] NumberFormatException
- [ ] NullPointerExcepetion
- [ ] MalformedParameterException
#### Q47. What would you change in this code to make it execute in parallel?
```
val myNums = (1 to 500).toList
list.map(_ + 1)
```
7 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
#### Q49. What's the best way to execute code in the background in a separate thread?
- [ ] AltThread
- [ ] AltFuture
- [ ] AltProcess
- [x] Future
```scala
x= List(1,2,4); x(1)?
```
- [ ] (1,2,4)
- [ ] 1
- [ ] Nil
- [x] 2
#### Q51. Which data type does Scala use instead of null for optional values?
- [ ] Nil
- [x] Option
- [ ] Singleton
- [ ] Collection
In Scala, the Option data type is used instead of null for optional values. It is a container that
can either hold a value or be empty, and it is used to represent the presence or absence of a
value. This makes it safer to work with than using null, as it eliminates the risk of null pointer
exceptions.
```val a = "baz"
s"Foo $a?"
```
#### Q53. Which expression is one way to iterate over a collection and generate a collection of
each iteration's result?
- [x] for-yield
- [ ] for-collect
- [ ] for-collect until
- [ ] collectuntil
- for-yield is one way to iterate over a collection and generate a collection of each iteration's
result. The for loop with the yield keyword is used to iterate over a collection and generate a
new collection with the results of each iteration.
#### Q54. Which statement accesses the third element of an array named foo?
- [x] foo[2]
- [ ] foo(3)
- [ ] foo[3]
- [ ] foo(2)
- In many programming languages, arrays are indexed starting at 0, so the first element of the
array is at index 0, the second element is at index 1, and so on. Therefore, to access the third
element of an array named "foo", you would use the index 2 (since the array is indexed starting at
0). This can be done using the syntax foo[2] or foo(2) depending on the programming language. In
some languages like Java, you can use foo[2] or foo[3] to access the third element.
8 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
#### Q55. What data type would you use to store an immutable collection of objects when you don't
know how many members will be in the collection?
- [ ] Tuple
- [x] List
- [ ] Object
- [ ] Array
- You would use a List data type to store an immutable collection of objects when you don't know
how many members will be in the collection. Lists are indexed collections of elements that can be
accessed by their position in the list, and they are commonly used to store collections of items
that need to be processed in order. Additionally, Lists are immutable, which means that their
elements cannot be modified once they have been created, making them ideal for use cases where
data integrity is important.
- [x] AnyRef
- [ ] AnyColl
- [ ] AnyVal
- [ ] AnyClass
- All classes in Scala inherit from the AnyRef class by default. AnyRef is the base class for all
reference types in Scala, and it is equivalent to the java.lang.Object class in Java. AnyVal is
the base class for all value types in Scala, and Any is the base class for all types in Scala.
#### Q58. Which code sample will print the integers 1 through 4, each on a separate line?
#### Q59. Which operator should you use to take the intersection of two sets?
- [x] &
- [ ] ||
- [ ] &&
- [ ] %
- The & or intersect method can be used to take the intersection of two sets in Scala.
#### Q60. Which data type does Scala use instead of null for optional values?
- [ ] Nil
- [x] Option
- [ ] Singleton
- [ ] Collection
- In Scala, the Option data type is used instead of null for optional values. It is a container
that can either hold a value or be empty, and it is used to represent the presence or absence of a
value. This makes it safer to work with than using null, as it eliminates the risk of null pointer
exceptions.
9 of 10 6/14/2024, 17:12
Firefox https://raw.githubusercontent.com/Ebazhanov/linkedin-skill-assessment...
#### Q61. What is the difference between a Scala trait and an interface?
[reference](https://www.geeksforgeeks.org/difference-between-traits-and-abstract-classes-in-
scala/)
10 of 10 6/14/2024, 17:12