MCQs > IT & Programming > Scala MCQs > Basic Scala MCQs

Basic Scala MCQ

1. What is the value of z after executing this code? val y = List('a','b') val z = y::List('c')

Answer

Correct Answer: List(List(a, b), c)

Note: This Question is unanswered, help us to find answer for this one

2. Q2. What value does this code return? val m1 = Map("a"->1,"b"->2,"c"->3) m1("a")

Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

3. What value does this code return? x= List(1,2,4); x(1)?

Answer

Correct Answer: 2

Note: This Question is unanswered, help us to find answer for this one

4. What's the best way to execute code in the background in a separate thread?

Answer

Correct Answer: Future

Note: This Question is unanswered, help us to find answer for this one

5. What is a free variable?

Answer

Correct Answer: A variable defined outside a function

Note: This Question is unanswered, help us to find answer for this one

6. What would you change in this code to make it execute in parallel?

Answer

Correct Answer: Val myNums = (1 to 500).toList

Note: This Question is unanswered, help us to find answer for this one

7. In scala what is precondition?

Answer

Correct Answer: A class of predifined error messages

Note: This Question is unanswered, help us to find answer for this one

8. What type of exception is thrown when a precondition is violated?

Answer

Correct Answer: IllegalArgumentException

Note: This Question is unanswered, help us to find answer for this one

9. What is denotes the intersection between two sets?

Answer

Correct Answer: &

Note: This Question is unanswered, help us to find answer for this one

10. What's the difference between .equals and ==?

Answer

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

Note: This Question is unanswered, help us to find answer for this one

11. Why would you make a field private?

Answer

Correct Answer: So only methods in the same class could access the field

Note: This Question is unanswered, help us to find answer for this one

12. When do you need to explicitly state the return type in a function definition?

Answer

Correct Answer: When the function is recursive

Note: This Question is unanswered, help us to find answer for this one

13. What defines methods and fields that can then be reused by mixing into classes?

Answer

Correct Answer: Trait

Note: This Question is unanswered, help us to find answer for this one

14. What are defined inside a class definition?

Answer

Correct Answer: Fields and methods

Note: This Question is unanswered, help us to find answer for this one

15. If you wanted to find the remainder after division, what operator would you use?

Answer

Correct Answer: %

Note: This Question is unanswered, help us to find answer for this one

16. Which term makes the contents of packages available without prefixing?

Answer

Correct Answer: Import

Note: This Question is unanswered, help us to find answer for this one

17. Which is not a member of the collections hierarchy?

Answer

Correct Answer: Hash

Note: This Question is unanswered, help us to find answer for this one

18. 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?

Answer

Correct Answer: Prepared statement

Note: This Question is unanswered, help us to find answer for this one

19. You want to create an iteration loop that tests the condition at the end of the loop body. Which iteration would you use?

Answer

Correct Answer: Do-while loop

Note: This Question is unanswered, help us to find answer for this one

20. What is an advantage of an immutable object?

Answer

Correct Answer: Immutable objects are threadsafe.

Note: This Question is unanswered, help us to find answer for this one

21. What is the code below equivalent to? myClass.foreach(println _)

Answer

Correct Answer: MyClass.foreach(x => println(x))

Note: This Question is unanswered, help us to find answer for this one

22. To denote a parameter that may be repeated, what should you place after the type?

Answer

Correct Answer: *

Note: This Question is unanswered, help us to find answer for this one

23. What do you call the process of changing the definition of an inherited method?

Answer

Correct Answer: Overriding methods

Note: This Question is unanswered, help us to find answer for this one

24. Which statement about if-else-if-else statements is true?

Answer

Correct Answer: If an else-if succeeds, then none of the remaining else-if statements or elses will tested.

Note: This Question is unanswered, help us to find answer for this one

25. One way to improve code reliability is to use __ , which will evaluate a condition and return an error if the condition is violated.

Answer

Correct Answer: Assertions

Note: This Question is unanswered, help us to find answer for this one

26. What is called when a superclass has more than one subclass in Scala?

Answer

Correct Answer: Hierarchical inheritance

Note: This Question is unanswered, help us to find answer for this one

27. To denote a parameter that may be repeated, what should you place after type?

Answer

Correct Answer: -

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: MyFuture.onComplete

Note: This Question is unanswered, help us to find answer for this one

29. What does this code return? val x = 3; if (x > 2) x = 4 else x = x*2

Answer

Correct Answer: An error

Note: This Question is unanswered, help us to find answer for this one

30. You have written a Scala script. How would you access command-line arguments in the script?

Answer

Correct Answer: Use array named args

Note: This Question is unanswered, help us to find answer for this one

31. What do you call objects with immutable state?

Answer

Correct Answer: Functional objects

Note: This Question is unanswered, help us to find answer for this one

