scala面试问题
Before reading this post, please go through my previous two posts at “Scala Basic” and “Scala Intermediate” Interview Questions and Answers to get some basic knowledge about Scala Language.
在阅读本文之前,请仔细阅读我之前的两篇文章《 Scala Basic 》和《 Scala Intermediate 》面试问答,以获取有关Scala语言的一些基础知识。
In this post, we are going discuss about some Advanced Scala Interview Questions that are related to Scala Advanced concepts and also some Real-time project related concepts.
在本文中,我们将讨论一些与Scala Advanced概念相关的Advanced Scala面试问题以及一些与实时项目相关的概念。
Note: As this list has already become very large, I’m going to deliver another post with remaining Questions and Answers. Please refer that post at: “Scala Intermediate and Advanced Interview Questions and Answers”
注意:由于此列表已经非常庞大,因此我将提供另一篇文章,其中包含剩余的“问题与解答”。 请在以下位置参阅该帖子:“ Scala中级和高级面试问答 ”
We will also discuss Scala/Java Concurrency and Parallelism Interview Questions and Answers, which are useful for Senior or Experienced Scala/Java Developer.
我们还将讨论Scala / Java并发性和并行性面试问题和答案,这对资深或有经验的Scala / Java开发人员很有用。
In this section, we are going to list out all Scala Intermediate Interview Questions and in next section we will discuss them in detail.
在本节中,我们将列出所有Scala中级面试问题,在下一节中,我们将详细讨论它们。
In this section, we will pickup each and every question from above list and discuss in-detail with suitable examples(if required). If you want to understand these concepts in-depth with examples, please go through my previous posts in Scala Tutorials section.
在本节中,我们将从上面的列表中提取每个问题,并详细讨论适当的示例(如果需要)。 如果您想通过示例深入了解这些概念,请阅读我之前在Scala教程部分中的文章。
Current Scala’s stable is 2.11.7. It supports Java SE 7.
当前Scala的稳定值为2.11.7。 它支持Java SE 7。
The major change or update in Scala 2.12 version is that it supports Java SE 8 or later versions only. Scala 2.12 is not a binary compatible with the 2.11.x series. It’s still in Mile Stone Builds only.
Scala 2.12版本中的主要更改或更新是它仅支持Java SE 8或更高版本。 Scala 2.12不是与2.11.x系列兼容的二进制文件。 它仍然仅在Mile Stone Builds中。
In Scala, Option is used to represent optional values that is either exist or not exist.
Option is an abstract class. Option has two subclasses: Some and None. All three (Option, Some and None) are defined in “scala” package like “scala.Option”.
在Scala中,Option用于表示存在或不存在的可选值。
Option是一个抽象类。 Option有两个子类:Some和None。 这三个选项(Option,Some和None)都在“ scala”包中定义,例如“ scala.Option”。
Option is a bounded collection in Scala, which contains either zero or one element. If Option contains zero elements that is None. If Option contains one element, that is Some.
Option是Scala中的有界集合,其中包含零或一个元素。 如果Option包含零个元素,则为None。 如果Option包含一个元素,则为Some。
Some is used to represent existing value. None is used to represent non-existent value.
Example:-
有些用于表示现有价值。 无用于表示不存在的值。
例:-
def get(val index: Int): Option[String]
Let us assume that this method is from List. This method has a return type of Option[String]. If List contains elements, this get method returns “Some[String]” element available in that index position. Otherwise, it returns “None” (that is no elements)
让我们假设此方法来自List。 此方法的返回类型为Option [String]。 如果List包含元素,则此get方法将返回在该索引位置可用的“ Some [String]”元素。 否则,它返回“无”(即没有元素)
Some is a case class and None is an Object. As both are case class/object, we can use them in Pattern Matching very well.
一些是案例类,无是对象。 由于两者都是案例类/对象,因此我们可以很好地在模式匹配中使用它们。
The combination of all these three definitions is known as Option/Some/None Design Pattern in Scala.
这三个定义的组合在Scala中称为“选项/某些/无设计模式”。
In Scala, Either is an abstract class. It is used to represent one value of two possible types. It takes two type parameters: Either[A,B].
在Scala中,Either是一个抽象类。 它用于表示两种可能类型的一个值。 它带有两个类型参数:Either [A,B]。
It exactly have two subtypes: Left and Right. If Either[A,B] represents an instance A that means it is Left. If it represents an instance B that means it is Right.
它恰好具有两个子类型:左和右。 如果Either [A,B]表示实例A,则表示它为Left。 如果它表示实例B,则表示它是对的。
This is known as Either/Left/Right Design Pattern in Scala.
这在Scala中被称为“任一/左/右设计模式”。
Scala’s Option is similar to Java SE 8’s Optional. Java SE 8 has introduced a new utility class Optional to represent existing or non-existing of some value. Optional is available in java.util package.
Scala的Option与Java SE 8的Optional类似。 Java SE 8引入了一个新的可选实用程序类Optional,以表示某些值的存在或不存在。 可选在java.util包中可用。
Both Scala’s Option and Java SE 8’s Optional are used to represent optional values. Both are used to avoid unwanted null checks and NullPointerException.
Scala的Option和Java SE 8的Optional都用来表示可选值。 两者都用于避免不必要的null检查和NullPointerException。
The following are the Advantages of Functional Programming (FP) or Advantages of Pure Functions:
以下是函数编程(FP)的优点或纯函数的优点:
There are many Scala-Based Framework to develop RESTful Web Services. Most popular frameworks are:
有许多基于Scala的框架可用于开发RESTful Web服务。 最受欢迎的框架是:
In Play, we call REST API URLs as routes. We place all routes at once place in Play framework. It is a stateless web framework to develop REST API easily.
在Play中,我们将REST API URL称为路由。 我们将所有路线一次放置在Play框架中。 这是一个易于开发REST API的无状态Web框架。
It is very simple and easy Scala-based web framework to develop REST API
这是一个非常简单,基于Scala的Web框架,用于开发REST API
It is very concise and built on top of Akka framework so it’s better to develop REST API using Actor Model.
它非常简洁,并基于Akka框架构建,因此最好使用Actor Model开发REST API。
It allows routing using Pattern Matching concept.
它允许使用模式匹配概念进行路由。
Swagger is is the best tool for this purpose. It is very simple and open-source tool for generating REST APIs documentation with JSON for Scala-based applications.
Swagger是用于此目的的最佳工具。 这是一个非常简单且开源的工具,用于为基于Scala的应用程序使用JSON生成REST API文档。
Like JPA, Hibernate and Toplink etc ORM Frameworks for Java-based applications, There are many ORM frameworks to use in Play/Scala based applications.
Popular ORM frameworks for Play/Scala based applications:
像JPA,Hibernate和Toplink等基于Java的应用程序的ORM框架一样,在基于Play / Scala的应用程序中可以使用许多ORM框架。
适用于基于Play / Scala的应用程序的流行ORM框架:
ReactiveMongo is the best Scala Driver to develop Play/Scala applications to persist data in MongoDB NoSQL data store. It supports fully non-blocking and asynchronous I/O operations.
ReactiveMongo是开发Play / Scala应用程序以将数据持久存储在MongoDB NoSQL数据存储中的最佳Scala驱动程序。 它支持完全非阻塞和异步I / O操作。
Thousands of clients are using Play and Scala in Production. The following list is the more popular clients who are using Play and Scala actively.
成千上万的客户正在生产中使用Play和Scala。 以下列表是积极使用Play和Scala的最受欢迎的客户。
Play 2 is completely written in Scala. If we use Java with Play framework, we need to face many issues because Java does not support full FP features.
播放2完全用Scala编写。 如果我们将Java与Play框架一起使用,则需要面对许多问题,因为Java不支持完整的FP功能。
Scala is the best option to use with Play framework to develop Highly Scalable, Better Performance with Concurrency/Parallelism and Low latency applications, because:
Scala是与Play框架一起使用来开发具有并发性/并行性和低延迟应用程序的高度可扩展,更好性能的最佳选择,因为:
As Scala supports Multi-Paradigm Programming(Both OOP and FP) and uses Actor Concurrency Model, we can develop very highly Scalable and high-performance applications very easily.
由于Scala支持Multi-Paradigm编程(OOP和FP),并使用Actor并发模型,因此我们可以非常轻松地开发具有高度可伸缩性和高性能的应用程序。
The following three are most popular available Build Tools to develop Play and Scala Applications:
以下是开发Play和Scala应用程序的最受欢迎的构建工具:
SBT stands for Scala Build Tool. Its a Simple Build Tool to develop Scala-based applications.
SBT代表Scala Build Tool。 它是一个简单的构建工具,用于开发基于Scala的应用程序。
Most of the people uses SBT Build tool for Play and Scala Applications. For example, IntelliJ IDEA Scala Plugin by default uses SBT as Build tool for this purpose.
大多数人都将SBT Build工具用于Play和Scala应用程序。 例如,默认情况下,IntelliJ IDEA Scala插件使用SBT作为构建工具。
The following are most popular available Unit Testing, Functional Testing and/or BDD Frameworks for Play/Scala Based applications:
以下是针对基于Play / Scala的应用程序中最受欢迎的单元测试,功能测试和/或BDD框架:
SCoverage is the Code-coverage tool for Play and Scala based applications.
SCoverage是用于基于Play和Scala的应用程序的代码覆盖工具。
SCoverage stands for Scala Code-coverage tool. It has three separate plug-ins to supports the following build tools:
SCoverage代表Scala代码覆盖率工具。 它具有三个单独的插件来支持以下构建工具:
Like Checkstyle for Java-Based Applications, Scalastyle is best Scala style checker tool available for Play and Scala based applications.
与基于Java的应用程序的Checkstyle一样,Scalastyle是可用于基于Play和Scala的应用程序的最佳Scala样式检查器工具。
Scalastyle observes our Scala source code and indicates potential problems with it. It has three separate plug-ins to supports the following build tools:
Scalastyle会观察我们的Scala源代码并指出潜在的问题。 它具有三个单独的插件来支持以下构建工具:
It has two separate plug-ins to supports the following two IDEs:
它具有两个单独的插件来支持以下两个IDE:
The following two popular IDEs support Play and Scala-Based Applications Development:
以下两个流行的IDE支持Play和基于Scala的应用程序开发:
They support by using Scala Plugins like Eclipse IDE has a Scala IDE for Eclipse to support Play and Scala-Based Applications Development.
它们通过使用Scala插件来支持,例如Eclipse IDE具有一个用于Eclipse的Scala IDE,以支持基于Play和Scala的应用程序开发。
IntelliJ IDEA has a plug-in like “Scala Plugin for IntelliJ IDEA” to support “Scala, SBT and Play 2 Framework” based applications.
IntelliJ IDEA具有类似于“ IntelliJ IDEA的Scala插件”的插件,可支持基于“ Scala,SBT和Play 2 Framework”的应用程序。
Play Framework’s default Unit and Functional Testing Framework is Spec2. It is very easy to test Play/Scala based applications using Spec2 Framework.
Play框架的默认单位和功能测试框架为Spec2。 使用Spec2 Framework测试基于Play / Scala的应用程序非常容易。
Play Framework’s Default built-in template is “Twirl”. It was developed in Scala. By using these templates, we can develop Play/Scala based applications very easily.
Play Framework的默认内置模板为“旋转”。 它是在Scala中开发的。 通过使用这些模板,我们可以非常轻松地开发基于Play / Scala的应用程序。
The Built-in or Default Web Server available for Play Framework is Netty Server.
可用于Play框架的内置或默认Web服务器是Netty Server。
Because Scala supports the following extra features, it is better than Java 8:
由于Scala支持以下额外功能,因此它比Java 8更好:
Anonymous Function is also a Function but it does not have any function name. It is also known as a Function Literal.
The advantages of a Anonymous Function/Function Literal in Scala:
匿名函数也是一个函数,但是它没有任何函数名称。 也称为功能文字。
Scala中的匿名函数/函数文字的优点:
Higher Order Function (HOF) is also a function but which performs one, two or both of the following things:
高阶函数(HOF)也是一个函数,但是它执行以下一项或多项任务:
Case class is also a class, however when we compare it with normal class, it gives the following extra features or benefits:
案例类也是一个类,但是当我们将其与普通类进行比较时,它具有以下额外的功能或好处:
All these features are added by Scala Compiler at compile-time. It is not possible with normal class.
所有这些功能都是由Scala编译器在编译时添加的。 普通班不可能。
The following are the major advantages of Play/Scala stack to develop web applications:
以下是Play / Scala堆栈开发Web应用程序的主要优点:
Play is an Open-source free-software framework to develop web applications.
Play是用于开发Web应用程序的开放源代码免费软件框架。
Play framework’s Auto-reload feature improves Developer Productivity. No need to build, deploy and test our changes. Just do our changes and refresh the page to see our changes.
Play框架的自动重载功能提高了开发人员的生产力。 无需构建,部署和测试我们的更改。 只需进行更改,然后刷新页面即可查看更改。
Play is HTTP based stateless model to serve web requests so it is very easy to develop REST API or RESTful Web Services.
Play是基于HTTP的无状态模型来服务Web请求,因此开发REST API或RESTful Web服务非常容易。
If we develop our web application using Play framework,it informs all errors in the browser in very useful format. It shows error message, the file location, line number where error occurred, highlighting the code-snippet to understand the error very easily.
如果我们使用Play框架开发Web应用程序,它将以非常有用的格式通知浏览器中的所有错误。 它显示错误消息,文件位置,发生错误的行号,并突出显示代码片段以非常容易地理解错误。
Play framework is developed by following Reactive design patterns and it is built on top of Netty sever to utilize Non-blocking IO Feature. Because of this feature, we can develop very highly Scalable and performance applications very easily.
Play框架是根据Reactive设计模式开发的,它是基于Netty服务器构建的,以利用非阻塞IO功能。 由于此功能,我们可以非常轻松地开发高度可伸缩和高性能的应用程序。
Play is very flexible framework and supports developing plug-ins very easy to extend it’s features and functionality.
Play是一个非常灵活的框架,并支持非常容易地扩展其特性和功能的开发插件。
As both Scala and Play supports Functional Programming, it is very easy to develop Highly Concurrency and Better Parallelism applications very easily because FP supports Immutability, Pure Functions (Functions without side-effects), Pattern Matching, Actor Model etc.
由于Scala和Play都支持函数式编程,因此很容易开发高度并发和更好的并行性应用程序,因为FP支持不变性,纯函数(无副作用的函数),模式匹配,Actor模型等。
As both Scala and Play supports Functional Programming, we can develop more modular and reusable applications. It is also very easy to test more modular applications.
由于Scala和Play都支持函数式编程,因此我们可以开发更多的模块化和可重用的应用程序。 测试更多模块化应用程序也非常容易。
Java’s OOP constructs, which are not supported by Scala:
Scala不支持Java的OOP构造:
Scala’s OOP constructs, which are not supported by Java:
OR
The new OOPs constructs introduced by Scala, but not supported by Java:
Scala的OOP构造,Java不支持:
要么
Scala引入了新的OOPs构造,但Java不支持:
Call-by-name means evaluates method/function parameters only when we need them or we access them. If we don’t use them, then it does not evaluate them.
按名称调用意味着仅在需要它们或访问它们时才评估方法/函数参数。 如果我们不使用它们,那么它不会评估它们。
Scala supports both call-by-value and call-by-name function parameters. However, Java supports only call-by-value, but not call-by-name.
Scala支持按值调用和按名称调用功能参数。 但是,Java仅支持按值调用,不支持按名称调用。
Difference between call-by-value and call-by-name:
The major difference between these two are described below:
按值致电和按姓名致电的区别:
两者之间的主要区别如下所述:
Call-by-value:
按值致电:
def myFunction(a: Int, b: Int) { }
Here both a and b are Call-by-value parameters to myFunction.
这里a和b都是myFunction的按值调用参数。
Call-by-name:
姓名呼叫:
def myFunction(a: Int, b: => Int) { }
Here both a is a Call-by-value parameter and b is Call-by-name to myFunction.
这里,a都是按值调用参数,b是myFunction的按名称调用。
The following are the most popular MVC frameworks available for Scala Language to develop Web Applications:
以下是可用于Scala语言开发Web应用程序的最受欢迎的MVC框架:
Most of the Java-based projects uses Maven as their Build tool. However, most of the people uses SBT as build tool to develop their Scala-based applications, but Some Teams uses Maven too as a build tool to develop their Scala-based applications.
大多数基于Java的项目都使用Maven作为其Build工具。 但是,大多数人都使用SBT作为构建工具来开发基于Scala的应用程序,但是某些团队也使用Maven作为构建工具来开发基于Scala的应用程序。
Maven folder structure for both Java-based and Scala-based projects is almost same just one folder name change as shown in the below diagrams: java and scala folder names.
基于Java的项目和基于Scala的项目的Maven文件夹结构几乎相同,只是更改了一个文件夹名称,如下图所示:Java和Scala文件夹名称。
Java-based Maven Project Folder Structure:
基于Java的Maven项目文件夹结构:
Scala-based Maven Project Folder Structure:
基于Scala的Maven项目文件夹结构:
Not only in Java and Scala, in almost all OOP languages Constructor is used to create (or assemble) an object or an instance of a Class using it’s parameters (or components). Extractor is quite opposite to Constructor.
不仅在Java和Scala中,几乎在所有OOP语言中,构造函数都用于使用其参数(或组件)创建(或组装)对象或Class的实例。 提取器与构造器完全相反。
In Scala, Extractor is used to decompose or disassemble an object into it’s parameters (or components).
在Scala中,Extractor用于将对象分解或分解成其参数(或组件)。
In Scala, apply method is a Constructor. Internally, Extractor uses unapply method to decompose an objects into it’s parts (or parameters). In Scala, Extractor is mainly used in Pattern Matching concept. We will discuss Pattern Matching concept soon.
在Scala中,apply方法是一个构造方法。 在内部,Extractor使用unapply方法将对象分解为其部分(或参数)。 在Scala中,提取器主要用于模式匹配概念。 我们将很快讨论模式匹配的概念。
This ‘???’ three question marks is not an operator, a method in Scala. It is used to mark a method which is ‘In Progress’ that means Developer should provide implementation for that one.
这个 '???' 三个问号不是运算符,是Scala中的一种方法。 它用于标记一种“进行中”的方法,这意味着开发人员应为该方法提供实现。
This method is define in scala.PreDef class as shown below:
此方法在scala.PreDef类中定义,如下所示:
def ??? : Nothing = throw new NotImplementedError
If we run that method without providing implementation, then it throws ‘NotImplementedError’ error as shown below:
如果我们在不提供实现的情况下运行该方法,那么它将引发“ NotImplementedError”错误,如下所示:
scala> def add(a:Int, b:Int) : Int = ???
add: (a: Int, b: Int)Int
scala> add(10,20)
scala.NotImplementedError: an implementation is missing
In Scala, both List and Stream are from Collection API and works almost similar. Both are Immutable collections.
在Scala中,List和Stream均来自Collection API,并且工作原理几乎相似。 两者都是不可变的集合。
However, there is one main difference between List and Stream in Scala Collection API: That is List elements are evaluated Eagerly and Stream elements are evaluated Lazily that means when we access them.
但是,Scala Collection API中的List和Stream之间有一个主要区别:即,对List元素进行早期评估,对Stream元素进行延迟评估,这意味着我们对其进行访问。
scala> var list1 = List(1,2,3,4)
list1: List[Int] = List(1, 2, 3, 4)
Here we can observe that all List elements evaluated at the time of creating List object. However, if we do same thing on Stream, we cannot see all elements. We can see only first evaluated element and remaining elements are evaluated lazily as shown below:
在这里,我们可以看到在创建List对象时评估了所有List元素。 但是,如果我们在Stream上执行相同的操作,则看不到所有元素。 我们可以看到只有第一个求值的元素和剩余的元素被懒惰求值,如下所示:
scala> var s1 = Stream(1,2,3,4)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
When we want Lazy collection to evaluate elements only when we access them then it’s better to use Stream.
如果我们希望Lazy集合仅在访问元素时才评估元素,那么最好使用Stream。
In Scala Collection API,
在Scala Collection API中,
scala> var list1 = List(1,2,3,4)
list1: List[Int] = List(1, 2, 3, 4)
scala> list1 = 0 :: list1
list1: List[Int] = List(0, 1, 2, 3, 4)
scala> var list1 = List(3,4,5)
list1: List[Int] = List(3, 4, 5)
scala> val list2 = List(1,2) ::: list1
list2: List[Int] = List(1, 2, 0, 1, 2, 3, 4)
scala> var s1 = Stream(1,2,3,4)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> s1 = 0 #:: s1
s1: scala.collection.immutable.Stream[Int] = Stream(0, ?)
scala> var s1 = Stream(1,2,3,4)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> val s2 = Stream(-1,0) #::: s1
s2: scala.collection.immutable.Stream[Int] = Stream(-1, ?)
If you want to become a Fullstack Scala Developer, you should learn the following technology stack:
如果您想成为Fullstack Scala开发人员,则应该学习以下技术堆栈:
NOTE:- In Scala, Extractor follows Extractor Design Pattern. If you want to learn it in depth, please go through my Scala Tutorial (Most of the posts follows this pattern: Scala xxxx In Depth where xxxx is a concept like Extractor).
注意:-在Scala中,提取器遵循提取器设计模式。 如果您想深入学习它,请阅读我的Scala教程(大多数文章都遵循这种模式:Scala xxxx在深度中,xxxx是类似Extractor的概念)。
That’s it all about “Scala Advanced Interview Questions and Answers”. We will discuss some more Scala Interview Questions and Answers in my coming posts.
关于“ Scala高级面试问答”的全部内容。 在我的后续文章中,我们将讨论更多Scala面试问答。
Please drop me a comment if you like my post or have any issues/suggestions.
如果您喜欢我的帖子或有任何问题/建议,请给我评论。
翻译自: https://www.journaldev.com/8960/scala-advanced-interview-questions
scala面试问题