scala面试问题

Before reading this post, please go through my previous post at “Scala Basic Interview Questions and Answers” to get some basic knowledge about Scala Language.

在阅读本文之前,请仔细阅读我以前的文章《 Scala基本面试问答 》,以获取有关Scala语言的一些基本知识。

In this post, we are going to discuss about some more Scala Interview Questions which are useful for some experienced Scala Developers.

在这篇文章中,我们将讨论更多的Scala面试问题,这些问题对一些有经验的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中级和高级面试问答 ”

Scala面试问题 (Scala Interview Questions)

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中级面试问题,在下一节中,我们将详细讨论它们。

  1. What is Primary Constructor? What is Secondary or Auxiliary Constructor in Scala?什么是主要构造函数? Scala中的二级或辅助构造函数是什么?
  2. What is the use of Auxiliary Constructors in Scala?Please explain the rules to follow in defining Auxiliary Constructors in Scala?Scala中的辅助构造函数有什么用途?请解释在Scala中定义辅助构造函数时应遵循的规则?
  3. What are the differences between Array and ArrayBuffer in Scala?Scala中的Array和ArrayBuffer有什么区别?
  4. What is case class? What is case object? What are the Advantages of case class?什么是案例类? 什么是案例对象? 案例类的优点是什么?
  5. What is the difference between Case Object and Object(Normal Object)?案例对象和对象(普通对象)有什么区别?
  6. When compare to Normal Class, What are the major advantages or benefits of a Case-class?与普通班相比,案例班的主要优点或好处是什么?
  7. What is the usage of isInstanceOf and asInstanceOf methods in Scala? Is there anything similar concept available in Java?Scala中isInstanceOf和asInstanceOf方法的用途是什么? Java是否有类似的概念可用?
  8. How do you prove that by default, Case Object is Serializable and Normal Object is not?如何证明默认情况下Case对象是可序列化的,而Normal Object不是?
  9. Difference between Array and List in Scala?Scala中的数组和列表之间的区别?
  10. What is the difference between “val” and “lazy val” in Scala? What is Eager Evaluation? What is Lazy Evaluation?Scala中的“ val”和“ lazy val”有什么区别? 什么是渴望评估? 什么是惰性评估?
  11. What is the Relationship between equals method and == in Scala? Differentiate Scala’s == and Java’s == Operator?Scala中equals方法和==之间的关系是什么? 区分Scala的==和Java的==运算符?
  12. Difference between Scala’s Inner class and Java’s Inner class?Scala的内部类和Java的内部类之间的区别?
  13. What is Diamond Problem? How Scala solves Diamond Problem?什么是钻石问题? Scala如何解决钻石问题?
  14. Why Scala does NOT have “static” keyword? What is the main reason for this decision?为什么Scala没有“ static”关键字? 做出此决定的主要原因是什么?
  15. What is the use of “object” keyword in Scala? How to create Singleton objects in Scala?Scala中“ object”关键字的用途是什么? 如何在Scala中创建Singleton对象?
  16. How to define Factory methods using object keyword in Scala? What is the use of defining Factory methods in object?如何在Scala中使用object关键字定义Factory方法? 在对象中定义Factory方法有什么用?
  17. What is apply method in Scala? What is unapply method in Scala? What is the difference between apply and unapply methods in Scala?Scala中的申请方法是什么? Scala中不适用的方法是什么? Scala中apply和unapply方法之间有什么区别?
  18. How does it work under-the-hood, when we create an instance of a Class without using ‘new’ keyword in Scala? When do we go for this approach?当我们在Scala中不使用'new'关键字创建类的实例时,它在后台如何工作? 我们什么时候采用这种方法?
  19. How do we declare a private Primary Constructor in Scala? How do we make a call to a private Primary Constructor in Scala?我们如何在Scala中声明私有的主要构造函数? 我们如何致电Scala中的私有主要构造函数?
  20. Does a Companion object access private members of it’s Companion class in Scala?Companion对象是否可以访问Scala中Companion类的私有成员?
  21. What is the main design decision about two separate keywords: class and object in Scala? How do we define Instance members and Static members in Scala?关于两个单独的关键字(Scala中的类和对象)的主要设计决策是什么? 我们如何在Scala中定义实例成员和静态成员?
  22. What is object in Scala? Is it a singleton object or instance of a class?Scala中的对象是什么? 它是单例对象还是类的实例?
  23. What is a Companion Object in Scala? What is a Companion Class in Scala? What is the use of Companion Object in Scala?Scala中的伴侣对象是什么? Scala中的伴侣类是什么? Scala中的Companion Object有什么用?
  24. How to implement interfaces in Scala?如何在Scala中实现接口?
  25. What is Range in Scala? How to create a Range in Scala?什么是Scala范围? 如何在Scala中创建范围?
  26. How many values of type Nothing have in Scala?Scala中有Nothing类型的值有多少?
  27. How many values of type Unit have in Scala?Scala中有多少个Unit类型的值?
  28. What is Range in Scala? How to create a Range in Scala?什么是Scala范围? 如何在Scala中创建范围?
  29. In FP, What is the difference between a function and a procedure?在FP中,函数和过程之间有什么区别?
  30. What are the major differences between Scala’s Auxiliary constructors and Java’s constructors?Scala的辅助构造函数和Java的构造函数之间的主要区别是什么?
  31. What is the use of ‘yield’ keyword in Scala’s for-comprehension construct?Scala的for-forhension构造中'yield'关键字的用途是什么?
  32. What is guard in Scala’s for-comprehension construct?Scala的理解构想中的守护者是什么?
  33. How Scala solves Inheritance Diamond Problem automatically and easily than Java 8?与Java 8相比,Scala如何自动,轻松地解决“继承钻石”问题?
  34. In Scala, Pattern Matching follows which Design Pattern? In Java, ‘isinstanceof’ operator follows which Design Pattern?在Scala中,模式匹配遵循哪种设计模式? 在Java中,“ isinstanceof”运算符遵循哪种设计模式?