32. You have created an array using val. Can you change the value of any element of the array—and why or why not?

Answer

Correct Answer: 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.

Note: This Question is unanswered, help us to find answer for this one

33. When using pattern matching, which character matches on any object?

Answer

Correct Answer: _

Note: This Question is unanswered, help us to find answer for this one

34. What is the data type of y after this code is executed? val y = (math floor 3.1415 * 2)

Answer

Correct Answer: Double

Note: This Question is unanswered, help us to find answer for this one

35. Which Scala type may throw an exception or a successfully computed value, and is commonly used to trap and propagate errors?

Answer

Correct Answer: Scala.util.Try

Note: This Question is unanswered, help us to find answer for this one

36. What term is used to specify a precondition?

Answer

Correct Answer: Assert

Note: This Question is unanswered, help us to find answer for this one

37. For the for-yield construct, is the scope separate between for-body and yield-body?

Answer

Correct Answer: No, because for-yield shares the same scope, even though they are within separate curly braces.

Note: This Question is unanswered, help us to find answer for this one

38. Which is a subclass of all classes?

Answer

Correct Answer: Null

Note: This Question is unanswered, help us to find answer for this one

39. What type of object does this code create? val x = (1234, "Active")

Answer

Correct Answer: Tuple

Note: This Question is unanswered, help us to find answer for this one

40. When you convert a map to a list using the toList method of the map, the result will be of which type?

Answer

Correct Answer: List

Note: This Question is unanswered, help us to find answer for this one

41. What type of number is 1234.e5?

Answer

Correct Answer: Floating point

Note: This Question is unanswered, help us to find answer for this one

42. After defining a function in the interpreter, Scala returns the following. What does the () indicate? myfnc: ()Unit

Answer

Correct Answer: The function returns no value.

Note: This Question is unanswered, help us to find answer for this one

43. What data type would you use to store an immutable collection of objects that contain a fixed number of varying types?

Answer

Correct Answer: Tuple

Note: This Question is unanswered, help us to find answer for this one

44. What do you use in ScalaTest to see a detailed diagram of error messages when a test fails?

Answer

Correct Answer: DiagrammedAssertions

Note: This Question is unanswered, help us to find answer for this one

45. What is one way to avoid low-level parallelization details?

Answer

Correct Answer: Parallel collections

Note: This Question is unanswered, help us to find answer for this one

46. Scala bytecode can run on top of Java VM. What is the fundamental difference between Java object.clone() and Scala object.copy()?

Answer

Correct Answer: Copy() allows you to change values during the copying process; clone() does not.

Note: This Question is unanswered, help us to find answer for this one

47.

Despite using the same code, the different browser and database modules created really are separate modules. This means that each module has its own contents, including any nested classes. Which of the following options is described by the above statements?

Answer

Correct Answer: Tracking module instances

Note: This Question is unanswered, help us to find answer for this one

48.

Regex class is available in the ........................ package.

Answer

Correct Answer: scala.util.matching

Note: This Question is unanswered, help us to find answer for this one

49.

Which of the following is false about Case Class?

Answer

Correct Answer: Case classes can not be initialised

Note: This Question is unanswered, help us to find answer for this one

50.

Choose the correct statement that describes an 'abstract sealed' class?

Answer

Correct Answer: sealed classes can be extended in the same file

Note: This Question is unanswered, help us to find answer for this one

51.

Scala is ................ with Java. The two languages are interoperable with each other.

Answer

Correct Answer: compatible

Note: This Question is unanswered, help us to find answer for this one

52.

Choose the correct statements that describe a Scala Case class?

Answer

Correct Answer: Case class objects are used to implement complex behaviour
Case class instances can be created with a compact syntax

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

53.

Which of the following describe using Scala class that is defined in a package object from Java?

Answer

Correct Answer: com/mycompany/test/MyTest.class com/mycompany/test/package.class com/mycompany/test/package$.class
package com.mycompany package object test { val foobar = 42 } package test { class MyTest { def foo(): Int = { 42 } } }

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

54.

Often a module is too large to fit comfortably into a single file. When that happens, you can use traits to split a module into separate files. Which of the following is described by the above statement?

Answer

Correct Answer: Splitting modules into traits

Note: This Question is unanswered, help us to find answer for this one

55.

Which of the following is not valid in Scala?

Answer

Correct Answer: lazy var = 10

Note: This Question is unanswered, help us to find answer for this one

56.

When Extractor object is using the match statement, the unapply method will be __ executed.

Answer

Correct Answer: automatically

Note: This Question is unanswered, help us to find answer for this one

57.

Which one of the following options is equivalent to the code below? for { x <- 1 to 10 y <- 1 to 10 } yield (x,y)

Answer

Correct Answer: (1 to 10).flatMap( x => (1 to 10).map(y => (x,y)))

Note: This Question is unanswered, help us to find answer for this one

