Home / Interview / Scala :: General Questions

Interview :: Scala

51) What is Seq in Scala collection?

Seq is a trait which represents indexed sequences that are guaranteed immutable. You can access elements by using their indexes. It maintains insertion order of elements.

Sequences support many methods to find occurrences of elements or subsequences. It returns a list.

Example

52) What is Vector in Scala collection?

Vector is a general-purpose, immutable data structure. It provides random access of elements. It is suitable for a large collection of elements.

It extends an abstract class AbstractSeq and IndexedSeq trait.

Example

53) What is List in Scala Collection?

The List is used to store ordered elements. It extends LinearSeq trait. It is a class for immutable linked lists. This class is useful for last-in-first-out (LIFO), stack-like access patterns. It maintains order, can contain duplicates elements.

Example

54) What is the Queue in the Scala Collection?

Queue implements a data structure that allows inserting and retrieving elements in a first-in-first-out (FIFO) manner.

In Scala, Queue is implemented as a pair of lists. One is used to insert the elements and second to contain deleted elements. Elements are added to the first list and removed from the second list.

Example

55) What is a stream in Scala?

The stream is a lazy list. It evaluates elements only when they are required. This is a feature of Scala. Scala supports lazy computation. It increases the performance of your program.

Example

56) What does Map in Scala Collection?

The map is used to store elements. It stores elements in pairs of key and values. In Scala, you can create a map by using two ways either by using comma separated pairs or by using rocket operator.

Example

57) What does ListMap in Scala?

This class implements immutable maps by using a list-based data structure. You can create empty ListMap either by calling its constructor or using ListMap.empty method. It maintains insertion order and returns ListMap. This collection is suitable for small elements.

Example

58) What is a tuple in Scala?

A tuple is a collection of elements in the ordered form. If there is no element present, it is called an empty tuple. You can use a tuple to store any data. You can store similar type of mixing type data. You can return multiple values by using a tuple in function.

Example

59) What is a singleton object in Scala?

Singleton object is an object which is declared by using object keyword instead by class. No object is required to call methods declared inside a singleton object.

In Scala, there is no static concept. So Scala creates a singleton object to provide an entry point for your program execution.

Example

60) What is a companion object in Scala?

In Scala, when you have a class with the same name as a singleton object, it is called a companion class and the singleton object is called a companion object. The companion class and its companion object both must be defined in the same source file.

Example