Scala Flashcards

1
Q
Methods and values that aren’t associated with individual instances of a class belong in \_\_\_\_\_\_\_
 denoted by using the keyword \_\_\_\_\_ instead of class
A

singleton objects

object

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

Most singleton objects do not stand alone, but instead are associated with a class of the same name. When this happens, the singleton object is called the ______ of the class and the class is called the _______ of the object

A

companion object

companion class

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

A class and its companion object, if any, must be defined

A

in the same source file

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

static is not a keyword in Scala. Instead, all members that would be static, including classes …

A

should go in a singleton object.

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

what does => do in scala

A

creates an instance of a function

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

in scala every function is

A

an instance of a class

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

when you say 1 + 2 in Scala, you are actually

A

invoking a method called + defined in class int

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

Traits are like interfaces in Java,

but they can also have

A

method implementations and even fields

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

Functional programming is guided by two main ideas.

A
  1. A function is a value of the same status as, say, an integer or a string. You can pass functions as arguments to other functions, return them as results from functions
  2. Operations of a program should map input values to output values rather than change data in place (data structures are immutable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
How would you write this in scala?
// this is Java
boolean nameHasUpperCase = false;
for (int i = 0; i < name.length(); ++i) {
if (Character.isUpperCase(name.charAt(i))) {
nameHasUpperCase = true;
break;
}
}
A

val nameHasUpperCase = name.exists(_.isUpper)

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

The predicate _.isUpper is an example

A

of a function literal

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

A function literal can be called a predicate if

A

its result type is Boolean

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

What does a static type system do?

A

classifies variables and expressions according to the

kinds of values they hold and compute

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

val msg = “Hello, world!”
neither java.lang.String nor String appear anywhere
in the val definition. Why?

A

this is an example of type inference

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

the first element in a Scala array

named steps is

A

steps(0) not steps[0] as in java

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

what is the syntax for a function literal

A

a list of named parameters, in parentheses, a right arrow, and then the body of the function.
(x: Int, y: Int) => x + y

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

when you type 1 + 2

into the Scala interpreter what are you actually doing

A

invoking a method

named + on the Int object 1, passing in 2 as a parameter

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

Scala’s List, scala.List, differs from Java’s java.util.List

type in that Scala Lists are …

A

always immutable

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

What is the method on Scala’s List that is used for concatenation

A

:::

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

What is :: in Scala

A

“Cons” it prepends a value to the beginning of a list

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

If a method is used in operator notation, such as a * b, the

method is invoked on the left operand, as in a.*(b)—unless …

A

the method name ends in a colon. If the method name ends in a colon, the method is
invoked on the right operand. Therefore, in 1 :: twoThree, the :: method
is invoked on twoThree

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

What is the append operation for a list?

A

:+

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

What does this do?
val thrill = “Will” :: “fill” ::
“until” :: Nil

A

Creates a new List[String] with the
three values “Will”, “fill”, and
“until”

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

val oneTwoThree = 1 :: 2 :: 3 :: Nil

why is there a Nil at the end?

A
The reason you need Nil at the end is that :: is defined on class List. If you try to just
say 1 :: 2 :: 3, it won’t compile because 3 is an Int, which doesn’t have a :: method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

what does

List(“a”, “b”) ::: List(“c”, “d”) do

A

concatenates the two lists into a single list

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

thrill.count(s => s.length == 4)

what does this do?

A

Counts the number of string elements in

thrill that have length 4

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

thrill.drop(2)

assuming thrill is a list what does this code do

A

returns the list without the first two entries

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

thrill.dropRight(2)

assuming thrill is a list what does this code do

A

Returns the thrill list without its

rightmost 2 elements

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

thrill.exists(s => s == “until”)

assuming thrill is a list what does this code do

A

Determines whether a string element
exists in thrill that has the value
“until”

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

assuming the list was called thrill how would you return a list of all elements, in order, of
the thrill list that have length 4

A

thrill.filter(s => s.length ==4)

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

Indicates whether all elements in the

thrill list end with the letter “l”

A

thrill.forall(s=>s.endsWith(“l”))

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

Returns the first element in the thrill

list

A

thrill.head

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

Returns a list of all but the last element in

the thrill list

A

thrill.init

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

Indicates whether the thrill list is

empty

A

thrill.isEmpty

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

Returns a list resulting from adding a “y”

to each string element in the thrill list

A

thrill.map(s=>s + “y”)

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

Another useful container object is the tuple. Like lists, tuples are immutable,
but unlike lists, tuples can

A

contain different types of elements

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

Whereas in Java you would often create a JavaBean-like class to hold the multiple return values, in Scala you can simply return a

A

Tuple

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

to instantiate a new tuple that contains some objects simply

A

place the objects in parentheses, separated

by commas.

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

why you can’t access the elements of a tuple

like the elements of a list, for example, with “pair(0)”.

A

The reason is that a list’s apply method always returns the same type (as a method always should) but each element of a tuple may be a different type.

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

Because Scala aims to help you take advantage of both functional and imperative styles, its collections libraries make a point to differentiate between mutable and immutable collections. For example

A

lists are always immutable

arrays are always mutable

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

are Sets and Maps mutable or immutable in Scala?

A

Scala supports mutable and immutable sets and maps though the default is immutable

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

If the default Set declaration myset = Set(“blah”,”blah”) creates an immutable set how do you create an immutable one

A

You need to import the library
import scala.collection.mutable.Set
first

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

The telltale sign of a function with

side effects is that its result type is

A

Unit

If the function is not returning anything interesting then it must be producing a side effect

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

val lines = Source.fromFile(args(0)).getLines().toList

why do you need the toList

A

The final toList is required because the getLines method returns an iterator.
Once you’ve iterated through an iterator, it is spent. By transforming it
into a list via the toList call, you gain the ability to iterate as many times
as you wish

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

what does the reduceLeft method do?

A

works by applying the function/operation you give it, and applying it to successive elements in the collection. The result of the first comparison is used in the second comparison, and so on.

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

how do you declare a method in scala

A

def

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

how do you make members public in scala

A

you don’t do anything explicitly

members are public by default

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

One im- portant characteristic of method parameters in Scala is that

A

they are vals not vars

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

One puzzler to watch out for is that whenever you leave off the equals sign before the body of a function, its result type will definitely be

A

Unit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q
what is returned by this
def g() { "blah" }
why?
A

Unit

there is no ‘=’ before the body of the function

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

one way in which Scala is more object-oriented than Java is

A

Scala cannot have static members, it uses singleton objects

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

When a singleton object shares the same name with a class, it is called that class’s

A

companion object

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

A class and its companion object can access …

A

each others private members

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

can singleton objects take parameters?

Why?/Why not?

A

No

You cannot instantiate an object using new - thus there is no way to pass parameters

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

To run a Scala program, you must

A

Supply the name of a standalone singleton object with a main method that takes one parameter, an Array[String],

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

What can you do instead of creating a singleton object with a main method?

A

write “extends Application” after the name of your singleton object

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

Inheriting from Application is shorter than writing an explicit main method, but it also has some shortcomings.

A
  1. you can’t use this trait if you need to access command-line arguments,
  2. you need an explicit main method if your program is multi-threaded.
  3. some implementations of the JVM do not optimize the initialization code of an object which is executed by the Application trait. So you should in- herit from Application only when your program is relatively simple and single-threaded.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

In Scala operators are not special language syntax - what does this mean

A

any method can be an operator

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

what is infix operator notation?

A

method to invoke sits between the object and the parameter or parameters you wish to pass to the method, as in “7 + 2”

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

what is prefix operator notation

A

method to invoke sits before the object e.g. -7

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

In Scala, you can leave off empty parentheses on method calls. The convention is that you include parentheses if …

A

the method has side effects, such as println()

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

How many parameters does a postfix operator take?

What does this mean for notation?

A

postfix operators take no parameters

no brackets

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

In Java, classes have constructors, which can take parameters, whereas in Scala,

A

classes can take parameters directly

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

The syntax of a for-yield expression is like this

A

for yield

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

scala’s ____ expression allows you to select from a number of alternatives, just like switch statements in other languages

A

match

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

(x: Int) => x + 1

what does => designate

A

designates that this function converts the thing on the left (any int) to the thing on the right (x+1)

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

you can use underscores as placeholders for one or more parameters, so long as

A

each parameter appears only one time within the function literal.

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

The function literal _ > 0, therefore, is equivalent to the slightly more verbose

A

x => x > 0

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

A partially applied function is an expression in which

A

you don’t supply all of the arguments needed by the function. Instead, you supply some, or none, of the needed arguments.

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

what is going on here

val partialSum2ArgumentsProvided = sum(1, 2, _:Int)

A

this is a partially applied function, what is returned is not a value but a function with two of its arguments pre-supplied

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

(x: Int) => x + 1 why is this not a closure in the strictest sense?

A

Because it is already closed there are no free variables

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

any function value created at runtime from (x: Int) => x + more will by definition require that a binding for its free variable “more” be
captured. The resulting function value, which will contain a reference to the captured “more” variable, is called a

A

closure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q
def plainOldSum(x: Int, y: Int) = x + y
write this as a curried function
A

def curriedSum(x: Int)(y: Int) = x + y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q
how would you invoke 
def curriedSum(x: Int)(y: Int) = x + y
A

curriedSum(1)(2)

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

curriedSum(x: Int)(y: Int) = x + y

how would you get an actual reference to curriedSum’s “second” function

A

val onePlus = curriedSum(1)_

is a placeholder for the second parameter list

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

what is happening here

val someValue = foobar(1)_

A

someValue is a reference to the second part of a partially applied curried function.
When invoked, adds one to its sole Int argument and returns the result

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

loan pattern

A

a control-abstraction function opens a resource and “loans” it to a function and cleans up when the function is finished

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

what are composing operators often called

A

combinators

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

Thinking in terms of combinators is generally a good way to approach library design - why

A

it pays to think about the fundamental ways to construct ob- jects in an application domain.

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

A class with abstract members must

A

itself be declared as abstract

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

how do you make a scala class abstract

A
by writing an abstract modifier in front of the class keyword:
abstract class Element
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

A method is abstract if

A

it does not have an implementation (i.e., no equals sign or body).

83
Q

uniform access principle

A

client code should not be affected by a decision to implement an attribute as a field or method

84
Q

what is an empty-paren method

A

methods defined with empty parentheses, such as def height(): Int

85
Q

when can you use an parameterless method rather than an empty-paren

A

when your method only reads local val’s and does not change mutable state

86
Q

Can you override a parameterless method with an empty-paren method, and vice versa

A

yes

87
Q

To summarize, it is encouraged style in Scala to define methods that take no parameters and have no side effects as

A

parameterless methods

88
Q

Inheritance means that all members of the superclass are also members of the subclass, with two exceptions.

A
  1. Private members of the superclass are not inherited

2. It is not inherited if a class with the same name and signature is already in the subclass (it is overridden)

89
Q

what does subtyping mean

A
a value of the subclass can be used wherever a
value of the superclass is required.
90
Q

Scala, fields and methods belong to the same namespace. This makes it possible for

A

a field to override a parameterless method

91
Q

Generally, Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are:

A
  • values (fields, methods, packages, and singleton objects)

* types (class and trait names)

92
Q

Difference in memory utilization in threaded and evented web servers

A

Evented web servers make much better use of hardware resources than threaded ones.

93
Q

In scala you must use _____ not ______ to convert between numeric types for example to convert 99.44 to an integer use

A

methods
casts
99.44.toInt

94
Q

a + b is shorthand for

A

a.+(b)

95
Q

in a.+(b) which is the method

A

+

96
Q

in general ______ is shorthand for a.method(b)

A

a method b

97
Q

from a style point of view when should you not use parenthesis

A

when there are no parameters and the method has no side effects

98
Q

to find the fourth element of a string you would use ____

not _____ as in other languages

A

mystring(4)

mystring[4]

99
Q

Occasionally the () notation conflicts with another scala feature namely

A

implicit parameters.

100
Q

Why would “Bonjour”.sorted(3) yield an error? What would be a solution

A

because the sorted method can optionally be called with an ordering (implicit parameter)
To solve this call the apply method explicitly as in “Bonjour”.sorted.apply(3)

101
Q

A method to determine how many uppercase letters are in a string “ThisString”

A

“ThisString”.count(_.isUpper)

102
Q

In scala what are square brackets used for

A

for Type parameters

103
Q

how would you determine whether the string “big bloody wombat” contained the word bloody?

A

“big bloody wombat”.constainsSlice(“bloody”)

104
Q

What is the value of a block in scala

A

the value of its last expression

105
Q

What is the void type in scala

A

Unit

106
Q

Why is
val s = if (x > 0) 1 else -1
better than
if (x >0) s=1 else s=-1

A

The first form is better because s is a val not a var

107
Q

A block that ends with an assignment such as

{r = r * n; n-= 1} has a value of

A

Unit

(Assignments have a Unit Value) the last statement in the block is an assignment

108
Q

since assignments have Unit value what should we not do

A

chain them together

x = y = 1

109
Q

what are the three string interpolators in the Scala library?

A

f, s and raw

110
Q

What is the difference between the string interpolators f, s and raw

A

f - contains expressions that prefixed with a $ and are optionally followed by a C style format string
s- contains expressions but not format directives
raw - escape sequences are ignored.

111
Q

How do you include a $ sign in an interpolated string

A

double it $$

if you want a dollar sign to appear before the value of price $$$price

112
Q

for (i

A

makes the variable i traverse all the values of the expression to the right

113
Q

how do you define a function

A
specify the functions name, parameters and body 
def abs(x: Double) = if (x >= 0) x else -x
114
Q

is a return type necessary in scala

A

only when the function is recursive

115
Q

Why would you not use a return function?

A

return is a kind of break statement that ends the execution of a function. While this is okay in a named function it is a problem in anonymous functions, because it would break out of any enclosing named function

116
Q

implement a function called sum that takes a variable number of arguments

A
def sum(args: Int*) {
blah
}
117
Q

What is the difference between a function and a procedure in the way that they are declared?

A

When a function body is enclosed in braces without a preceeding = symbol it returns a Unit and it is called a procedure

118
Q

declare a val whose initialisation is deferred until accessed for the first time

A

lazy val words = scala.io.Source.fromFile(“/usr/share/dict/words”).mkstring

119
Q

What are checked exceptions and how are they used in scala

A

In java checked exceptions are checked at compile time. If your method might throw an IOException you must declare it - in the method signature Scala does not use checked exceptions.

120
Q

Use an ______ if the length is fixed and an _______ _____ if the length can vary

A

Array

Array Buffer

121
Q

how do you travers an the elements of an array

A

(elem

122
Q

Java has ArrayList and C++ has vector for arrays that grow and shrink on demand what is the scala equivalent?

A

ArrayBuffer

123
Q

you can append any collection with …

A

++=

124
Q

how would you remove the last five elements of an array

A

arrayname.trimEnd(5)

125
Q

How would you add Array(1,2,3,4) to your existing array called stuff

A

stuff ++= Array(1,2,3,4)

126
Q

how do you add to an array at a specific location

A

insert(,)

127
Q

how would you convert an array to an array buffer

A

a.toBuffer

128
Q

how would you display the contents of an array with a “and” between each item

A

myArray.mkString(“ and “)

129
Q

To create a multidimensional array use the ____

A

ofDim method of the Array class

130
Q

What is another word for a map

A

a hash table

131
Q

maps are collections of

A

key value pairs

132
Q

what is a tuple

A

a collection of n objects not necessarily of the same type

133
Q

in a quiz alice scored 3 dave scored 5 and rod scored 12

how would you create an immutable map called scores containing these values

A

val scores = Map(“alice” -> 3, “dave” -> 5, “rod” -> 12)

134
Q

what does the -> operator do

A

makes a pair

135
Q

A tuple value is formed by

A

enclosing individual values in parenthesis

136
Q

If you have a tuple

val t = ( 1, 3.14, “Fred”) you can access its components with the methods

A

_1, _2, and _3

137
Q

unlike array or string positions the component positions of a tuple start with

A

1 not 0

138
Q

One reason for using tuples is to bundle together values so that they can be processed together this is commonly done with the ____ method

A

zip

139
Q

how would you create a pairs array that links the arrays symbols and counts together so that each element of symbols is paired with each element of counts

A

val pairs = symbols.zip(counts)

140
Q

what is age_=

A

an automatically generated setter method for the age propery

141
Q

what does the private [classname] qualifier for a field do

A

states that only methods of the given class can access the given field.

142
Q

scala has one constructor that is more important than all the others called the

A

primary constructor

143
Q

auxiliary constructors are called

A

this

144
Q

each auxiliary constructor must start with a call to

A

a previously defined auxiliary constructor or the primary constructor

145
Q

a class for which you not define a primary constructor has a

A

primary constructor with no arguments

146
Q
explain this code
def this(name: string) {
this()
this.name = name
}
A

It is an auxiliary constructor

it is called ‘this’ and the first thing it does is call the primary constructor this()

147
Q

What happens if a primary constructor is private

A

A user must then use an auxiliary constructor to construct the object

148
Q

scala has not static methods or fields instead you use

A

the object construct

149
Q

When is the constructor of an object called

A

when the object is first executed, if an object is never used its constructor is not executed

150
Q

An object can have all the features of other class it can even extend classes or traits. There is just one exception

A

You cannot provide constructor parameters

151
Q

When would you use an object in scala

A
  • As a home for utility functions or constraints
  • When a single immutable instance can be shared efficiently
  • when a singleton is required
152
Q

In Java and C++ you can have a class with instance methods and static methods. In Scala you achieve this by

A

having a class and a companion object

153
Q

Where must a companion object be located

A

In the same file source file as the class. In the REPL you must use :paste

154
Q

It is common for all objects to have an apply method. The apply method is called for expressions of the form

A

object(arg1 … argN)

155
Q

It is easy to confuse Array(100) and new Array(100) what is the difference

A

The first expression calls apply(100), yielding an Array[Int} with the single element the integer 100. The second expression invokes the constructor this(100). The result is an Array[Nothing] with 100 null elements

156
Q

Each Scala program must start with …

A

on objects main method of type Array[String] => Unit

157
Q

If you were to create a method called blowme of type Array[String] => Unit what would it look like

A
def blowme(args: Array[String])
{
println("I don't return anything")
}
158
Q

In scala you must use the _____ modifier when you override a method that is not abstract

A

override

159
Q

To test whether an object belongs to a given class use

A

isInstanceOf[TypeName] method

160
Q

If you want to test whether p references an Employee object but not a subclass use

A

if (p.getClass == classof[Employee])

161
Q

A scala class can extend a Java class. Its primary constructor must invoke

A

one of the constructors of the Java superclass

162
Q

As in Java you can use the abstract keyword to denote a class that

A

cannot be instantiated - usually because one or more of its methods are not defined

163
Q

In Scala unlike Java you do not use the abstract keyword for an abstract method you simply

A

omit its body

164
Q

In a subclass you need not use the override keyword when you

A

define a method that was abstract in the superclass

165
Q

To convert a string to a number use the

A

toInt or toDouble method

166
Q

how would you read from a text file in scala

A

import scala.io.Source
val source = Source.fromFile(“myfile.txt”)
val lineIterator = source.getLines

167
Q

val thing = source.getLines

What can you do with the result

A

It is an iterator you can use it as follows

for(j

168
Q

what should you do to when you are finished using the source object

A

call close

169
Q

to read individual characters from a file you can use

A

the source object directly as in

for (c

170
Q

Associative maps are very useful because they help keep programs legible and concise. However, sometimes you might not agree with their “one size fits all” philosophy, because you need to control the properties of the maps you use in your program in a more fine-grained way. Scala gives you this fine-grained control if you need it, because

A

maps in Scala are not lan- guage syntax. They are library abstractions that you can extend and adapt.

171
Q

What are actors

A

concurrency abstractions that can be implemented on top of threads

172
Q

An actor can perform two basic operations

A

message send and receive

173
Q

The send operation is denoted by

A

!

174
Q

Every actor has a mailbox in which

A

incoming messages are queued

175
Q

An actor handles messages that have arrived in its mailbox via a

A

receive block

176
Q

what problem with multiple inheritance does the traits system avoid

A

“diamond inheritance” problems which arise when the same class is inherited via several different paths.

177
Q

What do we mean when we say functions are first class values

A

a function is a value of the same status as, say, an integer or a string.

  1. You can pass functions as arguments to other functions, return them as results from functions, or store them in variables.
  2. You can also define a function inside another function, just as you can define an integer value inside a function.
  3. You can define functions without giving them a name - function literals
178
Q

Functional programming is guided by two main ideas.

A
  1. Functions are first-class values.
  2. A program should map input values to output values rather than trying to change data in place
    (no side effects)
179
Q

args.foreach(arg => println(arg))

what would also work? Why?

A

args.foreach(println)
If a function literal consists of one statement that takes a single argument, you need not explicitly
name and specify the argument.

180
Q

When you declare an array with val how is it still mutable?

A

The array itself cannot change (still the same length and type) but the elements inside the array can.

181
Q

For an immutable sequence of objects that share the same type you can use

A

Scala’s list class

182
Q

Scala’s List, scala.List, differs from Java’s java.util.List type in that

A

Scala Lists are always immutable (whereas Java Lists can be mutable).

183
Q

Perhaps the most common operator you’ll use with lists is ‘::’, which is pronounced …

A

cons

184
Q

Higher order functions

A

These are functions that take a function as a parameter or return function

185
Q

Call by value:

A

Call by value: evaluates the function arguments before calling the function

186
Q

evaluates the function arguments before calling the function

A

Call by value:

187
Q

Call by name:

A

Call by name: evaluates the function first, and then evaluates the arguments if need be

188
Q

evaluates the function first, and then evaluates the arguments if need be

A

Call by name:

189
Q

def example = 2 //when is this evaluated?

A

when called

190
Q

val example = 2 // when is this evaluated

A

immediately

191
Q

lazy val example = 2 //when is this evaluated

A

when needed

192
Q

def square(x: Double) //call by value or name

A

value

193
Q

def square(x: => Double) //call by value or name

A

name

194
Q

how do you ensure that the arguments in a class constructor have meet certain requirements?

A

use the require function

require(y > 0, “y must be positive”)

195
Q

In Scala, the standard is to indent using ? spaces (no tabs).

A

2

196
Q

The substitution model (lamda calculus) can only be applied to expressions that

A

do not have a side effect

197
Q

What is the advantage of call by value

A

It evaluates every function only once

198
Q

Both call by value and call by name evaluation strategies reduce an expression to the same value provided …

A

both evaluations terminate

199
Q

If the call by value (CBV) evaluation of an expression terminates then … but …

A

The call by name (CBN) evaluation of that expression will also terminate - but the same is not true the other way round

200
Q

What is the default method of evaluation for scala

A

Call by value

201
Q

How would you force a call by name?

A

You would insert => between the name of the value and the type myVal: => Int

202
Q

Work out the rewrite rules for booleans for

if(b) e1 else e2

A

if(true) e1, else e2 -> e1

if(false) e1, else e2 -> e2

203
Q
def loop: Boolean = loop
def x = loop
would this work? Why / Why Not?
A

Yes, because def x = loop evaluates only when it is executed (by name) so it would not try to execute the infinite loop