58.

Which of the following are types of function in Scala?

Answer

Correct Answer: All of the above

Note: This Question is unanswered, help us to find answer for this one

59.

Which of the following statement is about Scala anonymous function?

Answer

Correct Answer: In a source code, anonymous functions are called ‘function literals’ and at run time, function literals are instantiated into objects called function values. Scala provides a relatively easy syntax for defining anonymous functions.

Note: This Question is unanswered, help us to find answer for this one

60.

How to return a value from a scala function?

Answer

Correct Answer: private def checkValu() : Option[Integer] = { if(!list.isEmpty()){ for(value <-list){ val x = list.get(0) } } return (x) }
private def checkValu() : Option[Integer] = { if(!list.isEmpty()){ for(value <-list){ val x = list.get(0) } } return (x) }

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

61.

Which of the following one is Accessing scala object fields from java?

Answer

Correct Answer: public class JavaTest { public static void main(String[] args) { int i = TestObject.field(); } }

Note: This Question is unanswered, help us to find answer for this one

62.

Which of the following are functional trio in Scala?

Answer

Correct Answer: filter
map
reduce (foldLeft + foldRight)

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

63.

This trait consists of which of the following methods?

Answer

Correct Answer: Both 1 and 2

Note: This Question is unanswered, help us to find answer for this one

64.

If you want to compare two objects to see if they are equal, you should usually use either ==, or its inverse !=. Which of the following code is true?

Answer

Correct Answer: scala> 1 == 2 res24: Boolean = false scala> 1 != 2 res25: Boolean = true scala> 2 == 2 res26: Boolean = true

Note: This Question is unanswered, help us to find answer for this one

65.

Which of the following are function Literal's in Scala?

Answer

Correct Answer: “World” filter { c => c > 50 }
“World” filter { _ > 50 }
“World” filter ( _ > 50 )

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

66.

Choose the correct description for the Scala 'Function Type'?

Answer

Correct Answer: the trait FunctionN

Note: This Question is unanswered, help us to find answer for this one

67.

How to override type() java interface method in a scala class?

Answer

Correct Answer: override def `type` = { ... }

Note: This Question is unanswered, help us to find answer for this one

68.

Which of the following statement is false about Scala?

Answer

Correct Answer: Nil represents null similar in java

Note: This Question is unanswered, help us to find answer for this one

69.

Choose the statement that best describes the line def this(c: String) = this(0, c); for the code fragment below. class ABC(a: Int, b: Int, c: String) { def this(a: Int, c: String) = this(a, 0, c) def this(c: String) = this(0, c); }

Answer

Correct Answer: calls previous auxiliary constructor which calls the primary constructor

Note: This Question is unanswered, help us to find answer for this one

70.

A digital circuit is built from wires and function boxes. Wires carry signals which are transformed by function boxes. Signals will be represented by booleans: true for signal-on and false for signal-off. Which of the following are correct in context of the above statement?

Answer

Correct Answer: All of the above

Note: This Question is unanswered, help us to find answer for this one

71.

Which of the following are the Keywords used in Scala? Check all that apply.

Answer

Correct Answer: Var
Val
Sealed

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

72.

The expression List.apply(1,2,3) is the same as?

Answer

Correct Answer: List(1,2,3)

Note: This Question is unanswered, help us to find answer for this one

73.

Choose the correct output of the following code fragment?

List("Who","is","longest?").foldLeft((0,""))((i,s) => if (i._1 < s.length) (s.length,s) else i )

Answer

Correct Answer: (8,longest?)

Note: This Question is unanswered, help us to find answer for this one

74.

The primary tool Scala gives you to organize your programs is the class. A class is a blueprint for objects. Once you do a class, you can create objects from the class blueprint with the keyword new. Which of the following options is described by the above statements?

Answer

Correct Answer: Classes and types

Note: This Question is unanswered, help us to find answer for this one

75.

When writing the code of a Scala program, you create and interact with objects. To interact with a particular object, you can use a variable that refers to the object. You can do a variable with either a val or var, and assign an object to it. Which of the following option is described by the above statements?

Answer

Correct Answer: Objects and variables

Note: This Question is unanswered, help us to find answer for this one

76.

Which of the following are scala identifiers? Check all that apply.

Answer

Correct Answer: Mixed identifiers
Literal identifiers
Formatted identifiers

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

77.

Choose the best reason why the code fails to compile, for the type hierarchy and expressions as following?

