Scala face questions Summary

scala common pen questions (encompasses the essence of the scala)

 

scala recent years is the fire, mainly because of its application in the field of big data.

The following questions surface, substantially scala essence of functional programming.

Q1 var, val and the difference between the three def keyword?

  • A: var variable declaration is the keyword, similar to Java in the variable, the variable value can be changed, but you can not change the variable type. val constant declaration keyword. def keyword is used to create a method (note the difference between methods and functions) and a lazy val (inert val) declaration, meaning the use of computing when needed, to avoid double counting

 

Q2 trait (trait) and abstract class (abstract class) difference?

  • A: (1) A class can integrate an abstract class, but can be inherited by a keyword with a plurality of characteristics; (2) there is an abstract class constructor parameters, not the characteristics (e.g., trait t (i: Int) { } this statement is wrong)

 

The difference between Q3 object and class?

  • A: object class is singleton objects, developers do not need to use the new keyword to instantiate. If the class name and the name of the same object, the object is the companion objects (See Questions insight Q7)

 

What Q4 case class (sample type) that?

  • A: The sample class is an immutable class of syntactic sugar and biodegradable, this is probably the meaning of syntactic sugar in the building, some functions automatically. Sample class has the following characteristics: (1) consistent with the class name is automatically added to the constructor (this is the companion aforementioned objects, is achieved by apply method), i.e. the object is constructed, no new new; (2) the sample class adding a keyword default parameter val, i.e. the parameters can not be modified; (3) the default implementation toString, equals, hashcode, copy or the like; (4) the sample can compare two objects by class ==, construction method and is not defined properties It will not be used in the comparison.

 

Q5 Java and Scala difference asynchronous computation?

  • A: Here the author means that he probably do not know, please read this really clean and simple answer on StackOverflow, I understand is not in place on the follow-up.

 

Q6 unapply difference and apply methods, as well as their usage scenarios?

  • A: The talk about a concept - extractor, it achieved the opposite effect constructor, the constructor creates an object from the given parameters, but never the object extractor to extract the parameters of the construction of the object, scala pre-standard library defines the extractor, sample type as mentioned above, it will automatically create a companion object (comprising unapply apply and methods). In order to become an extractor, unapply methods need to be associated object. apply methods to automate sample class of objects without the new keyword.

 

What Q7 companion objects are?

  • A: As mentioned earlier, the object is associated with the same object class name, the amount of private companion object can access the class, the class can access objects associated private methods, static methods similar to Java class. Associated class definitions and object must at the same corresponding source file.

Q8 Scala type system Nil, Null, None, Nothing four types of difference?

  • A: Null is a trait (trait), it is a reference to a subtype of type AnyRef, null Null is the only instance. Nothing is a trait (trait), all types Any (including value types and reference types) sub-type, which does not have a subtype, there is no instance of it, in fact, to a method throws an exception, usually set a default return Types of. Nil represents a List empty type, equivalent List [Nothing] None of empty identity Option monad (Refer to understand the problem Q11)

 

What Q9 Unit type?

  • A: Unit representatives did not make any sense of the value type, similar to the type of java in the void, he is a subtype of anyval, only one instance of an object "()"

 

Difference Q10 call-by-value and call-by-name evaluation strategy of?

  • A: (1) call-by- value is calculated before calling this function; (2) Call-by-name is calculated when needed

 

Q11 Option type definition and usage scenarios?

  • A: In Java, null is a keyword, not an object, when developers want to return an empty object, but returned a keyword in order to solve this problem, Scala recommends that developers return value is null value, use Option type in Scala null null is the only object, it will cause abnormal, Option can be avoided. Option two subtypes, Some and None (null)

 

Q12 yield How It Works?

  • A: yield for loop iteration produces a new value, the yield is part of comprehensions, a plurality of operation (foreach, map, flatMap, filter or withFilter) of syntactic sugar composition. (Refer to understand the problem Q14)

 

Q13 explained implicit priority parameter

  • A: very strong in Scala implicit function. When the compiler to find implicits, if not pay attention to the implicit priority parameters, may cause unexpected errors. So the compiler looks for implicit keywords in order. In the following order: implicits (. 1) the current class declaration; (2) into the package implicits; (. 3) foreign domain (declared implicts in a foreign domain); (. 4) Inheritance (. 5) Package Object (. 6) Implicit scope like companion objects

 

Q14 comprehension (comprehension) of syntactic sugar is what to do?

  • A: comprehension (derivation) alternative syntax is composed of several operations. If not yield keyword, comprehensions (derivation) can alternatively be forech operation, or by map / flatMap, filter instead.

Q15 Streams: What to consider when using Scala Steams? Internal Streams Scala's what technology to use?

  • A: Not yet understanding, not this translation, the follow-up.

 

Q16 What is the vaule class?

  • A: This problem often encountered when developing, when you use integer, hope it represents something, but not all things, for example, represented by an integer of age, the other represents the height. For these reasons, we consider the original package generates a new type of meaningful types (such as age, type and height type). Value classes allows developers to add a new type of security, to avoid the run-time object allocation. There are some circumstances and restrictions must be allocated, but the basic idea is: at compile time by using the replacement value of the original type class instance, remove the object allocation.

 

Q17 Option, and the difference Try Either of the three?

  • A: The three monads function does not allow us to show the results expected by execution. Option indicates optional value s Some type of return (return on behalf of valid data), or None (behalf returns null). Try similar in Java try / catch, if the calculation is successful, returns an instance of Success, if an exception is thrown, return Failure. Either can provide some information to calculate failed, Either there are two possible types of return: Expected / correct / successful and erroneous information.

 

Q18 What is the function of currying?

  • A: Currie technique is a conversion function to accept a function of several parameters which accepts a plurality of parameters. It is often used to process higher-order functions.

Q19 What is tail-recursion?

Normal recursion, each recursive step, we need to save the information to stack inside, when a lot of recursive step, resulting in a stack overflow. Tail recursion to solve the above problems, all calculations are before the recursive call in the tail recursion, the compiler can use this property to avoid such errors, tail-recursive call stack can not insert the information, thus optimizing tail recursion. Use @tailrec tag enables the compiler enforce tail recursion.

Q20 What is the higher-order functions?

  • A: The higher-order function refers to the function can accept returns or other functions, scala the filter map flatMap function can accept other functions as arguments.

Guess you like

Origin www.cnblogs.com/szjblog/p/11272325.html