Unit 2: Introduction to Programming in Scala - Practice Quiz

INT315 — Cluster Computing 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which programming paradigms are primarily supported by Scala?

Features of Scala Easy
A. Declarative and logical
B. Object-oriented and functional
C. Visual and event-driven
D. Procedural and assembly

2 On which platform does Scala commonly run?

Features of Scala Easy
A. Java Virtual Machine
B. Python Virtual Machine
C. Common Language Runtime
D. Android Runtime only

3 Which Scala data type stores a whole number such as 25?

Basic data types and literals used in Scala Easy
A. String
B. Int
C. Double
D. Boolean

4 Which of the following is a Boolean literal in Scala?

Basic data types and literals used in Scala Easy
A. true
B. 't'
C. 1
D. "true"

5 Which literal correctly represents a character in Scala?

Basic data types and literals used in Scala Easy
A. "A"
B. [A]
C. {A}
D. 'A'

6 Which operator is used to calculate the remainder of a division in Scala?

Operators and methods used in Scala Easy
A. /
B. %
C. +
D. *

7 In Scala, the expression 1 + 2 can also be written as which method call?

Operators and methods used in Scala Easy
A. 1.+(2)
B. 1.plus{2}
C. 1.sum(2)
D. 1.add[2]

8 What is type inference in Scala?

Introduction to type inference Easy
A. Automatic deduction of a value's type
B. Runtime removal of all types
C. Creation of user interface types
D. Manual conversion of every value

9 What type is inferred for val count = 10?

Introduction to type inference Easy
A. Char
B. Int
C. Boolean
D. String

10 What is an immutable collection?

Mutable vs. immutable collections Easy
A. A collection that stores only numeric values
B. A collection that exists only inside functions
C. A collection that changes after every access
D. A collection that cannot be changed after creation

11 Which package contains Scala's mutable collection classes?

Mutable vs. immutable collections Easy
A. scala.collection.fixed
B. scala.collection.mutable
C. scala.collection.static
D. scala.collection.constant

12 Which keyword is commonly used to define a named function in Scala?

Functions in Scala Easy
A. fun
B. def
C. method
D. function

13 Which symbol separates the parameters from the body of an anonymous function in Scala?

Functions in Scala Easy
A. ::
B. =>
C. :=
D. <-

14 What does the function def square(x: Int): Int = x * x return for square(4)?

Functions in Scala Easy
A. 20
B. 12
C. 8
D. 16

15 Which expression creates a Scala list containing 1, 2, and 3?

Lists in Scala Easy
A. List(1, 2, 3)
B. Tuple(1, 2, 3)
C. Set(1 -> 2 -> 3)
D. Map(1, 2, 3)

16 Which method returns the first element of a non-empty Scala List?

Lists in Scala Easy
A. lastIndexOf
B. length
C. tail
D. head

17 What does a Scala Map store?

Maps in Scala Easy
A. Ordered function calls
B. Only numeric values
C. Only unique keys
D. Key-value pairs

18 Which expression creates a map that associates "A" with 1?

Maps in Scala Easy
A. List("A" -> 1)
B. Map("A" -> 1)
C. Set("A", 1)
D. Map("A", 1)

19 What is a key characteristic of a Scala Stream?

Streams in Scala Easy
A. Its values must always be mutable
B. Its elements must be strings
C. Its elements are evaluated lazily
D. Its size must always be fixed

20 Why can a Scala Stream represent an infinite sequence?

Streams in Scala Easy
A. Values are removed after each access
B. Values are limited to Boolean literals
C. Elements are stored in a fixed array
D. Elements are computed only when requested

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?

Features of Scala Medium
A. A case class
B. A package object
C. A trait
D. A type alias

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))

Features of Scala Medium
A. (4, 5, false)
B. (4, 5, true)
C. (5, 5, true)
D. (5, 4, false)

23 What type is inferred for data in the following declaration?

val data = ('A', 25L, 3.5)

Basic data types and literals used in Scala Medium
A. (Char, Long, Double)
B. (String, Long, Float)
C. (String, Int, Float)
D. (Char, Int, Double)

24 What value is assigned to result?

val decimal = 1e2

val hexadecimal = 0x0F

val result = decimal + hexadecimal

Basic data types and literals used in Scala Medium
A. 115.0
B. 115
C. 100.15
D. 1015.0

25 Scala operators are method calls. What does the expression 10.+(3).*(2) evaluate to?

Operators and methods used in Scala Medium
A. 20
B. 16
C. 23
D. 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

Operators and methods used in Scala Medium
A. 30
B. 18
C. 20
D. 12

27 What type does Scala infer for labels?

val labels = List(1, 2, 3).map(x => s"v$x")

Introduction to type inference Medium
A. List[Char]
B. Seq[Any]
C. List[Int]
D. List[String]

28 What function type is inferred for the following value?

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

Introduction to type inference Medium
A. Int => String
B. (Int, Int) => Int
C. Int => Int
D. 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)

Mutable vs. immutable collections Medium
A. (List(1, 2, 3), List(9, 2, 3))
B. (List(1, 9, 3), List(1, 2, 3))
C. (List(1, 2, 3), List(1, 9, 3))
D. (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")

Mutable vs. immutable collections Medium
A. 7
B. None
C. 5
D. 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

Functions in Scala Medium
A. 20
B. 26
C. 16
D. 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)

Functions in Scala Medium
A. 18
B. 24
C. 46
D. 10

33 What value does scale(3) return?

var factor = 2

val scale = (x: Int) => x * factor

factor = 4

scale(3)

Functions in Scala Medium
A. 6
B. 7
C. 9
D. 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)

Lists in Scala Medium
A. (6, 2)
B. (2, -6)
C. (-6, -2)
D. (-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

}

