Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Learn how to program in Scala, one of the most popular programming languages in the world right now — not just amongst developers, but even amongst massive companies like Twitter and LinkedIn.
Scala provides you the tools to build scalable programs easily and effectively. It’s a statically typed, high-level language that combines functional programming and object-oriented programming into one flexible package.
This course will help you stay ahead of the curve, make awesome, scalable apps, and learn a highly coveted new programming language. Start learning today.
Answer:
val myFirstVariable: Int = 100
print(myFirstVariable)
Answer:
val newType: Double = oldType
Q1. Variables are used for storing data.
Q2. What role does the variable name play when declaring a variable?
Q3. Which print satement should be used for printing on mulitple lines?
print()
println()
Q4. What will be the output of the following code:
val randomNumber: Int = 39
randomNumber = 72
println(randomNumber)
Q5. What is the default type of a Scala Variable?
Q6. Which subclass does the data type Byte
come under?
AnyVal
Anyref
Q7. What is the flow of Scala’s type hierarchy from top to bottom?
AnyRef
to AnyVal
AnyVal
to AnyRef
Q8. What is Scalas supertype?
All
Each
Every
Any
Q9. Which of the following is allowed in terms of type casting?
Long
=> Int
Double
=> Float
Short
=> Int
Byte
=> Char
Q10. What are the three requirements for declaring a variable?
Answer:
val compareCheck = check < 75 && check >= 8
Answer:
val celsius = (fahrenheit - 32) * (5D / 9D)
Q1. There are two ways to call a method in Scala.
Q2. Which method call uses the infix notation?
Q3. How many built-in operators does Scala provide?
Q4. What is the difference between ordinary method calls and operator method calls?
Q5. Select the correct output for the following code:
val operand1 = 7
val operand2 = 3
println(operand1 / operand2)
Q6. Select the correct output for the following code:
val operand1 = 's'
val operand2 = 'q'
println(operand1 <= operand2)
Q7. Select the correct output for the following code:
val A = true
val B = true
val exp = A || B
println(false && exp)
Q8. Select the correct output for the following code:
val operand1 = 7
val operand2 = 3
println(operand1 & operand2)
Q9. Select the correct output for the following code:
var operand1 = 7
var operand2 = 3
operand1 += operand2
println(operand1)
Q10. Which of the following is correct in terms of operator precedence?
<
, *
, &
!=
, |
, ^
==
, :
, ( )
Answer:
print(s"$name is $age years old.")
Answer:
print(f"The quotient is $quotient%.3f")
Answer:
val expressionToFind = "[a-z]{3}".r
val match1 = expressionToFind.findFirstIn(stringToFindExpression)
Q1. What is string interpolation?
Q2. How many string interpolation methods does Scala provide?
Q3. What are processed string literals?
Q4. What would the output be of the given code:
println(s"3 + 2 = ${3 + 2}")
Q5. Which Java method does the f
string interpolator represent?
fprint()
print()
printf()
Q6. What would the output be of the given code:
val insertSeparator = 12345678
println(f"$insertSeparator%,d")
Q7. What is the difference between the raw
string interpolator and the s
string interpolator?
raw
recognizes escape sequences while s
doesn’traw
doesn’t recognize escape sequences while s
doesraw
recognizes character literals while s
doesn’traw
doesn’t recognize character literals while s
doesQ8. The split()
method can only split at a comma ,
.
Q9. What encompasses multiline strings?
'''
""""
"""
Q10. What would the output be of the given code:
val regularExp = "[a-z]{4}".r
val replaceIn = "She is four years old "
val replaced = regularExp.replaceFirstIn(replaceIn,"five")
println(replaced)
Answer:
val array1 = Array.range(minRange, maxRange)
val array2 = array1.map(_ * 3)
val finalArray = array2.filter(_ % 2 == 0)
Answer:
val finalList = list :+ list(0)
Q1. How many collection classes are at the top of the collection library?
Q2. What is the difference between an immutable and mutable collection?
Q3. Which collection class does not store a single value more than once?
set
map
seq
Q4. Choose the correct syntax for the foreach
method.
foreach
(collection)foreach
)foreach
(method)Q5. What will be the output of the following code:
val array1 = Array(1,2,3,4,5)
val array2 = array1.map(_ * 5)
val finalArray = array2.filter(_ % 2 != 0)
finalArray.foreach(println)
Q6. What will be the output of the following code:
val list1 = 39::27::1::83::Nil
val finalList = 5 +: list1
finalList.foreach(println)
Q7. What is the difference between a Vector and a List?
seq
type collection and Lists are an immutable seq
type collectionseq
type collection and Vectors are an immutable seq
type collectionQ8. The following expressions use the Range
collection. Which one is incorrect?
10 to 20
10 to 20 by 5
20 from 10
10 until 20
Q9. Why is a Stream called a lazy List?
#::
instead of ::
Answer:
if (percentage >= 60)
print("pass")
else
print("fail")
Answer:
while (temperature<375) {
temperature += 25
count +=1
}
Answer:
for (i <- 0 until testArray.length) {
testArray(i) = testArray(i) * testArray(i)
}
Answer:
testVariable match {
case "blue" => print("orange")
case "yellow" => print("purple")
case "red" => print("green")
case _ => print("not a primary color")
}
Q1. What are control structures used for?
Q2. Which statements regarding imperative and declarative programming are true? (Multiple correct answers)
Q3. Which statement is depictive of imperative programming?
Q4. How many control structures does Scala provide?
Q5. How does Scala bring a functional approach to imperative control structures?
Q6. What will be the output of the following code:
val array = Array(1,2,3,4,5)
for (i <- 0 until array.length) {
if (array(i) % 2 == 0) {
println(array(i))
}
}
Q7. What will be the output of the following code:
val constantPattern: Any = List()
constantPattern match {
case 5 => print("five")
case true => print("truth")
case "hello" => print("hi!")
case Nil => print("the empty list")
case _ => print("something else")
}
Q8. What will be the output of the following code:
val sequencePattern = Array(1,2,0)
sequencePattern match {
case Array(0,_,_) => println("case1")
case Array(1,_,_) => println("case2")
case Array(_,_,0) => println("case3")
case _ => println("default")
}
Q9. while
is an expression.
Q10. How is while
different from the other control structures?
while
.Answer:
def absolute(x: Double): Double = {
if (x < 0) -x else x
}
Q1. evaluate(2,3)
Q2. evaluate(3+4, 8)
Q3. evaluate(7, 2*4)
Answer:
//Solution using match
def sum(numberList: List[Int]): Int = numberList match {
case Nil => 0
case x :: tail => x + sum(tail)
}
Answer:
def mainMax(a: Int, b: Int, c: Int): Int = {
def max(x: Int, y: Int) = {
if(x > y) x
else y
}
max(a,max(b,c))
}
Answer:
def factorial(x: Int): Int = {
def loop(accumulator: Int, x: Int): Int = {
if(x==0) accumulator
else loop(accumulator*x,x-1)
}
loop(1,x)
}
Q1. Which keyword is used for defining a function?
val
var
def
Q2. The following are optional when defining a basic function.
Q3. What is the basic idea of the substitution model?
Q4. What purpose do CBV and CBN have?
Q5. If the CBN evaluation of an expression terminates, then the CBV evaluation of the same expression is not guaranteed to terminate.
Q6. What does the following recursive function do?
def mysteryFunction(list: List[Int]): Int = {
def mysteryAccum(list: List[Int], theMystery: Int): Int = {
list match {
case Nil => theMystery
case x :: tail =>
val newMystery = if (x > theMystery) x else theMystery
mysteryAccum(tail, newMystery)
}
}
mysteryAccum(list, 0)
}
Q7. What is the general purpose of Newton’s method?
Q8. Which function definition will give an error?
def factorial(x: Int) = {
def loop(accumulator: Int, x: Int): Int = {
if(x==0) accumulator
else loop(accumulator*x,x-1)
}
loop(1,x)
}
def factorial(x: Int): Int = {
def loop(accumulator: Int, x: Int) = {
if(x==0) accumulator
else loop(accumulator*x,x-1)
}
loop(1,x)
}
def factorial(x: Int): Int = {
def loop(accumulator: Int, x: Int): Int = {
if(x==0) accumulator
else loop(accumulator*x,x-1)
}
return loop(1,x)
}
Q9. What is the benefit of lexical scoping?
Q10. Which of the following is a tail recursive function?
def fib(x: Int): BigInt = {
def fibHelper(x: Int, prev: BigInt = 0, next: BigInt = 1): BigInt = x match {
case 0 => prev
case 1 => next
case _ => fibHelper(x - 1, next, (next + prev))
}
fibHelper(x)
}
def sum(ints: List[Int]): Int = ints match {
case Nil => 0
case x :: tail => x + sum(tail)
}
def factorial(n: Int): Int = {
if (n == 0) 1
else n * factorial(n-1)
}
Q11. Which of the following statements is true for blocks?
Answer:
def arithmeticPrinter(f: (Int , Int) => Int, x: Int, y: Int) = {
print(f(x,y))
}
Answer:
def arithmeticPrinter(f: (Int,Int) => Int, x: Int, y: Int) = {
print(f(x,y))
}
def printAdd(x: Int, y: Int) = {
arithmeticPrinter((x,y) => x+y, x, y)
}
def printSubtract(x:Int, y: Int) = {
arithmeticPrinter((x,y) => x-y, x, y)
}
Answer:
def product(f: Int => Int)(a: Int, b: Int): Int ={
if(a > b) 1
else f(a) * product(f)(a+1,b)
}
def fact(n: Int) = product(x=>x)(1,n)
Q1. What does it mean for a function to be treated as a first-class value?
Q2. The code snippets below are using functions as function parameters. Which of the following will result in a syntax error?
def func1(f: Int => Int, x: Int) = {
print(f(x))
}
def func2(f: Int => Int, x: Int) = {
print(f)
}
def func3(x: Int, f: Int => Int) = {
print(f(x))
}
def func4(f: (Int,Int) => Int, x: Int, y: Int) = {
print(f(x,y))
}
Q3. The code snippets below are anonymous functions which return the product of two numbers. Which of the following are the incorrect implementation?
(x,y) => x*y
x,y => x*y
(x:Int, y:Int) => x*y
Q4. What is syntactic sugar?
Q5. Anonymous functions are syntatic sugar because they don’t bring any change to the expressive power of Scala.
Q6. The function saySomething
, returns an anonymous function. What will be the output of the following code?
def saySomething(prefix: String) = (s: String) => {
prefix + " " + s
}
def saySomethingElse = saySomething("hello")
print(saySomethingElse("there"))
Q7. What is currying?
Q8. add
is a curried function. What will be the output of the follwoing code?
def add(a: Int)(b: Int) = a + b
val first = add(1)(5)
val second = add(4)_
val third = second(2)
print(first == third)
true
false
Q1. A bluprint is used to create identical copies of the same object.
Q2. The members of a class can be divided into the following two parts:
Q3. Why is an object known as an instance of a class?
Q4. The code below declares a EqualShape
class and creates its instance sqaure
. Why will the code not compile?
class EqualShape{
var numOfSides: Int = 0
var lengthOfSides: Int = 0
def perimeter = numOfSides * lengthOfSides
}
val square = EqualShape
Q5. What will be the output of the following code?
class EqualShape{
var numOfSides: Int = 0
var lengthOfSides: Int = 0
def perimeter = numOfSides * lengthOfSides
}
val square = new EqualShape
val triangle = new EqualShape
square.numOfSides = 4
square.lengthOfSides = 5
print(triangle.perimeter)
Q6. When we can pass arguments to an object class, this means that the class was defined using…
Q7. What will be the output of the follwoing code?
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
}
val point = new Point(5, 10)
point.move(point.x+1, point.y+2)
Q8. What is the difference bewteen a singleton object and a class?
object
keyword while a class is created using the class
keyword.new
keyword while a class object must be created using the new
keyword.Q9. A companion object can access private members of its companion class.
Q10. What is the use of stand alone objects?
I hope this Learn Scala from Scratch Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>