Scala Getting Started Flashcards

1
Q

Expression

A

Expressions are computable statements
1 + 1

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

Declare a value

A
  • Use the val keyword
    val x = 1 + 1
  • Values cannot be re-assigned
    x = 3 // This does not compile.
  • The type of a value can be omitted and inferred, or it can be explicitly stated
    val x: Int = 1 + 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Declare a variable

A
  • Variables are like values, except you can re-assign them.
  • You can define a variable with the var keyword.
    ~~~
    var x = 1 + 1
    x = 3 // This compiles because “x” is declared with the “var” keyword.
    ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Anonymous function

Explain and write an example

A
  • Function that has no name.
    (x: Int) => x + 1
  • On the left of ` =>` is a list of parameters. On the right is an expression involving the parameters.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Function

Short explanation

A
  • Function that has name.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Function with 0 param

Write example

A

val getAge = () => 32

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

Function with 1 param

Write example

A

val addOne = (x: Int) => x + 1

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

Function with > 1 params

Write example

A

val add = (x: Int, y: Int) => x + y

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

Method

A
  • Methods look and behave very similar to functions but with differences.
  • Defined with the def keyword.
  • Followed by a name, parameter list(s), a return type, and a body.
    def add(x: Int, y: Int): Int = x + y
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Method with 1 param list

Write example

A

def add(x: Int, y: Int): Int = x + y

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

Method with > 1 param lists

Write example

A

def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier

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

Method with 0 param list

Write example

A

def name: String = System.getProperty("user.name")

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

Methods with multi-line expressions

Write example

A
def getSquareString(input: Double): String = {
  val square = input * input
  square.toString
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Class

Write a class with a name, 2 params, and a method that doesn’t return any value

A
class Greeter(prefix: String, suffix: String) {
  def greet(name: String): Unit =
    println(prefix + name + suffix)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a new instance of a class

Write example

A

val greeter = new Greeter("Hello, ", "!")

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

Case class

Explain

A

A special type of class that is primarily used to hold immutable data. It is designed to be used in functional programming and is often employed for modeling immutable data structures, such as records or data transfer objects (DTOs)

By default, instances of case classes are immutable, and they are compared by value (unlike classes, whose instances are compared by reference). This makes them additionally useful for pattern matching.

17
Q

Define a case class

Write example

A

case class Point(x: Int, y: Int)

18
Q

Instantiate a case class

Write example

A
  • ` val point = Point(1, 2)`
  • val anotherPoint = Point(1, 2)

You can instantiate case classes without the new keyword

19
Q

Compare instances of case classes

Write example

A
if (point == anotherPoint) {
  println(s"$point and $anotherPoint are the same.")
} else {
  println(s"$point and $anotherPoint are different.")
} // Point(1,2) and Point(1,2) are the same.

Instances of case classes are compared by value, not by reference

20
Q

String interpolation

write example

A
val name = "Linh"  	
print(s"Hello $name")
21
Q

Keyword object

Definition

A
  • An object is a singleton instance of a class. It is a way to define a single instance of a class directly, without needing to create an explicit instance.
  • It is commonly used for creating utility classes, holding global state, or implementing design patterns like the Factory pattern.
  • An object can have methods, fields, and implement interfaces.
  • Singleton objects are lazily instantiated and their initialization is thread-safe.
object MySingleton {
  def sayHello(): Unit = {
    println("Hello, I am a singleton object!")
  }
}
22
Q

Access an object

A

By inferring to its name
~~~
val newId: Int = IdFactory.create()
~~~

23
Q

Keyword class

Definition

A
  • A class is a blueprint for creating objects.
  • It defines the properties (fields) and behavior (methods) that objects of that class will have.
  • You can create multiple instances (objects) from a class, each with its own state.
  • Classes can also extend other classes or implement interfaces.
  • To create an instance of the class and access its methods, you need to instantiate it using the new keyword.
class Greeter(prefix: String, suffix: String) {
  def greet(name: String): Unit =
    println(prefix + name + suffix)
}
24
Q

Keyword trait

Definition

A
  • Traits are abstract data types containing certain fields and methods.
  • In Scala inheritance, a class can only extend one other class, but it can extend multiple traits.
trait Greeter {
  def greet(name: String): Unit
}

  • Traits can also have default implementations:
    ~~~
    trait Greeter {
    def greet(name: String): Unit =
    println(“Hello, “ + name + “!”)
    }
    ~~~
25
Q

Program entry point

Definition

A

The main method is the entry point of a Scala program. The Java Virtual Machine requires a main method, named main, that takes one argument: an array of strings.

In Scala 2 you must define a main method manually. Using an object, you can define the main method as follows:

object Main {
  def main(args: Array[String]): Unit =
    println("Hello, Scala developer!")
}
26
Q

Automatic generated methods of a case class

A
  • equals()
  • hashCode()
  • toString()
  • copy()
  • pattern matching support