Lists in Scala Medium
A. 10
B. 9
C. 6
D. 15

36 What list is produced by this expression?

List(1, 2, 3).flatMap(x => List(x, -x))

Lists in Scala Medium
A. List(List(1, -1), List(2, -2), List(3, -3))
B. List(1, 2, 3, -1, -2, -3)
C. List(1, -1, 2, -2, 3, -3)
D. 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)

Maps in Scala Medium
A. (1, 5, 3)
B. (5, 5, 2)
C. (1, 1, 2)
D. (1, 5, 2)

38 What value does the following expression produce?

Map("cpu" -> 8).get("gpu").map(_ * 2).getOrElse(4)

Maps in Scala Medium
A. 4
B. 8
C. 16
D. 0

39 What is produced by the following finite operation on an infinite stream?

Stream.from(1).filter(_ % 3 == 0).take(4).toList

Streams in Scala Medium
A. List(3, 6, 9, 12)
B. List(1, 4, 7, 10)
C. List(3, 4, 5, 6)
D. 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 }

Streams in Scala Medium
A. 13
B. 8
C. 21
D. 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?

Features of Scala Hard
A. Every value is an object and functions are first-class values
B. Only immutable objects can implement interfaces
C. All classes must inherit from a single universal superclass
D. Pattern matching replaces inheritance in every class hierarchy

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?

Features of Scala Hard
A. It is inferred as a common supertype of Int and String
B. It is inferred as Any because the branches have unrelated types
C. It is inferred as AnyVal because both branches are value types
D. It is inferred as 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)

Basic data types and literals used in Scala Hard
A. 0,0.0
B. 0.5,0.5
C. 1,0.5
D. 0,0.5

44 Which expression evaluates to a Long value without relying on implicit numeric widening from an already typed variable?

Basic data types and literals used in Scala Hard
A. 42L
B. 42
C. 42.0
D. '42'

45 Given val a = 10; val b = 3, what does a.+(b) demonstrate about Scala operators?

Operators and methods used in Scala Hard
A. Operators are reserved tokens unavailable as method names
B. Operators are macros expanded only for primitive values
C. Operators are syntax that bypasses method dispatch
D. Operators are ordinary methods that can be invoked explicitly

46 What is the result of the following expression?

2 :: 3 :: Nil

Operators and methods used in Scala Hard
A. A list containing only 2 and 3 in that order
B. A nested pair whose outer element is the integer 2
C. A compile-time error because Nil cannot infer an element type
D. A list containing 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?

Introduction to type inference Hard
A. The compiler infers the function's parameter and result types from its body
B. Every lambda is automatically typed as Function1[Any, Any]
C. A function value has no static type until it is invoked
D. The 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))

Introduction to type inference Hard
A. The compiler cannot infer unconstrained parameter types reliably
B. A method must always declare a Boolean return type
C. Scala forbids functions from being passed as parameters
D. Nested method calls are illegal inside method definitions

49 What happens when the following code executes?

val xs = List(1, 2, 3) val ys = xs :+ 4

Mutable vs. immutable collections Hard
A. xs is modified and ys references the same list
B. Both variables become mutable aliases of the original list
C. The append fails because immutable lists support only prepending
D. ys is a new list while xs remains unchanged

50 Which statement correctly distinguishes val from immutability in Scala?

Mutable vs. immutable collections Hard
A. val and var differ only in whether the compiler infers the type
B. var makes the referenced collection immutable after its first assignment
C. val prevents reassignment of the binding but not mutation of its object
D. 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))

Functions in Scala Hard
A. 4
B. 5
C. A type error because nested functions cannot be returned
D. 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?

Functions in Scala Hard
A. val f = square
B. val f = Function(square)
C. val f = method(square)
D. val f = square _

53 What is the result of the following expression?

List(1, 2, 3, 4).foldLeft(0)((acc, x) => acc - x)

Lists in Scala Hard
A. 0
B. 10
C. 2
D. -10

54 Which operation is asymptotically preferable when repeatedly adding elements to the front of an immutable Scala List?

Lists in Scala Hard
A. Use :+ because it preserves the original prefix
B. Use ++ with a singleton list for constant-time insertion
C. Use :: because prepending is constant time
D. Use 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?

Maps in Scala Hard
A. scores.getOrElse("B", 0)
B. scores.get("B")
C. scores.valueOrElse("B", 0)
D. scores("B")

56 What does the following expression produce?

Map("a" -> 1, "b" -> 2).map { case (k, v) => k -> (v * 2) }

Maps in Scala Hard
A. A sequence of tuples with all values converted to strings
B. A sequence containing only the keys
C. A map with the same keys and doubled values
D. A map whose keys and values have been swapped

57 What is the key semantic issue in the following code?

val m = Map(1 -> "one", 1 -> "uno")

Maps in Scala Hard
A. The map contains two entries because duplicate keys are preserved
B. The second value replaces the first value for the duplicate key
C. The map construction fails because keys must be unique at compile time
D. The first value replaces the second because maps are left-biased

58 Why does the following definition avoid immediately attempting to construct an infinite collection?

val naturals = Stream.from(1)

Streams in Scala Hard
A. A Stream stores all elements but compresses repeated values
B. A Stream truncates itself automatically after one million elements
C. A Stream stores elements lazily and computes tails on demand
D. A 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

Streams in Scala Hard
A. List(3, 6, 9)
B. List(2, 4, 8)
C. List(6, 12, 18)
D. It does not terminate because the source stream is infinite

60 Which change most directly prevents an unnecessary traversal of an infinite stream when only the first matching element is required?

Streams in Scala Hard
A. Call find directly on the lazy stream
B. Call size before calling head
C. Call reverse before calling filter
D. Call toList before calling find