Immutable objects use less memory than their mutable counterparts.
Immutable objects do not require error handling.
Immutable objects can be used in classes, mutable objects cannot.
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?
do-while loop
while loop
for loop
do-until 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?
Q43. What do you call a function defined in a block?
private function
block function
local function
method
A function defined within a block of code, such as within a method or another function, is called a local function. This is because it is only visible and accessible within the scope of the block in which it is defined, and is not accessible outside of that block.
Q44. What do you call a Scala method that is parametrized by type as well as by value?
multimode method
polymorphic method
closure
collection method
Q45. What type of exception is thrown when a precondition is violated?
IllegalArgumentException
NumberFormatException
NullPointerExcepetion
MalformedParameterException
Q46. In scala what is precondition?
a constraint on where a method may be called from
a constraint on values passed to a methode constructor
a class of predifined error messages
a class of Boolean operators
Q47. What would you change in this code to make it execute in parallel?
val myNums = (1 to 500).toList
list.map(_ + 1)
Change list.map to list.par.map.
Change toList to toListPar
Change val to val.par
Change toList to toParallelList
Q48. What is a free variable?
a variable defined outside a function
a variable referenced in a function that is not assigned a value by that function
a variable that has a global scope
a variable defined in a class and available to all methods in that class
Q49. What's the best way to execute code in the background in a separate thread?
AltThread
AltFuture
AltProcess
Future
Q50. What value does this code return?
x= List(1,2,4); x(1)?
(1,2,4)
1
Nil
2
Q51. Which data type does Scala use instead of null for optional values?
Nil
Option
Singleton
Collection
In Scala, the Option data type is used instead of null for optional values. It is a container that can either hold a value or be empty, and it is used to represent the presence or absence of a value. This makes it safer to work with than using null, as it eliminates the risk of null pointer exceptions.
Q52. What is equivalent to this code?
s"Foo $a?"
"Foo " + a + "?"
Q53. Which expression is one way to iterate over a collection and generate a collection of each iteration's result?
for-yield
for-collect
for-collect until
collectuntil
for-yield is one way to iterate over a collection and generate a collection of each iteration's result. The for loop with the yield keyword is used to iterate over a collection and generate a new collection with the results of each iteration.
Q54. Which statement accesses the third element of an array named foo?
foo[2]
foo(3)
foo[3]
foo(2)
In many programming languages, arrays are indexed starting at 0, so the first element of the array is at index 0, the second element is at index 1, and so on. Therefore, to access the third element of an array named "foo", you would use the index 2 (since the array is indexed starting at 0). This can be done using the syntax foo[2] or foo(2) depending on the programming language. In some languages like Java, you can use foo[2] or foo[3] to access the third element.
Q55. What data type would you use to store an immutable collection of objects when you don't know how many members will be in the collection?
Tuple
List
Object
Array
You would use a List data type to store an immutable collection of objects when you don't know how many members will be in the collection. Lists are indexed collections of elements that can be accessed by their position in the list, and they are commonly used to store collections of items that need to be processed in order. Additionally, Lists are immutable, which means that their elements cannot be modified once they have been created, making them ideal for use cases where data integrity is important.
Q56. From where do all classes in Scala inherit?
AnyRef
AnyColl
AnyVal
AnyClass
All classes in Scala inherit from the AnyRef class by default. AnyRef is the base class for all reference types in Scala, and it is equivalent to the java.lang.Object class in Java. AnyVal is the base class for all value types in Scala, and Any is the base class for all types in Scala.
Q57. In Scala, what is a precondition?
A class of boolean operators
A class of predefined error messages
A constraint on values passed to a method or constructor
A constraint on where a method may be called from
A precondition in Scala is a constraint on the input values passed to a method or constructor, specifying the conditions that must be met for the method or constructor to execute correctly. It is used to check the validity of input values before the method or constructor is executed, and can be used to ensure that the method or constructor is called with the correct arguments.
Q58. Which code sample will print the integers 1 through 4, each on a separate line?
for(i <- 0 to 4) println(i)
for(i <- 0 to 3) println(i+1)
for(i <- 1 to 8 if i < 5) println(i)
for(i <- 1 to 4) println(i)
The correct answer is for(i <- 0 to 3) println(i+1) because it will start the iteration from 0 and end at 3, incrementing the value by 1 each time and printing it.
Q59. Which operator should you use to take the intersection of two sets?
&
||
&&
%
The & or intersect method can be used to take the intersection of two sets in Scala.
Q60. Which data type does Scala use instead of null for optional values?
Nil
Option
Singleton
Collection
In Scala, the Option data type is used instead of null for optional values. It is a container that can either hold a value or be empty, and it is used to represent the presence or absence of a value. This makes it safer to work with than using null, as it eliminates the risk of null pointer exceptions.
Q61. What is the difference between a Scala trait and an interface?
A trait can have concrete implementations, while an interface cannot.
An interface can have concrete implementations, while a trait cannot.
Traits can be mixed together, while interfaces cannot.
Interfaces can be mixed together, while traits cannot.