Scala面试问答 (Scala Interview Questions and Answers)

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教程部分中的文章。

什么是主要构造函数? Scala中的二级或辅助构造函数是什么? Scala中的辅助构造函数的目的是什么? 是否可以在Scala中重载构造函数? (What is Primary Constructor? What is Secondary or Auxiliary Constructor in Scala? What is the purpose of Auxiliary Constructor in Scala? Is it possible to overload constructors in Scala?)

Scala has two kinds of constructors:

Scala有两种构造函数:

  • Primary Constructor主要建设者
  • Auxiliary Constructor辅助构造器

Primary Constructor
In Scala, Primary Constructor is a constructor which is defined with class definition itself. Each class must have one Primary Constructor: Either Parameter constructor or Parameterless constructor.

主要建设者
在Scala中,主要构造函数是使用类定义本身定义的构造函数。 每个类必须具有一个主构造函数:参数构造函数或无参数构造函数。

Example:-

例:-

class Person

Above Person class has one Zero-parameter or No-Parameter or Parameterless Primary constructor to create instances of this class.

上述Person类具有一个零参数或无参数或无参数的Primary构造函数来创建此类的实例。

class Person (firstName: String, lastName: String)

Above Person class has a two Parameters Primary constructor to create instances of this class.

在Person类之上,有两个Parameters Primary构造函数来创建此类的实例。

Auxiliary Constructor
Auxiliary Constructor is also known as Secondary Constructor. We can declare a Secondary Constructor using ‘def’ and ‘this’ keywords as shown below:

辅助构造器
辅助构造函数也称为辅助构造函数。 我们可以使用'def'和'this'关键字声明一个二级构造函数,如下所示:

class Person (firstName: String, middleName:String, lastName: String){def this(firstName: String, lastName: String){this(firstName, "", lastName)}
}

Scala中的辅助构造函数有什么用途?请解释在Scala中定义辅助构造函数时应遵循的规则? (What is the use of Auxiliary Constructors in Scala?Please explain the rules to follow in defining Auxiliary Constructors in Scala?)

In Scala, The main purpose of Auxiliary Constructors is to overload constructors. Like Java, We can provide various kinds of constructors so that use can choose the right one based on his requirement.

在Scala中,辅助构造函数的主要目的是重载构造函数。 像Java一样,我们可以提供各种构造函数,以便用户可以根据自己的需求选择合适的构造函数。

Auxiliary Constructor Rules:

辅助构造函数规则:

  • They are like methods only. Like methods, we should use ‘def’ keyword to define them.它们仅是方法。 像方法一样,我们应该使用'def'关键字来定义它们。
  • We should use same name ‘this’ for all Auxiliary Constructors.所有辅助构造函数都应使用相同的名称“ this”。
  • Each Auxiliary Constructor should start with a call to previous defined another Auxiliary Constructor or Primary Constructor. Otherwise compile-time error.每个辅助构造函数都应从对先前定义的另一个辅助构造函数或主构造函数的调用开始。 否则编译时错误。
  • Each Auxiliary Constructor should differ with their parameters list: may be by number or types.每个辅助构造函数的参数列表都应不同:可以是数字或类型。
  • Auxiliary Constructors cannot call a super class constructors. They should call them through Primary Constructor only.辅助构造函数不能调用超类构造函数。 他们应该仅通过主构造函数来调用它们。
  • All Auxiliary Constructors call their Primary Constructor either directly or indirectly through other Auxiliary Constructors.所有辅助构造函数都直接或通过其他辅助构造函数间接调用其主要构造函数。

NOTE:- If you want to learn about Scala’s Constructors, please refer my Scala posts at: Primary Constructor and Auxiliary Constructor.

注意:-如果您想了解Scala的构造函数,请参阅我的Scala帖子: 主要构造函数和辅助构造函数 。

Scala中的Array和ArrayBuffer有什么区别? (What are the differences between Array and ArrayBuffer in Scala?)

Differences between Array and ArrayBuffer in Scala:

Scala中Array和ArrayBuffer之间的区别:

  • Array is fixed size array. We cannot change its size once its created.数组是固定大小的数组。 一旦创建,我们将无法更改其大小。
  • ArrayBuffer is variable size array. It can increase or decrease it’s size dynamically.ArrayBuffer是可变大小的数组。 它可以动态增加或减少它的大小。
  • Array is something similar to Java’s primitive arrays.数组类似于Java的原始数组。
  • ArrayBuffer is something similar to Java’s ArrayList.ArrayBuffer与Java的ArrayList类似。

什么是案例类? 什么是案例对象? 案例类的优点是什么? (What is case class? What is case object? What are the Advantages of case class?)

Case class is a class which is defined with “case class” keywords. Case object is an object which is defined with “case object” keywords. Because of this “case” keyword, we will get some benefits to avoid boilerplate code.

案例类是使用“案例类”关键字定义的类。 案例对象是使用“案例对象”关键字定义的对象。 由于使用了“ case”关键字,我们将获得一些避免样板代码的好处。

We can create case class objects without using “new” keyword. By default, Scala compiler prefixes “val” for all constructor parameters. That’s why without using val or var, Case class’s constructor parameters will become class members, it is not possible for normal classes.

