Prev Next

Scala / Scala interview questions

1. What is Scala? 2. What does Scala support: OOP or FP? 3. Why Scala is statically typed or strongly typed language? 4. Advantages of using Scala. 5. Is Java a statically typed language? 6. Is scala a pure object-oriented programming language? 7. Explain about Scala variables. 8. Drawbacks of Scala Programming Language. 9. What is the latest version of Scala? 10. Who developed Scala Programming language? 11. How do you define a method in Scala? 12. Difference between an object and class in Scala. 13. How do you create a singleton object in Scala? 14. Difference between val and lazy val in Scala. 15. What is trait in Scala? 16. Does Scala allow methods in case classes? 17. What is case class in Scala? 18. What are the reserved keywords in Scala? 19. How do I use Scala reserved keyword as identifier? 20. What is Unit in Scala? 21. How to define class in Scala? 22. What is a Function in Scala? 23. What is a Main method in Scala? 24. What is “Any” type in Scala? 25. What is the equivalent type for java.lang.Object in Scala? 26. Explain AnyRef class in Scala. 27. Is Scala a functional language? 28. What are the predefined value types in Scala? 29. Type Casting in Scala. 30. Can we set default values for class parameters in Scala? 31. What is the rule for Scala named arguments? 32. How do you import all the classes from a package in Scala? 33. How to create constructor parameters as private in Scala? 34. Can a trait extend another trait in Scala? 35. Is Subtyping using trait allowed in Scala? 36. Can Scala classes extend more than one class? 37. What are mixins in Scala? 38. What are abstract types in Scala? 39. What is the advantage of using higher order functions in Scala? 40. Advantages of nested method in Scala. 41. What is method currying in Scala? 42. How == works with Case classes? 43. Can we reassign the parameter value for case class? 44. What are Sealed classes in Scala? 45. What is SBT tool? 46. Explain the implicit keyword in Scala. 47. Is Java increment operator supported in Scala? 48. Difference between Scala.List and java.util.List. 49. What is Play framework?

1. What is Scala?

Scala combines object-oriented and functional programming in one concise, high-level language. Scala runs on the Java platform (Java virtual machine) and is compatible with existing Java programs. Its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecos...

Read full answer

2. What does Scala support: OOP or FP?

Scala supports both Object oriented programming and Functional programming as in Java8.

Read full answer

3. Why Scala is statically typed or strongly typed language?

Scala is a statically-typed Language so type checking is done at compile-time by the compiler, not at run-time. Compiler checks many of the errors at compile-time so the developer would be resolving most of the errors during compile time itself.

Read full answer

4. Advantages of using Scala.

Simple and concise. Complete support for OOP concepts. Supports all Functional programming features. Type safe language. Java Interoperability and use of its libraries. Better parallel and concurrent programming constructs.

Read full answer

5. Is Java a statically typed language?

Yes, Java, Scala, C and C++ are strongly typed. Ruby, python, javascript are few examples of dynamically typed/loosely typed meaning that the type checking is done at run-time, not at compile-time by compiler.

Read full answer

6. Is scala a pure object-oriented programming language?

yes, scala is pure object oriented language because, functions and primitives are also objects in scala. Scala does not have static members.

Read full answer

7. Explain about Scala variables.

There are 2 types of variables in Scala, val and var . A value variable (val) is constant and its value cannot be changed once assigned. It is immutable, while a regular variable (var), is mutable, and you can change the value. val exVal: Int= 10 // cannot be changed var exVar : Int= 11

Read full answer

8. Drawbacks of Scala Programming Language.

Limited user community, Backward incompatiblity, Difficult to learn few concepts. Results in hybrid project using Scala integrating with Java libraries.

Read full answer

9. What is the latest version of Scala?

The current version of Scala is 2.12 .

Read full answer

10. Who developed Scala Programming language?

Martin Oderskey, a German computer scientist, designed and developed Scala language. Scala is initially released on Jan, 2004.

Read full answer

11. How do you define a method in Scala?

A method is defined in Scala using the def keyword. def main (args: Array[String]) { }

Read full answer

12. Difference between an object and class in Scala.

An object is a singleton instance of a class that does not need to be instantiated by the developer. If an object has the same name that a class, then the object is called a companion object. An object cannot accept parameter while class can.

Read full answer

13. How do you create a singleton object in Scala?

Using object keyword we can create a singleton object. object HelloWorld { def main (args:Array[String]) { println( "Hello world!" ) } }

Read full answer

14. Difference between val and lazy val in Scala.

The difference is, that a val is executed when it is defined whereas a lazy val is executed when it is accessed the first time.

Read full answer

15. What is trait in Scala?

Traits are used to share interfaces and fields between classes. They are similar to Java 8's interfaces. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters. Traits are types containing certain fields and methods. Even multiple traits can be co...

Read full answer

16. Does Scala allow methods in case classes?

Yes. Case class can have methods.

Read full answer

17. What is case class in Scala?

Scala has a special type of class called a "case" class. By default, case classes are immutable and compared by value. You can define case classes with the case class keywords. case class Circle (radius: Double) You can instantiate case classes without "new" keyword. val circleA = Circle(5.0) val...

Read full answer

18. What are the reserved keywords in Scala?

There are 39 keywords. They are abstract, case, catch, class, def, do, else, extends, false, final, finally, for, forSome, if, implicit, import, lazy, match, new, null, object, override, package, private, protected, return, sealed, super, this, throw, trait, try, true, type, val, var, while, with...

Read full answer

19. How do I use Scala reserved keyword as identifier?

Use backtick to access Scala reserved keyword as identifier. For example, use yield with backticks to ignore the Scala's yield and access the Java's Thread class's method yield instead. Thread.`yield`()

Read full answer

