Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

11. What is the use of apply and unapply methods in Scala?

In Scala, an extractor defines a method unapply(), as well as an optional method,


apply(). For mapping and unmapping data between form and model data, both apply
and unapply methods are used.

• Apply() method: Assembling an object from its components is done with this
method. For example, by using the two components firstName and lastName,
we can create the Employee object with the apply method.
• Unapply() method: This method follows the reverse order of apply order and
decomposes an object into components. For example, you can break or
decompose that employee object down into its components i.e., firstName and
lastName.

12. Explain BitSet.

A set is a collection of unique items that cannot be repeated. In Scala, non-negative


integer sets are called Bitsets, and they are represented as variable-size arrays of
bits packed into 64-bit words. The largest number in a bitset represents its
memory footprint.

Syntax:

var BS: BitSet = BitSet(element1, element2, element3, ....)

Here, BS represents the name of the BitSet that was created.

scala.collection.immutable.BitSet and scala.collection.mutable.BitSet are the two


versions of BitSet provided in Scala. Although both are identical, mutable data
structures change the bits in place, thus making them less concurrency friendly than
immutable data structures.

Example:

import scala.collection.immutable.BitSet
object InterviewBit
{
def main(args:Array[String])
{
println("Initialize a BitSet")
val numbers: BitSet = BitSet(5, 6, 7, 8)
println(s"Elements of BitSet are = $bitSet")
println(s"Element 6 = ${bitSet(6)}")
println(s"Element 3 = ${bitSet(3)}")
println(s"Element 7 = ${bitSet(7)}")
}

Output:

Initialize a BitSet
Elements of BitSet are = BitSet(5,6,7,8)
Element 6 = true
Element 3 = false
Element 7 = true

Get Access to 250+ Guides with Scaler Mobile App!


Experience free learning content on the Scaler Mobile App

4.5
100K+
Play Store

13. What do you mean by ofDim()?

Scala provides us with a function called ofDim that declares multidimensional arrays
using the matrix format. Array.ofDim can be used to declare multidimensional
arrays. There's no limit to the number of dimensional arrays you can create.

Syntax:

val arrayname = Array.ofDim[data_type](number of rows, number of columns)

or

var arrayname = Array(Array(elements), Array(elements))

Example:

object MultiArrayExample
{
def main(args: Array[String])
{ val multiArr= Array.ofDim[Int](2, 2)
multiArr(0)(0) = 5
multiArr(0)(1) = 10
multiArr(1)(0) = 15
multiArr(1)(1) = 20
for(i <- 0 to 1; j <- 0 to 1)
{
println("Element "+ i + j + " = " + multiArr(i)(j))
}
}
}

Output:

Element 00 = 5
Element 01 = 10
Element 10 = 15
Element 11 = 20

14. Write different types of scope provided for variables in Scala.


According to their declarations, Scala variables are categorized into three scopes as
given below:

• Fields: Variables of this type can be accessed from any method within a Web
object, or from outside the object, depending on access modifiers used.
Depending on the var and val keywords, they can be mutable or immutable.
• Method Parameters: When a method is invoked, these variables are used to
pass values to the method. All method parameters use the val keyword and are
strictly immutable. Normally, these are accessed within a method, but a
Reference allows accessing them outside the method.
• Local Variables: These are the variables (mutable or immutable) declared inside
a method and accessible only within that method. By returning them from the
method, they can be accessed outside of the method.

15. Explain map() and flatmap().

Map() and its close cousin flatMap() are often used when we deal with data
structures in Scala and both are higher-order functions.

• map() method: It functions similarly to mapping operations in other languages


like JavaScript since it allows us to iterate over a collection to translate it into
another collection. Every element of the collection is transformed by applying
a function to it. Its uses are heavily overloaded, making it useful for situations
that aren't immediately obvious. Scala's map() method is exceptionally
powerful.
• flatpmap() method: In Scala, flatMap() is the same as map(), except that
flatMap removes the inner grouping of items and creates a sequence.
Essentially, it is a combination of the flatten method and the map method. The
map method followed by the flatten method produces the same result as
flatMap(), so we can say that flatMap runs the map method first, followed by
the flatten method. Compared to the map method, flatMap is much more
powerful

You might also like