Question: What is Scala?
|
Answer: Scala is a multi-paradigm programming language that combines object-oriented and functional programming features.
It runs on the Java Virtual Machine (JVM) and is designed to be concise, expressive, and scalable.
|
Question: What are the key features of Scala?
|
Answer: Scala's key features include:
• Strong static typing
• Type inference
• Pattern matching
• Higher-order functions
• Immutable data structures
• Conciseness
• Compatibility with Java
|
Question: What is the difference between var and val in Scala?
|
Answer: In Scala, the var is a mutable variable, meaning its value can be reassigned,
while val is an immutable variable, meaning its value cannot be changed once assigned.
|
Question: What is a higher-order function in Scala?
|
Answer: A higher-order function is a function that takes other functions as parameters or returns a function.
Below please find an example in Scala:
def operate(f: Int => Int, x: Int): Int = f(x)
val result = operate(_ * 2, 5) // Returns 10
|
Question: What are traits in Scala?
|
Answer: The traits are similar to interfaces in other languages but can also contain concrete methods.
They allow for multiple inheritances and provide a way to share interfaces and fields between classes. For example:
trait Printable {
def print(): Unit
}
class MyClass extends Printable {
def print(): Unit = println("Printing...")
}
|
Question: Can you explain pattern matching in Scala?
|
Answer: Pattern matching is a powerful feature in Scala that allows you to match a value against a pattern and
execute code based on the match.
It is similar to switch statements in other languages but more flexible.
|