Introduction to Scala Flashcards
https://www.datacamp.com/courses/introduction-to-scala
What is Scala?
Scala is a general-purpose programming language providing support for functional programming and a strong static type system. Designed to be concise, many of Scala’s design decisions aimed to address criticisms of Java.
What language does Scala compile in?
Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine.
What does Scala stand for?
Scalable Language
Which companies use Scala?
Netflix, Morgan Stanley, Airbnb, Deutsche Bank
Why is Scala flexible?
Scala lets you add new types, collections and control constructs that fee llike they are built-in to the language.
Why is Scala convenient?
The Scala standard library has a set of convenient predefined types, collections and control constructs.
Who uses Scala?
Software Engineers, Data Engineers, Data Scientists, Machine Learning Engineers
Which framework is written in Scala?
Apache Spark
Whichi programming paradigms does Scala fuse?
Object-oriented and functional
What makes Scala scalable?
The fusion of object-oriented and functional paradigms.
Why is Scala an object-oriented programming language?
Every value is an object and every operation is a method call.
Why is Scala a functional programming language?
Functions are first-class values: you can pass them as argument to functions, return them from functions, store them in variables…
Operations of a program should map input values to output values rather than change data in place. In other words, functions should not have side effects.
Why use Scala?
Scala is scalable, concise, high-level, static, compatible with Java.
Which command lets you start the Scala interpreter?
scala
Which function lets you print values to standard output?
println(value)
What are the two kinds of variables Scala offers?
val and var
What distinguishes val variables?
val variables are immutable. They can’t be reassigned,
How do you declare an immutable integer variable in Scala?
val variableName: Int = 10
How do you declare an immutable string in Scala?
val variableName: String = “Alex”
What distinguishes var variables?
var variables are mutable. They can be reassigned.
How do you declare a mutable integer in Scala?
var variableName: Int = 1
How do you declare a mutable string in Scala?
var variableName: String = “Alex”
Why should you prefer vals over vars?
- Your data won’t be changed inadvertently (defensive programming).
- Your code is easier to reason about.
- You have to write fewer tests.
What is the main drawback of immutability?
More memory is needed due to data copying.
Which command lets you execute a Scala script?
scala script.scala
How do you write comments in Scala?
// Comment
How does an interpreted language work?
With an interpreated language, the program directly executes instructions written in a programming language, without requiring them previously to have been compiled into machine code.
How does a compiled language work?
With a compiled language, a program translates source code from a high-level programming language to a lower level language (e.g., machine code) to create an executable program.
How do Scala applications differ from Scala scripts?
- They need to be compiled explicitly and then run explicitly.
- They consist of many source files than can be compile individually.
- They are useful for larger programs.
- There’s no lag time since applications are precompiled.
Which command lets you translate Scala code to Java bytecode?
scalac App.scala
What are the advantage and inconvenient of a compiled language?
- Advantage: Increased performance once compiled.
- Disadvantage: It takes time to compile code.
How do you define a function in Scala?
def bust(hand: Int): Boolean = { hand \> 21 }
The function’s body follows the = sign and is written within {}.
”: Boolean = “ is optional and can be removed.
How do you create an immutable array in Scala?
val players = Array(“Alex”, “Chen”, “Marta”)
How do you parameterize an array in Scala?
val players: Array[String] = new ArrayString
to create an array holding 3 strings.
The type parameter is part of the instance; the value parameter is not.
How do you intialize elements of an array in Scala?
players(0) = “Alex”
Indexing is done using parentheses (), not square brackets like in Java and Python.
What is the difference between arrays and lists in Scala?
Arrays are mutable (you can add or replace elements as long as they are the same type), lists are immutable.
How do you create a list in Scala?
val players = List(“Alex”, “Chen”, “Marta”)
How do you prepend an element to a list in Scala?
val newPlayers = “Sindhu” :: players
or define players as a var, and use:
var players = “Sindhu” :: players
What are :: in Scala?
Cons.
How do you specify an empty list in Scala?
Nil
How do you prepend an element to a list?
element :: list
How do you concatenate lists in Scala?
list1 ::: list2
What are some advantages of static type systems?
- Increased performance at run time
- Properties of your program verified (absnece of common type-related bugs)
- Safe refactorings
- Explicit documentations with type annotation
*
What are some inconvenients of static type systems?
- Delay before execution (takes time to check types)
- Code is verbose (longer to write)
- Language is not flexible (one strict way of composing a type)
How do you create an if statement in Scala?
if (hand > 21) {
println(“This hand busts!”)
}
Curly braces are a style guide recommendation.
How do you create an if else statement in Scala?
if (handA > handB)
println(handA)
else
println(handB)
The bodies can be put on the same line if they are just one line of code. No curly braces are necessary.
How do you define a while loop in Scala?
while (i < repetitions) {
body
}
Why can Scala be imperative?
Scala can be imperative because:
- It can execute one command at a time
- It can iterate with loops
- It can mutate shared states (variables out of scope)
Which Scala method lets you loop over a variable and apply a function to its elements?
variable.foreach(function)
In computer science, what is a side effect?
A side effect happens when code modifies some variable outside of its local scope. Printing to the standard output stream is a side effect.
In Scala, what are some signs of the imperative style?
- var
- side effect
- Unit
In Scala, what are some signs of the functional style?
- val
- no side effects
- Non-Unit value types
- Int
- Boolean
- Double
What are some benefits of the functional style?
- Your data won’t be changed inadvertently
- Your code is easier to reason about
- You have to write fewer tests
- Functions are more reliable and reusable
In Scala, should you prefer the functional or the imperative style?
You should use the functional style at first instinct, and then use the imperative style if the need arises.