我们可以创建案例类对象,而无需使用“ new”关键字。 默认情况下,Scala编译器为所有构造函数参数添加“ val”前缀。 这就是为什么不使用val或var的情况下,Case类的构造函数参数将成为类成员,而普通类则不可能。

Advantages of case class:

案例类的优点:

  • By default, Scala Compiler adds toString, hashCode and equals methods. We can avoid writing this boilerplate code.默认情况下,Scala编译器添加toString,hashCode和equals方法。 我们可以避免编写此样板代码。
  • By default, Scala Compiler adds companion object with apply and unapply methods that’s why we don’t need new keyword to create instances of a case class.默认情况下,Scala编译器添加带有apply和unapply方法的伴随对象,这就是为什么我们不需要new关键字来创建case类的实例的原因。
  • By default, Scala Compiler adds copy method too.默认情况下,Scala编译器也会添加复制方法。
  • We can use case classes in Pattern Matching.我们可以在模式匹配中使用案例类。
  • By default, Case class and Case Objects are Serializable.默认情况下,Case类和Case对象是可序列化的。

案例对象和对象(普通对象)有什么区别? (What is the difference between Case Object and Object(Normal Object)?)

  • Normal object is created using “object” keyword. By default, It’s a singleton object.使用“ object”关键字创建普通对象。 默认情况下,它是一个单例对象。
