Scala Skill Basics and Multiple choice Quiz 2022

What is Scala?

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It seamlessly integrates features of object-oriented and functional languages.

Scala is object-oriented

Scala is a pure object-oriented language in the sense that every value is an object. Types and behaviors of objects are described by classes and traits. Classes can be extended by subclassing, and by using a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.

Scala is functional

Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and it supports currying. Scala’s case classes and its built-in support for pattern matching provide the functionality of algebraic types, which are used in many functional languages. Singleton objects provide a convenient way to group functions that aren’t members of a class.

Furthermore, Scala’s notion of pattern matching naturally extends to the processing of XML data with the help of right-ignoring sequence patterns, by way of general extension via extractor objects. In this context, for comprehensions are useful for formulating queries. These features make Scala ideal for developing applications like web services.

Scala is statically typed

Scala’s expressive type system enforces, at compile-time, that abstractions are used in a safe and coherent manner. In particular, the type system supports:

Type inference means the user is not required to annotate code with redundant type information. In combination, these features provide a powerful basis for the safe reuse of programming abstractions and for the type-safe extension of software.

Scala is extensible

In practice, the development of domain-specific applications often requires domain-specific language extensions. Scala provides a unique combination of language mechanisms that make it straightforward to add new language constructs in the form of libraries.

In many cases, this can be done without using meta-programming facilities such as macros. For example:

Scala MCQs

Q1. Scala bytecode can run on top of Java VM. What is the fundamental difference between Java object.clone() and Scala object.copy()?
a. One is a Java object, the other is a Scala object.
b. clone() will copy class structures but not the data, while copy() will also copy data into new objects.
c. There is no difference.
d. copy() allows you to change values during the copying process; clone() does not.
Correct answer:- copy() allows you to change values during the copying process; clone() does not.
Q2. What value does this code return?
val m1 = Map(“a”->1,”b”->2,”c”->3)
m1(“a”)
a. a
b. 2
c. b
d. 1
Correct answer is :- 1
Q3. What is one way to avoid low-level parallelization details?
a. monads
b. literal functions
c. partially applied functions
d. parallel collections
Correct answer is:- parallel collections

Q4. What do you use in ScalaTest to see a detailed diagram of error messages when a test fails?
a. ArgumentExceptions
b. AssertionException
c. DiagrammedAssertions
d. JUnit
Correct answer is:- DiagrammedAssertions
Q5. What data type would you use to store an immutable collection of objects that contain a fixed number of varying types?
a. Array
b. ImmutableCollection
c. List
d. Tuple
Correct answer is:- Tuple
Q6. After defining a function in the interpreter, Scala returns the following. What does the () indicate?
myfnc: ()Unit
a. The function has no side effects.
b. The function takes no parameters.
c. The function returns no value.
d. Returning unit types to the function is a closures.
Correct answer :- The function returns no value.
Q7. What type of number is 1234.e5?
a. hexadecimal
b. short
c. floating point
d. long
Correct answer: floating point

Q8. When you convert a map to a list using the toList method of the map, the result will be of which type?
a. List[(String, String)]
b. List[(Array, Array)]
c. List[(Collection, Collection)]
d. List
Correct answer:- List

Q9. What type of object does this code create?
val x = (1234, “Active”)
a. List
b. Map
c. Tuple
d. Array
Correct answer:- Tuple

Q10. Which is a subclass of all classes?
a. AnyVal
b. AnyRef
c. Method
d. Null
Correct answer :- Null

Null in Scala Standard library. The question is a bit incorrect – Null is a subtype of every type except those of value classes

Q11. For the for-yield construct, is the scope separate between for-body and yield-body?
a. Yes and no. It is different depending on the for construct and what it does.
b. Yes, because the for section does not expose its scope.
c. No, because for-yield shares the same scope, even though they are within separate curly braces.
d. Yes, because they are within different curly braces.
Correct answer is :- No, because for-yield shares the same scope, even though they are within separate curly braces.

Example: yield-body has access to the e variable from the for-body

val a = Array(1, 2, 3, 4, 5)
for {
e <- a if e > 2
} yield e
Q12. What is one way to implement pattern matching on methods?
a. using regex
b. using monads
c. using string matching
d. using case classes
Note: ambiguous question, it’s not clear what kind of pattern matching is meant here.

