scala面试问题_Scala面试问答

scala面试问题

Welcome to Scala interview questions and answers. Now-a-days, most of the Finance/Banking, Government, Telecom, Social Networking etc. companies are using Scala, Play and Akka Framework to develop their projects because these frameworks support both OOPs and FP features and also provide many advantages.

欢迎来到Scala面试问答。 如今,大多数金融/银行,政府,电信,社交网络等公司都在使用Scala,Play和Akka框架来开发其项目,因为这些框架既支持OOPs也支持FP功能,并且还具有许多优势。

Scala面试问题 (Scala Interview Questions)

scala面试问题_Scala面试问答_第1张图片

I am going to share my Interview’s Knowledge and Scala Ecosystem or Fullstack Scala (Scala/Play/Akka) Development experience with you through couple of posts.


我将通过几篇文章与您分享面试的知识和Scala生态系统或Fullstack Scala (Scala / Play / Akka)开发经验。

I’m going to share you 3 separate posts. Some questions are just one question and some have some sub-questions. Totally am going to discuss around 200 questions. I bet if you are familiarise with all these questions, you will definitely clear all Scala Developer interviews. However, if you want to continue as a Fullstack Scala developer, you should learn all these Fullstack Scala technologies thoroughly.

我将与您分享3个单独的帖子。 有些问题只是一个问题,有些有一些子问题。 总共将讨论200个问题。 我敢打赌,如果您熟悉所有这些问题,那么您一定会清除所有Scala Developer访谈。 但是,如果您想继续作为Fullstack Scala开发人员,则应该彻底学习所有这些Fullstack Scala技术。

介绍 (Introduction)

I’m going to deliver Scala/Play/Akka Interview Questions and Answers in three parts:

我将分三个部分提供Scala / Play / Akka面试问题和答案:

  1. Scala Interview Questions and Answers:

    Scala面试问答:
  2. We will discuss some basic Scala Interview questions here, which are useful for Freshers or Java Developers want to move to Scala development or 1+ Year of Experience as Scala Developer.

    我们将在这里讨论一些基本的Scala面试问题,这些对于那些希望转入Scala开发或希望拥有超过1年的Scala开发人员经验的Java或Java开发人员很有用。

  3. Scala Intermediate Interview Questions and Answers:-

    Scala中级面试问答 :
  4. We will discuss some interview questions, which are useful for 2+ Years of Experience as Scala/Java Developer.

    我们将讨论一些面试问题,这些问题对于作为Scala / Java开发人员2年以上的经验很有用。

  5. Scala Advanced Interview Questions and Answers:-

    Scala高级面试问答 :
  6. We will discuss some Interview Questions, which are useful for Senior or Experienced Scala/Java Developer.

    我们将讨论一些面试问题,这些问题对于资深或经验丰富的Scala / Java开发人员很有用。

  7. Scala/Java Concurrency and Parallelism Interview Questions and Answers:-

    Scala / Java并发性和并行性面试问答 :
  8. We will discuss Scala/Java Concurrency and Parallelism Interview Questions and Answers, which are useful for Senior or Experienced Scala/Java Developer.

    我们将讨论Scala / Java并发性和并行性面试问题和答案,这对资深或有经验的Scala / Java开发人员很有用。

We will also discuss about some difference between Scala and Java constructs so that Users (who are moving from Java to Scala) can get benefits from these posts.

我们还将讨论Scala与Java构造之间的一些区别,以便用户(从Java迁移到Scala)可以从这些文章中受益。

In this post, we are going discuss about some basic Scala Interview Questions. Read my coming posts for rest of the two sections.

在这篇文章中,我们将讨论一些基本的Scala面试问题。 阅读我即将发布的文章,了解这两部分的其余部分。

Scala基本面试问题 (Scala Basic Interview Questions)

In this section, we are going to list out all Scala Basic Interview Questions and in next section we will discuss them in detail.

