..
Scala Overview
- What is type of paradigm is scala?
- Multi paradigm (Functional and Object oriented)
- What is the key difference in terms of types in java vs scala?
- In Java there are two types
- Primitive
- Reference
- In scala every type is an object
- In Java there are two types
- Write “Hello, World” in scala
object Hello {
def main(args: Array[String]) = {
println("Hello, World")
}
}
- What is the difference between
val
andvar
?val
cannot be reassignedvar
can be reassigned
- What is the equivalent of
val
in java?final
- When is it compulsory to specify the return value of a function?
- When it is recursive
- What is the return type of a function that doesn’t return anything?
Unit
- How to access an element of an array?
- Using parenthesis
arr(5)
- How to increment a variable in scala?
i += 1
- Do
++
operators work in scala?- No
- Which method is used to loop over an array?
foreach
- What is the function signature of
foreach
?foreach
takes a function as its arguement
- How to loop over an array using
for
for (arg <- args) {
println(arg)
}
- In the above case is
arg
aval
or avar
?- It is a
val
- It cannot be modified
- It is a
- What is the syntax for using
match
myVar match {
case case1 => <statements>
case _ => <default>
}
- Suppose you want to print a string with variable values inside them. They require no formatting. What to use?
- use
s"String that needs substitution. $var"
$var
will be replaced by the value ofvar
- use
- The same scenario but not it needs to be formatted
- use f-strings
f"The value of tmp is $tmp%.2f"