What are the different types of Scala literals?
The different types of literals in scala are
a) Integer literals
b) Floating point literals
c) Boolean literals
d) Symbol literals
e) Character literals
f) String literals
g) Multi-Line strings
What are the different types of Scala literals?
The different types of literals in scala are
a) Integer literals
b) Floating point literals
c) Boolean literals
d) Symbol literals
e) Character literals
f) String literals
g) Multi-Line strings
What are the four types of scala identifiers ?
The four types of identifiers are
a) Alpha numeric identifiers
b) Operator identifiers
c) Mixed identifiers
d) Literal identifiers
Why scala prefers immutability?
Scala prefers immutability in design and in many cases uses it as default. Immutability can help when dealing with equality issues or concurrent programs.
How can you format a string?
To format a string, use the .format () method, in scala you can use
Val formatted= “%s %i”.format (mystring.myInt)
How do I append to the list?
In scala to append into a list, use “:+” single value
|
var myList = List.empty[String]
myList :+= “a”
myList :+= “b”
myList :+= “c”
use++ for appending a list
var myList = List.empty[String]
myList ++= List(“a”, “b”, “c”)
|
13) What is function currying in Scala?
Currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument Many of the same techniques as language like Haskell and LISP are supported by Scala. Function currying is one of the least used and misunderstood one.
14) What are implicit parameters in Scala?
Implicit parameter is the way that allows parameters of a method to be “found”. It is similar to default parameters, but it has a different mechanism for finding the “default” value. The implicit parameter is a parameter to method or constructor that is marked as implicit. This means if a parameter value is not mentioned then the compiler will search for an “implicit” value defined within a scope.
15) What is a closure in Scala?
A closure is a function whose return value depends on the value of the variables declared outside the function.
16) What is Monad in Scala?
A monad is an object that wraps another object. You pass the Monad mini-programs, i.e functions, to perform the data manipulation of the underlying object, instead of manipulating the object directly. Monad chooses how to apply the program to the underlying object.
17) What is Scala anonymous function?
In a source code, anonymous functions are called ‘function literals’ and at run time, function literals are instantiated into objects called function values. Scala provides a relatively easy syntax for defining anonymous functions.
18) Explain ‘Scala higher order’ functions?
Scala allows the definition of higher order functions. These are functions that take other functions as parameters, or whose result is a function. In the following example, apply () function takes another function ‘f’ and a value ‘v’ and applies function to v.
Example:
|
1
2
3
4
5
6
7
8
9
10
11
|
object Test {
def main(args: Array[String]) {
println( apply( layout, 10) )
}
def apply(f: Int => String, v: Int) = f(v)
def layout[A](x: A) = “[“ + x.toString() + “]”
|
When the above code is compiled and executed, it produces following result.
|
1
2
3
4
5
6
7
|
C:/>scalac Test.scala
C:/>scala Test
[10]
C:/>
|
19) What is the difference between var and value?
In scala, you can define a variable using either a, val or var keywords. The difference between val and var is, var is much like java declaration, but val is little different. We cannot change the reference to point to another reference, once the variable is declared using val. The variable defined using var keywords are mutable and can be changed any number of times.
20) What are option, some and none in scala?
‘Option’ is a Scala generic type that can either be ‘some’ generic value or none. ‘Queue’ often uses it to represent primitives that may be null.
What is the use of tuples in scala?
Scala tuples combine a fixed number of items together so that they can be passed around as whole. A tuple is immutable and can hold objects with different types, unlike an array or list.
Case classes provides a recursive decomposition mechanism via pattern matching, it is a regular classes which export their constructor parameter.
The constructor parameters of case classes can be accessed directly and are treated as public values
When can you use traits?
There is no specific rule when you can use traits, but there is a guideline which you can consider.
a) If the behaviour will not be reused, then make it a concrete class. Anyhow it is not a reusable behaviour.
b) In order to inherit from it in Java code, an abstract class can be used.
c) If efficiency is a priority then lean towards using a class
d) Make it a trait if it might be reused in multiple and unrelated classes. In different parts of the class hierarchy only traits can be mixed into different parts.
e) You can use abstract class, if you want to distribute it in compiled form and expects outside groups to write classes inheriting from it.
What is ‘scala trait’ in scala?
‘Traits’ are used to define object types specified by the signature of the supported methods. Scala allows to be partially implemented but traits may not have constructor parameters. A trait consists of method and field definition, by mixing them into classes it can be reused.