Q13. What is the value of z after executing this code?
val y = List(‘a’,’b’)
val z = y::List(‘c’)
a. List(a,b,c)
b. List(List(a, b), c)
c. List(c,a,b)
d. List(c,List(a,b))
Correct answer is:- List(List(a, b), c)

Q14. What term is used to specify a precondition?
a. assert
b. require
c. precondition
d. mustHave
Correct answer is:- assert

Q15. Which Scala type may throw an exception or a successfully computed value, and is commonly used to trap and propagate errors?
a. scala.util.ExceptionHandling
b. scala.Catch.Throw
c. scala.exception.TryFinally
d. scala.util.Try

Correct answer is: scala.util.Try

Q16. What is the data type of y after this code is executed?
val y = (math floor 3.1415 * 2)
a. short
b. double
c. int
d. bigInt
Correct answer is:- double

Q17. When using pattern matching, which character matches on any object?
a. %
b. _ [answer]
c. ^
d. –
Correct answer is _ [answer]

Q18. You have created an array using val. Can you change the value of any element of the array—and why or why not?
a. Yes, the reference to the array is immutable, so the location that the array points to is immutable. The values in the array are mutable.
b. The 0th element is immutable and cannot be modified. All other elements can be modified.
c. Yes, val does not make arrays immutable.
d. No, val makes the array and values of the array immutable.
Correct answer is :

Yes, the reference to the array is immutable, so the location that the array points to is immutable. The values in the array are mutable.
Explanation:

