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. Procedural and assembly
C. Visual and event-driven
D. Object-oriented and functional

2 On which platform does Scala commonly run?

Features of Scala Easy
A. Python Virtual Machine
B. Java 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. 't'
B. true
C. "true"
D. 1

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.sum(2)
C. 1.plus{2}
D. 1.add[2]

8 What is type inference in Scala?

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

9 What type is inferred for val count = 10?

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

10 What is an immutable collection?

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

11 Which package contains Scala's mutable collection classes?

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

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

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

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. 16
B. 12
C. 8
D. 20

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

Lists in Scala Easy
A. Set(1 -> 2 -> 3)
B. Tuple(1, 2, 3)
C. List(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. tail
C. length
D. head

17 What does a Scala Map store?

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

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

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

19 What is a key characteristic of a Scala Stream?

Streams in Scala Easy
A. Its elements must be strings
B. Its values must always be mutable
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. Elements are computed only when requested
B. Values are removed after each access
C. Elements are stored in a fixed array
D. Values are limited to Boolean literals

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 type alias
B. A package object
C. A case class
D. A trait

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, true)
B. (5, 5, true)
C. (5, 4, false)
D. (4, 5, 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. 100.15
C. 1015.0
D. 115

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

Operators and methods used in Scala Medium
A. 16
B. 20
C. 26
D. 23

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. 20
C. 12
D. 18

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. Seq[Any]
B. List[Char]
C. List[String]
D. List[Int]

28 What function type is inferred for the following value?

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

Introduction to type inference Medium
A. Int => Int
B. (Int, Int) => Int
C. Any => Int
D. Int => String

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, 9, 3), List(1, 9, 3))
B. (List(1, 2, 3), List(9, 2, 3))
C. (List(1, 9, 3), List(1, 2, 3))
D. (List(1, 2, 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. 2
B. 5
C. None
D. 7

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. 26
B. 20
C. 13
D. 16

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. 10
B. 24
C. 18
D. 46

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. 7
B. 6
C. 12
D. 9

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. (6, 2)
C. (2, -6)
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. 6
B. 15
C. 10
D. 9

36 What list is produced by this expression?

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

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

38 What value does the following expression produce?

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

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

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(3, 4, 5, 6)
C. List(1, 4, 7, 10)
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. 5
B. 8
C. 13
D. 21

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. Pattern matching replaces inheritance in every class hierarchy
B. Every value is an object and functions are first-class values
C. Only immutable objects can implement interfaces
D. All classes must inherit from a single universal superclass

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 String because the second branch is textual
D. It is inferred as AnyVal because both branches are value types

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. 0,0.5
D. 1,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.0
C. '42'
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 ordinary methods that can be invoked explicitly
D. Operators are syntax that bypasses method dispatch

46 What is the result of the following expression?

2 :: 3 :: Nil

Operators and methods used in Scala Hard
A. A compile-time error because Nil cannot infer an element type
B. A list containing 3 and 2 because :: is right-associative
C. A list containing only 2 and 3 in that order
D. A nested pair whose outer element is the integer 2

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

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. Scala forbids functions from being passed as parameters
C. A method must always declare a Boolean return type
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. ys is a new list while xs remains unchanged
B. Both variables become mutable aliases of the original list
C. The append fails because immutable lists support only prepending
D. xs is modified and ys references the same list

50 Which statement correctly distinguishes val from immutability in Scala?

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

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 = square
C. val f = Function(square)
D. val f = method(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. 10
B. 0
C. -10
D. 2

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 prepending is constant time
B. Use :+ because it preserves the original prefix
C. Use updated because indexed replacement is constant time
D. Use ++ with a singleton list for constant-time insertion

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.get("B")
B. scores.getOrElse("B", 0)
C. scores("B")
D. scores.valueOrElse("B", 0)

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 containing only the keys
B. A map with the same keys and doubled values
C. A sequence of tuples with all values converted to strings
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 second value replaces the first value for the duplicate key
B. The first value replaces the second because maps are left-biased
C. The map contains two entries because duplicate keys are preserved
D. The map construction fails because keys must be unique at compile time

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 truncates itself automatically after one million elements
B. A Stream stores all elements but compresses repeated values
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(6, 12, 18)
C. It does not terminate because the source stream is infinite
D. List(2, 4, 8)

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 reverse before calling filter
B. Call toList before calling find
C. Call find directly on the lazy stream
D. Call size before calling head