Question | Click to View Answer |
Is the following function pure? Explain. def add(x: Int, y: Int): Int = {
x + y
}
|
The
|
Is the following function pure? Explain. def changeStuff: Unit = {
x = x + 10
}
|
The var x = 33
changeStuff
// x is now 43
|
Is the following function pure? Explain. def funify: String = {
word.append("Fun!").toString
}
|
The var word = new StringBuilder("Beach")
funify
// word is now BeachFun!
funify
// word is now BeachFun!Fun!
|
Is the following class Person(n: String) {
var name = n
}
def changeName(p: Person): Unit = {
p.name = "cathy"
}
var bob = new Person("bob")
changeName(bob)
|
The |
Is the following function pure? Explain. def fullName(firstName: String, lastName: String): String = {
s"$firstName $lastName"
}
fullName("Ricky", "Bobby")
|
The |
Is the following function pure? Explain. def loco: Unit = {
println("toma 4loco!")
}
loco
|
The |
Is the following function pure? Explain. import java.io._
def shaggy: Unit = {
val path = s"${System.getProperty("user.home")}/Desktop/mr_lover.txt"
val file = new File(path)
file.createNewFile();
}
|
The |
Is the following function pure? Explain. def divide(numerator: Double, denominator: Double): Double = {
if (denominator == 0)
throw new IllegalArgumentException("The denominator can't be zero")
else
numerator / denominator
}
|
The |