val a1 = Array(1, 2, 3)
a1{1} = 3 // OK
a1 = Array(1, 3, 3) // error: reassignment to val
Q19. What is the output of this function?
def main () {
var a = 0
for (a<-1 until 5){println(a)}
a. 1,2,3,4,5
b. 0,1,2,3,4
c. 1,2,3,4
d. 2,3,4,5

Correct answer is:- 1,2,3,4
Q20. What do you call objects with immutable state?
a. singletons
b. stationary objects
c. functional objects
d. fixed objects
Correct answer is:- functional objects

Note: singletons may have mutable state

Q21. You have written a Scala script. How would you access command-line arguments in the script?
a. use array named args
b. use tuple named args
c. use numbered variables with a _ prefix for example _ 1, _ 2, _ 3
d. use numbered variables with a $ prefix – for example $1, $2, $3
Correct answer is :- use array named args

Q22. What does this code return? val x = 3; if (x > 2) x = 4 else x = x*2
a. 4
b. an error
c. 6
d. 3
Correct answer is:- an error

Q23. Which statement returns a success or a failure indicator when you execute this code?
val MyFuture = Future {runBackgroundFunction() }

a. myFuture.onComplete
b. myFuture(status)
c. myFuture.Finished
d. complete(myFuture)
Correct answer is :- myFuture.onComplete

Q24. To denote a parameter that may be repeated, what should you place after type?
a. %
b. &
c. _
d. – [answer]
Correct answer is:- – [answer]

Q25. What is called when a superclass has more than one subclass in Scala?
a. polyinheritance
b. multilevel inheritance
c. multimode inheritance
d. hierarchical inheritance
Correct answer is:- hierarchical inheritance

Q26. One way to improve code reliability is to use __ , which will evaluate a condition and return an error if the condition is violated.
a. packages
b. polymorphisms
c. assertions
d. traits
Correct answer is:- assertions
Q27. Which statement about if-else-if-else statements is true?
a. If the first else-if does not succeed, then no other else-ifs are tested.
b. If an else-if does not succeed, then none of the remaining else-if statements or else will be tested.
c. All else-if statements are tested in all cases.
d. If an else-if succeeds, then none of the remaining else-if statements or else will tested.
Correct answer is:- If an else-if succeeds, then none of the remaining else-if statements or else will tested.

Q28. What do you call the process of changing the definition of an inherited method?
a. recursive methods
b. currying methods
c. redefining methods
d. overriding methods
Correct answer is :- overriding methods

Q29. To denote a parameter that may be repeated, what should you place after the type?
a. _
b. *
c. %
d. &
Correct answer is :- *
Repeated Parameters in Scala

Q30. What is the code below equivalent to?
myClass.foreach(println _)
a. myClass.foreach(println ())
b. myClass.foreach(print NIL)
c. myClass.loop(println ())
d. myClass.foreach(x => println(x))
Correct answer is :- myClass.foreach(x => println(x))

Q31. What is an advantage of an immutable object?
a. Immutable objects use less memory than their mutable counterparts.
b. Immutable objects do not require error handling.
c. Immutable objects can be used in classes, mutable objects cannot.
d. Immutable objects are threadsafe.
Correct answer is:- Immutable objects are threadsafe.

Q32. You want to create an iteration loop that tests the condition at the end of the loop body. Which iteration would you use?
a. do-while loop
b. while loop
c. for loop
d. do-until loop
Correct answer is :- do-while loop

Q33. What can you use to make querying a database more efficient, by avoiding the need to parse the SQL string every time a query is executed from Scala?
a. database driver
b. connection
c. prepared statement
d. SQL view
correct answer is :- prepared statement

PreparedStatement from Java which is also used in Scala

Q34. Which is not a member of the collections hierarchy?
a. Set
b. Seq
c. Hash
d. Map
Correct answer is :- Hash

Q35. Which term makes the contents of packages available without prefixing?
a. use
b. include
c. import
d. assertion
Corect answer is :- import

Q36. If you wanted to find the remainder after division, what operator would you use?
a. %
b. DIV
c. //
d. /

Correct answer is :- %
Q37. What are defined inside a class definition?
a. method
b. fields and methods
c. fields, methods, and packages
d. fields
Correct answer is :- fields and methods

Q38. What defines methods and fields that can then be reused by mixing into classes?
a. singleton
b. assertion
c. trait
d. monad
Correct answer is :- trait

Q39. When do you need to explicitly state the return type in a function definition?
a. when the function has no side effects
b. when the function returns a Unit type
c. when the function is recursive
d. when the function has side effects
Correct answer is :- when the function is recursive

Q40. Why would you make a field private?
a. so only methods in the same file can access the field
b. so only methods in the same package can access the field
c. so only methods in the same class could access the field
d. so only methods defined in a Java class can access the field
Correct answer is :- so only methods in the same class could access the field

Q41. What’s the difference between .equals and ==?
a. They do the exact same thing
b. == won’t work on objects
c. == cannot be applied to String
d. == is a wrapper of .equals() and checks for nulls

Correct answer is :- == is a wrapper of .equals() and checks for nulls

Q42. What is denotes the intersection between two sets?
a. ||
b. &&
c. &
d. %
Correct answer is :- &

Click here to read more technical q&a

Q43. What do you call a function defined in a block?
a. private function
b. block function
c. local function
d. method

Q44. What do you call a Scala method that is parametrized by type as well as by value?
a. multimode method
b. polymorphic method
c. closure
d. collection method

Q45. What type of exception is thrown when a precondition is violated?
a. IllegalArgumentException
b. NumberFormatException
c. NullPointerExcepetion
d. MalformedParameterException
Correct answer is :- IllegalArgumentException

Q46. In scala what is precondition?
a. a constraint on where a method may be called from
b. a constraint on values passed to a methode constructor
c. a class of predifined error messages
d. a class of Boolean operators

Correct answer is:-
a class of predifined error messages

Q47. What would you change in this code to make it execute in parallel?
val myNums = (1 to 500).toList
list.map(_ + 1)
a. Change list.map to list.par.map.
b. Change toList to toListPar
c. Change val to val.par
d. Change toList to toParallelList
Correct answer is:- Change list.map to list.par.map.
Q48. What is a free variable?
a. a variable defined outside a function
b. a variable referenced in a function that is not assigned a value by that function
c. a variable that has a global scope
d. a variable defined in a class and available to all methods in that class
Correct answer is:-
a variable defined outside a function
Q49. What’s the best way to execute code in the background in a separate thread?
a. AltThread
b. AltFuture
c. AltProcess
d. Future
Correct answer is :- Future

Q50. What value does this code return?
x= List(1,2,4); x(1)?
a. (1,2,4)
b. 1
c. Nil
d. 2

Correct answer is:- 2

Click here to read more blogs

Click here to read more technical Questions and Answers

About Author


After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.

Leave a Comment