Scala MCQs

Scala MCQs

Answer these 200+ Scala MCQs and see how sharp is your knowledge of Scala.
Scroll down and let's start!

1: When importing all the names of a package or class, which character do you use instead of " * "?

A.   "&"

B.   "@"

C.   "$"

D.   "_"

2: If you are defining scala classes in a 'package examplepackage', and want to ensure that a function 'foo' is only accessible by classes defined in the same package, how would you declare that function?

A.   private[examplepackage] def foo = {...}

B.   package[examplepackage] def foo={...}

C.   def foo={...} //default access

D.   package def foo={...}//package private

E.   [examplepackage]private def foo={...}

3: How would you get a List that was the result of appending `5:Int` to a `List(1,2,3)`. The order of the elements in the resulting List is irrelevant.

A.   List(1,2,3) :: List(5)

B.   List(1,2,3) + 5

C.   List(1,2,3) :+ 5

D.   List(1,2,3) ::= 5

E.   List(1,2,3) :: 5

4: In the expression: List(1,2,3).foldLeft(x){case (a,b) => a+b} `x` is:

A.   A default value, only used if the list is empty

B.   A list, to which the results are appended.

C.   The "accumulator," which is the initial value for `b`

D.   The "accumulator," which is the initial value for `a`

5: Describe class AnyRef

A.   AnyRef is the root Object in Scala

B.   There is no such class

C.   AnyRef is derived from AnyVal

D.   All types except the value types descend from AnyRef

6: A valid description of a covariant type parameter would be:

A.   A type parameter which is fixed when the class is subtyped.

B.   A type parameter which is allowed to vary up for super types.

C.   A type parameter which is fixed for super types.

D.   A type parameter which is allowed to vary down as the class is subtyped.

7: Does Scala support tail-call recursion?

A.   Yes, the JVM runtime supports tail-call optimization.

B.   No

C.   Partly at the compiler level. The compiler will try and unwind the recursive call into a loop.

D.   Partly, because the stack is infinite in Scala.

8: `Nil` is generally the same as:

A.   Nothing

B.   null

C.   List()

D.   None

9: What is the tool "schema2src" used for?

A.   Data-binding

B.   Unifying types

C.   Sealing classes

D.   Currying

10: Classes in Scala, in contrast to Java, can have ______.

A.   Functions

B.   Parameters

C.   Concepts

D.   Constructs

11: What is the defaut parameter call semantics?

A.   By Inference

B.   By Value

C.   By Name

D.   By Reference

12: Which statement about case classes is false?

A.   Case classes as sealed and therefor cannot be extended

B.   You can construct instances of these classes without using the new keyword

C.   The toString method is automatically redefined

D.   The equals method is automatically redefined

13: In the expression: List(1,2,3) reduceLeft ( (a,b) => a+b ) `b` refers to:

A.   The "fold" operation

B.   The current sum while iterating through the list

C.   The next element in the list

D.   The return value for the expression

14: Which statement best describes a partial function?

A.   When applying the function, you do not pass in arguments for all of the parameters defined by the function, but only for some of them, leaving the remaining ones blank

B.   A function that support currying

C.   A partially defined function.

D.   An internal function type that is used by the scala.collection.immutable package.

15: What is the result type of the following expression? List(1, 2, true, false)

A.   List[Any]

B.   List[Int]

C.   List[AnyRef]

D.   List[Boolean]

E.   List[AnyVal]

16: It is possible to override methods inherited from a _____ in Scala.

A.   Base-class

B.   Super-script

C.   Super-class

D.   Function-class

17: Which statement is true about sealed classes.

A.   There is no such thing as a sealed class.

B.   A subclass of a sealed class can inherited anywhere only within the same package.

C.   A sealed class may not be directly inherited, except if it is defined in the same source file.

D.   A sealed class' instances cannot be modified.

18: Witch one of the following operators is use for sequencing Parsers

A.   ~

B.   *

C.   !

D.   |

19: What is the largest Tuple that Scala supports?

A.   22

B.   16

C.   2

D.   20

20: Scala supports which types of polymorphism?

A.   Subtype, ad-hoc and parametric polymorphism

B.   Subtype and parametric

C.   None of these

D.   Parametric

E.   Ad-hoc and parametric

21: In Scala, type parameters and abstract types may be constrained by a _____.

A.   Type safe

B.   Type call

C.   Type bound

D.   Type function

22: The following code will > var x=100; var y=200; x->y

A.   automatically create a List[T] with x and y as members with T of 'Int' type

B.   a Tuple with Arity 2

C.   assign 100 to variable y

D.   treat x and y as same references in the further code

E.   produce a compile error

23: True or False? Scala compiler will never require you to specify the result type of a function.

A.   True

B.   False

24: Which of the following is a pattern matching any value, without giving it a name, represented by " _ "?

A.   A simple class