object MyNormalObject
  • Case Object is created using “case object” keywords.By default, It’s also a singleton objectCase Object是使用“ case object”关键字创建的。默认情况下,它也是一个单例对象
  • case object MyCaseObject
  • By Default, Case Object gets toString and hashCode methods. But normal object cannot.默认情况下,Case Object获取toString和hashCode方法。 但是普通物体不能。
  • By Default, Case Object is Serializable. But normal object is not.默认情况下,Case对象是可序列化的。 但是普通对象不是。
  • 与普通班相比,案例班的主要优点或好处是什么? (When compare to Normal Class, What are the major advantages or benefits of a Case-class?)

    The following are the major advantages or benefits of a Case class over Normal Classes:

    以下是案例类相对于普通类的主要优点或好处:

    • Avoids lots of boiler-plate code by adding some useful methods automatically.通过自动添加一些有用的方法,避免了很多样板代码。
    • By default, supports Immutability because it’s parameters are ‘val’默认情况下,支持不可变,因为它的参数是'val'
    • Easy to use in Pattern Matching.易于在模式匹配中使用。
    • No need to use ‘new’ keyword to create instance of Case Class.无需使用“ new”关键字来创建Case类的实例。
    • By default, supports Serialization and Deserialization.默认情况下,支持序列化和反序列化。

    Scala中isInstanceOf和asInstanceOf方法的用途是什么? Java是否有类似的概念可用? (What is the usage of isInstanceOf and asInstanceOf methods in Scala? Is there anything similar concept available in Java?)

    Both isInstanceOf and asInstanceOf methods are defined in Any class. So no need import to get these methods into any class or object.

    isInstanceOf和asInstanceOf方法都在Any类中定义。 因此,无需导入即可将这些方法导入任何类或对象。

    “isInstanceOf” method is used to test whether the object is of a given type or not. If so, it returns true. Otherwise returns false.

    “ isInstanceOf”方法用于测试对象是否为给定类型。 如果是这样,则返回true。 否则返回false。

    scala> val str = "Hello"scala>str.isInstanceOf[String]res0: Boolean = false

    “asInstanceOf” method is used to cast the object to the given a type. If the given object and type are of same type, then it cast to given type. Otherwise, it throws java.lang.ClassCastException.

    “ asInstanceOf”方法用于将对象转换为给定的类型。 如果给定的对象和类型属于同一类型,则将其转换为给定的类型。 否则,它将引发java.lang.ClassCastException。

    scala> val str = "Hello".asInstanceOf[String]str: String = Hello

    In Java, ‘instanceof’ keyword is similar to Scala’s ‘isInstanceOf’ method. In Java, the following kind of manual type casting is similar to Scala’s ‘asInstanceOf’ method.

    在Java中,“ instanceof”关键字类似于Scala的“ isInstanceOf”方法。 在Java中,以下类型的手动类型转换类似于Scala的'asInstanceOf'方法。

    AccountService service = (AccountService)context.getBean("accountService");

    如何证明默认情况下Case对象是可序列化的,而Normal Object不是? (How do you prove that by default, Case Object is Serializable and Normal Object is not?)

    Yes, By Default, Case Object is Serializable. But normal object is not. We can prove this by using isInstanaceOf method as shown below:

    是的,默认情况下,Case对象是可序列化的。 但是普通对象不是。 我们可以使用isInstanaceOf方法证明这一点,如下所示:

    scala> object MyNormalObject
    defined object MyNormalObjectscala> MyNormalObject.isInstanceOf[Serializable]
    res0: Boolean = falsescala> case object MyCaseObject
    defined object MyCaseObjectscala> MyCaseObject.isInstanceOf[Serializable]
    res1: Boolean = true

    Scala中的数组和列表之间的区别? (Difference between Array and List in Scala?)

    • Arrays are always Mutable where as List is always Immutable.数组总是可变的,而列表总是不可变的。
    • Once created, We can change Array values where as we cannot change List Object.创建后,我们可以更改数组值,而不能更改列表对象。
    • Arrays are fixed-size data structures where as List is variable-sized data structures. List’s size is automatically increased or decreased based on it’s operations we perform on it.数组是固定大小的数据结构,其中List是可变大小的数据结构。 列表的大小会根据我们对其执行的操作自动增加或减少。
    • Arrays are Invariants where as Lists are Covariants.数组是不变量,其中列表是协变量。

    NOTE:- If you are not sure about Invariant and Covariant, please read my next post on Scala Interview Questions.

    注意:-如果您不确定不变式和协变式,请阅读我关于Scala面试问题的下一篇文章。

    Scala中的“ val”和“ lazy val”有什么区别? 什么是渴望评估? 什么是惰性评估? (What is the difference between “val” and “lazy val” in Scala? What is Eager Evaluation? What is Lazy Evaluation?)

    As we discussed in my Basic Scala Interview Questions, “val” means value or constant which is used to define Immutable variables.

    正如我们在我的基本Scala面试问题中所讨论的那样,“ val”表示用于定义不可变变量的值或常数。

    There are two kinds of program evaluations:

    有两种程序评估:

    • Eager Evaluation渴望评估
    • Lazy Evaluation懒惰评估

    Eager Evaluation means evaluating program at compile-time or program deployment-time irrespective of clients are using that program or not.

    急切评估是指在编译时或程序部署时评估程序,而不管客户端是否正在使用该程序。

    Lazy Evaluation means evaluating program at run-time on-demand that means when clients access the program then only its evaluated.

    惰性评估是指在运行时按需评估程序,这意味着客户端访问该程序时仅评估它。

    The difference between “val” and “lazy val” is that “val” is used to define variables which are evaluated eagerly and “lazy val” is also used to define variables but they are evaluated lazily.

    “ val”和“ lazy val”之间的区别在于,“ val”用于定义热切评估的变量,“ lazy val”也用于定义变量,但它们是延迟计算的。

    Scala中equals方法和==之间的关系是什么? 区分Scala的==和Java的==运算符? (What is the Relationship between equals method and == in Scala? Differentiate Scala’s == and Java’s == Operator?)

    In Scala, we do NOT need to call equals() method to compare two instances or objects. When we compare two instances with ==, Scala calls that object’s equals() method automatically.

    在Scala中,我们不需要调用equals()方法来比较两个实例或对象。 当我们用==比较两个实例时,Scala会自动调用该对象的equals()方法。

    Java’s == operator is used to check References Equality that is whether two references are pointing to the same object or not. Scala’s == is used to check Instances Equality that is whether two instances are equal or not.

    Java的==运算符用于检查“引用相等”,即两个引用是否指向同一对象。 Scala的==用于检查实例是否相等,即两个实例是否相等。

    Scala的内部类和Java的内部类之间的区别? (Difference between Scala’s Inner class and Java’s Inner class?)

    In Java, Inner class is associated with Outer class that is Inner class a member of the Outer class.
    Unlike Java, Scala treats the relationship between Outer class and Inner class differently. Scala’s Inner class is associated with Outer class object.

    在Java中,内部类与外部类相关联,外部类是内部类的外部类的成员。
    与Java不同,Scala对待外部类和内部类之间的关系有所不同。 Scala的内部类与外部类对象相关联。

    什么是钻石问题? Scala如何解决钻石问题? (What is Diamond Problem? How Scala solves Diamond Problem?)

    A Diamond Problem is a Multiple Inheritance problem. Some people calls this problem as Deadly Diamond Problem.

    钻石问题是多重继承问题。 有人将此问题称为致命钻石问题。

    In Scala, it occurs when a Class extends more than one Traits which have same method definition as shown below.

    在Scala中,当一个类扩展了多个具有以下相同方法定义的特性时,就会发生这种情况。

    Unlike Java 8, Scala solves this diamond problem automatically by following some rules defined in Language. Those rules are called “Class Linearization”.

    与Java 8不同,Scala通过遵循Language中定义的一些规则自动解决了这一钻石问题。 这些规则称为“类线性化”。

    Example:-

    例:-

    trait A{   def display(){ println("From A.display")  }
    }
    trait B extends A{ override def display() { println("From B.display") }
    }
    trait C extends A{ override def display() { println("From C.display") }
    }
    class D extends B with C{ }object ScalaDiamonProblemTest extends App {val d = new Dd display
    }

    Here output is “From C.display” form trait C. Scala Compiler reads “extends B with C” from right to left and takes “display” method definition from lest most trait that is C.

    这里的输出是“ From C.display”形式的特征C。Scala编译器从右到左读取“用C扩展B”,并从最主要的特征C中获取“ display”方法定义。

    NOTE:- See my post on “Scala Traits in Depth” to know this with clear explanation.

    注意:-请参阅我在“深度上的斯卡拉特征”中的帖子,以清楚的说明了解这一点。

    为什么Scala没有“ static”关键字? 做出此决定的主要原因是什么? (Why Scala does NOT have “static” keyword? What is the main reason for this decision?)

    As we know, Scala does NOT have “static” keyword at all. This is the design decision done by Scala Team.

    众所周知,Scala根本没有“ static”关键字。 这是Scala团队做出的设计决定。

    The main reason to take this decision is to make Scala as a Pure Object-Oriented Language. “static” keyword means that we can access that class members without creating an object or without using an object. This is completely against with OOP principles.

    做出此决定的主要原因是使Scala成为纯面向对象的语言。 “静态”关键字表示无需创建对象或使用对象就可以访问该类成员。 这完全违反OOP原则。

    If a Language supports “static” keyword, then that Language is not a Pure Object-Oriented Language. For instance, as Java supports “static” keyword, it is NOT a Pure Object-Oriented Language. But Scala is a Pure Object-Oriented Language.

    如果一种语言支持“静态”关键字,则该语言不是纯面向对象的语言。 例如,由于Java支持“静态”关键字,因此它不是纯面向对象的语言。 但是Scala是一种纯面向对象的语言。

    Scala中“ object”关键字的用途是什么? 如何在Scala中创建Singleton对象? (What is the use of “object” keyword in Scala? How to create Singleton objects in Scala?)

    In Scala, object keyword is used the following purposes:

    在Scala中,使用object关键字的目的如下:

    • It is used to create singleton object in Scala.它用于在Scala中创建单例对象。
    object MySingletonObject

    Here, MySingletonObject becomes singleton object automatically.

    在这里,MySingletonObject自动成为单例对象。

  • object keyword is used to define Scala Applications that is executable Scala programs.object关键字用于定义可执行的Scala程序的Scala应用程序。
  • object MyScalaExecutableProgram{   def main(args: Array[String]){println("Hello World")}
    }

    When we define main method in object as shown above (its same as main() method in Java), it becomes automatically as a executable Scala program.

    当我们如上所述在对象中定义main方法时(与Java中的main()方法相同),它将自动成为可执行的Scala程序。

  • It is used to define static members like static variables and static methods without using ‘static’ keyword.它用于定义静态成员,如静态变量和静态方法,而无需使用“ static”关键字。
  • object MyScalaStaticMembers{ val PI: Double = 3.1414  def add(a: Int, b: Int) = a + b
    }

    By def PI variable and add methods will become as static members. That means we can call them without creating a separate object like MyScalaStaticMembers.add(10,20).

    通过def PI变量和add方法将成为静态成员。 这意味着我们可以调用它们而无需创建像MyScalaStaticMembers.add(10,20)这样的单独对象。

  • It is used to define Factory methods. Please see my next question about this.它用于定义Factory方法。 请参阅我的下一个问题。
  • 如何在Scala中使用object关键字定义Factory方法? 在对象中定义Factory方法有什么用? (How to define Factory methods using object keyword in Scala? What is the use of defining Factory methods in object?)

    In Scala, we use ‘object’ keyword to define Factory methods. The main purpose of these Factory methods in Scala is to avoid using ‘new’ keyword. Without using ‘new’ keyword we can create objects.

    在Scala中,我们使用'object'关键字定义Factory方法。 Scala中这些Factory方法的主要目的是避免使用'new'关键字。 不使用“ new”关键字,我们可以创建对象。

    To define Factory methods:
    We can use apply method to define Factory methods in Scala. If we have Primary Constructor and Multiple Auxiliary constructors, then we need to define multiple apply methods as shown below.

    定义工厂方法:
    我们可以使用apply方法在Scala中定义Factory方法。 如果我们有主构造函数和多个辅助构造函数,则需要定义多个apply方法,如下所示。

    class Person(val firstName: String, val middleName: String, val lastName: String){def this(firstName: String, lastName: String){this(firstName,"",lastName)}
    }
    object Person{def apply(val firstName: String, val middleName: String, val lastName: String) = new Person(firstName,middleName,lastName)def apply(val firstName: String, val lastName: String) = new Person(firstName, lastName)
    }

    Now we can create Person objects without using new keyword or with new keyword upto your wish.

    现在,我们可以创建Person对象,而无需使用new关键字或根据您的意愿使用new关键字。

    val p1 = new Person("Scala","Java")
    or
    val p1 = Person("Scala","Java")

    Scala中的申请方法是什么? Scala中不适用的方法是什么? Scala中apply和unapply方法之间有什么区别? (What is apply method in Scala? What is unapply method in Scala? What is the difference between apply and unapply methods in Scala?)

    In Scala, apply and unapply methods play very important role. They are also very useful in Play Framework in mapping and unmapping data between Form data and Model data.

    在Scala中,套用和套用的方法起着非常重要的作用。 它们在Play框架中在Form数据和Model数据之间映射和取消映射数据时也非常有用。

    In simple words,

    简单来说

    • apply method: To compose or assemble an object from it’s components.apply方法:从其组件组成或组装一个对象。
    • unapply method: To decompose or dis-assemble an object into it’s components.unapply方法:将对象分解或分解为组件。

    Scala’s apply method:
    It is used to compose an object by using its components. Suppose if we want to create a Person object, then use firstName and laststName two components and compose Person object as shown below.

    Scala的apply方法:
    它用于通过使用其组件来构成对象。 假设我们要创建一个Person对象,则使用firstName和laststName两个组件并组成Person对象,如下所示。

    class Person(val firstName: String, val lastName: String)object Person{def apply(firstName: String, lastName: String) = new Person(firstName, lastName)
    }

    Scala’s unapply method:
    It is used to decompose an object into its components. It follows reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below.

    Scala的未应用方法:
    它用于将对象分解为其组件。 它遵循申请方法的相反过程。 假设我们有一个Person对象,则可以将该对象分解为两个组件:firstName和laststName,如下所示。

    class Person(val firstName: String, val lastName: String)object Person{def apply(firstName: String, lastName: String) = new Person(firstName, lastName)def unapply(p: Person): (String,String) = (p.firstName, p.lastName)
    }

    当我们在Scala中不使用'new'关键字创建类的实例时,它在后台如何工作? 我们什么时候采用这种方法? 如何在Scala中声明私有构造函数? (How does it work under-the-hood, when we create an instance of a Class without using ‘new’ keyword in Scala? When do we go for this approach? How to declare private constructors in Scala?)

    In Scala, when we create an instance of a Class without using ‘new’ keyword, internally it make a call to appropriate apply method available in Companion object. Here appropriate apply method means that matched with parameters.

    在Scala中,当我们在不使用'new'关键字的情况下创建Class的实例时,在内部它会调用Companion对象中可用的适当apply方法。 这里合适的应用方法意味着与参数匹配。

    When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same set of parameters and allow our class users to create it without new keyword.

    什么时候选择此选项:当我们需要提供私有的私有构造函数并且需要避免使用'new'关键字时,我们只能实现具有相同参数集的apply方法,并允许我们的类用户创建不带有new关键字的方法。

    我们如何在Scala中声明私有的主要构造函数? 我们如何致电Scala中的私有主要构造函数? (How do we declare a private Primary Constructor in Scala? How do we make a call to a private Primary Constructor in Scala?)

    In Scala, we can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and add ‘private’ just after class name and before parameter list as shown below:

    在Scala中,我们可以非常轻松地声明私有的Primary Constructor。 只需按原样定义一个主构造函数,并在类名之后和参数列表之前添加“ private”,如下所示:

    class Person private (name: String)
    object Person{def apply(name: String) = new Person(name)
    }

    As it’s a private constructor, we cannot call it from outside. We should provide a factory method (that is apply method) as shown above and use that constructor indirectly.

    由于它是私有构造函数,因此我们不能从外部调用它。 我们应该提供如上所述的工厂方法(即apply方法),并间接使用该构造函数。

    Companion对象是否可以访问Scala中Companion类的私有成员? (Does a Companion object access private members of it’s Companion class in Scala?)

    Generally, private members means accessible only within that class. However Scala’s Companion class and Companion Object has provided another feature.

    通常,私有成员意味着只能在该类中访问。 但是,Scala的Companion类和Companion对象提供了另一个功能。

    In Scala, a Companion object can access private members of it’s Companion class and Companion class can access it’s Companion object’s private members.

    在Scala中,Companion对象可以访问其Companion类的私有成员,而Companion类可以访问其Companion对象的私有成员。

    关于两个单独的关键字(Scala中的类和对象)的主要设计决策是什么? 我们如何在Scala中定义实例成员和静态成员? (What is the main design decision about two separate keywords: class and object in Scala? How do we define Instance members and Static members in Scala?)

    In Scala, we use class keyword to define instance members and object keyword to define static members. Scala does not have static keyword, but still we can define them by using object keyword.

    在Scala中,我们使用class关键字定义实例成员,使用object关键字定义静态成员。 Scala没有static关键字,但是我们仍然可以使用object关键字定义它们。

    The main design decision about this is that the clear separation between instance and static members. Loosely coupling between them. And other major reason is to avoid static keyword so that Scala will become a Pure-OOP Language.

    关于此的主要设计决策是实例和静态成员之间的明确分隔。 它们之间的耦合松散。 另外一个主要原因是避免使用静态关键字,以便Scala成为Pure-OOP语言。

    Scala中的对象是什么? 它是单例对象还是类的实例? (What is object in Scala? Is it a singleton object or instance of a class?)

    Unlike Java, Scala has two meanings about ‘object’. Don’t get confuse about this, I will explain it clearly. In Java, we have only one meaning for object that is “An instance of a class”.

    与Java不同,Scala关于“对象”有两种含义。 不要对此感到困惑,我会清楚地解释它。 在Java中,对象的唯一含义是“类的实例”。

    • Like Java, the first meaning of object is “An instance of a class”.像Java一样,对象的第一个含义是“类的实例”。
    val p1 = new Person("Scala","Java")
    or
    val p1 = Person("Scala","Java")
  • Second meaning is that object is a keyword in Scala. It is used to define Scala Executable programs, Companion Objects, Singleton Objects etc.第二个含义是object是Scala中的关键字。 它用于定义Scala可执行程序,伴侣对象,单例对象等。
  • Scala中的伴侣对象是什么? Scala中的伴侣类是什么? Scala中的Companion Object有什么用? (What is a Companion Object in Scala? What is a Companion Class in Scala? What is the use of Companion Object in Scala?)

    In simple words, if a Scala class and object shares the same name and defined in the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”.

    简而言之,如果Scala类和对象共享相同的名称并在同一源文件中定义,则该类称为“同伴类”,而该对象称为“同伴对象”。

    When we create a Class by using Scala “class” keyword and Object by using Scala “object” keyword with same name and within the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”.

    当我们在同一个源文件中使用具有相同名称的Scala“类”关键字和使用Scala“对象”关键字来创建对象时,该类称为“伴侣类”,而该对象称为“伴侣对象” ”。

    Example:-
    Employee.scala

    例:-
    员工规模

    class Employee{ }
    object Employee{ }

    In Scala, The main purpose of Companion Object is to define apply methods and avoid using new keyword in creating an instance of that Companion class object.

    在Scala中,Companion对象的主要目的是定义应用方法,并避免在创建该Companion类对象的实例时使用new关键字。

    如何在Scala中实现接口? (How to implement interfaces in Scala?)

    As we know from Java background, we use interface to define contact.

    从Java背景知道,我们使用接口定义联系人。

    However, there is no interface concept in Scala. Even, Scala doesn’t have interface keyword. Scala has a more powerful and flexible concept i.e. trait for this purpose.

    但是,Scala中没有接口概念。 甚至,Scala没有interface关键字。 Scala具有更强大和灵活的概念,即为此目的的特征。

    什么是Scala范围? 如何在Scala中创建范围? (What is Range in Scala? How to create a Range in Scala?)

    Range is a Lazy Collection in Scala. Range is a class available in ‘scala’ package like ‘scala.Range’. It is used to represent a sequence of integer values. It is an ordered sequence of integers.

    Example:-

    Range是Scala中的一个惰性集合。 Range是“ scala”包中可用的类,例如“ scala.Range”。 它用于表示整数值序列。 它是整数的有序序列。

    例:-

    scala> 1 to 10
    res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> 1 until 10
    res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

    Scala中有Nothing类型的值有多少? (How many values of type Nothing have in Scala?)

    In Scala, Nothing type have no values that is zero. It does not have any values. It is a subtype of all Value classes and Reference classes.

    在Scala中,Nothing类型没有零值。 它没有任何值。 它是所有Value类和Reference类的子类型。

    Scala中有多少个Unit类型的值? (How many values of type Unit have in Scala?)

    In Scala, Unit is something similar to Java’s void keyword. It is used to represent “No value exists”. It has one and only one value that is ().

    在Scala中,Unit类似于Java的void关键字。 它用来表示“不存在任何值”。 它只有一个值()

    什么是纯函数? (What is a pure function?)

    A pure function is a function without any observable side-effects. That means it returns always same results irrespective how many times we call it with same inputs.

    纯函数是没有任何可观察到的副作用的函数。 这意味着无论我们用相同的输入调用多少次,它始终返回相同的结果。

    A pure function always gives same output for the same inputs.

    纯函数始终为相同的输入提供相同的输出。

    For Example:-

    例如:-

    scala> 10 + 20
    res0: Int = 30
    scala>
    scala> 10 + 20
    res0: Int = 30

    Here “+” a pure function available in Int class. It gives same result 30 for same inputs 10 and 30, irrespective how many times we call it.

    在这里,“ +”是Int类中可用的纯函数。 对于相同的输入10和30,无论我们调用多少次,它都会给出相同的结果30。

    在FP中,函数和过程之间有什么区别? (In FP, What is the difference between a function and a procedure?)

    Both are used to perform computation, however they have one major difference in Functional Programming world.

    两者都用于执行计算,但是它们在函数式编程世界中有一个主要区别。

    A function is a computation unit without side-effect where as a Procedure is also a computation unit with side-effects.

    函数是没有副作用的计算单元,而过程(Procedure)也是具有副作用的计算单元。

    Scala的辅助构造函数和Java的构造函数之间的主要区别是什么? (What are the major differences between Scala’s Auxiliary constructors and Java’s constructors?)

    Scala’s Auxiliary constructor is almost similar to Java’s constructor with few differences.

    Scala的辅助构造函数几乎与Java的构造函数相似,几乎没有区别。

    Compared to Java’s constructors, Auxiliary constructors have the following few differences:

    与Java的构造函数相比,辅助构造函数具有以下一些区别:

    • The auxiliary constructors are called using “this” keyword.使用“ this”关键字调用辅助构造函数。
    • All auxiliary constructor are defined with the same name that is “this”. In Java, we use class name to define constructors.所有辅助构造函数的名称均与“ this”相同。 在Java中,我们使用类名称定义构造函数。
    • Each auxiliary constructor must start with a call to a previously defined auxiliary constructor or the primary constructor.每个辅助构造函数必须从对先前定义的辅助构造函数或主构造函数的调用开始。
    • We use ‘def’ keyword to define auxiliary constructors like method/function definition. In Java, constructor definition and Method definition is different.我们使用'def'关键字来定义辅助构造函数,例如方法/函数定义。 在Java中,构造函数定义和方法定义不同。

    Scala的for-forhension构造中'yield'关键字的用途是什么? (What is the use of ‘yield’ keyword in Scala’s for-comprehension construct?)

    We can use ‘yield’ keyword in Scala’s for-comprehension construct. ‘for/yield’ is used to iterate a collection of elements and generates new collection of same type. It does not change the original collection. It generates new collection of same type as original collection type.

    我们可以在Scala的for-comprehension构造中使用'yield'关键字。 “ for / yield”用于迭代元素集合并生成相同类型的新集合。 它不会更改原始集合。 它生成与原始集合类型相同类型的新集合。

    For example, if we use ‘for/yield’ construct to iterate a List then it generates a new List only.

    例如,如果我们使用“ for / yield”构造来迭代列表,则仅生成一个新的列表。

    scala> val list = List(1,2,3,4,5)
    list: List[Int] = List(1, 2, 3, 4, 5)scala> for(l <- list) yield l*2
    res0: List[Int] = List(2, 4, 6, 8, 10)

    Scala的理解构想中的守护者是什么? (What is guard in Scala’s for-comprehension construct?)

    In Scala, for-comprehension construct has an if clause which is used to write a condition to filter some elements and generate new collection. This if clause is also known as “Guard”.

    在Scala中,for-comprehension构造包含一个if子句,该子句用于编写条件以过滤某些元素并生成新的集合。 此if子句也称为“ Guard”。

    If that guard is true, then add that element to new collection. Otherwise, it does not add that element to original collection.

    如果该防护为真,则将该元素添加到新集合中。 否则,它不会将该元素添加到原始集合中。

    Example:- For-comprehension Guard to generate only Even numbers into new collection.

    示例:-对于综合警卫只能将偶数生成到新集合中。

    scala> val list = List(1,2,3,4,5,6,7,8,9,10)
    list: List[Int] = List(1, 2, 3, 4, 5 , 6 , 7 , 8 , 9 , 10)scala> for(l <- list if l % 2 =0 ) yield l
    res0: List[Int] = List(2, 4, 6, 8, 10)

    与Java 8相比,Scala如何自动,轻松地解决“继承钻石”问题? (How Scala solves Inheritance Diamond Problem automatically and easily than Java 8?)

    If we use Java 8’s Interface with Default methods, we will get Inheritance Diamond Problem. Developer has to solve it manually in Java 8. It does not provide default or automatic resolution for this problem.

    如果我们将Java 8的Interface与Default方法一起使用,我们将得到Inheritance Diamond Problem。 开发人员必须在Java 8中手动解决它。它没有为该问题提供默认或自动解决方案。

    In Scala, we will get same problem with Traits but Scala is very clever and solves Inheritance Diamond Problem automatically using Class Linearization concept.

    在Scala中,我们会遇到与Traits相同的问题,但是Scala非常聪明,并使用类线性化概念自动解决了继承钻石问题。

    在Scala中,模式匹配遵循哪种设计模式? 在Java中,“ isinstanceof”运算符遵循哪种设计模式? (In Scala, Pattern Matching follows which Design Pattern? In Java, ‘isinstanceof’ operator follows which Design Pattern?)

    In Scala, Pattern Matching follows Visitor Design Pattern. In the same way, Java’s ‘isinstanceof’ operator also follows Visitor Design Pattern.

    在Scala中,模式匹配遵循游客设计模式。 同样,Java的“ isinstanceof”运算符也遵循“访客设计模式”。

    That’s it all about “Scala Intermediate Interview Questions and Answers”. We will discuss some Advanced 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/9663/scala-interview-questions

    scala面试问题