class A { } class B extends A {} class C { } class D extends C { } case class S[T <: A ](a: T*) {} val aa: S[A ] = S[( new B(), new A() ) val cc = S( new D() )

Answer

Correct Answer: In the expression "val cc = S( new D() )" The inferred type arguments [D] do not conform to apply method's type parameter bounds

Note: This Question is unanswered, help us to find answer for this one

78.

Choose the statement that is not correct about Scala's List?

Answer

Correct Answer: List is an abstract class

Note: This Question is unanswered, help us to find answer for this one

79.

Which one of the following is a correct example of using toArray to convert to an array and toList to convert to a list?

Answer

Correct Answer: scala> buf.toArray res13: Array[Int] = Array(12, 15) scala> buf.toList res14: List[Int] = List(12, 15)

Note: This Question is unanswered, help us to find answer for this one

80.

What will be the output for the code fragment?

List(1, 2, 1, 2, 1, 2, 1, 2, 1, 2).drop(1).sliding(1, 2).flatten.toList

Answer

Correct Answer: List(2, 2, 2, 2, 2)

Note: This Question is unanswered, help us to find answer for this one

81.

What will be the output for the code fragment below?

List(Set(1), Set(1,2,3), Set(1, 2)) diff List(Set(1,2,3))

Answer

Correct Answer: List( Set(1), Set(1, 2) )

Note: This Question is unanswered, help us to find answer for this one

82.

Which of the following code snippets correctly represent Type inference on map method of Scala collections?

Answer

Correct Answer: case class MapResult(input: Any, output: Map[_ <: Any, Any]) { override def toString = output.map{ case (k: Any, v: Any) => input + " " + k + " " + v }.mkString("\n") }

Note: This Question is unanswered, help us to find answer for this one

83.

For the code below, Choose the correct statement?

List.tabulate(100)(a => a.toString)

Answer

Correct Answer: Results in an imutable list

Note: This Question is unanswered, help us to find answer for this one

84.

Create an array buffer just like an array, except that you do not need to specify a size. Which of the following implementation will adjust the allocated space automatically?

Answer

Correct Answer: scala> val buf = new ArrayBuffer[Int]() buf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

Note: This Question is unanswered, help us to find answer for this one

85.

Which one of the following code example of Scala collection type for filter is correct?

Answer

Correct Answer: scala> List(1, "1") collect { | case x: Int => x | } res0: List[Int] = List(1)

Note: This Question is unanswered, help us to find answer for this one

86.

Which one of the following statement is not correct for trait Iterator?

Answer

Correct Answer: Iterator extends Iterable

Note: This Question is unanswered, help us to find answer for this one

87.

Traits are used to define _ types by specifying the signature of the supported __?

Answer

Correct Answer: object
methods

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

88.

For the code below, which of the following statement is True?

val data = "test" list = List("this", "is", "a", "test") what will be the effect of applying the expression below to the list list.filterNot(elm => elm == data)

Answer

Correct Answer: will return all elements from the list except those which aren't equal to "test"

Note: This Question is unanswered, help us to find answer for this one

89.

Which of the following is false about Monads?

Answer

Correct Answer: Try is a monad with unit(x) = Try(x)

Note: This Question is unanswered, help us to find answer for this one

90.

What is output of the following code?

def main(args: Array[String]) { val capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") println("capitals.get( \"France\" ) : " + capitals.get( "France" )) println("capitals.get( \"India\" ) : " + capitals.get( "India" )) }

Answer

Correct Answer: Some(Paris),None

Note: This Question is unanswered, help us to find answer for this one

91.

Which of the following statement is false for traits?

Answer

Correct Answer: It is not possible to define default implementations for some methods

Note: This Question is unanswered, help us to find answer for this one

92.

Which one is not reserved word in Scala?

Answer

Correct Answer: switch

Note: This Question is unanswered, help us to find answer for this one

93.

Which of the following is Web Frameworks of Scala?

Answer

Correct Answer: The Play framework

Note: This Question is unanswered, help us to find answer for this one

94.

Choose the option that best describes the difference between a var and val definition in Scala?

Answer

Correct Answer: The object assigned to a val cannot be replaced

Note: This Question is unanswered, help us to find answer for this one

95.

In Scala everything is an Object, No ___?

Answer

Correct Answer: primitives

Note: This Question is unanswered, help us to find answer for this one

96.

Choose the best description for the variance expression as following?

A[B <: C]

Answer

Correct Answer: A[B <: C] means that class A can take any class B that is a subclass of C.

Note: This Question is unanswered, help us to find answer for this one

97.

Which of the following is false about Futures and Promises?

Answer

Correct Answer: Futures are non-blocking while promises are blocking

Note: This Question is unanswered, help us to find answer for this one

98.

For the type heirachy defined below, Choose the most correct statement below for the output?

trait A{ def a = 1 } trait X extends A{ override def a = { println("X") super.a } } trait Y extends A{ override def a = { println("Y") super.a } } For the expression val xy = new AnyRef with X with Y xy.a

Answer

Correct Answer: Prints Y then X

Note: This Question is unanswered, help us to find answer for this one

99.

Given the type hierarchy defined below, Choose the most correct statement below?

trait A{ def a = 1 } trait X extends A{ override def a = { println("X") super.a } } trait Y extends A{ override def a = { println("Y") super.a } } For the expression val xy = new AnyRef with X with Y

Answer

Correct Answer: The expression “with X with Y” is an example of the mixin syntax

Note: This Question is unanswered, help us to find answer for this one

100.

For the code below abstract class A { type B <: C abstract class C { def superClassM(b: B): D } class EImpl extends C { def superClassM(b: B): D = { val e = func1(this, b) e } } def func1(b: B, b1: B): D = D(b, b1) case class D(a: B, b: B) } The code produces the following compile error found : EImpl.this.type (with underlying type A.this.EImpl) required: A.this.B val e = func1(this, b) ^

Choose the expression that allows the code to compile

Answer

Correct Answer: type B = C

Note: This Question is unanswered, help us to find answer for this one

101.

For the code below, Choose the correct statement which explains the error?

abstract class A { type B <: C abstract class C { def superClassM(b: B): D } class EImpl extends C { def superClassM(b: B): D = { val e = func1(this, b) e } } def func1(b: B, b1: B): D = D(b, b1) case class D(a: B, b: B) } When complied produces the following error found : EImpl.this.type (with underlying type A.this.EImpl) required: A.this.B val e = func1(this, b) ^

Answer

Correct Answer: B is an abstract type

Note: This Question is unanswered, help us to find answer for this one

102.

Choose the best description that describes the Scala classes 'scala.AnyRef ' and 'scala.AnyVal'?

Answer

Correct Answer: Value classes extend scala.AnyVal and are predefined types, while all other classes are reference classes that extend scala.AnyRef

Note: This Question is unanswered, help us to find answer for this one

103.

What will the result using the following code?

List(None, Some(1), None, Some(2)).flatMap(x => x)

Answer

Correct Answer: List(1,2)

Note: This Question is unanswered, help us to find answer for this one

104.

How can you make a List[String] from a given val x = List[List[String]]?

Answer

Correct Answer: x.flatten

Note: This Question is unanswered, help us to find answer for this one

105.

Which of the following is not valid using List?

Answer

Correct Answer: val list :List[Int] = List() :: 1

Note: This Question is unanswered, help us to find answer for this one

106.

What is the output of the following code?

def main(args: Array[String]) { printMe(c = 4, b = 5, a = 7); def printMe( a:Int, b:Int, c:Int) = { println( a,b,c ); } }

Answer

Correct Answer: 7,5,4

Note: This Question is unanswered, help us to find answer for this one

107.

The map method on List (and Seq), transforms each element of a collection based on a function. For example, if we have a List[String] and want to convert it to all lowercase?

Answer

Correct Answer: scala> List("A", "Cat").map(s => s.toLowerCase)

Note: This Question is unanswered, help us to find answer for this one

108.

Choose the statement that is not correct for Scala's mutable.Map?

Answer

Correct Answer: mutable.Map has a withDefaultValue method that returns a default for undefined keys

Note: This Question is unanswered, help us to find answer for this one

109.

Choose the correct output using the code fragment below?

listOfInts.sliding(2).toList

Answer

Correct Answer: List( List(1, 2), List(2, 3) )

Note: This Question is unanswered, help us to find answer for this one

110.

How to groupBy using multiple columns in Scala collections?

Answer

Correct Answer: records.groupBy(record => (record.column1, record.column2, record.column3))

Note: This Question is unanswered, help us to find answer for this one

111.

What is the output of following code?

def main(args: Array[String]) { val x = {print("x");1} lazy val y = {print("y");2} def z = {print("z"); 3} z+y+x+z+y+x }

Answer

Correct Answer: xzyz

Note: This Question is unanswered, help us to find answer for this one

112.

For the code below, Choose the correct statement that describes an issue with the code?

val L = List[String]() for (a <- 1 to 100) { L :+ a.toString }

Answer

Correct Answer: “L:+ a.toString” is returning a new list each time

Note: This Question is unanswered, help us to find answer for this one

113.

What is the output of given code below?

def addInt( a:Int = 5, b:Int = 7 ) : Int = a + b print(addInt(3,7),addInt(3),addInt(),addInt(b=5))

Answer

Correct Answer: 10,10,12,10

Note: This Question is unanswered, help us to find answer for this one

114.

What is the output of the following code?

object MatchTest { def matchTest(x: Any): Any = x match { case 1 => "one" case "two" => 2 case y: Int => "scala.Int" } def main(args: Array[String]) { println(matchTest(2)) } }

Answer

Correct Answer: scala.Int

Note: This Question is unanswered, help us to find answer for this one

115.

Choose the statement that is not correct using the code below?

(1 to 10).par map println

Answer

Correct Answer: If you add par you get a parallel collection and operations on it will always be processed in parallel only if more than one system thread is avaiable

Note: This Question is unanswered, help us to find answer for this one

116.

For the code below, Choose the correct statement which shows the output?

var l1 = scala.collection.mutable.MutableList[String]() val l2 = List[String]() for (a <- 1 to 100) { l1 += a.toString } println(l1 .size); for (a <- 1 to 100) { l2 :+ a.toString } println(l2 .size);

Answer

Correct Answer: 100 then 0

Note: This Question is unanswered, help us to find answer for this one

117.

Choose the following statement that is not correct for parallel collections?

Answer

Correct Answer: Calling toSeq on a parallel collection will return a serial collection.

Note: This Question is unanswered, help us to find answer for this one

118.

If Seq is a trait then why is var x = Seq(1, 2, 3) legal?

Answer

Correct Answer: You are calling the apply method on the object

Note: This Question is unanswered, help us to find answer for this one

119.

You can convert any collection. to an array or a list. Such conversion requires copying all of the elements of the collection,and thusis slow for large collections. Sometimes you need to do it, though, due to an existing API. Further, many collections only have a few elements anyway, in which case there is only a small speed penalty?

Answer

Correct Answer: Conversion

Note: This Question is unanswered, help us to find answer for this one

120.

Scala provides __ versions of all of its collection types. These versions cannot be changed after they are initialized. You should use them whenever you know a collection should not be changed, so that you do not accidentally change it later?

Answer

Correct Answer: immutable

Note: This Question is unanswered, help us to find answer for this one

121.

For the code below, choose the correct statement which describes the code?

object A extends B with C { def f(x: Any): Any = ??? }

Answer

Correct Answer: Declares an anonymous class that extends both B and C

Note: This Question is unanswered, help us to find answer for this one

122.

What is the best way to “clone” a case class?

Answer

Correct Answer: Use the copy method

Note: This Question is unanswered, help us to find answer for this one

123.

String interpolation allows users to _____ variable references directly in processed string literals?

Answer

Correct Answer: Embed

Note: This Question is unanswered, help us to find answer for this one

124.

Using the following code fragment, Choose the correct statement which solves the error?

class A(n: Int) { var value = n } class B(n: Int) { val value = new A(n) } object Test { class A(n: Int) { var value = n } class B(n: Int) { val value = new A(n) } object Main extends App { val x = new B(5) x.value = 6 } When complied produces the following error Main.scala:7: error: reassignment to val x.value = 6 ^

Answer

Correct Answer: x.value.value = 6

Note: This Question is unanswered, help us to find answer for this one

125.

Choose the statement that is not correct for trait Traversable?

Answer

Correct Answer: Traversible objects can not be infinite

Note: This Question is unanswered, help us to find answer for this one

126.

Read the following code and tell when do Scala functions execute?

Answer

Correct Answer: All of the above

Note: This Question is unanswered, help us to find answer for this one

127.

For the code fragment below, Choose the expression that shows the correct output?

object Main extends App { val adder = new Adder(2) System.out.println(adder.apply(4)) } class Adder(x: Int) { var y = 5 def apply(y: Int) = x + y }

Answer

Correct Answer: 6

Note: This Question is unanswered, help us to find answer for this one

128.

Choose the statement that best describe's the Scala 'apply' function?

Answer

Correct Answer: When you give parameters directly to an object then you you pass these parameters to the apply function of that object

Note: This Question is unanswered, help us to find answer for this one

129.

Which of the following is false for Scala?

Answer

Correct Answer: Scala is pure functional

Note: This Question is unanswered, help us to find answer for this one

130.

For the code fragment below choose the correct statement?

val aa = bb filter (x => x % 2 == 0)

Answer

Correct Answer: aa will be a new collection, while bb will still be the same as before

Note: This Question is unanswered, help us to find answer for this one

131.

For the code fragment below

object Main extends App { def mySum[T <: Number](as: T*): Double = as.foldLeft(0d)(_ + _.doubleValue) implicit def intToInteger(n: Int): Integer = new Integer(n.toInt) var r = mySum(2, 3) }

When complied produces the following error, Choose the correct statement which solves the error when you remove the line implicit def intToInteger(n: Int): Integer = new Integer(n.toInt) Main.scala:4: error: inferred type arguments [Int] do not conform to method mySum's type parameter bounds [T <: Number] var r = mySum(2, 3) ^

Answer

Correct Answer: def mySum[T <% Number](as: T*): Double = as.foldLeft(0d)(_ + _.doubleValue)

Note: This Question is unanswered, help us to find answer for this one

132.

For the code fragment below, Choose the expression that shows the correct output?

object Main extends App { val adder = new Adder(2) System.out.println(adder.apply(4)) var a = new Adder(4) System.out.println(a.applyObj()) } class Adder(x: Int) { var y = 5 def apply(y: Int) = x + y def applyObj() = x + y }

Answer

Correct Answer: 6 then 9

Note: This Question is unanswered, help us to find answer for this one

133.

Which of the following is true for Objects, Traits and Classes?

Answer

Correct Answer: To create a program you should use main method in class

Note: This Question is unanswered, help us to find answer for this one

134.

Which of the following code statement is correct, Using scala object inside java?

Answer

Correct Answer: java.util.List res = scala.collection.JavaConverters$.MODULE$.seqAsJavaListConverter(variableName).asJava();

Note: This Question is unanswered, help us to find answer for this one

135.

Choose the correct description for both expressions, for the two expressions as following? expression 1 def m1(i:Int)=i+2 expression 2 (i:Int)=>i+2

Answer

Correct Answer: expression 1 is a method

Note: This Question is unanswered, help us to find answer for this one

136.

How do you provide overloaded constructors in Scala?

Answer

Correct Answer: Use the “this” keyword like def this(...

Note: This Question is unanswered, help us to find answer for this one

137.

For the code below, Choose the best statement to describe this error?

class A(n: Int) { var value = n } class B(n: Int) { val value = new A(n) } object Test { def main(args: Array[String]) { val x = new B(5) x = new B(6) } } Main.scala:7: error: reassignment to val x = new B(6) ^

Answer

Correct Answer: I can not replace the object created on the line above B(6) with this new one.

Note: This Question is unanswered, help us to find answer for this one

138.

Which one of the following about functional combinators is false?

Answer

Correct Answer: find returns the all elements of a collection that matches a predicate function

Note: This Question is unanswered, help us to find answer for this one

139.

Using the code fragment below, Choose the correct statement which explains the error?

object Main extends App { def mySum[T <: Number](as: T*): Double = as.foldLeft(0d)(_ + _.doubleValue) implicit def intToInteger(n: Int): Integer = new Integer(n.toInt) var r = mySum(2, 3) } When complied produces the following error Main.scala:4: error: inferred type arguments [Int] do not conform to method mySum's type parameter bounds [T <: Number] var r = mySum(2, 3) ^

Answer

Correct Answer: [T <: Number] type bounds means that T must be a subtype of Number

Note: This Question is unanswered, help us to find answer for this one

140.

Which of the following statement is false about the functions in Scala?

Answer

Correct Answer: It is possible to define def add(x:Int, y:Int) = x + y as def add(x:Int) (y:Int) = x + y c ) Following code gives compile error

Note: This Question is unanswered, help us to find answer for this one

141. True or False? Scala compiler automatically infers function parameter types.

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

142. def f(): Int = try { return 1 } finally { return 2 } Calling f() results in

Answer

Correct Answer: 2

Note: This Question is unanswered, help us to find answer for this one

143. A function which associates a value to a (variable) name is known as a(n):

Answer

Correct Answer: Environment

Note: This Question is unanswered, help us to find answer for this one

144. What is the runtime class of Unit?

Answer

Correct Answer: Class[Unit]

Note: This Question is unanswered, help us to find answer for this one

145. What is the Scala development environment designed for use in schools?

Answer

Correct Answer: Kojo

Note: This Question is unanswered, help us to find answer for this one

146. "Option" is:

Answer

Correct Answer: an abstract class

Note: This Question is unanswered, help us to find answer for this one

147. Which of the following is not one of the ways to increment the value of a variable i of type Int by 1.

Answer

Correct Answer: i++

Note: This Question is unanswered, help us to find answer for this one

148. def g(): Int = try { 1 } finally { 2 } Calling g() results in

Answer

Correct Answer: 1

Note: This Question is unanswered, help us to find answer for this one

149. Which of the following descriptions of case classes is *NOT* correct:

Answer

Correct Answer: Case classes are always immutable.

Note: This Question is unanswered, help us to find answer for this one

150. Which statement about the Option[+A] class is false?

Answer

Correct Answer: scala.Nothing is derived from Option[+A]

Note: This Question is unanswered, help us to find answer for this one

151. Which of these are NOT ways in which case classes differ from standard classes?

Answer

Correct Answer: Default definitions for the methods "equals" and "hashCode" are not provided

Note: This Question is unanswered, help us to find answer for this one

152. Which predicate tests whether x is object identical to y?

Answer

Correct Answer: x eq y

Note: This Question is unanswered, help us to find answer for this one

153. Are parenthesis `(` and curly braces `{` interchangeable?

Answer

Correct Answer: Only if the method expects a single parameter.

Note: This Question is unanswered, help us to find answer for this one

154. What is a lazy var?

Answer

Correct Answer: `lazy var` is not supported by Scala

Note: This Question is unanswered, help us to find answer for this one

155. What would be returned by the following function: def foo(l:List[Int]) = { var x=l.head; l.collect{ case a:Int if a>x => x=a; a }; x } When passed: List(2,4,6,8,6,3,1)

Answer

Correct Answer: 8

Note: This Question is unanswered, help us to find answer for this one

156. `() => Unit` is best described as

Answer

Correct Answer: A procedure type definition

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: It won't compile

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: No

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Any

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

161. Which statement about List is false?

Answer

Correct Answer: List is a proxy for java.util.ArrayList

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: public int $plus(int a)

Note: This Question is unanswered, help us to find answer for this one

163. Which statement about pattern matching is true?

Answer

Correct Answer: The case set must be exhaustive

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: scala.AnyRef

Note: This Question is unanswered, help us to find answer for this one

166. What is the name of the Scala compiler?

Answer

Correct Answer: "scalac"

Note: This Question is unanswered, help us to find answer for this one

167. Everything, including numbers and functions, are _______ in Scala.

Answer

Correct Answer: Objects

Note: This Question is unanswered, help us to find answer for this one

168. What is a class with a single instance?

Answer

Correct Answer: A singleton object

Note: This Question is unanswered, help us to find answer for this one

169. Which of the following best describes Scala?

Answer

Correct Answer: All of these choices describe Scala

Note: This Question is unanswered, help us to find answer for this one

170. Scala's "Unit" roughly corresponds to which Java type?

Answer

Correct Answer: "void"

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Interface

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

173. Does Scala support the return keyword?

Answer

Correct Answer: Yes, but it is not idiomatic Scala and therefor discouraged.

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

175. Scala is:

Answer

Correct Answer: An object-functional language that supports functional programming constructs

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A MatchError would be thrown.

Note: This Question is unanswered, help us to find answer for this one

177. What is an expression following the "if" keyword?

Answer

Correct Answer: A guard

Note: This Question is unanswered, help us to find answer for this one

178. True or False? Methods taking one argument can be used with infix syntax?

Answer

Correct Answer: True

Note: This Question is unanswered, help us to find answer for this one

179. Which statement best describes an Iterator

Answer

Correct Answer: An Iterator is a stream of incoming items where advancing to the next item consumes the current item

Note: This Question is unanswered, help us to find answer for this one

180. What is a higher-order function?

Answer

Correct Answer: Higher-order functions are functions that take other functions as parameters.

Note: This Question is unanswered, help us to find answer for this one

181. Explain how "abc".length returns 3

Answer

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

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: List(5,7,9)

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: A placeholder

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: False

Note: This Question is unanswered, help us to find answer for this one

185. The following code will > var x=100; var y=200; x->y

Answer

Correct Answer: a Tuple with Arity 2

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Type bound

Note: This Question is unanswered, help us to find answer for this one

187. Scala supports which types of polymorphism?

Answer

Correct Answer: Subtype, ad-hoc and parametric polymorphism

Note: This Question is unanswered, help us to find answer for this one

188. Witch one of the following operators is use for sequencing Parsers

Answer

Correct Answer: ~

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: Super-class

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: List[AnyVal]

Note: This Question is unanswered, help us to find answer for this one

191. Which statement best describes a partial function?

Answer

Correct Answer: 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

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The next element in the list

Note: This Question is unanswered, help us to find answer for this one

193. Which statement about case classes is false?

Answer

Correct Answer: Case classes as sealed and therefor cannot be extended

Note: This Question is unanswered, help us to find answer for this one

194. What is the defaut parameter call semantics?

Answer

Correct Answer: By Value

Note: This Question is unanswered, help us to find answer for this one

195. Classes in Scala, in contrast to Java, can have ______.

Answer

Correct Answer: Parameters

Note: This Question is unanswered, help us to find answer for this one

196. What is the tool "schema2src" used for?

Answer

Correct Answer: Data-binding

Note: This Question is unanswered, help us to find answer for this one

197. `Nil` is generally the same as:

Answer

Correct Answer: List()

Note: This Question is unanswered, help us to find answer for this one

198. Does Scala support tail-call recursion?

Answer

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

Note: This Question is unanswered, help us to find answer for this one

199. A valid description of a covariant type parameter would be:

Answer

Correct Answer: A type parameter which is allowed to vary down as the class is subtyped.

Note: This Question is unanswered, help us to find answer for this one

200. Describe class AnyRef

Answer

Correct Answer: All types except the value types descend from AnyRef

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: The "accumulator," which is the initial value for `a`

Note: This Question is unanswered, help us to find answer for this one

202. 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.

Answer

Correct Answer: List(1,2,3) :+ 5

Note: This Question is unanswered, help us to find answer for this one

203. 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?

Answer

Correct Answer: private[examplepackage] def foo = {...}

Note: This Question is unanswered, help us to find answer for this one

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

Answer

Correct Answer: "_"

Note: This Question is unanswered, help us to find answer for this one