B.   A placeholder

C.   A guard

D.   A function

25: What is the value of the following expression? { val a = List(1,2,3) val b = List(4,5,6) (a,b).zipped.map(_+_) }

A.   (List(1,2,3),List(4,5,6))

B.   List(1,2,3,4,5,6)

C.   21

D.   List(5,7,9)

E.   List((1,4),(2,5),(3,6))

26: Explain how "abc".length returns 3

A.   All string literals are an instance of scala.collection.immutable.StringOps

B.   A subclass of java.lang.String is generated at runtime, adding the `length` method to its signature.

C.   An implicit conversion converts the java.lang.String into a scala.collection.immutable.StringOps, which supports a length method.

D.   All string literals can be processed by Scala String interpreter.

27: What is a higher-order function?

A.   Scala does not support higher-order functions

B.   Higher-order functions are parameterless functions that return themselves

C.   Higher-order functions are functions that take other functions as parameters.

D.   Higher-order functions are functions that return functions

28: Which statement best describes an Iterator

A.   An iterator is a collection type

B.   Scala does not support Iterators

C.   An Iterator is a stream of incoming items where advancing to the next item consumes the current item

D.   An Iterator trait is mixed into all collection types

29: True or False? Methods taking one argument can be used with infix syntax?

A.   False

B.   True

30: What is an expression following the "if" keyword?

A.   A guard

B.   A tree

C.   An array

D.   A wild-card

31: What would be the result of: Option[String]("hi") match { case None=> "hello!" }

A.   A MatchError would be thrown.

B.   Nothing would happen because "hi" is not of type "None"

C.   The statement would return "hello!"

D.   A NullPointerException would be thrown.

32: Scala is:

A.   A dynamic language that can be used to program in object-oriented style efficiently

B.   An advanced Object-Oriented Language much better than Java that runs on the JVM

C.   An advanced Java language implemented by Martin Odersky

D.   An object-functional language that supports functional programming constructs

E.   A pure functional programming language

33: True or False? Multiple classes can be imported from the same package by enclosing them in curly braces {}.

A.   True

B.   False

34: Does Scala support the return keyword?

A.   Yes, but only for traits.

B.   No

C.   Yes, but it is not idiomatic Scala and therefor discouraged.

D.   Yes, all methods and functions must have at least one return statement.

35: True or False? Like pre 1.5 Java, Scala suffers from lack of genericity.

A.   True

B.   False

36: When a class inherits from a trait, it inherits all the code contained in the trait and implements the trait's:

A.   Interface

B.   Framework

C.   Arguments

D.   Platform

37: Scala's "Unit" roughly corresponds to which Java type?

A.   "void"

B.   "bool"

C.   "null"

D.   "get"

38: Which of the following best describes Scala?

A.   A functional language

B.   All of these choices describe Scala

C.   An object oriented language

D.   A language that runs on the VM

39: What is a class with a single instance?

A.   A mono object

B.   A single argument

C.   A singleton object

D.   A static object

40: Everything, including numbers and functions, are _______ in Scala.

A.   Objects

B.   Methods

C.   Booleans

D.   Operations

41: What is the name of the Scala compiler?

A.   "scalacom"

B.   "cScala"

C.   "scala.comp"

D.   "scalac"

42: When no super-class is specified, ______ is implicitly used.

A.   scala.AnyRef

B.   You must always specify super-class

C.   scala.importRef

D.   scalac use.ref

43: True or False? Scala provides static members (members or fields).

A.   True

B.   False

44: Which statement about pattern matching is true?

A.   The order of the pattern match is irrelevant

B.   The case set must be exhaustive

C.   Pattern matching does not work with case classes

D.   Patterns do not work with structural types

45: How would you define the method: def +(a:Int):Int in a Java interface that will be overriden or used in Scala code?

A.   public int %plus(int a)

B.   You can't define a scala operator using Java source code.

C.   public int #plus(int a)

D.   private int $plus(int a)

E.   public int $plus(int a)

46: Which statement about List is false?

A.   A List is a finite immutable sequence

B.   A List is optimal for last-in-first-out (LIFO), stack-like access patterns

C.   List is a proxy for java.util.ArrayList

D.   A List is covariant

47: True or False? In the Interpreter, you can define a new val with a name that was already used before.

A.   False

B.   True

48: What is the data type of myVariable in the following: val myVariable = if (true) "Hello"

A.   String

B.   Any

C.   Unit

49: Is it possible in Scala to declare a variable of type `Int` with a value of `null`?

A.   No

B.   Yes

50: What will the following function return: def foo(o:Any) = { o match { case Option(x) => "hi!" case anything => anything } } When passed a 'None' object?

A.   it will return the None because it matches on 'anything'

B.   It will return "hi!" because a 'None' is a type of 'Option'

C.   It won't compile

D.   It will return 'anything'