scala面试问题_Scala面试问题相关推荐

  1. scala面试问题_Scala高级面试问答

    scala面试问题 Before reading this post, please go through my previous two posts at "Scala Basic&quo ...

  2. 刷题百道却跪在白板面试?白板面试FAQ为你答疑解惑

    专栏 | 九章算法 网址 | www.jiuzhang.com 你花了大量的时间学习如何编程.学习如何创建项目.终于你通过自己的努力收到了梦寐以求的公司发来的面试邀请函.但是这时候,你却发现你的面前又 ...

  3. 工作那些事(十六)面试时,面试官喜欢的非技术问题汇总

    在找工作的过程中,进行技术和人事面试时不可避免的.有的人笔试答的挺好,挂在了面试的时候,确实很可惜的.一般来说,技术性的岗位是笔试(有的还有性格测试题).技术面试.人事面试.这里汇总一下面试时,面试官 ...

  4. 美国面试之旅——面试经验分享

    鉴于前几篇日志仅仅是分享了面试的经历,并没有太多实质性的"有用信息"(当然面试流程,公司概况等等信息其实也是有点用的,不过远远不够),如果不写一篇有用一点的日志,估计要遭大家扔鸡蛋 ...

  5. 疫情之下的求职姿势:视频面试,电话面试这样做,你就已经赢一半了!

    谨以这篇攻略文,送给诸位疫情之下的求职者,希望添点助力,帮你逆风飞扬,闪亮职场! 泰然处之,从容淡定 欲善其事,先利其器 树立良好的第一印象 1泰然处之,从容淡定 第一条基本原则:不要慌! 不同于现场 ...

  6. 上海集成电路材料研究院-面试经历-取消面试

    网上关于这个单位的信息啥都没有, 负责面试的人力,跟她沟通是什么都问不出来. 哎... 根据面试通知,是这么几位,查了下他们的背景. 面试官们 面试官背景 专业方向 朱雷 <lei.zhu@si ...

  7. 面试官是怎样高效面试的(面试官的“套路”

    大家好,我是若川.持续组织了6个月源码共读活动,感兴趣的可以点此加我微信 ruochuan12 参与,每周大家一起学习200行左右的源码,共同进步.同时极力推荐订阅我写的<学习源码整体架构系列& ...

  8. 程序员美团面试挂了,7天后去腾讯面试,见到面试官:好巧啊!!

    说起来,职场上很多事情,只能用一个"缘分"来解释.近日在职场论坛上看到有员工发帖,他说自己在面试时遇到了一件最悲哀的事情?在上个月去美团面试的时候,由于发挥不太好,面试挂了.当时那 ...

  9. Java 小记 — Spring Boot 的实践与思考,互联网 面试官 如何面试

    写在最前面,我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家.扫码加微信好友进[程序员面试学习交流群],免费领取.也欢迎各位一起在群里探讨技术. 前言 ...

最新文章

  1. hibernate分页
  2. CMU赵越 关于异常检测的分享!
  3. EL表达式从数组 Map List集合取值
  4. OSPF第十章:OSPF 一
  5. 会议交流 | 2021年全国知识图谱与语义计算大会(CCKS 2021)征稿通知
  6. PAT乙级 1032 挖掘机技术哪家强 (20 分)
  7. .Net学习(三):初识ASP.Net
  8. 发票专用驱动sjz_税务局紧急提醒!10月31日前,不能领票,不能勾选!11月1日后,发票开具将大不同!...
  9. Sqlite字段长度填坑
  10. 成都有哪些牛批的互联网公司?
  11. 1024程序员节活动继续:购书优惠劵,折后再折,赶紧来抢啊
  12. MySQL 8.0 物理备份xtrabackup简介
  13. TikTok搬运视频怎么才会不被限流?
  14. 理解HTTP Referer
  15. HTML table表格 固定表头 tbody加滚动条
  16. 阿里云服务器,解析二级域名
  17. Mysql Tigger触发器创建和使用
  18. 一个人如果不结婚也无儿无女无兄弟姐妹,虽然可以自由自在一辈子,但老了怎么办?
  19. u盘提示格式化怎么修复?80%的人都在这么做!
  20. X264算法参数五(码控参数)

热门文章

  1. Java邮件服务学习之一:邮件服务概述
  2. java事务的类型——面试被问到
  3. 王者调整期选股技术之喇叭花开
  4. R - 变化plot字形,嵌入字体以pdf
  5. 如何在繁忙的工作中锻炼身体?
  6. [转载] python divmod()函数
  7. verilog状态机以及编码详解
  8. 大兄dei,早点看清this吧
  9. CF EDU - E. Lomsat gelral 树上启发式合并
  10. linux grep (转)