20. What is Unit in Scala?

The Unit type is used to define a function that doesn't return data. It is similar to the void keyword in Java. def main (args: Array[String]) : Unit = { }

Read full answer

21. How to define class in Scala?

You can define classes with the class keyword followed by its name and constructor parameters. class GreeterClass (prefix: String, suffix: String) { def greet (name: String){ println(prefix + name + suffix) } } You can make an instance of a class with the “new” keyword. val greeter = new Greeterc...

Read full answer

22. What is a Function in Scala?

Functions are expressions that take parameters and sometimes no parameters. You may define an anonymous function (without a name) that returns a given integer plus five. (y: Int) => y+5 In the above example, left of the => operator is the list of parameters and on the right is the expressions bas...

Read full answer

23. What is a Main method in Scala?

The main method is an entry point of a program. The Java Virtual Machine requires a main method to be named main and take one argument, an array of strings that accepts command line arguments. object MyMain { def main (args: Array[String]): Unit = println( "Hello world!" ) }

Read full answer

24. What is “Any” type in Scala?

Any is the supertype of all types , also called the top type. It defines certain methods such as equals, hashCode, and toString . Any has two direct subclasses: AnyVal and AnyRef .

Read full answer

25. What is the equivalent type for java.lang.Object in Scala?

If Scala is used in the context of a Java runtime environment, AnyRef corresponds to java.lang.Object.

Read full answer

26. Explain AnyRef class in Scala.

Class AnyRef is the root class of all the reference types. All types except the value types descend from this class. class AnyRef extends Any

Read full answer

27. Is Scala a functional language?

Yes, although few claim it is not fully functional, it supports many functional programming features than Java8. Scala supports, pattern matching, Tail recursion, Lazy evaluation, Function currying, Type inference and immutability.

Read full answer

28. What are the predefined value types in Scala?

There are 9 predefined types under AnyVal and they are non-nullable. It is Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean .

Read full answer

29. Type Casting in Scala.

Value types can be cast in the following order: Byte -> Short -> Int -> Long -> Float -> Double. Also Char value type can be casted to Int. The above orders are unidirectional and other castings lead to compilation error. You can also cast a reference type to a subtype.

Read full answer

30. Can we set default values for class parameters in Scala?

Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters as it becomes optional. class MyClass (defaultVar : String = "Hello" ) {} The method parameters also can have default values.

Read full answer

31. What is the rule for Scala named arguments?

The order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature. def printDetails (first: String, last: String): Unit = { println(first + " " + last) } print...

Read full answer

32. How do you import all the classes from a package in Scala?

Use underscore (_) following the package name to import all classes. import employees._ To import only 2 classes from a package use the following structure. import employees. {Employee,Dept}

Read full answer

33. How to create constructor parameters as private in Scala?

Parameters without val or var are private values, visible only within the class.

Read full answer

34. Can a trait extend another trait in Scala?

Yes. Use extends keyword to extend a trait. If a class extends a trait, then implement any abstract members of the trait using the override keyword.

Read full answer

35. Is Subtyping using trait allowed in Scala?

Yes . Where a given trait is required, a subtype of the trait can be used instead.

Read full answer

36. Can Scala classes extend more than one class?

No. Classes can only have one superclass but can many mixins.

Read full answer

37. What are mixins in Scala?

Mixins are traits which are used to compose a class. Mixins are added to class using with keyword. A class can have more than one mixins.

Read full answer

38. What are abstract types in Scala?

Abstract types are defined using type keyword and it is used to describe the type of element whose actual type is provided by the concrete implementation. Traits and abstract classes can have an abstract type member.

Read full answer

39. What is the advantage of using higher order functions in Scala?

Higher-order functions eliminates redundancy in code by grouping common functionality as a function passed to other functions.

Read full answer

40. Advantages of nested method in Scala.

Nested method helps structure the code and promotes readability.

Read full answer

41. What is method currying in Scala?

Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments which is known as method currying.

Read full answer

42. How == works with Case classes?

Case classes are compared by structure and not by reference. case class Greeting (message: String) val greet1 = Greeting ( “ Hello ” ) val greet2 = Greeting ( “ Hello ” ) val greetingsAreSame = greet1 == greet2 // true Even though greet1 and greet2 refer to different objects, the value of each ob...

Read full answer

43. Can we reassign the parameter value for case class?

No. When you create a case class with parameters, the parameters are public val implicitly and the parameter becomes immutable. It is possible to use var keyword in case classes but this is discouraged.

Read full answer

44. What are Sealed classes in Scala?

Traits and classes can be marked sealed which means all subtypes must be declared in the same file. This assures that all subtypes are known. sealed abstract class Communication case class VoiceCall () extends Communication case class Message () extends Communication def findPlaceToSit (commtype:...

Read full answer

45. What is SBT tool?

sbt is an open-source build tool for Scala and Java projects, similar to Java's Maven and Ant. Its main features are: Native support for compiling Scala code and integrating with many Scala test frameworks.

Read full answer

46. Explain the implicit keyword in Scala.

implicit keyword makes the class's primary constructor available for implicit conversions when the class is in scope. To create an implicit class, simply place the implicit keyword in front of an appropriate class. They must be defined inside of another trait/class/object.

Read full answer

47. Is Java increment operator supported in Scala?

No, increment/ decrement operators (for example, i++ and ++i) is not supported in Scala. Instead you may have to use i=i+1 or i+=1.

Read full answer

48. Difference between Scala.List and java.util.List.

Scala.List represents an immutable collection while Java List is mutable.

Read full answer

49. What is Play framework?

Play is a high-velocity web framework for Java and Scala. Play is built on the Akka toolkit. It is lightweight, stateless and uses web-friendly architecture.

Read full answer

«
»

Comments & Discussions