在本节中,我们将列出所有Scala基本面试问题,在下一节中,我们将详细讨论它们。

  1. What is Scala? Is it a Language or Platform? Does it support OOP or FP? Who is the father of Scala?

    什么是Scala? 是语言还是平台? 它支持OOP还是FP? 斯卡拉的父亲是谁?
  2. Is Scala Statically-Typed Language? What is Statically-Typed Language and What is Dynamically-Typed Language? What is the difference between statically typed and dynamically typed languages?

    Scala是静态类型的语言吗? 什么是静态类型语言和什么是动态类型语言? 静态类型和动态类型的语言有什么区别?
  3. Is Scala a Pure OOP Language? Is Java a Pure OOP Language?

    Scala是纯OOP语言吗? Java是纯OOP语言吗?
  4. Does Scala support all Functional Programming concepts? Does Java 8 support all Functional Programming concepts?

    Scala是否支持所有函数式编程概念? Java 8是否支持所有函数式编程概念?
  5. What are the major advantages of Scala Language? Are there any drawbacks of Scala Language?

    Scala语言的主要优势是什么? Scala语言有什么缺点吗?
  6. What is the Main drawback of Scala Language?

    Scala语言的主要缺点是什么?
  7. What is the main motto of Scala Language?

    Scala语言的主要座右铭是什么?
  8. What are the popular JVM Languages available now?

    现在有哪些流行的JVM语言?
  9. Like Java’s java.lang.Object class, what is the super class of all classes in Scala?

    像Java的java.lang.Object类一样,Scala中所有类的超类是什么?
  10. What is default access modifier in Scala?Does Scala have “public” keyword?

    Scala中的默认访问修饰符是什么?Scala是否具有“ public”关键字?
  11. What is “Type Inference” in Scala?

    Scala中的“类型推断”是什么?
  12. Difference between Scala’s Int and Java’s java.lang.Integer? What is the relationship between Int and RichInt in Scala?

    Scala的Int和Java的java.lang.Integer之间的区别? Scala中Int和RichInt之间是什么关系?
  13. What is Nothing in Scala? What is Nil in Scala? What is the relationship between Nothing and Nil in Scala?

    Scala什么都没有? 什么是Scala中的Nil? Scala中Nothing和Nil之间是什么关系?
  14. What is Null in Scala? What is null in Scala? What is difference between Null and null in Scala?

    Scala中的Null是什么? Scala中的null是什么? Scala中的Null和null有什么区别?
  15. What is Unit in Scala? What is the difference between Java’s void and Scala’s Unit?

    Scala中的单位是什么? Java的void和Scala的Unit有什么区别?
  16. What is the difference between val and var in Scala?

    Scala中val和var有什么区别?
  17. What is REPL in Scala? What is the use of Scala’s REPL? How to access Scala REPL from CMD Prompt?

    什么是Scala中的REPL? Scala的REPL有什么用? 如何从CMD Prompt访问Scala REPL?
  18. What are the Scala Features?

    Scala功能是什么?
  19. How do we implement loops functionally? What is the difference between OOP and FP style loops?

    我们如何在功能上实现循环? OOP和FP样式循环之间有什么区别?
  20. What is “Application” in Scala or What is Scala Application? What is “App” in Scala? What is the use of Scala’s App?

    Scala中的“应用程序”是什么?什么是Scala应用程序? Scala中的“ App”是什么? Scala的应用程序有什么用?
  21. Does Scala support Operator Overloading? Does Java support Operator Overloading?

    Scala是否支持运算符重载? Java是否支持运算符重载?
  22. What is an Expression? What is a Statement? Difference between Expression and Statement?

    什么是表情? 什么是声明? 表达式和语句之间的区别?
  23. What is the difference between Java’s “If..Else” and Scala’s “If..Else”?

    Java的“ If..Else”和Scala的“ If..Else”有什么区别?
  24. Is Scala an Expression-Based Language or Statement-Based Language? Is Java an Expression-Based Language or Statement-Based Language?

    Scala是基于表达的语言还是基于语句的语言? Java是基于表达式的语言还是基于语句的语言?
  25. Tell me some features which are supported by Java, but not by Scala and Vice versa?

    告诉我一些Java支持的功能,但Scala不支持,反之亦然?
  26. What is the difference between Function and Method in Scala?

    Scala中的Function和Method有什么区别?
  27. How many public class files are possible to define in Scala source file?

    Scala源文件中可以定义多少个公共类文件?
  28. Like Java, what are the default imports in Scala Language?

    与Java一样,Scala语言的默认导入是什么?
  29. How many operators are there in Scala and Why?

    Scala中有多少个运营商,为什么?
  30. Mention Some keywords which are used by Java and not required in Scala? Why Scala does not require them?

    提及某些Java使用的关键字,而Scala不需要这些关键字? 为什么Scala不需要它们?
  31. What is PreDef in Scala?

    什么是Scala中的PreDef?

Please refer my previous post to learn more about Functional Programming (Click this link to open my previous post).

请参考我以前的文章,以了解有关函数式编程的更多信息(单击此链接以打开我的先前的文章)。

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? 是语言还是平台? 它支持OOP还是FP? 斯卡拉的父亲是谁? (What is Scala? Is it a Language or Platform? Does it support OOP or FP? Who is the father of Scala?)

Scala stands for SCAlable LAnguage. Martin Odersky is the father of Scala.

Scala代表SCA标签LA语言。 Martin Odersky是Scala的父亲。

Scala is a Multi-Paradigm Programming Language, which supports both Object-Oriented and Functional Programming concepts. It is designed and developed by Martin Odersky.

Scala是一种多天堂编程语言,它支持面向对象和功能编程概念。 它是由Martin Odersky设计和开发的。

Scala is a Type-Safe Object-Functional Programming JVM Language. Scala runs on JVM(Java Virtual Machine).

Scala是一种类型安全的对象函数编程JVM语言。 Scala在JVM(Java虚拟机)上运行。

Scala is a Hybrid Functional (Object-Oriented and Functional) Programming JVM Language. Scala has a Strong and Statically Type System. In Scala, all types are checked at compile-time.

Scala是一种混合功能(面向对象和功能)编程JVM语言。 Scala具有强大的静态类型系统。 在Scala中,所有类型都在编译时检查。

Scala是静态类型的语言吗? 什么是静态类型语言和什么是动态类型语言? 静态类型和动态类型的语言有什么区别? (Is Scala Statically-Typed Language? What is Statically-Typed Language and What is Dynamically-Typed Language? What is the difference between statically typed and dynamically typed languages?)

Yes, Scala is a Statically-Typed Language.

是的,Scala是一种静态类型的语言。

Statically-Typed Language means that Type checking is done at compile-time by compiler, not at run-time. The main Advantage of these kinds of Languages is: As a Developer, we should care about writing right code to avoid all compile-time errors. As Compiler checks many of the errors at compile-time, we don’t get much issues or bugs at run-time.

静态类型语言意味着类型检查是在编译时由编译器完成的,而不是在运行时完成的。 这些语言的主要优点是:作为开发人员,我们应该关心编写正确的代码,以避免所有编译时错误。 当编译器在编译时检查许多错误时,在运行时我们不会遇到太多问题或错误。

Examples:- Java, Scala, C, C++, Haskell etc.

范例 :-Java,Scala,C,C ++,Haskell等。

Dynamically-Typed Language means that Type checking is done at run-time, not at compile-time by compiler. As a compiler won’t check any type checking at compile-time, We can expect more run-time issues or bugs.

动态类型语言意味着类型检查在运行时完成,而不是在编译时由编译器完成。 由于编译器在编译时不会检查任何类型检查,因此我们可以期待更多的运行时问题或错误。

Example:- Groovy, JavaScript, Ruby, Python, Smalltalk etc.

示例 :-Groovy,JavaScript,Ruby,Python,Smalltalk等。

Scala是纯OOP语言吗? Java是纯OOP语言吗? (Is Scala a Pure OOP Language? Is Java a Pure OOP Language?)

Pure Object-Oriented Programming Language means that everything should be an Object.

纯面向对象的编程语言意味着一切都应该是一个对象。

Java is not a Pure Object-Oriented Programming (OOP) Language because it supports the following two Non-OOP concepts:

Java不是纯面向对象编程(OOP)语言,因为它支持以下两个非OOP概念:

  • Java supports primitive data types. They are not objects.

    Java支持原始数据类型。 它们不是对象。
  • Java supports Static members. They are not related to objects.

    Java支持静态成员。 它们与对象无关。

Yes, Scala is a Pure Object-Oriented Programming Language because in Scala, everything is an Object and everything is a value. Functions are values and values are Objects.

是的,Scala是一种纯面向对象的编程语言,因为在Scala中,一切都是对象,一切都是值。 函数是值,值是对象。

Scala does not have primitive data types and also does not have static members.

Scala没有原始数据类型,也没有静态成员。

Scala是否支持所有函数式编程概念? Java 8是否支持所有函数式编程概念? (Does Scala support all Functional Programming concepts? Does Java 8 support all Functional Programming concepts?)

Yes, Scala supports all Functional Programming (FP) concepts. Java 8 has introduced some Functional Programming constructs, but it does NOT support all Functional Programming concepts.

是的,Scala支持所有函数式编程(FP)概念。 Java 8引入了一些函数式编程结构,但是它不支持所有的函数式编程概念。

For instance, Java 8 does not support Pattern Matching, Function Currying, Implicits etc.

例如,Java 8不支持模式匹配,函数固化,隐式等。

Scala语言的主要优势是什么? Scala语言有什么缺点吗? (What are the major advantages of Scala Language? Are there any drawbacks of Scala Language?)

If we use Scala Language to develop our applications, we can get the following benefits or advantages and drawbacks:
Advantages of Scala Language:-

如果使用Scala语言开发应用程序,则可以得到以下好处或优点和缺点:
Scala语言的优点:-

  • Simple and Concise Code

    简洁代码
  • Very Expressive Code

    极富表现力的代码
  • More Readable Code

    更易读的代码
  • 100% Type-Safe Language

    100%类型安全的语言
  • Immutability and No Side-Effects

    不变性,无副作用
  • More Reusable Code

    更可重用的代码
  • More Modularity

    更多模块化
  • Do More With Less Code

    用更少的代码做更多的事情
  • Very Flexible Syntax

    非常灵活的语法
  • Supports all OOP Features

    支持所有OOP功能
  • Supports all FP Features. Highly Functional.

    支持所有FP功能。 高度实用。
  • Less Error Prone Code

    更少的容易出错的代码
  • Better Parallel and Concurrency Programming

    更好的并行和并发编程
  • Highly Scalable and Maintainable code

    高度可扩展和可维护的代码
  • Highly Productivity

    高生产力
  • Distributed Applications

    分布式应用
  • Full Java Interoperability

    全面的Java互操作性
  • Powerful Scala DSLs available

    强大的Scala DSL可用
  • REPL to learn Scala Basics

    REPL学习Scala基础知识

Drawbacks of Scala Language:-

Scala语言的缺点:

  • Less Readable Code

    可读性较低的代码
  • Bit tough to Understand the Code for beginners

    很难理解初学者的代码
  • Complex Syntax to learn

    复杂语法学习
  • Less Backward Compatibility

    向后兼容性较差

NOTE:- We can write Scala Code either more readable or less readable way.

注意:-我们可以以更易读或不易读的方式编写Scala代码。

Scala语言的主要缺点是什么? (What is the Main drawback of Scala Language?)

Apart from many benefits of Scala, it has one major Drawback: Backward Compatibility Issue. If we want to upgrade to latest version of Scala, then we need to take care of changing some package names, class names, method or function names etc.

除了Scala的许多优点之外,它还有一个主要缺点:向后兼容性问题。 如果我们想升级到最新版本的Scala,则需要注意更改某些软件包名称,类名称,方法或函数名称等。

For instance, If you are using old Scala version and your project is using BeanProperty annotation. It was available in “scala.reflect” like “scala.reflect.BeanProperty” in old versions. If we want to upgrade to new Scala versions, then we need to change this package from “scala.reflect” to “scala.beans”.

例如,如果您正在使用旧的Scala版本,而您的项目正在使用BeanProperty批注。 与旧版本中的“ scala.reflect.BeanProperty”类似,它在“ scala.reflect”中可用。 如果要升级到新的Scala版本,则需要将此软件包从“ scala.reflect”更改为“ scala.beans”。

Scala语言的主要座右铭是什么? (What is the main motto of Scala Language?)

Like Java’s Motto “Write Once Run Anywhere”, Scala has “Do More With Less” or “Do More With Less Code” Motto.
“Do More With Less” means that we can develop more complex program or logic with less code.

就像Java的座右铭“编写一次在任何地方运行”一样,Scala的座右铭是“ 事半功倍”或“事半功倍”
“事半功倍”意味着我们可以用更少的代码开发更复杂的程序或逻辑。

现在有哪些流行的JVM语言? (What are the popular JVM Languages available now?)

Java, Scala, Groovy and Closure are most popular JVM (Java Virtual Machine) languages.

Java,Scala,Groovy和Closure是最流行的JVM(Java虚拟机)语言。

Scala, Groovy and Closure JVM languages supports both Object-Oriented Programming Features and Functional Programming Features.

Scala,Groovy和Closure JVM语言同时支持面向对象的编程功能和功能编程功能。

Java SE 8 supports all Object-Oriented Programming Features. However, it supports very few Functional Programming Features like Lambda Expressions, Functions, Type Inference, Higher-Order Functions.

Java SE 8支持所有面向对象的编程功能。 但是,它仅支持极少数的函数式编程功能,例如Lambda表达式,函数,类型推断,高阶函数。

像Java的java.lang.Object类一样,Scala中所有类的超类是什么? (Like Java’s java.lang.Object class, what is the super class of all classes in Scala?)

As we know in Java, the super class of all classes (Java API Classes or User Defined Classes) is java.lang.Object. In the same way in Scala, the super class of all classes or traits is “Any” class.

众所周知,在Java中,所有类(Java API类或用户定义的类)的超类都是java.lang.Object。 在Scala中,所有类或特征的超类都是“ Any”类。

Any class is defined in scala package like “scala.Any”.

任何类都在scala包中定义,例如“ scala.Any”。

Scala中的默认访问修饰符是什么? Scala是否具有“ public”关键字? (What is default access modifier in Scala? Does Scala have “public” keyword?)

In Scala, if we don’t mention any access modifier to a method, function, trait, object or class, the default access modifier is “public”. Even for Fields also, “public” is the default access modifier.

在Scala中,如果我们没有对方法,函数,特征,对象或类提及任何访问修饰符,则默认访问修饰符为“ public”。 即使对于字段,“ public”也是默认的访问修饰符。

Because of this default feature, Scala does not have “public” keyword.

由于此默认功能,Scala没有“ public”关键字。

Scala中的“类型推断”是什么? (What is “Type Inference” in Scala?)

Types can be inferred by the Scala Compiler at compile-time. It is known as “Type Inference”. Types means Data type or Result type. We use Types at many places in Scala programs like Variable types, Object types, Method/Function Parameter types, Method/Function return types etc.

可以在编译时由Scala编译器推断类型。 它被称为“类型推断”。 类型是指数据类型或结果类型。 我们在Scala程序的许多地方使用类型,例如变量类型,对象类型,方法/函数参数类型,方法/函数返回类型等。

In simple words, determining the type of a variable or expression or object etc at compile-time by compiler is known as “Type Inference”.

简单来说,由编译器在编译时确定变量,表达式或对象等的类型称为“类型推断”。

Scala的Int和Java的java.lang.Integer之间有什么异同? Scala中Int和RichInt之间是什么关系? (What are the similarities and differences between Scala’s Int and Java’s java.lang.Integer? What is the relationship between Int and RichInt in Scala?)

Similarities between Scala’s Int and Java’s java.lang.Integer:

Scala的Int和Java的java.lang.Integer之间的相似之处:

  • Both are classes.

    两者都是课程。
  • Both are used to represent integer numbers.

    两者均用于表示整数。
  • Both are 32-bit signed integers.

    两者都是32位带符号整数。

Differences between Scala’s Int and Java’s java.lang.Integer:

Scala的Int与Java的java.lang.Integer之间的区别:

  • Scala’s Int class does not implement Comparable interface.

    Scala的Int类未实现Comparable接口。
  • Java’s java.lang.Integer class implements Comparable interface.

    Java的java.lang.Integer类实现Comparable接口。

Java’s Integer is something similar to Scala’s Int and RichInt. RichInt is a final class defined in scala.runtime package like “scala.runtime.RichInt”.

Java的Integer与Scala的Int和RichInt类似。 RichInt是在scala.runtime包中定义的最终类,例如“ scala.runtime.RichInt”。

In Scala, the Relationship between Int and RichInt is that when we use Int in a Scala program, it will automatically convert into RichInt to utilize all methods available in that Class. We can say that RichInt is an Implicit class of Int. (We will discuss “What is Implicit and the advantages of Implicits in my next post).

在Scala中,Int和RichInt之间的关系是,当我们在Scala程序中使用Int时,它将自动转换为RichInt以利用该Class中可用的所有方法。 我们可以说RichInt是Int的隐式类。 (我们将在下一篇文章中讨论“什么是隐式的以及隐式的优点”)。

Scala什么都没有? 什么是Scala中的Nil? Scala中Nothing和Nil之间是什么关系? (What is Nothing in Scala? What is Nil in Scala? What is the relationship between Nothing and Nil in Scala?)

In Scala, Nothing is a Type (final class). It is defined at the bottom of the Scala Type System that means it is a subtype of anything in Scala. There are no instances of Nothing.

在Scala中,什么都不是类型(最终类)。 它在Scala类型系统的底部定义,这意味着它是Scala中任何内容的子类型。 没有Nothing的实例。

Use Cases of Nothing In Scala:-
If Nothing does not have any instances, then when do we use this one in Scala Applications?

Scala中没有内容的用例:-
如果Nothing没有任何实例,那么我们何时在Scala应用程序中使用该实例?

  • Nil is defined using Nothing (See below for example).

    Nil是使用Nothing定义的(例如,参见下文)。
  • None is defined using Nothing.

    没有使用Nothing定义的内容。
object None extends Option[Nothing]
  • We can use Nothing as a return type of methods which never return.

    我们可以将Nothing用作永不返回的方法的返回类型。
  • We can use Nothing as a return type of methods which terminates abnormally.

    我们可以将Nothing用作异常终止的方法的返回类型。
  • Nil is an object, which is used to represent an empty list. It is defined in “scala.collection.immutable” package as shown below:

    Nil是一个对象,用于表示一个空列表。 它在“ scala.collection.immutable”包中定义,如下所示:

    object Nil extends List[Nothing]

    Example:-

    例:-

    scala> Nil
    res5: scala.collection.immutable.Nil.type = List()
    
    scala> Nil.length
    res6: Int = 0

    Scala中的Null是什么? Scala中的null是什么? Scala中的Null和null有什么区别? (What is Null in Scala? What is null in Scala? What is difference between Null and null in Scala?)

    Null is a Type (final class) in Scala. Null type is available in “scala” package as “scala.Null”. It has one and only one instance that is null.

    Null是Scala中的Type(最终类)。 Null类型在“ scala”包中作为“ scala.Null”可用。 它只有一个实例为null。

    In Scala, “null” is an instance of type scala.Null type.

    在Scala中,“ null”是scala.Null类型的实例。

    Example:-

    例:-

    scala> val myNullRef : Null = null
    myNullRef: Null = null

    We cannot assign other values to Null type references. It accepts only ‘null’ value.

    我们不能将其他值分配给Null类型引用。 它仅接受“空”值。

    Null is a subtype of all Reference types. Null is at the bottom of the Scala Type System. As it is NOT a subtype of Value types, we can assign “null” to any variable of Value type.

    Null是所有引用类型的子类型。 Null在Scala类型系统的底部。 由于它不是值类型的子类型,因此我们可以将“空”分配给任何值类型的变量。

    Example:-

    例:-

    scala> val myInt : Int = null
    :10: error: an expression of type Null is ineligible for implicit conversion
           val myInt : Int = null

    ^

    ^

    Here type mismatch error. found : Null(null) but required: Int. The implicit conversions between Null and Int are not applicable because they are ambiguous.

    在这里输入不匹配错误。 找到:Null(null),但必需:Int。 Null和Int之间的隐式转换不适用,因为它们是模棱两可的。

    Scala中的单位是什么? Java的void和Scala的Unit有什么区别? (What is Unit in Scala? What is the difference between Java’s void and Scala’s Unit?)

    In Scala, Unit is used to represent “No value” or “No Useful value”. Unit is a final class defined in “scala” package that is “scala.Unit”.

    在Scala中,单位用于表示“无值”或“无有用值”。 Unit是在“ scala”包中定义的最终类,即“ scala.Unit”。

    Unit is something similar to Java’s void. But they have few differences.

    单位类似于Java的void。 但是它们几乎没有区别。

    • Java’s void does not any value. It is nothing.

      Java的void没有任何价值。 没事
    • Scala’s Unit has one value ()

      Scala的单位有一个值()
    • () is the one and only value of type Unit in Scala. However, there are no values of type void in Java.

      ()是Scala中Unit类型的唯一值。 但是,Java中没有类型为void的值。
    • Java’s void is a keyword. Scala’s Unit is a final class.

      Java的void是一个关键字。 Scala的单元是最后一堂课。

    Both are used to represent a method or function is not returning anything.

    两者均用于表示方法或函数未返回任何内容。

    Scala中val和var有什么区别? (What is the difference between val and var in Scala?)

    In Scala, both val and var are used to define variables. However, they have some significant differences.

    在Scala中,val和var均用于定义变量。 但是,它们之间存在一些显着差异。

    • var stands for variable.

      var代表变量。
    • val stands for value.

      val代表价值。
    • As we know, variable means changeable and value means constant.

      众所周知,变量意味着可变,值意味着恒定。
    • var is used to define Mutable variables that means we can reassign values once its created.

      var用于定义Mutable变量,这意味着一旦创建变量,便可以重新分配值。
    • val is used to define Immutable variables that means we cannot reassign values once its created.

      val用于定义不可变变量,这意味着一旦创建了值便无法重新分配值。
    • In simple Java terminology, var means ‘variable’ and val means ‘final variable’.

      用简单的Java术语来说,var表示“变量”,而val表示“最终变量”。

    什么是Scala中的REPL? Scala的REPL有什么用? 如何从CMD Prompt访问Scala REPL? (What is REPL in Scala? What is the use of Scala’s REPL? How to access Scala REPL from CMD Prompt?)

    REPL stands for Read-Evaluate-Print Loop. We can pronounce it as ‘ripple’. In Scala, REPL is acts as an Interpreter to execute Scala code from command prompt. That’s why REPL is also known as Scala CLI(Command Line Interface) or Scala command-line shell.

    REPL代表读取-评估-打印循环。 我们可以将其发音为“涟漪”。 在Scala中,REPL充当从命令提示符执行Scala代码的解释器。 这就是为什么REPL也称为Scala CLI(命令行界面)或Scala命令行外壳的原因。

    The main purpose of REPL is that to develop and test small snippets of Scala code for practice purpose. It is very useful for Scala Beginners to practice basic programs.

    REPL的主要目的是为实践目的开发和测试Scala代码的小片段。 对于Scala初学者来说,练习基本程序非常有用。

    We can access REPL by using “scala” command. When we type “scala” command at CMD Prompt, we will get REPL shell where we can type and execute scala code.

    我们可以使用“ scala”命令访问REPL。 当在CMD Prompt键入“ scala”命令时,我们将获得REPL shell,可以在其中键入和执行scala代码。

    D:\> scala
    
    scala>

    Scala功能是什么? (What are the Scala Features?)

    Scala Language supports the following features:

    Scala语言支持以下功能:

    • Supports both OOP-style(Imperative-Style) and Functional-Style Programming

      同时支持OOP风格(命令式)和功能风格的编程
    • Pure Object-Oriented Programming Language

      纯面向对象的编程语言
    • Supports all Functional Features

      支持所有功能
    • REPL(Read-Evaluate-Print Loop) Interpreter

      REPL(读取-评估-打印循环)口译员
    • Strong Type System

      强类型系统
    • Statically-Typed Language

      静态类型语言
    • Type Inference

      类型推断
    • Supports Pattern Matching

      支持模式匹配
    • Supports Closures

      支持关闭
    • Supports Persistent Data Structures

      支持持久数据结构
    • Uses Actor Model to develop Concurrency Applications

      使用Actor模型开发并发应用程序
    • Interoperable with Java

      可与Java互操作
    • Available all development tools – IDEs, Build Tools, Web Frameworks, TDD and BDD Frameworks

      可用的所有开发工具– IDE,构建工具,Web框架,TDD和BDD框架

    我们如何在功能上实现循环? OOP和FP样式循环之间有什么区别? (How do we implement loops functionally? What is the difference between OOP and FP style loops?)

    We know how to implement loops in Object-Oriented style: Using Mutable Temporary variables, update the variable value and use Loop constructs. It is very tedious and unsafe approach. It is not Thread-Safe.

    我们知道如何以面向对象的方式实现循环:使用Mutable Temporary变量,更新变量值并使用Loop构造。 这是非常繁琐和不安全的方法。 它不是线程安全的。

    Object-Oriented style uses the following constructs to implement Loops:

    面向对象的样式使用以下构造来实现循环:

    • Loop Constructs

      循环构造
    • Mutability

      变异性
    • Side Effects

      副作用

    We can implement same Loops differently in Functional way. It is Thread-Safe. We can use the following two techniques to implement the loops in functional style:

    我们可以以功能方式不同地实现相同的循环。 这是线程安全的。 我们可以使用以下两种技术以功能样式实现循环:

    • Recursion

      递归
    • Tail-Recursion

      尾递归
    • Immutability

      不变性
    • No Side-Effects

      没有副作用

    Scala中的“应用程序”是什么?什么是Scala应用程序? Scala中的“ App”是什么? Scala的应用程序有什么用? (What is “Application” in Scala or What is Scala Application? What is “App” in Scala? What is the use of Scala’s App?)

    Scala Application:
    In Scala, App is a trait defined in scala package like “scala.App”. It defines main method. If an Object or a Class extends this trait, then they will become as Scala Executable programs automatically because they will inherit main method from Application.

    Scala应用程序:
    在Scala中,App是在scala包(例如“ scala.App”)中定义的特征。 它定义了主要方法。 如果对象或类扩展了此特性,则它们将自动成为Scala可执行程序,因为它们将从Application继承主方法。

    The main advantage of using App is that we don’t need to write main method. The main drawback of using App is that we should use same name “args” to refer command line argument because scala.App’s main() method uses this name.

    使用App的主要优点是我们不需要编写main方法。 使用App的主要缺点是我们应该使用相同的名称“ args”来引用命令行参数,因为scala.App的main()方法使用此名称。

    Example:-
    Without Scala App:

    例:-
    如果没有Scala应用:

    object MyApp {
        def main( args: Array[String]){
            println("Hello World!")
        }
    }

    With Scala App:

    使用Scala应用程序:

    object MyApp extends App{
       println("Hello World!")
    }

    If we observe above two examples, in second example we have not defined main method because we have inherited from Scala App(Application).

    如果我们观察以上两个示例,则在第二个示例中,由于我们已从Scala App(Application)继承而未定义main方法。

    Before Scala 2.9, we have scala.Application trait. But it is deprecated by scala.App since Scala 2.9 version.

    在Scala 2.9之前,我们具有scala.Application特性。 但是从Scala 2.9版本开始,scala.App弃用了它。

    Scala是否支持运算符重载? Java是否支持运算符重载? (Does Scala support Operator Overloading? Does Java support Operator Overloading?)

    Java does not support Operator Overloading. Scala supports Operator Overloading.

    Java不支持运算符重载。 Scala支持操作员重载。

    The reason is that Java does not want to support some misleading method names like “+*/”. Scala has given this flexibility to Developer to decide which methods/functions name should use.

    原因是Java不想支持某些令人误解的方法名称,例如“ + * /”。 Scala使开发人员可以灵活地决定应使用的方法/函数名称。

    When we call 2 + 3 that means ‘+’ is not an operator, it is a method available in Int class (or it’s implicit type). Internally, this call is converted into “2.+(3)“.

    当我们调用2 + 3时,意味着“ +”不是运算符,它是Int类(或其隐式类型)中可用的方法。 在内部,此调用转换为“ 2。+(3) ”。

    什么是表情? 什么是声明? 表达式和语句之间的区别? (What is an Expression? What is a Statement? Difference between Expression and Statement?)

    Expression:
    Expression is a value that means it will evaluate to a Value. As an Expression returns a value, We can assign it to a variable.

    表达:
    表达式是一个值,表示它将计算为一个值。 当表达式返回值时,我们可以将其分配给变量。

    Example:- Scala’s If condition, Java’s Ternary operator.

    示例:-Scala的If条件,Java的Ternary运算符。

    Statement:
    Statement defines one or more actions or operations. That means Statement performs actions. As it does not return a value, we cannot assign it to a Variable.

    声明:
    语句定义一个或多个动作或操作。 这意味着声明执行动作。 由于它不返回值,因此我们无法将其分配给变量。

    Example:- Java’s If condition.

    示例:-Java的If条件。

    Java的“ If..Else”和Scala的“ If..Else”有什么区别? (What is the difference between Java’s “If..Else” and Scala’s “If..Else”?)

    Java’s “If..Else”:
    In Java, “If..Else” is a statement, not an expression. It does not return a value and cannot assign it to a variable.

    Java的“ If..Else”:
    在Java中,“ If..Else”是语句,而不是表达式。 它不返回值,也不能将其分配给变量。

    Example:-

    例:-

    int year;
     if( count == 0) 
       year = 2014;
     else 
       year = 2015;

    Scala’s “If..Else”:
    In Scala, “If..Else” is an expression. It evaluates a value i.e. returns a value. We can assign it to a variable.

    Scala的“ If..Else”:
    在Scala中,“ If..Else”是一个表达式。 它评估一个值,即返回一个值。 我们可以将其分配给变量。

    val year = if( count == 0) 2014 else 2015

    NOTE:-Scala’s “If..Else” works like Java’s Ternary Operator. We can use Scala’s “If..Else” like Java’s “If..Else” statement as shown below:

    注意: -Scala的“ If..Else”的工作方式类似于Java的三元运算符。 我们可以像Java的“ If..Else”语句一样使用Scala的“ If..Else”,如下所示:

    val year = 0
     if( count == 0) 
       year = 2014 
     else 
       year = 2015

    Scala是基于表达的语言还是基于语句的语言? Java是基于表达式的语言还是基于语句的语言? (Is Scala an Expression-Based Language or Statement-Based Language? Is Java an Expression-Based Language or Statement-Based Language?)

    In Scala, everything is a value. All Expressions or Statements evaluates to a Value. We can assign Expression, Function, Closure, Object etc. to a Variable. So Scala is an Expression-Oriented Language.

    在Scala中,一切都是价值。 所有表达式或语句均求值。 我们可以将表达式,函数,闭包,对象等分配给变量。 因此Scala是一种面向表达的语言。

    In Java, Statements are not Expressions or Values. We cannot assign them to a Variable. So Java is not an Expression-Oriented Language. It is a Statement-Based Language.

    在Java中,语句不是表达式或值。 我们不能将它们分配给变量。 因此,Java不是面向表达式的语言。 它是一种基于语句的语言。

    告诉我一些Java支持的功能,但Scala不支持,反之亦然? (Tell me some features which are supported by Java, but not by Scala and Vice versa?)

    • Java does not support Operator Overloading, but Scala supports it.

      Java不支持运算符重载,但是Scala支持它。
    • Java supports ++ and operators , but Scala does not support them.

      Java支持++-运算符,但Scala不支持它们。
    • Java has Checked and Unchecked Exceptions, but Scala does not have Checked Exceptions.

      Java具有Checked和Unchecked异常,但是Scala没有Checked Exception。
    • Scala does not support break and continue statements, but Java uses them.

      Scala不支持break和继续语句,但是Java使用它们。
    • Scala does not have explicit Type casting, but Java supports this feature.

      Scala没有显式的类型转换,但是Java支持此功能。
    • Scala supports Pattern Matching, but Java does not.

      Scala支持模式匹配,但是Java不支持。
    • Java uses Primitive Data types, but Scala does not have.

      Java使用原始数据类型,但是Scala没有。
    • Java supports static members, but Scala does not have static members concept.

      Java支持静态成员,但是Scala没有静态成员概念。
    • Scala supports Implicits and Traits, Java does not support them.

      Scala支持隐式和特质,Java不支持它们。

    NOTE:-This list goes beyond one page. However, these are some important points to remember about differences in Scala and Java features to face Scala Interviews.

    注意:-此列表超出一页。 但是,这些是要记住有关面对Scala采访的Scala和Java功能差异的一些重要要点。

    Scala中的Function和Method有什么区别? (What is the difference between Function and Method in Scala?)

    Scala supports both functions and methods. We use same syntax to define functions and methods, there is no syntax difference.

    Scala支持功能和方法。 我们使用相同的语法定义函数和方法,没有语法差异。

    However, they have one minor difference:

    但是,它们之间只有一个细微的差别:

    • We can define a method in a Scala class or trait. Method is associated with an object (An instance of a Class). We can call a method by using an instance of a Class. We cannot use a Scala Method directly without using object.

      我们可以在Scala类或特征中定义一个方法。 方法与对象(类的实例)相关联。 我们可以通过使用Class的实例来调用方法。 不使用对象,我们不能直接使用Scala方法。
    • Function is not associated with a class or trait. It is defined in a Scala Package. We can access functions without using objects, like Java’s Static Methods.

      函数与类或特征无关。 它在Scala包中定义。 我们无需使用对象即可访问函数,例如Java的静态方法。

    NOTE:- We will discuss about Class, Trait,Package, Object etc in my coming posts.

    注意:-我们将在我的后续文章中讨论有关类,特性,包装,对象等的信息。

    Scala源文件中可以定义多少个公共类文件? (How many public class files are possible to define in Scala source file?)

    In Java, we can define at-most one public class/interface in a Source file. Unlike Java, Scala supports multiple public classes in the same source file.

    在Java中,我们可以在Source文件中定义至少一个公共类/接口。 与Java不同,Scala在同一源文件中支持多个公共类。

    We can define any number of public classes/interfaces/traits in a Scala Source file.

    我们可以在Scala源文件中定义任意数量的公共类/接口/特征。

    与Java一样,Scala语言的默认导入是什么? (Like Java, what are the default imports in Scala Language?)

    We know, java.lang is the default package imported into all Java Programs by JVM automatically. We don’t need to import this package explicitly.

    我们知道,java.lang是JVM自动导入所有Java程序的默认软件包。 我们不需要显式导入此程序包。

    In the same way, the following are the default imports available in all Scala Programs:

    同样,以下是所有Scala程序中可用的默认导入:

    • java.lang package

      java.lang包
    • scala package

      标量包
    • scala.PreDef

      Scala.PreDef

    Scala中有多少个运营商,为什么? (How many operators are there in Scala and Why?)

    Unlike Java and like C++, Scala supports Operator Overloading. Scala has one and only operator that is “=” (equalto) operator. Other than this all are methods only.

    与Java和C ++不同,Scala支持运算符重载。 Scala有一个唯一的运算符,即“ =”(等于)运算符。 除此之外,仅是方法。

    For instance 2 + 3, here “+” is not an Operator in Scala. “+” is method available in Int class. Scala Compiler observes 2 and 3 are Integers and tries to find that “+” method in Int class. So Scala Compiler converts “2 + 3” expression into “2.+(3)” and make a call to “+” method on integer object “2” and pass integer object “3” as parameter to “+” method.

    例如2 + 3,这里的“ +”不是Scala中的运算符。 “ +”是Int类中可用的方法。 Scala编译器观察到2和3是整数,并尝试在Int类中找到“ +”方法。 因此,Scala编译器将“ 2 + 3”表达式转换为“ 2。+(3)”,并在整数对象“ 2”上调用“ +”方法,并将整数对象“ 3”作为参数传递给“ +”方法。

    Both “2 + 3” and “2.+(3)” are equal. It’s just Scala’s syntactic sugar to write programs in Functional style.

    “ 2 + 3”和“ 2。+(3)”相等。 用功能风格编写程序只是Scala的语法工具。

    提及某些Java使用的关键字,而Scala不需要这些关键字? 为什么Scala不需要它们? (Mention Some keywords which are used by Java and not required in Scala? Why Scala does not require them?)

    Java uses the following keywords extensively:

    Java广泛使用以下关键字:

    • ‘public’ keyword – to define classes, interfaces, variables etc.

      “ public”关键字–定义类,接口,变量等
    • ‘static’ keyword – to define static members.

      “静态”关键字–定义静态成员。

    Scala does not required these two keywords. Scala does not have ‘public’ and ‘static’ keywords.

    Scala不需要这两个关键字。 Scala没有“ public”和“ static”关键字。

    • In Scala, default access modifier is ‘public’ for classes,traits, methods/functions, fields etc. That’s why, ‘public’ keyword is not required.

      在Scala中,对于类,特征,方法/函数,字段等,默认访问修饰符为“公共”。这就是为什么不需要“公共”关键字的原因。
    • To support OOP principles, Scala team has avoided ‘static’ keyword. That’s why Scala is a Pure-OOP Langauge. It is very tough to deal static members in Concurrency applications.

      为了支持OOP原则,Scala团队避免使用“静态”关键字。 这就是为什么Scala是Pure-OOP语言。 在并发应用程序中处理静态成员非常困难。

    h3>What is PreDef in Scala? What is the main purpose of PreDef in Scala?

    h3> Scala中的PreDef是什么? PreDef在Scala中的主要目的是什么?

    In Scala, PreDef is an object defined in scala package as “scala.PreDef”. It is an utility object.

    在Scala中,PreDef是在Scala包中定义为“ scala.PreDef”的对象。 它是一个实用程序对象。

    It defines many utility methods as shown below:

    它定义了许多实用程序方法,如下所示:

    • Console IO (print,println etc)

      控制台IO(打印,打印等)
    • Collection utility methods

      收集实用程序方法
    • String utility methods

      字符串实用程序方法
    • Implicit conversion methods

      隐式转换方法
    • Assertion utility methods etc.

      断言实用程序方法等

    For instance, print, println, readLine, readInt, require etc methods are defined in PreDef object.

    例如,在PreDef对象中定义了print,println,readLine,readInt,require等方法。

    In Scala, PreDef is available to use its methods without importing in all Scala Programs because Scala Compiler imports this object into all compilation units like Class, Object, Trait etc. automatically.

    在Scala中,PreDef可以使用其方法而无需在所有Scala程序中导入,因为Scala编译器会自动将此对象导入所有编译单元(如Class,Object,Trait等)中。

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

    scala面试问题

    你可能感兴趣的:(scala面试问题_Scala面试问答)