Introduction to Scala Flashcards

https://www.datacamp.com/courses/introduction-to-scala

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
Q

Which command lets you execute a Scala script?

A

scala script.scala

26
Q

How do you write comments in Scala?

A

// Comment

27
Q

How does an interpreted language work?

A

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
Q

How does a compiled language work?

A

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
Q

How do Scala applications differ from Scala scripts?

A
  • 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
Q

Which command lets you translate Scala code to Java bytecode?

A

scalac App.scala

31
Q

What are the advantage and inconvenient of a compiled language?

A
  • Advantage: Increased performance once compiled.
  • Disadvantage: It takes time to compile code.
32
Q

How do you define a function in Scala?

A
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
Q

How do you create an immutable array in Scala?

A

val players = Array(“Alex”, “Chen”, “Marta”)

34
Q

How do you parameterize an array in Scala?

A

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.

35
Q

How do you intialize elements of an array in Scala?

A

players(0) = “Alex”

Indexing is done using parentheses (), not square brackets like in Java and Python.

36
Q

What is the difference between arrays and lists in Scala?

A

Arrays are mutable (you can add or replace elements as long as they are the same type), lists are immutable.

37
Q

How do you create a list in Scala?

A

val players = List(“Alex”, “Chen”, “Marta”)

38
Q

How do you prepend an element to a list in Scala?

A

val newPlayers = “Sindhu” :: players

or define players as a var, and use:

var players = “Sindhu” :: players

39
Q

What are :: in Scala?

A

Cons.

40
Q

How do you specify an empty list in Scala?

A

Nil

41
Q

How do you prepend an element to a list?

A

element :: list

42
Q

How do you concatenate lists in Scala?

A

list1 ::: list2

43
Q

What are some advantages of static type systems?

A
  • Increased performance at run time
  • Properties of your program verified (absnece of common type-related bugs)
  • Safe refactorings
  • Explicit documentations with type annotation
    *
44
Q

What are some inconvenients of static type systems?

A
  • 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
Q

How do you create an if statement in Scala?

A

if (hand > 21) {
println(“This hand busts!”)
}

Curly braces are a style guide recommendation.

46
Q

How do you create an if else statement in Scala?

A

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
Q

How do you define a while loop in Scala?

A

while (i < repetitions) {
body
}

48
Q

Why can Scala be imperative?

A

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
Q

Which Scala method lets you loop over a variable and apply a function to its elements?

A

variable.foreach(function)

50
Q

In computer science, what is a side effect?

A

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
Q

In Scala, what are some signs of the imperative style?

A
  • var
  • side effect
  • Unit
52
Q

In Scala, what are some signs of the functional style?

A
  • val
  • no side effects
  • Non-Unit value types
    • Int
    • Boolean
    • Double
53
Q

What are some benefits of the functional style?

A
  • 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
Q

In Scala, should you prefer the functional or the imperative style?

A

You should use the functional style at first instinct, and then use the imperative style if the need arises.