Multiple choice practice

Scala MCQ questions

Take a short practice test with instant feedback, then revise every question with its answer and the reason behind it.

Questions
34
From lessons
24
Your last test
—

Practice test

Scala practice testMultiple choice Question 1 of 10

How many traits can a class mix in?

Score so far: 0/0

All questions with answers

  1. Which keyword declares an immutable value?

    • Avar
    • Bval
    • Cdef
    • Dlet
    Show answer

    Answer: B. val

    val cannot be reassigned.

  2. What does List(1, 2, 3).map(_ * 2) return?

    • AList(1, 2, 3)
    • BList(2, 4, 6)
    • C12
    • DList(3)
    Show answer

    Answer: B. List(2, 4, 6)

    map applies the function to each element.

  3. Which represents a missing value safely?

    • Anull
    • BNone
    • CNil only
    • DUnit
    Show answer

    Answer: B. None

    None is the empty Option.

  4. Which feature generates copy, equals and toString automatically?

    • Atrait
    • Bobject
    • Ccase class
    • Denum only
    Show answer

    Answer: C. case class

    Case classes are data classes.

  5. What is a singleton in Scala written as?

    • Astatic class
    • Bobject
    • Cgiven
    • Dval
    Show answer

    Answer: B. object

    object defines a single instance.

  6. What does s"Hello, $name" do?

    • ARegex
    • BString interpolation
    • CEscape sequence
    • DComment
    Show answer

    Answer: B. String interpolation

    The s interpolator inserts values.

  7. Which build tool is standard for Scala projects?

    • AMaven only
    • Bsbt
    • Cnpm
    • Dpip
    Show answer

    Answer: B. sbt

    sbt (and scala-cli for scripts).

  8. Either is right-biased. What does Left usually represent?

    • ASuccess
    • BAn error
    • CA list
    • DA Future
    Show answer

    Answer: B. An error

    Left carries the error, Right the success.

  9. Which keyword introduces an extension method in Scala 3?

    • Aimplicit
    • Bextension
    • Cextends
    • Dwith
    Show answer

    Answer: B. extension

    extension (x: T) def ... adds methods to T.

  10. What does foldLeft(0)(_ + _) compute on List(1, 2, 3)?

    • A0
    • B3
    • C6
    • DList(1, 3, 6)
    Show answer

    Answer: C. 6

    It sums the list starting from 0.

  11. Which platform does Scala mainly run on?

    • A.NET CLR
    • BThe JVM
    • CPython VM
    • DBrowser only
    Show answer

    Answer: B. The JVM

    Scala compiles to JVM bytecode (Scala.js and Native exist too).

    From the lesson: Introduction to Scala
  12. Which command installs Scala, sbt and scala-cli?

    • Anpm install scala
    • Bcs setup
    • Capt scala
    • Dsbt install
    Show answer

    Answer: B. cs setup

    Coursier’s setup installs the whole toolchain.

    From the lesson: Installing Scala and sbt
  13. What happens when you reassign a val?

    • AIt works
    • BCompile error
    • CRuntime warning
    • DIt becomes a var
    Show answer

    Answer: B. Compile error

    vals cannot be reassigned.

    From the lesson: Values and Variables
  14. Which type means “no useful value”, like void?

    • ANothing
    • BNull
    • CUnit
    • DAny
    Show answer

    Answer: C. Unit

    Unit has one value: ().

    From the lesson: Types and Type Inference
  15. What does 1 until 5 contain?

    • A1–5
    • B1–4
    • C0–5
    • D2–5
    Show answer

    Answer: B. 1–4

    until excludes the upper bound.

    From the lesson: Control Structures
  16. What is the type of (x: Int) => x.toString?

    • AInt
    • BString
    • CInt => String
    • DUnit
    Show answer

    Answer: C. Int => String

    A function from Int to String.

    From the lesson: Functions
  17. Which interpolator formats numbers and is type-checked?

    • As
    • Bf
    • Craw
    • Dnone
    Show answer

    Answer: B. f

    f supports printf-style format specifiers.

    From the lesson: String Interpolation
  18. What does list :+ 5 do to the original list?

    • AAppends 5 in place
    • BNothing — it returns a new list
    • CThrows an error
    • DClears it
    Show answer

    Answer: B. Nothing — it returns a new list

    Immutable collections never change.

    From the lesson: Collections
  19. What happens if no case matches?

    • AReturns null
    • BMatchError at runtime
    • CCompile error always
    • DReturns Unit
    Show answer

    Answer: B. MatchError at runtime

    scala.MatchError is thrown.

    From the lesson: Pattern Matching
  20. Which method creates a modified copy of a case class?

    • Aclone
    • Bcopy
    • Cupdate
    • Dwith
    Show answer

    Answer: B. copy

    copy(field = newValue).

    From the lesson: Case Classes
  21. By convention, which side of Either holds the success value?

    • ALeft
    • BRight
    • CBoth
    • DNeither
    Show answer

    Answer: B. Right

    Right is “right” (correct).

    From the lesson: Option and Either
  22. A for-comprehension over Options returns None when…

    • AAll are None
    • BAny generator is None
    • Cyield is missing
    • DNever
    Show answer

    Answer: B. Any generator is None

    The chain short-circuits on the first None.

    From the lesson: For Comprehensions
  23. What replaces Java’s static members in Scala?

    • ATraits
    • BSingleton objects
    • CPackages
    • DCase classes
    Show answer

    Answer: B. Singleton objects

    Members of an object behave like statics.

    From the lesson: Classes and Objects
  24. How many traits can a class mix in?

    • AOne
    • BTwo
    • CMany
    • DNone
    Show answer

    Answer: C. Many

    Any number via with.

    From the lesson: Traits
  25. What does (f andThen g)(x) compute?

    • Af(g(x))
    • Bg(f(x))
    • Cf(x) + g(x)
    • Dx
    Show answer

    Answer: B. g(f(x))

    f runs first, then g.

    From the lesson: Higher-Order Functions