Unit 2: Introduction to Programming in Scala - Practice Quiz
1 Which programming paradigms are primarily supported by Scala?
2 On which platform does Scala commonly run?
3
Which Scala data type stores a whole number such as 25?
String
Int
Double
Boolean
4 Which of the following is a Boolean literal in Scala?
true
't'
1
"true"
5 Which literal correctly represents a character in Scala?
"A"
[A]
{A}
'A'
6 Which operator is used to calculate the remainder of a division in Scala?
/
%
+
*
7
In Scala, the expression 1 + 2 can also be written as which method call?
1.+(2)
1.plus{2}
1.sum(2)
1.add[2]
8 What is type inference in Scala?
9
What type is inferred for val count = 10?
Char
Int
Boolean
String
10 What is an immutable collection?
11 Which package contains Scala's mutable collection classes?
scala.collection.fixed
scala.collection.mutable
scala.collection.static
scala.collection.constant
12 Which keyword is commonly used to define a named function in Scala?
fun
def
method
function
13 Which symbol separates the parameters from the body of an anonymous function in Scala?
::
=>
:=
<-
14
What does the function def square(x: Int): Int = x * x return for square(4)?
20
12
8
16
15
Which expression creates a Scala list containing 1, 2, and 3?
List(1, 2, 3)
Tuple(1, 2, 3)
Set(1 -> 2 -> 3)
Map(1, 2, 3)
16
Which method returns the first element of a non-empty Scala List?
lastIndexOf
length
tail
head
17
What does a Scala Map store?
18
Which expression creates a map that associates "A" with 1?
List("A" -> 1)
Map("A" -> 1)
Set("A", 1)
Map("A", 1)
19
What is a key characteristic of a Scala Stream?
20
Why can a Scala Stream represent an infinite sequence?
21 A Scala application needs reusable behavior that can be mixed into several unrelated classes while each class still extends its own superclass. Which Scala construct is most appropriate?
22
What is the result of the following code?
case class Node(value: Int)
val a = Node(4)
val b = a.copy(value = 5)
(a.value, b.value, a == Node(4))
(4, 5, false)
(4, 5, true)
(5, 5, true)
(5, 4, false)
23
What type is inferred for data in the following declaration?
val data = ('A', 25L, 3.5)
(Char, Long, Double)
(String, Long, Float)
(String, Int, Float)
(Char, Int, Double)
24
What value is assigned to result?
val decimal = 1e2
val hexadecimal = 0x0F
val result = decimal + hexadecimal
115.0
115
100.15
1015.0
25
Scala operators are method calls. What does the expression 10.+(3).*(2) evaluate to?
20
16
23
26
26
What is the value of result after this collection-method pipeline?
val result = List(1, 2, 3, 4).filter(_ % 2 == 0).map(_ * 3).sum
30
18
20
12
27
What type does Scala infer for labels?
val labels = List(1, 2, 3).map(x => s"v$x")
List[Char]
Seq[Any]
List[Int]
List[String]
28
What function type is inferred for the following value?
val increment = (x: Int) => x + 1
Int => String
(Int, Int) => Int
Int => Int
Any => Int
29
What is the value of (original, changed) after this code?
val original = List(1, 2, 3)
val changed = original.updated(1, 9)
(List(1, 2, 3), List(9, 2, 3))
(List(1, 9, 3), List(1, 2, 3))
(List(1, 2, 3), List(1, 9, 3))
(List(1, 9, 3), List(1, 9, 3))
30
What does the final expression return?
val m = scala.collection.mutable.Map("jobs" -> 2)
val alias = m
alias.update("jobs", 5)
m("jobs")
7
None
5
2
31
What is returned by applyTwice(addThree, 10)?
def applyTwice(f: Int => Int, x: Int): Int = f(f(x))
val addThree: Int => Int = _ + 3
20
26
16
13
32
What is the result of the following curried-function code?
def multiply(a: Int)(b: Int): Int = a * b
val timesFour: Int => Int = multiply(4)
timesFour(6)
18
24
46
10
33
What value does scale(3) return?
var factor = 2
val scale = (x: Int) => x * factor
factor = 4
scale(3)
6
7
9
12
34
What pair is produced by the following expressions?
val left = List(1, 2, 3).foldLeft(0)(_ - _)
val right = List(1, 2, 3).foldRight(0)(_ - _)
(left, right)
(6, 2)
(2, -6)
(-6, -2)
(-6, 2)
35
What does total(List(4, 5, 6)) return?
def total(xs: List[Int]): Int = xs match {
case head :: tail => head + tail.sum
case Nil => 0
}
10
9
6
15
36
What list is produced by this expression?
List(1, 2, 3).flatMap(x => List(x, -x))
List(List(1, -1), List(2, -2), List(3, -3))
List(1, 2, 3, -1, -2, -3)
List(1, -1, 2, -2, 3, -3)
List(-1, 1, -2, 2, -3, 3)
37
What is the final tuple?
val m1 = Map("a" -> 1, "b" -> 2)
val m2 = m1.updated("a", 5)
(m1("a"), m2("a"), m2.size)
(1, 5, 3)
(5, 5, 2)
(1, 1, 2)
(1, 5, 2)
38
What value does the following expression produce?
Map("cpu" -> 8).get("gpu").map(_ * 2).getOrElse(4)
4
8
16
0
39
What is produced by the following finite operation on an infinite stream?
Stream.from(1).filter(_ % 3 == 0).take(4).toList
List(3, 6, 9, 12)
List(1, 4, 7, 10)
List(3, 4, 5, 6)
List(0, 3, 6, 9)
40
What does fibs.drop(6).head return?
lazy val fibs: Stream[Int] = 0 #:: 1 #:: fibs.zip(fibs.tail).map { case (a, b) => a + b }
13
8
21
5
41 Which design choice most directly explains why Scala can express both object-oriented and functional abstractions without treating them as separate programming models?
42
Consider the Scala declaration val result = if (condition) 10 else "ten". Which statement best describes the inferred type of result in standard Scala typing?
Int and String
Any because the branches have unrelated types
AnyVal because both branches are value types
String because the second branch is textual
43
What is printed by the following Scala code?
val x = 1 / 2 val y = 1.0 / 2 println(x + "," + y)
0,0.0
0.5,0.5
1,0.5
0,0.5
44
Which expression evaluates to a Long value without relying on implicit numeric widening from an already typed variable?
42L
42
42.0
'42'
45
Given val a = 10; val b = 3, what does a.+(b) demonstrate about Scala operators?
46
What is the result of the following expression?
2 :: 3 :: Nil
2 and 3 in that order
2
Nil cannot infer an element type
3 and 2 because :: is right-associative
47
What is the most accurate reason that val f = (x: Int) => x.toString can be assigned to a value without an explicit result type?
Function1[Any, Any]
toString method forces all lambdas to return String
48
Why can the following definition fail to compile in Scala?
def applyTwice(f, x) = f(f(x))
49
What happens when the following code executes?
val xs = List(1, 2, 3) val ys = xs :+ 4
xs is modified and ys references the same list
ys is a new list while xs remains unchanged
50
Which statement correctly distinguishes val from immutability in Scala?
val and var differ only in whether the compiler infers the type
var makes the referenced collection immutable after its first assignment
val prevents reassignment of the binding but not mutation of its object
val guarantees that every object reachable through the binding is immutable
51
What is the result of the following code?
val add = (x: Int) => (y: Int) => x + y val increment = add(1) println(increment(4))
4
5
1
52
Which expression correctly converts a method def square(x: Int): Int = x * x into a function value that can be stored in val f?
val f = square
val f = Function(square)
val f = method(square)
val f = square _
53
What is the result of the following expression?
List(1, 2, 3, 4).foldLeft(0)((acc, x) => acc - x)
0
10
2
-10
54
Which operation is asymptotically preferable when repeatedly adding elements to the front of an immutable Scala List?
:+ because it preserves the original prefix
++ with a singleton list for constant-time insertion
:: because prepending is constant time
updated because indexed replacement is constant time
55
For an immutable map val scores = Map("A" -> 10), which expression safely returns a score or a fallback without throwing when the key is absent?
scores.getOrElse("B", 0)
scores.get("B")
scores.valueOrElse("B", 0)
scores("B")
56
What does the following expression produce?
Map("a" -> 1, "b" -> 2).map { case (k, v) => k -> (v * 2) }
57
What is the key semantic issue in the following code?
val m = Map(1 -> "one", 1 -> "uno")
58
Why does the following definition avoid immediately attempting to construct an infinite collection?
val naturals = Stream.from(1)
Stream stores all elements but compresses repeated values
Stream truncates itself automatically after one million elements
Stream stores elements lazily and computes tails on demand
Stream executes only when the variable is declared mutable
59
What is the result of the following expression?
Stream.from(1).map(_ * 2).filter(_ % 3 == 0).take(3).toList
List(3, 6, 9)
List(2, 4, 8)
List(6, 12, 18)
60 Which change most directly prevents an unnecessary traversal of an infinite stream when only the first matching element is required?
find directly on the lazy stream
size before calling head
reverse before calling filter
toList before calling find
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →