Scala Flashcards

Flashcards companion to Introduction to Scala on DataCamp

1
Q

What is Scala?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What language does Scala compile in?

A

Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does Scala stand for?

A

Scalable Language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which companies use Scala?

A

Netflix, Morgan Stanley, Airbnb, Deutsche Bank

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is Scala flexible?

A

Scala lets you add new types, collections and control constructs that fee llike they are built-in to the language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why is Scala convenient?

A

The Scala standard library has a set of convenient predefined types, collections and control constructs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Who uses Scala?

A

Software Engineers, Data Engineers, Data Scientists, Machine Learning Engineers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which framework is written in Scala?

A

Apache Spark

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Whichi programming paradigms does Scala fuse?

A

Object-oriented and functional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What makes Scala scalable?

A

The fusion of object-oriented and functional paradigms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is Scala an object-oriented programming language?

A

Every value is an object and every operation is a method call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is Scala a functional programming language?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why use Scala?

A

Scala is scalable, concise, high-level, static, compatible with Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which command lets you start the Scala interpreter?

A

scala

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which function lets you print values to standard output?

A

println(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the two kinds of variables Scala offers?

A

val and var

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What distinguishes val variables?

A

val variables are immutable. They can’t be reassigned,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you declare an immutable integer variable in Scala?

A

val variableName: Int = 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you declare an immutable string in Scala?

A

val variableName: String = “Alex”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What distinguishes var variables?

A

var variables are mutable. They can be reassigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you declare a mutable integer in Scala?

A

var variableName: Int = 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you declare a mutable string in Scala?

A

var variableName: String = “Alex”

23
Q

Why should you prefer vals over vars?

A
  • Your data won’t be changed inadvertently (defensive programming).
  • Your code is easier to reason about.
  • You have to write fewer tests.
24
Q

What is the main drawback of immutability?

A

More memory is needed due to data copying.

25
Which command lets you execute a Scala script?
scala script.scala
26
How do you write comments in Scala?
// Comment
27
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.
28
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.
29
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.
30
Which command lets you translate Scala code to Java bytecode?
scalac App.scala
31
What are the advantage and inconvenient of a compiled language?
* Advantage: Increased performance once compiled. * Disadvantage: It takes time to compile code.
32
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.
33
How do you create an immutable array in Scala?
val players = Array("Alex", "Chen", "Marta")
34
How do you parameterize an array in Scala?
val players: Array[String] = new Array[String](3) to create an array holding 3 strings. The type parameter is part of the instance; the value parameter is not.
35
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.
36
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.
37
How do you create a list in Scala?
val players = List("Alex", "Chen", "Marta")
38
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
39
What are :: in Scala?
Cons.
40
How do you specify an empty list in Scala?
Nil
41
How do you prepend an element to a list?
element :: list
42
How do you concatenate lists in Scala?
list1 ::: list2
43
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 *
44
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)
45
How do you create an if statement in Scala?
if (hand \> 21) { println("This hand busts!") } Curly braces are a style guide recommendation.
46
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.
47
How do you define a while loop in Scala?
while (i \< repetitions) { body }
48
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)
49
Which Scala method lets you loop over a variable and apply a function to its elements?
variable.foreach(function)
50
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.
51
In Scala, what are some signs of the imperative style?
* var * side effect * Unit
52
In Scala, what are some signs of the functional style?
* val * no side effects * Non-Unit value types * Int * Boolean * Double
53
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
54
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.